From 19cbd6dd2af700da29c941dd9f3f9ced98aeb1b0 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 27 Oct 2022 10:25:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BC=98=E5=8C=96=E4=B8=80?= =?UTF-8?q?=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ai/client.py | 29 ++++++----------------------- apps/ai/main.py | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/apps/ai/client.py b/apps/ai/client.py index b7cc8c22..37e527fc 100644 --- a/apps/ai/client.py +++ b/apps/ai/client.py @@ -36,9 +36,7 @@ def helmet(ip: str, pic_url: str): stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub( channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接 image_id = 1 - img_path = save_dahua_pic(pic_url) - request = BeiHangGrpc_pb2.JinYu_Request( - zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path) + request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) response = stub.sendHelmet_Info(request) if response.helmetinfoList: for i in response.helmetinfoList: @@ -58,11 +56,8 @@ def helmet2(ip: str, pic_url: str): stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub( channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接 image_id = 2 - img_path = save_dahua_pic(pic_url) - request = BeiHangGrpc_pb2.JinYu_Request( - zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path) + request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) response = stub.sendHelmet_Info(request) - os.remove(settings.BASE_DIR + img_path) if response.helmetinfoList: for i in response.helmetinfoList: if i.head_helmet == 1: # 未戴安全帽事件成立 @@ -75,15 +70,12 @@ def fire1(ip: str, pic_url: str): channel = grpc.insecure_channel(ip+':2001') stub = BeiHangGrpc_pb2_grpc.Fire_RegStub(channel) image_id = 3 - img_path = save_dahua_pic(pic_url) # img = cv2.imread(img_path) # resized_img = cv2.resize(img, None, fx=0.25, fy=0.25) # resized_img = cv2.imencode('.png', resized_img)[1] # image_base64 = str(base64.b64encode(resized_img))[2:-1] - request = BeiHangGrpc_pb2.JinYu_Request( - zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path) + request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) response = stub.sendFire_Info(request) - os.remove(settings.BASE_DIR + img_path) # 删除临时图片 if response.FireinfoList: for i in response.FireinfoList: if i.fire == 0: # 配置了灭火器 @@ -96,11 +88,8 @@ def fangtang(ip: str, pic_url: str): channel = grpc.insecure_channel(ip+':2003') stub = BeiHangGrpc_pb2_grpc.FA_RegStub(channel) image_id = 4 - img_path = save_dahua_pic(pic_url) - request = BeiHangGrpc_pb2.JinYu_Request( - zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path) + request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) response = stub.sendFire_Info(request) - os.remove(settings.BASE_DIR + img_path) # 删除临时图片 if response.FAinfo == False: return True, response return False, response @@ -111,11 +100,8 @@ def jingjiedai(ip: str, pic_url: str): channel = grpc.insecure_channel(ip+':2001') stub = BeiHangGrpc_pb2_grpc.Fire_RegStub(channel) image_id = 3 - img_path = save_dahua_pic(pic_url) - request = BeiHangGrpc_pb2.JinYu_Request( - zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path) + request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) response = stub.sendFire_Info(request) - os.remove(settings.BASE_DIR + img_path) # 删除临时图片 if response.FireinfoList: for i in response.FireinfoList: if i.fire == 2: # 配置了警戒带 @@ -128,11 +114,8 @@ def qiping(ip: str, pic_url: str): channel = grpc.insecure_channel(ip+':2001') stub = BeiHangGrpc_pb2_grpc.Fire_RegStub(channel) image_id = 3 - img_path = save_dahua_pic(pic_url) - request = BeiHangGrpc_pb2.JinYu_Request( - zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path) + request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) response = stub.sendFire_Info(request) - os.remove(settings.BASE_DIR + img_path) # 删除临时图片 if response.FireinfoList: for i in response.FireinfoList: if i.fire == 3: # 发现气瓶倾倒 diff --git a/apps/ai/main.py b/apps/ai/main.py index 2fd4abdd..ebb3e770 100644 --- a/apps/ai/main.py +++ b/apps/ai/main.py @@ -1,7 +1,9 @@ import importlib +import os from django.conf import settings import logging + myLogger = logging.getLogger('log') @@ -9,8 +11,8 @@ algo_dict = { "helmet": "apps.ai.client.helmet", "fire1": "apps.ai.client.fire1", "fangtang": "apps.ai.client.fangtang", - "jingjiedai": "apps.ai.jingjiedai", - "qiping": "apps.ai.qiping" + "jingjiedai": "apps.ai.client.jingjiedai", + "qiping": "apps.ai.client.qiping" } @@ -22,7 +24,13 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None): global_img (str): 全景图片地址 face_img (str): 人脸图片地址 """ + 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 for i in codes: if i in algo_dict and i not in results: # 如果算法支持且没有识别过 module, func = algo_dict[i].rsplit(".", 1) @@ -56,4 +64,7 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None): results.update({i: {'rectangles': 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) return results