算法识别结果尝试加入矩形框
This commit is contained in:
parent
19cbd6dd2a
commit
c05add8952
|
@ -83,13 +83,13 @@ def fire1(ip: str, pic_url: str):
|
|||
return True, response
|
||||
|
||||
|
||||
def fangtang(ip: str, pic_url: str):
|
||||
def fangtangfu(ip: str, pic_url: str):
|
||||
# 未穿防烫服事件
|
||||
channel = grpc.insecure_channel(ip+':2003')
|
||||
stub = BeiHangGrpc_pb2_grpc.FA_RegStub(channel)
|
||||
image_id = 4
|
||||
request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url)
|
||||
response = stub.sendFire_Info(request)
|
||||
response = stub.sendFA_Info(request)
|
||||
if response.FAinfo == False:
|
||||
return True, response
|
||||
return False, response
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
|
||||
import importlib
|
||||
from operator import is_
|
||||
import os
|
||||
import cv2
|
||||
from django.conf import settings
|
||||
import logging
|
||||
|
||||
|
@ -10,27 +12,28 @@ myLogger = logging.getLogger('log')
|
|||
algo_dict = {
|
||||
"helmet": "apps.ai.client.helmet",
|
||||
"fire1": "apps.ai.client.fire1",
|
||||
"fangtang": "apps.ai.client.fangtang",
|
||||
"fangtangfu": "apps.ai.client.fangtangfu",
|
||||
"jingjiedai": "apps.ai.client.jingjiedai",
|
||||
"qiping": "apps.ai.client.qiping"
|
||||
}
|
||||
|
||||
|
||||
def ai_analyse(codes: list, global_img: str, face_img: str = None):
|
||||
def ai_analyse(codes: list, global_img: str, face_img: str = None, is_dahua_pic: bool = True):
|
||||
"""算法分析图片
|
||||
|
||||
Args:
|
||||
codes: 算法列表
|
||||
global_img (str): 全景图片地址
|
||||
face_img (str): 人脸图片地址
|
||||
global_img (str): 全景图片url地址
|
||||
face_img (str): 人脸图片url地址
|
||||
"""
|
||||
from apps.ecm.service import save_dahua_pic
|
||||
results = {} # dict key: 触发的事件标识字符 value: 多个矩形框坐标列表
|
||||
global_img_path = save_dahua_pic(global_img)
|
||||
global_img = settings.BASE_URL_OUT + global_img_path
|
||||
if face_img:
|
||||
face_img_path = save_dahua_pic(face_img)
|
||||
face_img = settings.BASE_URL_OUT + face_img_path
|
||||
results = {} # dict key: 触发的事件标识字符 value: 多个矩形框坐标列表; 有两个图片key值
|
||||
if is_dahua_pic: # 先保存到本地/主要是现在算法必须得用可访问的远程地址
|
||||
from apps.ecm.service import save_dahua_pic
|
||||
global_img_path = save_dahua_pic(global_img)
|
||||
global_img = settings.BASE_URL_OUT + global_img_path # 用新地址
|
||||
if face_img:
|
||||
face_img_path = save_dahua_pic(face_img)
|
||||
face_img = settings.BASE_URL_OUT + face_img_path
|
||||
for i in codes:
|
||||
if i in algo_dict and i not in results: # 如果算法支持且没有识别过
|
||||
module, func = algo_dict[i].rsplit(".", 1)
|
||||
|
@ -53,18 +56,51 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None):
|
|||
for x in res.FireinfoList:
|
||||
if x.fire == 3 and 'qiping' in codes: # 气瓶倾倒
|
||||
qiping_qd = True
|
||||
rectangle_dict['qiping'].append([x.coord.x, x.coord.y]) # 加入矩形框
|
||||
rectangle_dict['qiping'].append([(x.coord.uleft.x, x.coord.uleft.y), (x.coord.lright.x, x.coord.lright.y)]) # 加入矩形框
|
||||
if x.fire == 0:
|
||||
has_fire = True
|
||||
if x.fire == 2:
|
||||
has_jingjiedai = True
|
||||
if (i == 'fire1' and not has_fire) or (i == 'jingjiedai' and not has_jingjiedai) or (i == 'qiping' and qiping_qd):
|
||||
results.update({i: {'rectangles': rectangle_dict.get(i, [])}})
|
||||
results.update({i: rectangle_dict.get(i, [])})
|
||||
if is_happend and (i not in results):
|
||||
results.update({i: {'rectangles': rectangle_dict.get(i, [])}})
|
||||
results.update({i: rectangle_dict.get(i, [])})
|
||||
except Exception:
|
||||
myLogger.error('算法处理错误', exc_info=True)
|
||||
os.remove(settings.BASE_DIR + global_img_path) # 删除临时图片
|
||||
if face_img:
|
||||
os.remove(settings.BASE_DIR + face_img_path)
|
||||
if is_dahua_pic:
|
||||
os.remove(settings.BASE_DIR + global_img_path) # 删除临时图片
|
||||
if face_img_path:
|
||||
os.remove(settings.BASE_DIR + face_img_path)
|
||||
return results
|
||||
|
||||
|
||||
def ai_analyse_2(codes: list, global_img: str, face_img: str = None, is_dahua_pic: bool = True):
|
||||
"""算法分析图片后保存并返回相对地址
|
||||
|
||||
Args:
|
||||
codes: 算法列表
|
||||
global_img (str): 全景图片url地址
|
||||
face_img (str): 人脸图片url地址
|
||||
"""
|
||||
results = ai_analyse_2(codes, global_img, face_img, is_dahua_pic)
|
||||
if results: # 如果触发事件先保存下来
|
||||
from apps.ecm.service import save_dahua_pic
|
||||
global_img_path = save_dahua_pic(global_img)
|
||||
global_img_local = settings.BASE_DIR + global_img_path
|
||||
draw(global_img_local, results)
|
||||
results.update({'global_img': global_img_path})
|
||||
return results
|
||||
|
||||
def draw(path, results):
|
||||
rects = []
|
||||
for i in results:
|
||||
rects.extend(results[i])
|
||||
if not rects:
|
||||
img = cv2.imread(path)
|
||||
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
|
||||
for i in results:
|
||||
for m in results[i]:
|
||||
cv2.rectangle(img, results[i][m][0], results[i][m][1], (0, 255, 0), 4)
|
||||
cv2.putText(img, i, results[i][m][0], font, 2, (0,0,255), 1)
|
||||
cv2.imwrite(path, img)
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@ class TestAlgoSerializer(serializers.Serializer):
|
|||
codes = serializers.ListField(child=serializers.CharField(), label='需要识别的算法')
|
||||
face_img = serializers.CharField()
|
||||
global_img = serializers.CharField()
|
||||
is_dahua_pic = serializers.BooleanField()
|
||||
|
||||
|
||||
class SpeakerSerializer(serializers.Serializer):
|
||||
|
|
|
@ -188,10 +188,11 @@ class TestViewSet(CustomGenericViewSet):
|
|||
|
||||
测试算法
|
||||
"""
|
||||
from apps.ai.main import ai_analyse
|
||||
ret = ai_analyse(codes=request.data['codes'],
|
||||
from apps.ai.main import ai_analyse_2
|
||||
ret = ai_analyse_2(codes=request.data['codes'],
|
||||
face_img=request.data.get('face_img', None),
|
||||
global_img=request.data.get('global_img', None))
|
||||
global_img=request.data.get('global_img', None),
|
||||
is_dahua_pic=request.data.get('is_dahua_pic', True))
|
||||
return Response(ret)
|
||||
|
||||
@action(methods=['post'], detail=False, serializer_class=Serializer)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
from datetime import timedelta
|
||||
from apps.vm.models import Visit
|
||||
from apps.ai.main import ai_analyse
|
||||
from apps.ai.main import ai_analyse, ai_analyse_2
|
||||
from apps.opm.models import Operation, Opl, OplWorker
|
||||
from apps.utils.sms import send_sms
|
||||
import requests
|
||||
|
@ -317,7 +317,7 @@ def dispatch_dahua_event(data: dict):
|
|||
else:
|
||||
algo_codes.append(i['algo__code'])
|
||||
if algo_codes:
|
||||
ec_codes = ai_analyse(algo_codes, global_img=global_img_o, face_img=face_img_o) # 算法处理
|
||||
ec_codes = ai_analyse_2(algo_codes, global_img=global_img_o, face_img=face_img_o) # 算法处理
|
||||
for m in ec_codes.keys():
|
||||
for n in algo_channels:
|
||||
if m == n['algo__code']:
|
||||
|
@ -331,7 +331,7 @@ def dispatch_dahua_event(data: dict):
|
|||
cates.append(EventCate.objects.filter(code='miss_lcard').first().id)
|
||||
if cates:
|
||||
event = Event()
|
||||
event.global_img = save_dahua_pic(global_img_o)
|
||||
event.global_img = ec_codes['global_img'] if ec_codes.get('global_img', None) else save_dahua_pic(global_img_o)
|
||||
if face_img_o:
|
||||
event.face_img = save_dahua_pic(face_img_o)
|
||||
event.area = area
|
||||
|
@ -604,7 +604,7 @@ def get_area_from_point(x: int, y: int, floorNo: str, area_fix_id):
|
|||
def snap_and_analyse(vchannel: TDevice, algo_codes: list, opl: Opl = None):
|
||||
global_img_o = dhClient.snap(vchannel.code)
|
||||
happen_time = timezone.now()
|
||||
ec_codes = ai_analyse(algo_codes, global_img=global_img_o) # 算法处理返回的事件结果
|
||||
ec_codes = ai_analyse_2(algo_codes, global_img=global_img_o) # 算法处理返回的事件结果
|
||||
if ec_codes:
|
||||
# 获取本次所有发生事件种类
|
||||
ecs = EventCate.objects.filter(code__in=ec_codes.keys())
|
||||
|
@ -619,7 +619,7 @@ def snap_and_analyse(vchannel: TDevice, algo_codes: list, opl: Opl = None):
|
|||
# ep = Employee.objects.filter(id_number=res[0]['identity']).first()
|
||||
pass
|
||||
event = Event()
|
||||
event.global_img = save_dahua_pic(global_img_o)
|
||||
event.global_img = ec_codes['global_img'] if ec_codes.get('global_img', None) else save_dahua_pic(global_img_o)
|
||||
event.vchannel = vchannel
|
||||
event.area = vchannel.area
|
||||
event.obj_cate = obj_cate
|
||||
|
|
|
@ -2,7 +2,6 @@ from __future__ import absolute_import, unicode_literals
|
|||
from threading import Thread
|
||||
|
||||
from celery import shared_task
|
||||
from apps.ai.main import ai_analyse
|
||||
|
||||
from apps.am.models import Area
|
||||
from apps.ecm.models import EventCate, Eventdo, Event
|
||||
|
|
Loading…
Reference in New Issue