去除本地存储 | 优化代码风格
This commit is contained in:
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)}")
|
||||
|
||||
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 数据存储到数据库
|
||||
|
Reference in New Issue
Block a user