wait update RX cam sub

This commit is contained in:
Pongsatorn 2025-08-28 16:46:18 +07:00
parent 5bf2d49e6b
commit 80d9c925de
3 changed files with 530 additions and 60 deletions

View file

@ -822,6 +822,31 @@ def get_camera_stability_data(camera_id, model_id):
return _camera_stability_tracking[camera_id][model_id]
def reset_camera_stability_tracking(camera_id, model_id):
"""Reset all stability tracking data for a specific camera and model."""
if camera_id in _camera_stability_tracking and model_id in _camera_stability_tracking[camera_id]:
stability_data = _camera_stability_tracking[camera_id][model_id]
# Clear all tracking data
track_counters = stability_data["track_stability_counters"]
stable_tracks = stability_data["stable_tracks"]
old_counters = dict(track_counters)
old_stable = list(stable_tracks)
track_counters.clear()
stable_tracks.clear()
# Reset occupancy state to validation
stability_data["occupancy_state"]["phase"] = "validation"
stability_data["occupancy_state"]["absence_counter"] = 0
stability_data["occupancy_state"]["pipeline_completed"] = False
logger.info(f"🧹 Camera {camera_id}: CLEARED stability tracking - old_counters={old_counters}, old_stable={old_stable}")
# Occupancy state reset logging removed - not used in enhanced lightweight mode
else:
logger.debug(f"🧹 Camera {camera_id}: No stability tracking data to clear for model {model_id}")
def update_single_track_stability(node, detection, camera_id, frame_shape=None, stability_threshold=4):
"""Update track stability validation for a single highest confidence car."""
model_id = node.get("modelId", "unknown")
@ -932,21 +957,13 @@ def update_single_track_stability(node, detection, camera_id, frame_shape=None,
logger.debug(f"⏳ Camera {camera_id}: Stable track {current_track_id} still present")
elif current_phase == "occupancy":
# ═══ OCCUPANCY PHASE: Monitor track presence ═══
logger.debug(f"🏢 Camera {camera_id}: OCCUPANCY MONITORING")
logger.debug(f"🏢 Camera {camera_id}: Current track_id: {current_track_id}, Stable tracks: {list(stable_tracks)}")
logger.debug(f"🏢 Camera {camera_id}: Absence counter before: {occupancy_state['absence_counter']}")
# ═══ OCCUPANCY PHASE: UNUSED in enhanced lightweight mode ═══
# This phase is bypassed by the new lightweight mode system
# Keeping minimal logic for backward compatibility but no CLI logging
if current_track_id is not None and current_track_id in stable_tracks:
old_absence = occupancy_state["absence_counter"]
occupancy_state["absence_counter"] = 0
if old_absence > 0:
logger.debug(f"🏢 Camera {camera_id}: Stable car returned - absence counter reset from {old_absence} to 0")
else:
occupancy_state["absence_counter"] += 1
logger.debug(f"🏢 Camera {camera_id}: Stable car absent - absence counter: {occupancy_state['absence_counter']}")
if occupancy_state["absence_counter"] == 1:
logger.info(f"👻 Camera {camera_id}: Stable car disappeared - absence counter: {occupancy_state['absence_counter']}")
# Final return - validation not complete
result = {
@ -1157,11 +1174,11 @@ def occupancy_detector(camera_id, model_id, enable=True):
if enable:
session_state["occupancy_mode"] = True
session_state["occupancy_enabled_at"] = time.time()
logger.info(f"Camera {camera_id}: 🏢 OCCUPANCY MODE ENABLED - model will stop after pipeline completion")
# Occupancy mode logging removed - not used in enhanced lightweight mode
else:
session_state["occupancy_mode"] = False
session_state.pop("occupancy_enabled_at", None)
logger.info(f"Camera {camera_id}: 🔄 OCCUPANCY MODE DISABLED - model will continue running")
# Occupancy mode logging removed - not used in enhanced lightweight mode
return session_state.get("occupancy_mode", False)
@ -1534,7 +1551,7 @@ def run_pipeline(frame, node: dict, return_bbox: bool=False, context=None):
if absence_counter >= max_absence_frames:
# Stable tracks have been absent for too long - trigger "none" detection and reset
logger.info(f"🕒 Camera {camera_id}: OCCUPANCY TIMEOUT - Stable tracks absent for {absence_counter} frames, resetting to validation (sessionId: {backend_session_id or 'none'})")
# Occupancy timeout logging removed - not used in enhanced lightweight mode
# Reset occupancy state to validation phase
stability_data = get_camera_stability_data(camera_id, model_id)
@ -1552,10 +1569,12 @@ def run_pipeline(frame, node: dict, return_bbox: bool=False, context=None):
# Still in occupancy phase - check if stable tracks are present
if stable_tracks_present:
# Stable tracks detected - continue with cached result or light processing
logger.debug(f"👁️ Camera {camera_id}: OCCUPANCY PHASE - stable tracks present: {set(stable_tracks) & set(current_tracks)} (sessionId: {backend_session_id or 'none'})")
# Occupancy phase logging removed - not used in enhanced lightweight mode
pass
else:
# No stable tracks - absence counter was already incremented in track validation
logger.debug(f"👁️ Camera {camera_id}: OCCUPANCY PHASE - stable tracks absent {absence_counter}/{max_absence_frames} (sessionId: {backend_session_id or 'none'})")
# Occupancy phase logging removed - not used in enhanced lightweight mode
pass
# Continue with normal pipeline processing
pass
@ -1760,7 +1779,7 @@ def run_pipeline(frame, node: dict, return_bbox: bool=False, context=None):
model_id = node.get("modelId", "unknown")
# Enable occupancy detector automatically after first successful pipeline
logger.info(f"Camera {camera_id}: Pipeline completed successfully - auto-enabling occupancy mode")
# Auto-enabling occupancy logging removed - not used in enhanced lightweight mode
occupancy_detector(camera_id, model_id, enable=True)
logger.info(f"✅ Camera {camera_id}: Pipeline completed, detection data will be sent to backend")