2025-09-02 18:51:50 +08:00
|
|
|
|
import asyncio
|
2025-09-03 16:27:53 +08:00
|
|
|
|
import datetime
|
2025-09-02 18:51:50 +08:00
|
|
|
|
import json
|
2025-09-03 17:02:22 +08:00
|
|
|
|
import os
|
2025-09-02 18:51:50 +08:00
|
|
|
|
from contextlib import asynccontextmanager
|
2025-09-03 17:08:28 +08:00
|
|
|
|
from typing import Dict, Optional, AsyncGenerator
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
2025-09-03 17:02:22 +08:00
|
|
|
|
import cv2
|
2025-09-03 16:27:53 +08:00
|
|
|
|
import numpy as np
|
|
|
|
|
from fastapi import WebSocket, APIRouter, WebSocketDisconnect, FastAPI
|
|
|
|
|
|
2025-09-03 17:02:22 +08:00
|
|
|
|
from ocr.model_violation_detector import MultiModelViolationDetector
|
2025-09-03 17:08:28 +08:00
|
|
|
|
|
2025-09-03 17:02:22 +08:00
|
|
|
|
# 配置文件相对路径(根据实际目录结构调整)
|
2025-09-03 17:08:28 +08:00
|
|
|
|
YOLO_MODEL_PATH = r"D:\Git\bin\video\ocr\models\best.pt"
|
2025-09-03 17:02:22 +08:00
|
|
|
|
FORBIDDEN_WORDS_PATH = r"D:\Git\bin\video\ocr\forbidden_words.txt"
|
|
|
|
|
OCR_CONFIG_PATH = r"D:\Git\bin\video\ocr\config\1.yaml"
|
|
|
|
|
KNOWN_FACES_DIR = r"D:\Git\bin\video\ocr\known_faces"
|
|
|
|
|
|
|
|
|
|
# 创建检测器实例
|
|
|
|
|
detector = MultiModelViolationDetector(
|
|
|
|
|
forbidden_words_path=FORBIDDEN_WORDS_PATH,
|
|
|
|
|
ocr_config_path=OCR_CONFIG_PATH,
|
|
|
|
|
yolo_model_path=YOLO_MODEL_PATH,
|
|
|
|
|
known_faces_dir=KNOWN_FACES_DIR,
|
|
|
|
|
ocr_confidence_threshold=0.5
|
|
|
|
|
)
|
|
|
|
|
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# -------------------------- 配置常量 --------------------------
|
2025-09-03 16:27:53 +08:00
|
|
|
|
HEARTBEAT_INTERVAL = 30 # 心跳检查间隔(秒)
|
2025-09-03 17:02:22 +08:00
|
|
|
|
HEARTBEAT_TIMEOUT = 600 # 客户端超时阈值(秒)
|
2025-09-03 16:27:53 +08:00
|
|
|
|
WS_ENDPOINT = "/ws" # WebSocket端点路径
|
2025-09-03 17:08:28 +08:00
|
|
|
|
FRAME_QUEUE_SIZE = 1 # 帧队列大小限制
|
2025-09-03 16:27:53 +08:00
|
|
|
|
|
|
|
|
|
# -------------------------- 核心数据结构与全局变量 --------------------------
|
2025-09-02 18:51:50 +08:00
|
|
|
|
ws_router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# 客户端连接封装(包含帧队列)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
class ClientConnection:
|
|
|
|
|
def __init__(self, websocket: WebSocket, client_ip: str):
|
|
|
|
|
self.websocket = websocket
|
|
|
|
|
self.client_ip = client_ip
|
2025-09-03 16:27:53 +08:00
|
|
|
|
self.last_heartbeat = datetime.datetime.now()
|
2025-09-03 17:08:28 +08:00
|
|
|
|
self.frame_queue = asyncio.Queue(maxsize=FRAME_QUEUE_SIZE) # 帧队列,长度为1
|
|
|
|
|
self.consumer_task: Optional[asyncio.Task] = None # 消费者任务
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 更新心跳时间
|
2025-09-02 18:51:50 +08:00
|
|
|
|
def update_heartbeat(self):
|
|
|
|
|
self.last_heartbeat = datetime.datetime.now()
|
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 检查是否存活(超时返回False)
|
|
|
|
|
def is_alive(self) -> bool:
|
2025-09-02 18:51:50 +08:00
|
|
|
|
timeout = (datetime.datetime.now() - self.last_heartbeat).total_seconds()
|
2025-09-03 16:27:53 +08:00
|
|
|
|
return timeout < HEARTBEAT_TIMEOUT
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# 启动帧消费任务
|
|
|
|
|
def start_consumer(self):
|
|
|
|
|
self.consumer_task = asyncio.create_task(self.consume_frames())
|
|
|
|
|
return self.consumer_task
|
|
|
|
|
|
|
|
|
|
# 帧消费协程
|
|
|
|
|
async def consume_frames(self) -> None:
|
|
|
|
|
"""从队列中获取帧并进行处理"""
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
# 从队列获取帧数据
|
|
|
|
|
frame_data = await self.frame_queue.get()
|
|
|
|
|
try:
|
|
|
|
|
# 处理帧数据
|
|
|
|
|
await self.process_frame(frame_data)
|
|
|
|
|
finally:
|
|
|
|
|
# 标记任务完成
|
|
|
|
|
self.frame_queue.task_done()
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{self.client_ip}:帧消费任务已取消")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{self.client_ip}:帧处理错误 - {str(e)}")
|
|
|
|
|
|
|
|
|
|
async def process_frame(self, frame_data: bytes) -> None:
|
|
|
|
|
"""处理单帧图像数据"""
|
|
|
|
|
# 将二进制数据转换为NumPy数组(uint8类型)
|
|
|
|
|
nparr = np.frombuffer(frame_data, np.uint8)
|
|
|
|
|
# 解码为图像,返回与cv2.imread相同的格式(BGR通道的ndarray)
|
|
|
|
|
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
|
|
|
|
|
|
|
|
|
# 确保images文件夹存在
|
|
|
|
|
if not os.path.exists('images'):
|
|
|
|
|
os.makedirs('images')
|
|
|
|
|
|
|
|
|
|
# 生成唯一的文件名,包含时间戳和客户端IP,避免文件名冲突
|
|
|
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
|
|
|
|
filename = f"images/{self.client_ip.replace('.', '_')}_{timestamp}.jpg"
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 保存图像到本地
|
|
|
|
|
cv2.imwrite(filename, img)
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 图像已保存至:{filename}")
|
|
|
|
|
|
|
|
|
|
# 进行检测
|
|
|
|
|
if img is not None:
|
|
|
|
|
has_violation, violation_type, details = detector.detect_violations(img)
|
|
|
|
|
if has_violation:
|
|
|
|
|
print(f"检测到违规 - 类型: {violation_type}, 详情: {details}")
|
|
|
|
|
# 可以在这里添加发送检测结果回客户端的逻辑
|
|
|
|
|
await self.websocket.send_json({
|
|
|
|
|
"type": "detection_result",
|
|
|
|
|
"has_violation": has_violation,
|
|
|
|
|
"violation_type": violation_type,
|
|
|
|
|
"details": details,
|
|
|
|
|
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
})
|
|
|
|
|
else:
|
|
|
|
|
print("未检测到任何违规内容")
|
|
|
|
|
else:
|
|
|
|
|
print(f"无法解析图像数据")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 图像处理错误 - {str(e)}")
|
|
|
|
|
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 全局连接管理(IP -> 连接实例)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
connected_clients: Dict[str, ClientConnection] = {}
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 心跳任务(全局引用,用于关闭时清理)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
heartbeat_task: Optional[asyncio.Task] = None
|
|
|
|
|
|
|
|
|
|
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# -------------------------- 心跳检查逻辑 --------------------------
|
2025-09-02 18:51:50 +08:00
|
|
|
|
async def heartbeat_checker():
|
|
|
|
|
while True:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
# 1. 筛选超时客户端(避免遍历中修改字典)
|
|
|
|
|
timeout_ips = [ip for ip, conn in connected_clients.items() if not conn.is_alive()]
|
|
|
|
|
|
|
|
|
|
# 2. 处理超时连接(关闭+移除)
|
|
|
|
|
if timeout_ips:
|
|
|
|
|
print(f"[{now:%H:%M:%S}] 心跳检查:{len(timeout_ips)}个客户端超时({timeout_ips})")
|
|
|
|
|
for ip in timeout_ips:
|
2025-09-02 18:51:50 +08:00
|
|
|
|
try:
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# 取消消费者任务
|
|
|
|
|
if connected_clients[ip].consumer_task and not connected_clients[ip].consumer_task.done():
|
|
|
|
|
connected_clients[ip].consumer_task.cancel()
|
2025-09-03 16:27:53 +08:00
|
|
|
|
await connected_clients[ip].websocket.close(code=1008, reason="心跳超时")
|
2025-09-02 18:51:50 +08:00
|
|
|
|
finally:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
connected_clients.pop(ip, None)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
else:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
print(f"[{now:%H:%M:%S}] 心跳检查:{len(connected_clients)}个客户端在线,无超时")
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 3. 等待下一轮检查
|
|
|
|
|
await asyncio.sleep(HEARTBEAT_INTERVAL)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
|
|
|
|
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# -------------------------- 应用生命周期 --------------------------
|
2025-09-02 18:51:50 +08:00
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
global heartbeat_task
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 启动心跳任务
|
2025-09-02 18:51:50 +08:00
|
|
|
|
heartbeat_task = asyncio.create_task(heartbeat_checker())
|
2025-09-03 16:27:53 +08:00
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 心跳任务启动(ID:{id(heartbeat_task)})")
|
|
|
|
|
yield
|
|
|
|
|
# 关闭时取消心跳任务
|
2025-09-02 18:51:50 +08:00
|
|
|
|
if heartbeat_task and not heartbeat_task.done():
|
|
|
|
|
heartbeat_task.cancel()
|
|
|
|
|
try:
|
2025-09-02 21:52:28 +08:00
|
|
|
|
await heartbeat_task
|
2025-09-03 16:27:53 +08:00
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 心跳任务已取消")
|
2025-09-02 18:51:50 +08:00
|
|
|
|
except asyncio.CancelledError:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
pass
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
|
|
|
|
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# -------------------------- 消息处理 --------------------------
|
2025-09-03 16:27:53 +08:00
|
|
|
|
async def send_heartbeat_ack(client_ip: str):
|
|
|
|
|
"""回复心跳确认"""
|
2025-09-02 18:51:50 +08:00
|
|
|
|
if client_ip not in connected_clients:
|
|
|
|
|
return False
|
|
|
|
|
try:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
ack = {
|
|
|
|
|
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
|
"type": "heartbeat"
|
|
|
|
|
}
|
|
|
|
|
await connected_clients[client_ip].websocket.send_json(ack)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
return True
|
2025-09-03 16:27:53 +08:00
|
|
|
|
except Exception:
|
|
|
|
|
connected_clients.pop(client_ip, None)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
async def handle_text_msg(client_ip: str, text: str, conn: ClientConnection):
|
|
|
|
|
"""处理文本消息(核心:心跳+JSON解析)"""
|
2025-09-02 21:52:28 +08:00
|
|
|
|
try:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
msg = json.loads(text)
|
|
|
|
|
# 仅处理心跳类型消息
|
|
|
|
|
if msg.get("type") == "heartbeat":
|
|
|
|
|
conn.update_heartbeat()
|
|
|
|
|
await send_heartbeat_ack(client_ip)
|
|
|
|
|
else:
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:收到消息:{msg}")
|
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:无效JSON消息")
|
2025-09-02 21:52:28 +08:00
|
|
|
|
|
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
async def handle_binary_msg(client_ip: str, data: bytes):
|
2025-09-03 17:08:28 +08:00
|
|
|
|
"""处理二进制消息(使用队列控制帧处理)"""
|
|
|
|
|
if client_ip not in connected_clients:
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:连接不存在,丢弃{len(data)}字节数据")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
conn = connected_clients[client_ip]
|
|
|
|
|
|
|
|
|
|
# 检查队列是否已满
|
|
|
|
|
if conn.frame_queue.full():
|
|
|
|
|
# 队列已满,丢弃当前帧
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:队列已满,丢弃{len(data)}字节数据")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 队列未满,添加帧到队列
|
|
|
|
|
try:
|
|
|
|
|
# 非阻塞添加(因为已检查队列未满,所以不会阻塞)
|
|
|
|
|
conn.frame_queue.put_nowait(data)
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:已添加{len(data)}字节数据到队列")
|
|
|
|
|
except asyncio.QueueFull:
|
|
|
|
|
# 理论上不会走到这里,因为上面已检查
|
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:队列突然满了,丢弃{len(data)}字节数据")
|
|
|
|
|
|
2025-09-02 21:52:28 +08:00
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# -------------------------- WebSocket核心端点 --------------------------
|
|
|
|
|
@ws_router.websocket(WS_ENDPOINT)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
async def websocket_endpoint(websocket: WebSocket):
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 接受连接 + 获取客户端IP
|
2025-09-02 18:51:50 +08:00
|
|
|
|
await websocket.accept()
|
2025-09-03 16:27:53 +08:00
|
|
|
|
client_ip = websocket.client.host if websocket.client else "unknown"
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
print(f"[{now:%H:%M:%S}] 客户端{client_ip}:连接成功")
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
2025-09-03 17:08:28 +08:00
|
|
|
|
consumer_task = None
|
2025-09-02 18:51:50 +08:00
|
|
|
|
try:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 处理重复连接(关闭旧连接)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
if client_ip in connected_clients:
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# 取消旧连接的消费者任务
|
|
|
|
|
if connected_clients[client_ip].consumer_task and not connected_clients[client_ip].consumer_task.done():
|
|
|
|
|
connected_clients[client_ip].consumer_task.cancel()
|
2025-09-03 16:27:53 +08:00
|
|
|
|
await connected_clients[client_ip].websocket.close(code=1008, reason="同一IP新连接")
|
|
|
|
|
connected_clients.pop(client_ip)
|
|
|
|
|
print(f"[{now:%H:%M:%S}] 客户端{client_ip}:关闭旧连接")
|
|
|
|
|
|
|
|
|
|
# 注册新连接
|
|
|
|
|
new_conn = ClientConnection(websocket, client_ip)
|
|
|
|
|
connected_clients[client_ip] = new_conn
|
2025-09-03 17:08:28 +08:00
|
|
|
|
|
|
|
|
|
# 启动帧消费任务
|
|
|
|
|
consumer_task = new_conn.start_consumer()
|
|
|
|
|
print(f"[{now:%H:%M:%S}] 客户端{client_ip}:注册成功,已启动帧消费任务,当前在线{len(connected_clients)}个")
|
2025-09-03 16:27:53 +08:00
|
|
|
|
|
|
|
|
|
# 循环接收消息
|
2025-09-02 18:51:50 +08:00
|
|
|
|
while True:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
data = await websocket.receive()
|
|
|
|
|
if "text" in data:
|
|
|
|
|
await handle_text_msg(client_ip, data["text"], new_conn)
|
|
|
|
|
elif "bytes" in data:
|
|
|
|
|
await handle_binary_msg(client_ip, data["bytes"])
|
2025-09-02 18:51:50 +08:00
|
|
|
|
|
2025-09-03 16:27:53 +08:00
|
|
|
|
# 异常处理(断开/错误)
|
2025-09-02 18:51:50 +08:00
|
|
|
|
except WebSocketDisconnect as e:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:主动断开(代码:{e.code})")
|
2025-09-02 18:51:50 +08:00
|
|
|
|
except Exception as e:
|
2025-09-03 16:27:53 +08:00
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:连接异常({str(e)[:50]})")
|
2025-09-02 18:51:50 +08:00
|
|
|
|
finally:
|
2025-09-03 17:08:28 +08:00
|
|
|
|
# 清理连接和任务
|
|
|
|
|
if client_ip in connected_clients:
|
|
|
|
|
# 取消消费者任务
|
|
|
|
|
if connected_clients[client_ip].consumer_task and not connected_clients[client_ip].consumer_task.done():
|
|
|
|
|
connected_clients[client_ip].consumer_task.cancel()
|
|
|
|
|
connected_clients.pop(client_ip, None)
|
2025-09-03 16:27:53 +08:00
|
|
|
|
print(f"[{datetime.datetime.now():%H:%M:%S}] 客户端{client_ip}:连接已清理,当前在线{len(connected_clients)}个")
|