webcam streamer

This commit is contained in:
Siwat Sirichai 2025-02-15 22:28:47 +07:00
parent 827b77011d
commit b12e4ccb7f
2 changed files with 64 additions and 10 deletions

23
app.py
View file

@ -66,25 +66,28 @@ async def detect(websocket: WebSocket):
# Save data you want to persist across frames in the persistent_data dictionary
async def handle_detection(camera_id, stream, frame, websocket, model: YOLO, persistent_data):
try:
boxes = []
highest_conf_box = None
max_conf = -1
for r in model.track(frame, stream=False, persist=True):
for box in r.boxes:
track_id = None
if hasattr(box, "id") and box.id is not None:
track_id = box.id.item()
box_cpu = box.cpu()
boxes.append({
"class": model.names[int(box_cpu.cls[0])],
"confidence": float(box_cpu.conf[0]),
"id": track_id,
})
conf = float(box_cpu.conf[0])
if conf > max_conf and hasattr(box, "id") and box.id is not None:
max_conf = conf
highest_conf_box = {
"class": model.names[int(box_cpu.cls[0])],
"confidence": conf,
"id": box.id.item(),
}
# Broadcast to all subscribers of this URL
detection_data = {
"type": "imageDetection",
"cameraIdentifier": camera_id,
"timestamp": time.time(),
"data": {
"detections": boxes,
"detections": highest_conf_box if highest_conf_box else None,
"modelId": stream['modelId'],
"modelName": stream['modelName']
}