Spaces:
Sleeping
Sleeping
Update api/websocket.py
Browse files- api/websocket.py +18 -20
api/websocket.py
CHANGED
|
@@ -11,7 +11,7 @@ from core.state import active_connections
|
|
| 11 |
async def websocket_endpoint(websocket: WebSocket):
|
| 12 |
"""
|
| 13 |
Hard-Locked WebSocket Connection.
|
| 14 |
-
|
| 15 |
"""
|
| 16 |
await websocket.accept()
|
| 17 |
active_connections.append(websocket)
|
|
@@ -26,43 +26,41 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 26 |
continue
|
| 27 |
|
| 28 |
if payload.get("type") == "frame":
|
| 29 |
-
# 1. Decode Image
|
| 30 |
encoded_data = payload["image"].split(',')[1]
|
| 31 |
nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
|
| 32 |
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 33 |
|
| 34 |
-
# 2. RUN SYNCHRONOUS HARD PIPELINE (Locked Step)
|
| 35 |
-
# This will await until EVERY face is recognized or rejected
|
| 36 |
report, stats = await asyncio.to_thread(process_frame_synchronous, frame)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
|
|
|
| 40 |
for entry in report:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
status, time_str = mark_attendance(name)
|
| 46 |
-
|
| 47 |
-
names_and_scores.append(f"{name} ({score}%)")
|
| 48 |
|
| 49 |
-
# Notify UI of match
|
| 50 |
await websocket.send_json({
|
| 51 |
"type": "attendance",
|
| 52 |
"name": name,
|
| 53 |
"time": time_str or "Just Now",
|
| 54 |
"status": "success"
|
| 55 |
})
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
# 5. Tell the frontend we are READY for the next frame
|
| 62 |
-
# The browser stayed locked this whole time!
|
| 63 |
await websocket.send_json({
|
| 64 |
"type": "ready",
|
| 65 |
-
"debug":
|
| 66 |
})
|
| 67 |
|
| 68 |
except WebSocketDisconnect:
|
|
|
|
| 11 |
async def websocket_endpoint(websocket: WebSocket):
|
| 12 |
"""
|
| 13 |
Hard-Locked WebSocket Connection.
|
| 14 |
+
Returns highly detailed Truth Reports for every single face found.
|
| 15 |
"""
|
| 16 |
await websocket.accept()
|
| 17 |
active_connections.append(websocket)
|
|
|
|
| 26 |
continue
|
| 27 |
|
| 28 |
if payload.get("type") == "frame":
|
|
|
|
| 29 |
encoded_data = payload["image"].split(',')[1]
|
| 30 |
nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
|
| 31 |
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 32 |
|
|
|
|
|
|
|
| 33 |
report, stats = await asyncio.to_thread(process_frame_synchronous, frame)
|
| 34 |
|
| 35 |
+
# Build the Truth Report
|
| 36 |
+
results_summary = []
|
| 37 |
+
|
| 38 |
for entry in report:
|
| 39 |
+
name = entry["name"]
|
| 40 |
+
score = entry["score"]
|
| 41 |
+
|
| 42 |
+
if entry["status"] == "match":
|
| 43 |
+
# Success
|
| 44 |
status, time_str = mark_attendance(name)
|
| 45 |
+
results_summary.append(f"✅ {name} ({score}%)")
|
|
|
|
| 46 |
|
|
|
|
| 47 |
await websocket.send_json({
|
| 48 |
"type": "attendance",
|
| 49 |
"name": name,
|
| 50 |
"time": time_str or "Just Now",
|
| 51 |
"status": "success"
|
| 52 |
})
|
| 53 |
+
else:
|
| 54 |
+
# Failed match
|
| 55 |
+
results_summary.append(f"❌ {name} ({score}%)")
|
| 56 |
+
|
| 57 |
+
# Detailed Terminal Output
|
| 58 |
+
debug_msg = f"Faces: {stats['detected']} | "
|
| 59 |
+
debug_msg += " | ".join(results_summary) if results_summary else "No faces found."
|
| 60 |
|
|
|
|
|
|
|
| 61 |
await websocket.send_json({
|
| 62 |
"type": "ready",
|
| 63 |
+
"debug": debug_msg
|
| 64 |
})
|
| 65 |
|
| 66 |
except WebSocketDisconnect:
|