From 1dba0327373c30054822a60e139986351cf4a6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=89=8D=E6=98=8E?= <909355014@qq.com> Date: Thu, 27 Oct 2022 21:37:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=97=E6=B3=95img=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ai/main.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/apps/ai/main.py b/apps/ai/main.py index 03ac4246..3c62f982 100644 --- a/apps/ai/main.py +++ b/apps/ai/main.py @@ -17,7 +17,7 @@ algo_dict = { } -def ai_analyse(codes: list, global_img: str, face_img: str = None, is_dahua_pic: bool = True): +def ai_analyse(codes: list, global_img: str, face_img: str = '', is_dahua_pic: bool = True): """算法分析图片 Args: @@ -26,6 +26,8 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None, is_dahua_pic: face_img (str): 人脸图片url地址 """ results = {} # dict key: 触发的事件标识字符 value: 多个矩形框坐标列表; 有两个图片key值 + global_img_path = '' + face_img_path = '' if is_dahua_pic: # 先保存到本地/主要是现在算法必须得用可访问的远程地址 from apps.ecm.service import save_dahua_pic global_img_path = save_dahua_pic(global_img) @@ -55,7 +57,8 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None, is_dahua_pic: for x in res.FireinfoList: if x.fire == 3 and 'qiping' in codes: # 气瓶倾倒 qiping_qd = True - rectangle_dict['qiping'].append([(x.coord.uleft.x, x.coord.uleft.y), (x.coord.lright.x, x.coord.lright.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: @@ -66,14 +69,14 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None, is_dahua_pic: results.update({i: rectangle_dict.get(i, [])}) except Exception: myLogger.error('算法处理错误', exc_info=True) - if is_dahua_pic: + if global_img_path: os.remove(settings.BASE_DIR + global_img_path) # 删除临时图片 - if face_img: - os.remove(settings.BASE_DIR + face_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): +def ai_analyse_2(codes: list, global_img: str, face_img: str = '', is_dahua_pic: bool = True): """算法分析图片后保存并返回相对地址 Args: @@ -82,14 +85,15 @@ def ai_analyse_2(codes: list, global_img: str, face_img: str = None, is_dahua_pi face_img (str): 人脸图片url地址 """ results = ai_analyse(codes, global_img, face_img, is_dahua_pic) - if results: # 如果触发事件先保存下来 + 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: @@ -100,6 +104,5 @@ def draw(path, results): 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.putText(img, i, results[i][m][0], font, 2, (0, 0, 255), 1) cv2.imwrite(path, img) -