update/car-in_no_fueling//next: car-in_fueling
This commit is contained in:
parent
5875b76d74
commit
72eb7d55ea
7 changed files with 4184 additions and 357 deletions
201
app.py
201
app.py
|
@ -646,10 +646,13 @@ def get_or_init_session_pipeline_state(camera_id):
|
|||
"session_id_received": False,
|
||||
"full_pipeline_completed": False,
|
||||
"absence_counter": 0,
|
||||
"validation_counter": 0, # Counter for validation phase
|
||||
"validation_threshold": 4, # Default validation threshold
|
||||
"max_absence_frames": 3,
|
||||
"yolo_inference_enabled": True, # Controls whether to run YOLO inference
|
||||
"cached_detection_dict": None, # Cached detection dict for lightweight mode
|
||||
"stable_track_id": None # The stable track ID we're monitoring
|
||||
"stable_track_id": None, # The stable track ID we're monitoring
|
||||
"validated_detection": None # Stored detection result from validation phase for full_pipeline reuse
|
||||
}
|
||||
return session_pipeline_states[camera_id]
|
||||
|
||||
|
@ -659,6 +662,16 @@ def update_session_pipeline_mode(camera_id, new_mode, session_id=None):
|
|||
old_mode = state["mode"]
|
||||
state["mode"] = new_mode
|
||||
|
||||
# Reset counters based on mode transition
|
||||
if new_mode == "validation_detecting":
|
||||
# Transitioning to validation mode - reset both counters for fresh start
|
||||
old_validation_counter = state.get("validation_counter", 0)
|
||||
old_absence_counter = state.get("absence_counter", 0)
|
||||
state["validation_counter"] = 0
|
||||
state["absence_counter"] = 0
|
||||
if old_validation_counter > 0 or old_absence_counter > 0:
|
||||
logger.info(f"🧹 Camera {camera_id}: VALIDATION MODE RESET - validation_counter: {old_validation_counter}→0, absence_counter: {old_absence_counter}→0")
|
||||
|
||||
if session_id:
|
||||
state["session_id_received"] = True
|
||||
state["absence_counter"] = 0 # Reset absence counter when session starts
|
||||
|
@ -812,6 +825,7 @@ async def detect(websocket: WebSocket):
|
|||
|
||||
logger.debug(f"🔍 SESSIONID LOOKUP: display='{display_identifier}', session_id={repr(backend_session_id)}, mode='{current_mode}'")
|
||||
logger.debug(f"🔍 Available session_ids: {session_ids}")
|
||||
logger.debug(f"🔍 VALIDATED_DETECTION TRACE: {pipeline_state.get('validated_detection')}")
|
||||
|
||||
# ═══ SESSION ID-BASED PROCESSING MODE ═══
|
||||
if not backend_session_id:
|
||||
|
@ -832,7 +846,8 @@ async def detect(websocket: WebSocket):
|
|||
pipeline_context = {
|
||||
"camera_id": camera_id,
|
||||
"display_id": display_identifier,
|
||||
"backend_session_id": backend_session_id
|
||||
"backend_session_id": backend_session_id,
|
||||
"current_mode": current_mode # Pass current mode to pipeline
|
||||
}
|
||||
|
||||
start_time = time.time()
|
||||
|
@ -880,7 +895,13 @@ async def detect(websocket: WebSocket):
|
|||
"bbox": stable_detection.get("bbox", [0, 0, 0, 0]),
|
||||
"track_id": stable_detection.get("id")
|
||||
}
|
||||
|
||||
# Store validated detection for full_pipeline mode to reuse
|
||||
pipeline_state["validated_detection"] = detection_result.copy()
|
||||
logger.debug(f"🔍 Camera {camera_id}: VALIDATION DEBUG - storing detection_result = {detection_result}")
|
||||
logger.debug(f"🔍 Camera {camera_id}: VALIDATION DEBUG - pipeline_state after storing = {pipeline_state.get('validated_detection')}")
|
||||
logger.info(f"🚗 Camera {camera_id}: SENDING STABLE DETECTION - track ID {detection_result['track_id']}")
|
||||
logger.info(f"💾 Camera {camera_id}: STORED VALIDATED DETECTION for full_pipeline reuse")
|
||||
else:
|
||||
logger.warning(f"⚠️ Camera {camera_id}: Stable tracks found but no matching detection")
|
||||
else:
|
||||
|
@ -917,6 +938,11 @@ async def detect(websocket: WebSocket):
|
|||
"confidence": best_detection.get("confidence", 0.0),
|
||||
"bbox": best_detection.get("bbox", [0, 0, 0, 0])
|
||||
}
|
||||
|
||||
# Store validated detection for full_pipeline mode to reuse
|
||||
pipeline_state["validated_detection"] = detection_result.copy()
|
||||
logger.debug(f"🔍 Camera {camera_id}: BASIC VALIDATION DEBUG - storing detection_result = {detection_result}")
|
||||
logger.info(f"💾 Camera {camera_id}: STORED BASIC VALIDATED DETECTION for full_pipeline reuse")
|
||||
logger.info(f"🎯 Camera {camera_id}: BASIC VALIDATION COMPLETED after {current_count} frames")
|
||||
else:
|
||||
logger.info(f"📊 Camera {camera_id}: Basic validation progress {current_count}/{threshold}")
|
||||
|
@ -955,8 +981,20 @@ async def detect(websocket: WebSocket):
|
|||
|
||||
elif current_mode == "full_pipeline":
|
||||
# ═══ FULL PIPELINE MODE ═══
|
||||
logger.info(f"🔥 Camera {camera_id}: Running FULL PIPELINE (detection + branches + Redis + PostgreSQL)")
|
||||
detection_result = run_pipeline(cropped_frame, model_tree, context=pipeline_context)
|
||||
logger.info(f"🔥 Camera {camera_id}: Running FULL PIPELINE (classification branches + Redis + PostgreSQL)")
|
||||
|
||||
# Use validated detection from validation phase instead of detecting again
|
||||
validated_detection = pipeline_state.get("validated_detection")
|
||||
logger.debug(f"🔍 Camera {camera_id}: FULL_PIPELINE DEBUG - validated_detection = {validated_detection}")
|
||||
logger.debug(f"🔍 Camera {camera_id}: FULL_PIPELINE DEBUG - pipeline_state keys = {list(pipeline_state.keys())}")
|
||||
if validated_detection:
|
||||
logger.info(f"🔄 Camera {camera_id}: Using validated detection for full pipeline: track_id={validated_detection.get('track_id')}")
|
||||
detection_result = run_pipeline(cropped_frame, model_tree, context=pipeline_context, validated_detection=validated_detection)
|
||||
# Clear the validated detection after using it
|
||||
pipeline_state["validated_detection"] = None
|
||||
else:
|
||||
logger.warning(f"⚠️ Camera {camera_id}: No validated detection found for full pipeline - this shouldn't happen")
|
||||
detection_result = run_pipeline(cropped_frame, model_tree, context=pipeline_context)
|
||||
|
||||
if detection_result and isinstance(detection_result, dict):
|
||||
# Cache the full pipeline result
|
||||
|
@ -975,89 +1013,98 @@ async def detect(websocket: WebSocket):
|
|||
else:
|
||||
logger.warning(f"⚠️ Camera {camera_id}: No track_id found in detection_result: {detection_result.keys()}")
|
||||
|
||||
# Ensure we have a cached detection dict for lightweight mode
|
||||
if not pipeline_state.get("cached_detection_dict"):
|
||||
# Create fallback cached detection dict if branch processing didn't populate it
|
||||
fallback_detection = {
|
||||
"carModel": None,
|
||||
"carBrand": None,
|
||||
"carYear": None,
|
||||
"bodyType": None,
|
||||
"licensePlateText": None,
|
||||
"licensePlateConfidence": None
|
||||
}
|
||||
pipeline_state["cached_detection_dict"] = fallback_detection
|
||||
logger.warning(f"⚠️ Camera {camera_id}: Created fallback cached detection dict (branch processing may have failed)")
|
||||
|
||||
# Switch to lightweight mode
|
||||
update_session_pipeline_mode(camera_id, "lightweight")
|
||||
logger.info(f"✅ Camera {camera_id}: Full pipeline completed - switching to LIGHTWEIGHT mode")
|
||||
|
||||
elif current_mode == "lightweight":
|
||||
# ═══ ENHANCED LIGHTWEIGHT MODE ═══
|
||||
# Only run YOLO11n.pt to check stable track presence, use cached detection dict
|
||||
# ═══ SIMPLIFIED LIGHTWEIGHT MODE ═══
|
||||
# Send cached detection dict + check for 2 consecutive empty frames to reset
|
||||
|
||||
stable_track_id = pipeline_state.get("stable_track_id")
|
||||
cached_detection_dict = pipeline_state.get("cached_detection_dict")
|
||||
|
||||
logger.debug(f"🪶 Camera {camera_id}: LIGHTWEIGHT MODE - monitoring stable track_id={stable_track_id}")
|
||||
logger.debug(f"🪶 Camera {camera_id}: LIGHTWEIGHT MODE - stable_track_id={stable_track_id}")
|
||||
|
||||
if not pipeline_state.get("yolo_inference_enabled", True):
|
||||
# YOLO inference disabled - car considered gone, wait for reset
|
||||
# YOLO inference disabled - waiting for reset
|
||||
logger.debug(f"🛑 Camera {camera_id}: YOLO inference disabled - waiting for reset")
|
||||
detection_result = None # Don't send anything
|
||||
else:
|
||||
# Run lightweight YOLO inference to check track presence only (no full pipeline)
|
||||
# Run YOLO inference to check car presence for reset logic
|
||||
from siwatsystem.pympta import run_detection_with_tracking
|
||||
all_detections, regions_dict, track_validation_result = run_detection_with_tracking(cropped_frame, model_tree, pipeline_context)
|
||||
|
||||
# OPTION A: Car presence only (track ID kept for internal use)
|
||||
any_car_detected = len(all_detections) > 0
|
||||
current_tracks = track_validation_result.get("current_tracks", [])
|
||||
|
||||
logger.debug(f"🪶 Camera {camera_id}: LIGHTWEIGHT - any_cars={any_car_detected} (main decision), current_tracks={current_tracks} (internal only)")
|
||||
|
||||
if not any_car_detected:
|
||||
# NO cars detected at all - increment absence counter
|
||||
if any_car_detected:
|
||||
# Car detected - reset absence counter, continue sending cached detection dict
|
||||
pipeline_state["absence_counter"] = 0 # Reset absence since cars are present
|
||||
|
||||
if cached_detection_dict:
|
||||
detection_result = cached_detection_dict # Always send cached data
|
||||
logger.info(f"💾 Camera {camera_id}: LIGHTWEIGHT - car detected, sending cached detection dict")
|
||||
else:
|
||||
logger.warning(f"⚠️ Camera {camera_id}: LIGHTWEIGHT - car detected but no cached detection dict available")
|
||||
detection_result = None
|
||||
else:
|
||||
# No car detected - increment absence counter
|
||||
pipeline_state["absence_counter"] += 1
|
||||
absence_count = pipeline_state["absence_counter"]
|
||||
max_absence = 2 # Changed from 3 to 2 consecutive frames
|
||||
max_absence = 3 # Need 3 consecutive empty frames
|
||||
|
||||
logger.info(f"👻 Camera {camera_id}: NO CARS detected - absence {absence_count}/{max_absence}")
|
||||
logger.info(f"👻 Camera {camera_id}: LIGHTWEIGHT - no car detected (absence {absence_count}/{max_absence})")
|
||||
|
||||
# Check robust AND condition: backend confirmed AND detection confirmed
|
||||
backend_confirmed_gone = (backend_session_id is None)
|
||||
detection_confirmed_gone = (absence_count >= max_absence)
|
||||
|
||||
logger.debug(f"🔍 Camera {camera_id}: Reset conditions - backend_null={backend_confirmed_gone}, absence_2frames={detection_confirmed_gone}")
|
||||
|
||||
if backend_confirmed_gone and detection_confirmed_gone:
|
||||
# BOTH conditions met - RESET TO VALIDATION PHASE
|
||||
logger.info(f"🔄 Camera {camera_id}: ROBUST RESET - both conditions met (backend=null AND absence>=2)")
|
||||
if absence_count >= max_absence:
|
||||
# SIMPLE RESET CONDITION: 2 consecutive empty frames
|
||||
logger.info(f"🔄 Camera {camera_id}: RESET CONDITION MET - {max_absence} consecutive empty frames")
|
||||
|
||||
# Clear all state and prepare for next car
|
||||
cached_full_pipeline_results.pop(camera_id, None)
|
||||
pipeline_state["cached_detection_dict"] = None
|
||||
pipeline_state["stable_track_id"] = None
|
||||
pipeline_state["validated_detection"] = None
|
||||
old_absence_counter = pipeline_state["absence_counter"]
|
||||
old_validation_counter = pipeline_state.get("validation_counter", 0)
|
||||
pipeline_state["absence_counter"] = 0
|
||||
pipeline_state["yolo_inference_enabled"] = True # Re-enable for next car
|
||||
pipeline_state["validation_counter"] = 0 # Clear validation counter
|
||||
pipeline_state["yolo_inference_enabled"] = True
|
||||
|
||||
logger.info(f"🧹 Camera {camera_id}: CLEARING ALL COUNTERS - absence_counter: {old_absence_counter}→0, validation_counter: {old_validation_counter}→0")
|
||||
|
||||
# Clear stability tracking data for this camera
|
||||
from siwatsystem.pympta import reset_camera_stability_tracking
|
||||
reset_camera_stability_tracking(camera_id, model_tree.get("modelId", "unknown"))
|
||||
|
||||
# Switch back to validation phase - ready for next car
|
||||
update_session_pipeline_mode(camera_id, "detection_dict")
|
||||
logger.info(f"🔄 Camera {camera_id}: RESET TO VALIDATION - model ready for next car")
|
||||
# Switch back to validation phase
|
||||
update_session_pipeline_mode(camera_id, "validation_detecting")
|
||||
logger.info(f"✅ Camera {camera_id}: RESET TO VALIDATION COMPLETE - ready for new car")
|
||||
|
||||
detection_result = None # Stop sending data during reset
|
||||
# Now in validation mode - send what YOLO detection finds (will be null since no car)
|
||||
detection_result = {"class": "none", "confidence": 1.0, "bbox": [0, 0, 0, 0]}
|
||||
else:
|
||||
# One or both conditions not met - keep sending cached detection dict
|
||||
# Still within absence threshold - continue sending cached detection dict
|
||||
if cached_detection_dict:
|
||||
detection_result = cached_detection_dict # Always send cached data
|
||||
logger.info(f"⏳ Camera {camera_id}: NO CARS absence {absence_count}/2, backend_null={backend_confirmed_gone} - sending cached detection dict")
|
||||
detection_result = cached_detection_dict # Send cached data
|
||||
logger.info(f"⏳ Camera {camera_id}: LIGHTWEIGHT - no car but absence<{max_absence}, still sending cached detection dict")
|
||||
else:
|
||||
logger.warning(f"⚠️ Camera {camera_id}: NO CARS but no cached detection dict available")
|
||||
logger.warning(f"⚠️ Camera {camera_id}: LIGHTWEIGHT - no cached detection dict available")
|
||||
detection_result = None
|
||||
|
||||
else:
|
||||
# Cars detected - reset absence counter, send cached detection dict
|
||||
pipeline_state["absence_counter"] = 0 # Reset absence since cars are present
|
||||
|
||||
if cached_detection_dict:
|
||||
detection_result = cached_detection_dict # Always send cached data
|
||||
logger.info(f"🪶 Camera {camera_id}: CARS DETECTED - sending cached detection dict:")
|
||||
logger.info(f"🪶 Camera {camera_id}: - Cached dict: {cached_detection_dict}")
|
||||
logger.info(f"🪶 Camera {camera_id}: - Track info (internal): {current_tracks}")
|
||||
else:
|
||||
logger.warning(f"⚠️ Camera {camera_id}: Cars detected but no cached detection dict available")
|
||||
detection_result = None
|
||||
|
||||
elif current_mode == "car_gone_waiting":
|
||||
# ═══ CAR GONE WAITING STATE ═══
|
||||
|
@ -1072,6 +1119,7 @@ async def detect(websocket: WebSocket):
|
|||
pipeline_state["absence_counter"] = 0
|
||||
pipeline_state["stable_track_id"] = None
|
||||
pipeline_state["cached_detection_dict"] = None
|
||||
pipeline_state["validated_detection"] = None
|
||||
|
||||
# Clear stability tracking data for this camera
|
||||
from siwatsystem.pympta import reset_camera_stability_tracking
|
||||
|
@ -1126,35 +1174,26 @@ async def detect(websocket: WebSocket):
|
|||
if backend_session_id:
|
||||
logger.debug(f"🔄 Camera {camera_id}: Note - sessionId {backend_session_id} exists but still in send_detections mode (transition pending)")
|
||||
|
||||
elif detection_result.get("class") == "none":
|
||||
# "None" detection - skip override if lightweight mode already made the decision
|
||||
if current_mode == "lightweight":
|
||||
# Lightweight mode already set detection_result correctly, don't override
|
||||
logger.debug(f"🪶 Camera {camera_id}: Lightweight mode - respecting detection_result decision")
|
||||
if detection_result is None:
|
||||
detection_dict = None
|
||||
logger.info(f"📤 LIGHTWEIGHT SENDING 'NONE' - Reset conditions met for camera {camera_id}")
|
||||
else:
|
||||
# detection_result should be the cached_detection_dict
|
||||
detection_dict = detection_result
|
||||
logger.info(f"💾 LIGHTWEIGHT SENDING CACHED - Maintaining session for camera {camera_id}")
|
||||
elif current_mode == "lightweight":
|
||||
# ═══ SIMPLIFIED LIGHTWEIGHT MODE DETECTION PROCESSING ═══
|
||||
if detection_result.get("class") == "none":
|
||||
# No car detected - this happens when resetting to validation
|
||||
detection_dict = None # Send detection: null
|
||||
logger.info(f"🚫 LIGHTWEIGHT - no car detected, sending detection=null")
|
||||
elif isinstance(detection_result, dict) and ("carBrand" in detection_result or "carModel" in detection_result):
|
||||
# This is a cached detection dict - send it
|
||||
detection_dict = detection_result
|
||||
logger.info(f"💾 LIGHTWEIGHT - sending cached detection dict")
|
||||
else:
|
||||
# Other modes - send null to clear session
|
||||
logger.warning(f"⚠️ LIGHTWEIGHT - unexpected detection_result type: {type(detection_result)}")
|
||||
detection_dict = None
|
||||
logger.info(f"📤 SENDING 'NONE' (detection: null) - Car absent, expecting backend to clear session for camera {camera_id}")
|
||||
elif detection_result.get("cached_mode", False):
|
||||
# Cached mode in lightweight - use cached detection dict directly
|
||||
cached_dict = detection_result.get("branch_results", {})
|
||||
detection_dict = cached_dict if cached_dict else {
|
||||
"carModel": None,
|
||||
"carBrand": None,
|
||||
"carYear": None,
|
||||
"bodyType": None,
|
||||
"licensePlateText": None,
|
||||
"licensePlateConfidence": None
|
||||
}
|
||||
|
||||
elif detection_result.get("class") == "none":
|
||||
# Other modes - send null to clear session
|
||||
detection_dict = None
|
||||
logger.info(f"📤 SENDING 'NONE' (detection: null) - Car absent, expecting backend to clear session for camera {camera_id}")
|
||||
elif detection_result and "carBrand" in detection_result:
|
||||
# Lightweight mode - detection_result IS the cached detection dict
|
||||
# Handle cached detection dict format (fallback for compatibility)
|
||||
detection_dict = detection_result
|
||||
logger.info(f"💾 Camera {camera_id}: LIGHTWEIGHT MODE - using detection_result as detection_dict:")
|
||||
logger.info(f"💾 Camera {camera_id}: - detection_dict: {detection_dict}")
|
||||
|
@ -1235,13 +1274,17 @@ async def detect(websocket: WebSocket):
|
|||
# Backend manages sessionIds independently based on detection content
|
||||
logger.debug(f"TX message prepared (no sessionId) - detection_dict type: {type(detection_dict)}")
|
||||
|
||||
# Log detection details
|
||||
if detection_result and "class" in detection_result and detection_result.get("class") != "none":
|
||||
# Log detection details for different modes
|
||||
if current_mode == "lightweight":
|
||||
if detection_result and detection_result.get("class") == "none":
|
||||
logger.info(f"🚫 Camera {camera_id}: LIGHTWEIGHT - No car detected (resetting to validation)")
|
||||
elif isinstance(detection_result, dict) and ("carBrand" in detection_result or "carModel" in detection_result):
|
||||
logger.info(f"💾 Camera {camera_id}: LIGHTWEIGHT - Sending cached detection data")
|
||||
else:
|
||||
logger.info(f"🪶 Camera {camera_id}: LIGHTWEIGHT - Processing detection")
|
||||
elif detection_result and "class" in detection_result and detection_result.get("class") != "none":
|
||||
confidence = detection_result.get("confidence", 0.0)
|
||||
logger.info(f"Camera {camera_id}: Detected {detection_result['class']} with confidence {confidence:.2f} using model {stream['modelName']}")
|
||||
elif detection_result and "carBrand" in detection_result:
|
||||
# Lightweight mode cached detection dict - different format
|
||||
logger.info(f"Camera {camera_id}: Using cached detection dict (lightweight mode) - {detection_result.get('carBrand', 'Unknown')} {detection_result.get('bodyType', '')}")
|
||||
logger.info(f"🚗 Camera {camera_id}: Detected {detection_result['class']} with confidence {confidence:.2f} using model {stream['modelName']}")
|
||||
|
||||
# Send detection data to backend (session gating handled above in processing logic)
|
||||
logger.debug(f"📤 SENDING TO BACKEND for camera {camera_id}: {json.dumps(detection_data, indent=2)}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue