From 2571da3c2dacdce2fafd1fa78781a6576d6aeff9 Mon Sep 17 00:00:00 2001 From: ninghongbin <2409766686@qq.com> Date: Mon, 8 Sep 2025 18:24:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=9C=AC=E5=9C=B0=E5=AD=98?= =?UTF-8?q?=E5=82=A8=20|=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E9=A3=8E?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/all.py | 34 +++++++++++++++++++--------------- ws/ws.py | 22 +++++----------------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/core/all.py b/core/all.py index 0907e35..4dd1ef9 100644 --- a/core/all.py +++ b/core/all.py @@ -8,7 +8,7 @@ import numpy as np # -------------------------- 核心配置参数 -------------------------- MAX_WORKERS = 6 # 线程池最大线程数 -DETECTION_ORDER = ["yolo", "face", "ocr"] # 检测优先级顺序 +DETECTION_ORDER = ["yolo", "face", "ocr"] # 检测执行顺序 TIMEOUT = 30 # 检测超时时间(秒) 【确保此常量可被外部导入】 # -------------------------- 全局状态管理 -------------------------- @@ -80,30 +80,30 @@ def shutdown(): # -------------------------- 检测逻辑实现 -------------------------- def _detect_in_thread(frame: np.ndarray, task_id: int) -> tuple: - """在子线程中执行检测逻辑(返回4元素tuple:是否成功、结果、检测器类型、任务ID)""" + """在子线程中执行检测逻辑(返回4元素tuple:检测是否成功、结果数据、检测器类型、任务ID)""" thread_name = threading.current_thread().name print(f"任务[{task_id}] 开始执行、线程: {thread_name}") try: - # 按照优先级执行检测 + # 按照配置顺序执行检测 for detector in DETECTION_ORDER: if detector == "yolo": - flag, result = yoloDetect(frame) + success, result = yoloDetect(frame) elif detector == "face": - flag, result = faceDetect(frame) + success, result = faceDetect(frame) elif detector == "ocr": - flag, result = ocrDetect(frame) + success, result = ocrDetect(frame) else: - flag, result = False, None + success, result = False, None - print(f"任务[{task_id}] {detector}检测结果: {'成功' if flag else '失败'}") - if flag: + print(f"任务[{task_id}] {detector}检测状态: {'成功' if success else '未检测到内容'}") + if success: print(f"任务[{task_id}] 完成检测、使用检测器: {detector}") - return (True, result, detector, task_id) # 4元素tuple + return (success, result, detector, task_id) # 4元素tuple # 所有检测器均未检测到结果 - print(f"任务[{task_id}] 所有检测器均未检测到内容") - return (False, "未检测到任何内容", "none", task_id) # 4元素tuple + print(f"任务[{task_id}] 所有检测器均未检测到有效内容") + return (False, "未检测到任何有效内容", "none", task_id) # 4元素tuple except Exception as e: print(f"任务[{task_id}] 检测过程发生错误: {str(e)}") @@ -119,7 +119,11 @@ def detect(frame: np.ndarray) -> Future: frame: 待检测图像(ndarray格式、cv2.imdecode生成) 返回: - Future对象、result()返回tuple: (has_violation, data, detector_type, task_id) + Future对象、result()返回tuple: (success, data, detector_type, task_id) + success: 布尔值,表示是否检测到有效内容 + data: 检测结果数据(成功时为具体结果,失败时为错误信息) + detector_type: 使用的检测器类型("yolo"/"face"/"ocr"/"none"/"error") + task_id: 任务唯一标识 """ # 确保模型已加载 if not _model_loaded: @@ -131,5 +135,5 @@ def detect(frame: np.ndarray) -> Future: # 提交任务到线程池(返回Future) future = _executor.submit(_detect_in_thread, frame, task_id) - print(f"任务[{task_id}]: 已提交到线程池") - return future \ No newline at end of file + print(f"任务[{task_id}]: 已提交到线程池") + return future diff --git a/ws/ws.py b/ws/ws.py index 9e237d2..bcc86b8 100644 --- a/ws/ws.py +++ b/ws/ws.py @@ -93,7 +93,7 @@ class ClientConnection: print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 帧消费逻辑错误 - {str(e)}") async def process_frame(self, frame_data: bytes) -> None: - """处理单帧图像数据(【核心修改:等待检测结果+修正解包】)""" + """处理单帧图像数据""" # 二进制数据转OpenCV图像 nparr = np.frombuffer(frame_data, np.uint8) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) @@ -101,16 +101,8 @@ class ClientConnection: print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 无法解析图像数据") return - # 确保图像保存目录存在 - os.makedirs('images', exist_ok=True) - - # 保存图像(按IP+时间戳命名、避免冲突) - filename = f"images/{self.client_ip.replace('.', '_')}_{get_current_time_file_str()}.jpg" try: - cv2.imwrite(filename, img) - print(f"[{get_current_time_str()}] 图像已保存至: {filename}") - - # -------------------------- 【核心修改1:提交检测任务并等待结果】 -------------------------- + # -------------------------- 提交检测任务并等待结果 -------------------------- # 1. 提交检测任务获取Future对象(非阻塞) detection_future = detect(img) # 2. 用asyncio.to_thread等待Future结果(避免阻塞asyncio事件循环),设置超时 @@ -129,13 +121,11 @@ class ClientConnection: task_id = -1 # 超时任务ID标记为-1 # ----------------------------------------------------------------------------------------- - # -------------------------- 【核心修改2:修正日志打印变量名】 -------------------------- - # 打印检测结果(避免使用Python关键字"type") + # 打印检测结果 print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 检测结果 - " f"违规: {has_violation}, 类型: {detector_type}, 数据: {data}, 任务ID: {task_id}") - # ----------------------------------------------------------------------------------------- - # 处理违规逻辑(变量名从type改为detector_type) + # 处理违规逻辑 if has_violation: print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 检测到违规 - " f"类型: {detector_type}, 详情: {data}") @@ -151,9 +141,7 @@ class ClientConnection: danger_msg = { "type": "danger", "timestamp": get_current_time_str(), - "client_ip": self.client_ip, - "detector_type": detector_type, - "detail": str(data) + "client_ip": self.client_ip } # TODO 数据存储到数据库