You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
8.7 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import re
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
import os
import subprocess
from datetime import datetime
import learn.yolov5.detect as DL
from pathlib import PosixPath
import requests, os, json
import cv2
import numpy as np
def adb_shell(cmd):
result = subprocess.getstatusoutput(cmd)
return result
'''改程序执行慢且因为C++和python在cv2中处理细节不同返回值并不相同
def detect_brightness():
img = cv2.imread("/home/wangchunlin/img_brightness/1.jpeg")
gray_img= cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
a=0
Ma = 0
hist = np.zeros(256,dtype=int)
print(hist.dtype)
h, w = gray_img.shape[:2]
print(h, w)
for i in range(h):
for j in range(w):
if(float(gray_img[i,j]-128)>128):
input(">")
a+=float(gray_img[i,j]-128)
hist[int(gray_img[i,j])]+=1
da = a /float(h*w)
print(da)
for i in range(256):
Ma+=abs(i-128-da)*hist[i]
Ma /= float(h*w)
print(Ma)
cast=abs(da)/abs(Ma)
return cast
'''
def upload(request):
if request.method == 'POST':
file = request.FILES.get('file') #获取前端上传的文件
m_model=request.POST.get('model')
m_class=request.POST.get('class')
fix = datetime.now().strftime('%Y%m%d%H%M%S%f')+'1' #给文件加前缀防止文件名重复
#以下用绝对路径存储文件,之前我用相对路径一直写不对
curPath = os.path.abspath( os.path.dirname( __file__ ) )
img_path = os.path.abspath(curPath+'/static/upload/'+fix+file.name)
#返回给前端的图片路径用相对路径,前端用绝对路径反而加载不了图片
img_path_res = '/static/detected/'+fix+file.name
print(img_path)
f = open(img_path,'wb')
for i in file.chunks():
f.write(i)
f.close()
result = adb_shell("ImageDetect/build/ImageDetect {}".format(img_path))
if result[0]==0:
cast = result[1].split("||")[0]
da = result[1].split("||")[1]
satu = result[1].split("||")[2]
CON = result[1].split("||")[3]
ASM = result[1].split("||")[4]
ENT = result[1].split("||")[5]
MEAN = result[1].split("||")[6]
sb = result[1].split("||")[7]
lap = result[1].split("||")[8]
dev = result[1].split("||")[9]
print(cast, da, satu)
mesg = "颜色分布(0-1合理):{}&nbsp;&nbsp;&nbsp;分布方差(负数偏暗):{}&nbsp;&nbsp;&nbsp;色彩饱和度(零为补光):{}<br/>对比度:{}&nbsp;&nbsp;&nbsp;角度方向二阶矩:{}&nbsp;&nbsp;&nbsp;熵:{}&nbsp;&nbsp;&nbsp;平均值:{}&nbsp;&nbsp;&nbsp;Sobel:{}&nbsp;&nbsp;&nbsp;Lap:{}&nbsp;&nbsp;&nbsp;dev:{}".format(cast, da, satu, CON, ASM, ENT, MEAN,sb,lap,dev)
if(int(m_class)==1):
if(int(m_model)==1): #小模型
RR = DL.run(weights=(curPath+"/yolov5/v3s.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火 小模型】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_model)==2): #大模型
RR = DL.run(weights=(curPath+"/yolov5/v6m6.pt"), source=img_path, imgsz=(1280, 1280), project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火 大模型】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_model)==3): #泛模型
RR = DL.run(weights=(curPath+"/yolov5/best.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火 泛模型】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==2):
RR = DL.run(weights=(curPath+"/yolov5/yolov5s.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【一般物体检测】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==3):
RR = DL.run(weights=(curPath+"/yolov5/fire_smoke_towercrane.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火+塔吊】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==4):
RR = DL.run(weights=(curPath+"/yolov5/vehicle_4class_ljp_v3.0.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【外破四类】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==5):
RR = DL.run(weights=(curPath+"/yolov5/vehicle_4class_ljp_v7.0.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【外破四类】"+RR+' <br/> '+mesg, 'flag': True})
def dlurl(request):
if request.method == 'GET':
url = request.GET["url"] #获取前端上传的文件
m_model=request.GET["model"]
m_class=request.GET["class"]
print("m_model:",m_model)
print("url:",url)
name = url.split(r'/')[-1]
if not (name.endswith(".jpg") or name.endswith(".jpeg") or name.endswith(".png") or name.endswith(".bmp")):
return JsonResponse({'flag': False})
fix = datetime.now().strftime('%Y%m%d%H%M%S%f')+'1' #给文件加前缀防止文件名重复
curPath = os.path.abspath( os.path.dirname( __file__ ) )
img_path = os.path.abspath(curPath+'/static/download/'+fix+name)
r = requests.get(url)
# 保存
with open (img_path, 'wb') as f:
f.write(r.content)
f.close
#返回给前端的图片路径用相对路径,前端用绝对路径反而加载不了图片
img_path_res = '/static/detected/'+fix+name
result = adb_shell("ImageDetect/build/ImageDetect {}".format(img_path))
if result[0]==0:
cast = result[1].split("||")[0]
da = result[1].split("||")[1]
satu = result[1].split("||")[2]
CON = result[1].split("||")[3]
ASM = result[1].split("||")[4]
ENT = result[1].split("||")[5]
MEAN = result[1].split("||")[6]
sb = result[1].split("||")[7]
lap = result[1].split("||")[8]
dev = result[1].split("||")[9]
print(cast, da, satu)
mesg = "颜色分布(0-1合理):{}&nbsp;&nbsp;&nbsp;分布方差(负数偏暗):{}&nbsp;&nbsp;&nbsp;色彩饱和度(零为补光):{}<br/>对比度:{}&nbsp;&nbsp;&nbsp;角度方向二阶矩:{}&nbsp;&nbsp;&nbsp;熵:{}&nbsp;&nbsp;&nbsp;平均值:{}&nbsp;&nbsp;&nbsp;Sobel:{}&nbsp;&nbsp;&nbsp;Lap:{}&nbsp;&nbsp;&nbsp;dev:{}".format(cast, da, satu, CON, ASM, ENT, MEAN,sb,lap,dev)
if(int(m_class)==1):
if(int(m_model)==1): #小模型
RR = DL.run(weights=(curPath+"/yolov5/v3s.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火 小模型】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_model)==2): #大模型
RR = DL.run(weights=(curPath+"/yolov5/v6m6.pt"), source=img_path, imgsz=(1280, 1280), project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火 大模型】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_model)==3): #泛模型
RR = DL.run(weights=(curPath+"/yolov5/best.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火 泛模型】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==2):
RR = DL.run(weights=(curPath+"/yolov5/yolov5s.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【一般物体检测】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==3):
RR = DL.run(weights=(curPath+"/yolov5/fire_smoke_towercrane.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【烟火+塔吊】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==4):
RR = DL.run(weights=(curPath+"/yolov5/vehicle_4class_ljp_v3.0.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【外破四类】"+RR+' <br/> '+mesg, 'flag': True})
if(int(m_class)==5):
RR = DL.run(weights=(curPath+"/yolov5/vehicle_4class_ljp_v7.0.pt"), source=img_path, project=(curPath+"/static/detected"))
return JsonResponse({'img_name':img_path_res,'code':"【外破四类】"+RR+' <br/> '+mesg, 'flag': True})
def home(request):
return render(request, 'home.html')