Enhance car detection logic with bbox area validation and update .gitignore

This commit is contained in:
Pongsatorn 2025-09-10 23:26:03 +07:00
parent 356da144fc
commit 34ecff244c
3 changed files with 94 additions and 4 deletions

30
app.py
View file

@ -1069,10 +1069,34 @@ async def detect(websocket: WebSocket):
from siwatsystem.pympta import run_lightweight_detection
basic_detection = run_lightweight_detection(cropped_frame, model_tree)
any_car_detected = basic_detection and basic_detection.get("car_detected", False)
logger.debug(f"🔍 Camera {camera_id}: LIGHTWEIGHT - simple car presence check: {any_car_detected}")
# Enhanced car detection: requires both confidence pass AND bbox >= 50% of frame
car_detected_confidence = basic_detection and basic_detection.get("car_detected", False)
car_detected_with_bbox_validation = False
if any_car_detected:
if car_detected_confidence:
# Car passed confidence - now check bbox area
best_detection = basic_detection.get("best_detection")
if best_detection and best_detection.get("bbox"):
bbox = best_detection["bbox"]
x1, y1, x2, y2 = bbox
bbox_area = (x2 - x1) * (y2 - y1)
frame_height, frame_width = cropped_frame.shape[:2]
frame_area = frame_height * frame_width
bbox_area_ratio = bbox_area / frame_area if frame_area > 0 else 0
min_area_ratio = 0.2 # 20% of frame
car_detected_with_bbox_validation = bbox_area_ratio >= min_area_ratio
if not car_detected_with_bbox_validation:
logger.info(f"🚫 Camera {camera_id}: LIGHTWEIGHT - car detected but bbox {bbox_area_ratio:.1%} < {min_area_ratio:.0%} (too distant) - counting as absent")
else:
logger.debug(f"✅ Camera {camera_id}: LIGHTWEIGHT - car detected with valid bbox {bbox_area_ratio:.1%} >= {min_area_ratio:.0%}")
else:
logger.debug(f"⚠️ Camera {camera_id}: LIGHTWEIGHT - car detected but no bbox info available")
logger.debug(f"🔍 Camera {camera_id}: LIGHTWEIGHT - enhanced car presence check: confidence={car_detected_confidence}, bbox_valid={car_detected_with_bbox_validation}")
if car_detected_with_bbox_validation:
# Car detected - reset absence counter, continue sending cached detection dict
pipeline_state["absence_counter"] = 0 # Reset absence since cars are present