From 9a7df21526d09114a87f4b8106f7f38805593277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=89=8D=E6=98=8E?= <909355014@qq.com> Date: Mon, 19 Sep 2022 10:32:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=97=E6=B3=95=E5=85=A8=E9=83=A8=E9=87=87?= =?UTF-8?q?=E7=94=A8url=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ai/client.py | 67 ++++++++++++++++++++++++++++++----------------- apps/ai/main.py | 10 +++---- apps/ecm/tasks.py | 2 +- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/apps/ai/client.py b/apps/ai/client.py index 1011db20..9f308377 100644 --- a/apps/ai/client.py +++ b/apps/ai/client.py @@ -5,6 +5,8 @@ from io import BytesIO import os import requests import grpc + +from apps.ecm.service import save_dahua_pic from . import BeiHangGrpc_pb2_grpc from . import BeiHangGrpc_pb2 import uuid @@ -12,29 +14,29 @@ from django.conf import settings import cv2 requests.packages.urllib3.disable_warnings() +# 本地图像输入 +# image_path = "./onnx/image/single3.jpg" +# with open(image_path, 'rb') as f: +# image_base64 = str(base64.b64encode(f.read()), encoding='utf-8') +# request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id,imgsbase64=image_base64) +# base64img = str(base64.b64encode(BytesIO(requests.get(url=pic_url, verify=False).content).read()), 'utf-8') +# with open('face_test.txt', 'w') as f: +# f.write(base64img) +# request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) + def helmet(ip: str, pic_url: str): - """安全帽事件 + """安全帽事件(抠图) Args: ip (_type_): _description_ pic_url (_type_): _description_ """ - # print(pic_url) channel = grpc.insecure_channel(ip+':2000') # 监听频道 stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub(channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接 image_id = 3 - - # 本地图像输入 - # image_path = "./onnx/image/single3.jpg" - # with open(image_path, 'rb') as f: - # image_base64 = str(base64.b64encode(f.read()), encoding='utf-8') - # request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id,imgsbase64=image_base64) - base64img = str(base64.b64encode(BytesIO(requests.get(url=pic_url, verify=False).content).read()), 'utf-8') - # with open('face_test.txt', 'w') as f: - # f.write(base64img) - request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgsbase64=base64img) - # request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) + img_path = save_dahua_pic(pic_url) + request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path) response = stub.sendHelmet_Info(request) if response.helmetinfoList: for i in response.helmetinfoList: @@ -43,23 +45,40 @@ def helmet(ip: str, pic_url: str): return False, response +def helme2(ip: str, pic_url: str): + """安全帽事件(全图) + + Args: + ip (_type_): _description_ + pic_url (_type_): _description_ + """ + channel = grpc.insecure_channel(ip+':2002') # 监听频道 + stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub(channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接 + 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) + 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: # 未戴安全帽事件成立 + return True, response + return False, response + + def fire1(ip: str, pic_url: str): # 未配置灭火器事件 channel = grpc.insecure_channel(ip+':2001') stub = BeiHangGrpc_pb2_grpc.Fire_RegStub(channel) image_id = 3 - img_name = '{}.png'.format(uuid.uuid4()) - img_path = settings.BASE_DIR + '/temp/' + img_name - with open(img_path, "wb") as f: - f.write(requests.get(url=pic_url, verify=False).content) - 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, imgsbase64=image_base64) + 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) response = stub.sendFire_Info(request) - os.remove(img_path) # 删除临时图片 + os.remove(settings.BASE_DIR + img_path) # 删除临时图片 if response.FireinfoList: for i in response.FireinfoList: if i.fire == 0: # 配置了灭火器 diff --git a/apps/ai/main.py b/apps/ai/main.py index 5f11e860..57f1c3d5 100644 --- a/apps/ai/main.py +++ b/apps/ai/main.py @@ -7,12 +7,13 @@ myLogger = logging.getLogger('log') algo_dict = { "helmet": "apps.ai.client.helmet", + "helmet2": "apps.ai.client.helmet2", "fire1": "apps.ai.client.fire1" } def ai_analyse(codes: list, global_img: str, face_img: str = None): - """多线程进行算法分析图片 + """算法分析图片 Args: codes: 算法列表 @@ -27,11 +28,8 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None): f = getattr(m, func) try: is_happend, res = False, None - if i == 'helmet': # 如果是安全帽识别 - if face_img: - is_happend, res = f(ip=settings.AI_IP, pic_url=face_img) - else: - is_happend, res = f(ip=settings.AI_IP, pic_url=global_img) + if i == 'helmet' and face_img: # 如果是安全帽抠图识别 + is_happend, res = f(ip=settings.AI_IP, pic_url=face_img) else: is_happend, res = f(ip=settings.AI_IP, pic_url=global_img) if is_happend: diff --git a/apps/ecm/tasks.py b/apps/ecm/tasks.py index d210ccc7..38c81231 100644 --- a/apps/ecm/tasks.py +++ b/apps/ecm/tasks.py @@ -86,7 +86,7 @@ def opl_task(vc_codes: list, opl_id: str): algo_codes = list(EventCate.objects.filter(opl_cates=opl_cate).values_list('code', flat=True)) vchannels = TDevice.objects.filter(code__in=vc_codes) opl = Opl.objects.get(id=opl_id) - while time.time()-start_time < 28800: # 一次任务不手动关闭最多持续8小时 + while time.time()-start_time < 14400: # 一次任务不手动关闭最多持续8小时 for i in vchannels: Thread(target=snap_and_analyse, args=(i, algo_codes, opl)).start() time.sleep(10)