去除本地存储 | 优化代码风格
This commit is contained in:
		
							
								
								
									
										30
									
								
								core/all.py
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								core/all.py
									
									
									
									
									
								
							| @ -8,7 +8,7 @@ import numpy as np | |||||||
|  |  | ||||||
| # -------------------------- 核心配置参数 -------------------------- | # -------------------------- 核心配置参数 -------------------------- | ||||||
| MAX_WORKERS = 6  # 线程池最大线程数 | MAX_WORKERS = 6  # 线程池最大线程数 | ||||||
| DETECTION_ORDER = ["yolo", "face", "ocr"]  # 检测优先级顺序 | DETECTION_ORDER = ["yolo", "face", "ocr"]  # 检测执行顺序 | ||||||
| TIMEOUT = 30  # 检测超时时间(秒) 【确保此常量可被外部导入】 | TIMEOUT = 30  # 检测超时时间(秒) 【确保此常量可被外部导入】 | ||||||
|  |  | ||||||
| # -------------------------- 全局状态管理 -------------------------- | # -------------------------- 全局状态管理 -------------------------- | ||||||
| @ -80,30 +80,30 @@ def shutdown(): | |||||||
|  |  | ||||||
| # -------------------------- 检测逻辑实现 -------------------------- | # -------------------------- 检测逻辑实现 -------------------------- | ||||||
| def _detect_in_thread(frame: np.ndarray, task_id: int) -> tuple: | def _detect_in_thread(frame: np.ndarray, task_id: int) -> tuple: | ||||||
|     """在子线程中执行检测逻辑(返回4元素tuple:是否成功、结果、检测器类型、任务ID)""" |     """在子线程中执行检测逻辑(返回4元素tuple:检测是否成功、结果数据、检测器类型、任务ID)""" | ||||||
|     thread_name = threading.current_thread().name |     thread_name = threading.current_thread().name | ||||||
|     print(f"任务[{task_id}] 开始执行、线程: {thread_name}") |     print(f"任务[{task_id}] 开始执行、线程: {thread_name}") | ||||||
|  |  | ||||||
|     try: |     try: | ||||||
|         # 按照优先级执行检测 |         # 按照配置顺序执行检测 | ||||||
|         for detector in DETECTION_ORDER: |         for detector in DETECTION_ORDER: | ||||||
|             if detector == "yolo": |             if detector == "yolo": | ||||||
|                 flag, result = yoloDetect(frame) |                 success, result = yoloDetect(frame) | ||||||
|             elif detector == "face": |             elif detector == "face": | ||||||
|                 flag, result = faceDetect(frame) |                 success, result = faceDetect(frame) | ||||||
|             elif detector == "ocr": |             elif detector == "ocr": | ||||||
|                 flag, result = ocrDetect(frame) |                 success, result = ocrDetect(frame) | ||||||
|             else: |             else: | ||||||
|                 flag, result = False, None |                 success, result = False, None | ||||||
|  |  | ||||||
|             print(f"任务[{task_id}] {detector}检测结果: {'成功' if flag else '失败'}") |             print(f"任务[{task_id}] {detector}检测状态: {'成功' if success else '未检测到内容'}") | ||||||
|             if flag: |             if success: | ||||||
|                 print(f"任务[{task_id}] 完成检测、使用检测器: {detector}") |                 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}] 所有检测器均未检测到内容") |         print(f"任务[{task_id}] 所有检测器均未检测到有效内容") | ||||||
|         return (False, "未检测到任何内容", "none", task_id)  # 4元素tuple |         return (False, "未检测到任何有效内容", "none", task_id)  # 4元素tuple | ||||||
|  |  | ||||||
|     except Exception as e: |     except Exception as e: | ||||||
|         print(f"任务[{task_id}] 检测过程发生错误: {str(e)}") |         print(f"任务[{task_id}] 检测过程发生错误: {str(e)}") | ||||||
| @ -119,7 +119,11 @@ def detect(frame: np.ndarray) -> Future: | |||||||
|         frame: 待检测图像(ndarray格式、cv2.imdecode生成) |         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: |     if not _model_loaded: | ||||||
|  | |||||||
							
								
								
									
										22
									
								
								ws/ws.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								ws/ws.py
									
									
									
									
									
								
							| @ -93,7 +93,7 @@ class ClientConnection: | |||||||
