去除本地存储 | 优化代码风格

This commit is contained in:
2025-09-08 18:24:32 +08:00
parent 1dd832e18d
commit 2571da3c2d
2 changed files with 24 additions and 32 deletions

View File

@ -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
print(f"任务[{task_id}]: 已提交到线程池")
return future