修改WS兼容检测的Future对象
This commit is contained in:
19
core/all.py
19
core/all.py
@ -9,7 +9,7 @@ import numpy as np
|
||||
# -------------------------- 核心配置参数 --------------------------
|
||||
MAX_WORKERS = 6 # 线程池最大线程数
|
||||
DETECTION_ORDER = ["yolo", "face", "ocr"] # 检测优先级顺序
|
||||
TIMEOUT = 30 # 检测超时时间(秒)
|
||||
TIMEOUT = 30 # 检测超时时间(秒) 【确保此常量可被外部导入】
|
||||
|
||||
# -------------------------- 全局状态管理 --------------------------
|
||||
_executor = None # 线程池实例
|
||||
@ -80,7 +80,7 @@ def shutdown():
|
||||
|
||||
# -------------------------- 检测逻辑实现 --------------------------
|
||||
def _detect_in_thread(frame: np.ndarray, task_id: int) -> tuple:
|
||||
"""在子线程中执行检测逻辑"""
|
||||
"""在子线程中执行检测逻辑(返回4元素tuple:是否成功、结果、检测器类型、任务ID)"""
|
||||
thread_name = threading.current_thread().name
|
||||
print(f"任务[{task_id}] 开始执行、线程: {thread_name}")
|
||||
|
||||
@ -99,27 +99,27 @@ def _detect_in_thread(frame: np.ndarray, task_id: int) -> tuple:
|
||||
print(f"任务[{task_id}] {detector}检测结果: {'成功' if flag else '失败'}")
|
||||
if flag:
|
||||
print(f"任务[{task_id}] 完成检测、使用检测器: {detector}")
|
||||
return (True, result, detector, task_id)
|
||||
return (True, result, detector, task_id) # 4元素tuple
|
||||
|
||||
# 所有检测器均未检测到结果
|
||||
print(f"任务[{task_id}] 所有检测器均未检测到内容")
|
||||
return (False, "未检测到任何内容", "none", task_id)
|
||||
return (False, "未检测到任何内容", "none", task_id) # 4元素tuple
|
||||
|
||||
except Exception as e:
|
||||
print(f"任务[{task_id}] 检测过程发生错误: {str(e)}")
|
||||
return (False, f"检测错误: {str(e)}", "error", task_id)
|
||||
return (False, f"检测错误: {str(e)}", "error", task_id) # 4元素tuple
|
||||
|
||||
|
||||
# -------------------------- 外部调用接口 --------------------------
|
||||
def detect(frame: np.ndarray) -> Future:
|
||||
"""
|
||||
提交检测任务到线程池
|
||||
提交检测任务到线程池(返回Future对象,需调用result()获取4元素结果)
|
||||
|
||||
参数:
|
||||
frame: 待检测图像(ndarray格式、cv2.imdecode生成)
|
||||
|
||||
返回:
|
||||
Future对象、通过result()方法获取检测结果
|
||||
Future对象、result()返回tuple: (has_violation, data, detector_type, task_id)
|
||||
"""
|
||||
# 确保模型已加载
|
||||
if not _model_loaded:
|
||||
@ -129,8 +129,7 @@ def detect(frame: np.ndarray) -> Future:
|
||||
# 生成任务ID
|
||||
task_id = _get_next_task_id()
|
||||
|
||||
# 提交任务到线程池
|
||||
# 提交任务到线程池(返回Future)
|
||||
future = _executor.submit(_detect_in_thread, frame, task_id)
|
||||
print(f"任务[{task_id}]: 已提交到线程池")
|
||||
return future
|
||||
|
||||
return future
|
Reference in New Issue
Block a user