|             print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 帧消费逻辑错误 - {str(e)}") |             print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 帧消费逻辑错误 - {str(e)}") | ||||||
|  |  | ||||||
|     async def process_frame(self, frame_data: bytes) -> None: |     async def process_frame(self, frame_data: bytes) -> None: | ||||||
|         """处理单帧图像数据(【核心修改:等待检测结果+修正解包】)""" |         """处理单帧图像数据""" | ||||||
|         # 二进制数据转OpenCV图像 |         # 二进制数据转OpenCV图像 | ||||||
|         nparr = np.frombuffer(frame_data, np.uint8) |         nparr = np.frombuffer(frame_data, np.uint8) | ||||||
|         img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |         img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) | ||||||
| @ -101,16 +101,8 @@ class ClientConnection: | |||||||
|             print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 无法解析图像数据") |             print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 无法解析图像数据") | ||||||
|             return |             return | ||||||
|  |  | ||||||
|         # 确保图像保存目录存在 |  | ||||||
|         os.makedirs('images', exist_ok=True) |  | ||||||
|  |  | ||||||
|         # 保存图像(按IP+时间戳命名、避免冲突) |  | ||||||
|         filename = f"images/{self.client_ip.replace('.', '_')}_{get_current_time_file_str()}.jpg" |  | ||||||
|         try: |         try: | ||||||
|             cv2.imwrite(filename, img) |             # -------------------------- 提交检测任务并等待结果 -------------------------- | ||||||
|             print(f"[{get_current_time_str()}] 图像已保存至: {filename}") |  | ||||||
|  |  | ||||||
|             # -------------------------- 【核心修改1:提交检测任务并等待结果】 -------------------------- |  | ||||||
|             # 1. 提交检测任务获取Future对象(非阻塞) |             # 1. 提交检测任务获取Future对象(非阻塞) | ||||||
|             detection_future = detect(img) |             detection_future = detect(img) | ||||||
|             # 2. 用asyncio.to_thread等待Future结果(避免阻塞asyncio事件循环),设置超时 |             # 2. 用asyncio.to_thread等待Future结果(避免阻塞asyncio事件循环),设置超时 | ||||||
| @ -129,13 +121,11 @@ class ClientConnection: | |||||||
|                 task_id = -1  # 超时任务ID标记为-1 |                 task_id = -1  # 超时任务ID标记为-1 | ||||||
|             # ----------------------------------------------------------------------------------------- |             # ----------------------------------------------------------------------------------------- | ||||||
|  |  | ||||||
|             # -------------------------- 【核心修改2:修正日志打印变量名】 -------------------------- |             # 打印检测结果 | ||||||
|             # 打印检测结果(避免使用Python关键字"type") |  | ||||||
|             print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 检测结果 - " |             print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 检测结果 - " | ||||||
|                   f"违规: {has_violation}, 类型: {detector_type}, 数据: {data}, 任务ID: {task_id}") |                   f"违规: {has_violation}, 类型: {detector_type}, 数据: {data}, 任务ID: {task_id}") | ||||||
|             # ----------------------------------------------------------------------------------------- |  | ||||||
|  |  | ||||||
|             # 处理违规逻辑(变量名从type改为detector_type) |             # 处理违规逻辑 | ||||||
|             if has_violation: |             if has_violation: | ||||||
|                 print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 检测到违规 - " |                 print(f"[{get_current_time_str()}] 客户端{self.client_ip}: 检测到违规 - " | ||||||
|                       f"类型: {detector_type}, 详情: {data}") |                       f"类型: {detector_type}, 详情: {data}") | ||||||
| @ -151,9 +141,7 @@ class ClientConnection: | |||||||
|                 danger_msg = { |                 danger_msg = { | ||||||
|                     "type": "danger", |                     "type": "danger", | ||||||
|                     "timestamp": get_current_time_str(), |                     "timestamp": get_current_time_str(), | ||||||
|                     "client_ip": self.client_ip, |                     "client_ip": self.client_ip | ||||||
|                     "detector_type": detector_type, |  | ||||||
|                     "detail": str(data) |  | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 # TODO 数据存储到数据库 |                 # TODO 数据存储到数据库 | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user