enhance detection logic to prioritize highest confidence results; improve logging for detection data and heartbeat state reports
All checks were successful
Build Backend Application and Docker Image / build-docker (push) Successful in 9m40s

This commit is contained in:
Siwat Sirichai 2025-05-28 20:19:42 +07:00
parent f6014abb7a
commit a6cf9c20c6

55
app.py
View file

@ -114,22 +114,67 @@ async def detect(websocket: WebSocket):
process_time = (time.time() - start_time) * 1000 process_time = (time.time() - start_time) * 1000
logger.debug(f"Detection for camera {camera_id} completed in {process_time:.2f}ms") logger.debug(f"Detection for camera {camera_id} completed in {process_time:.2f}ms")
# Log the raw detection result for debugging
logger.debug(f"Raw detection result for camera {camera_id}:\n{json.dumps(detection_result, indent=2, default=str)}")
# Direct class result (no detections/classifications structure)
if detection_result and isinstance(detection_result, dict) and "class" in detection_result and "confidence" in detection_result:
highest_confidence_detection = {
"class": detection_result.get("class", "none"),
"confidence": detection_result.get("confidence", 1.0),
"box": [0, 0, 0, 0] # Empty bounding box for classifications
}
# Handle case when no detections found or result is empty
elif not detection_result or not detection_result.get("detections"):
# Check if we have classification results
if detection_result and detection_result.get("classifications"):
# Get the highest confidence classification
classifications = detection_result.get("classifications", [])
highest_confidence_class = max(classifications, key=lambda x: x.get("confidence", 0)) if classifications else None
if highest_confidence_class:
highest_confidence_detection = {
"class": highest_confidence_class.get("class", "none"),
"confidence": highest_confidence_class.get("confidence", 1.0),
"box": [0, 0, 0, 0] # Empty bounding box for classifications
}
else:
highest_confidence_detection = {
"class": "none",
"confidence": 1.0,
"box": [0, 0, 0, 0]
}
else:
highest_confidence_detection = {
"class": "none",
"confidence": 1.0,
"box": [0, 0, 0, 0]
}
else:
# Find detection with highest confidence
detections = detection_result.get("detections", [])
highest_confidence_detection = max(detections, key=lambda x: x.get("confidence", 0)) if detections else {
"class": "none",
"confidence": 1.0,
"box": [0, 0, 0, 0]
}
detection_data = { detection_data = {
"type": "imageDetection", "type": "imageDetection",
"cameraIdentifier": camera_id, "cameraIdentifier": camera_id,
"timestamp": time.time(), "timestamp": time.time(),
"data": { "data": {
"detection": detection_result if detection_result else None, "detection": highest_confidence_detection, # Send only the highest confidence detection
"modelId": stream["modelId"], "modelId": stream["modelId"],
"modelName": stream["modelName"] "modelName": stream["modelName"]
} }
} }
if detection_result: if highest_confidence_detection["class"] != "none":
detection_count = len(detection_result.get("detections", [])) logger.info(f"Camera {camera_id}: Detected {highest_confidence_detection['class']} with confidence {highest_confidence_detection['confidence']:.2f} using model {stream['modelName']}")
logger.info(f"Camera {camera_id}: Detected {detection_count} objects with model {stream['modelName']}")
await websocket.send_json(detection_data) await websocket.send_json(detection_data)
logger.debug(f"Sent detection data to client for camera {camera_id}:\n{json.dumps(detection_data, indent=2)}")
return persistent_data return persistent_data
except Exception as e: except Exception as e:
logger.error(f"Error in handle_detection for camera {camera_id}: {str(e)}", exc_info=True) logger.error(f"Error in handle_detection for camera {camera_id}: {str(e)}", exc_info=True)
@ -307,7 +352,7 @@ async def detect(websocket: WebSocket):
"cameraConnections": camera_connections "cameraConnections": camera_connections
} }
await websocket.send_text(json.dumps(state_report)) await websocket.send_text(json.dumps(state_report))
logger.debug("Sent stateReport as heartbeat") logger.debug(f"Sent stateReport as heartbeat: CPU {cpu_usage:.1f}%, Memory {memory_usage:.1f}%, {len(camera_connections)} active cameras")
await asyncio.sleep(HEARTBEAT_INTERVAL) await asyncio.sleep(HEARTBEAT_INTERVAL)
except Exception as e: except Exception as e:
logger.error(f"Error sending stateReport heartbeat: {e}") logger.error(f"Error sending stateReport heartbeat: {e}")