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

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:

View File

@ -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 数据存储到数据库