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
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build Backend Application and Docker Image / build-docker (push) Successful in 9m40s
				
			This commit is contained in:
		
							parent
							
								
									f6014abb7a
								
							
						
					
					
						commit
						a6cf9c20c6
					
				
					 1 changed files with 51 additions and 6 deletions
				
			
		
							
								
								
									
										57
									
								
								app.py
									
										
									
									
									
								
							
							
						
						
									
										57
									
								
								app.py
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -114,22 +114,67 @@ async def detect(websocket: WebSocket):
 | 
			
		|||
            process_time = (time.time() - start_time) * 1000
 | 
			
		||||
            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 = {
 | 
			
		||||
                "type": "imageDetection",
 | 
			
		||||
                "cameraIdentifier": camera_id,
 | 
			
		||||
                "timestamp": time.time(),
 | 
			
		||||
                "data": {
 | 
			
		||||
                    "detection": detection_result if detection_result else None,
 | 
			
		||||
                    "detection": highest_confidence_detection,  # Send only the highest confidence detection
 | 
			
		||||
                    "modelId": stream["modelId"],
 | 
			
		||||
                    "modelName": stream["modelName"]
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            if detection_result:
 | 
			
		||||
                detection_count = len(detection_result.get("detections", []))
 | 
			
		||||
                logger.info(f"Camera {camera_id}: Detected {detection_count} objects with model {stream['modelName']}")
 | 
			
		||||
                
 | 
			
		||||
            if highest_confidence_detection["class"] != "none":
 | 
			
		||||
                logger.info(f"Camera {camera_id}: Detected {highest_confidence_detection['class']} with confidence {highest_confidence_detection['confidence']:.2f} using model {stream['modelName']}")
 | 
			
		||||
            
 | 
			
		||||
            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
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            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
 | 
			
		||||
                }
 | 
			
		||||
                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)
 | 
			
		||||
            except Exception as e:
 | 
			
		||||
                logger.error(f"Error sending stateReport heartbeat: {e}")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue