[Pongsatorn K. 2025/09/01] fixing pympta.py #6
1 changed files with 67 additions and 70 deletions
|
@ -62,18 +62,18 @@ def crop_region_by_class(frame, regions_dict, class_name):
|
|||
bbox = regions_dict[class_name]['bbox']
|
||||
x1, y1, x2, y2 = bbox
|
||||
|
||||
# TEMP DEBUG: Diagnostic logging for crop issues
|
||||
# Diagnostic logging for crop issues
|
||||
frame_h, frame_w = frame.shape[:2]
|
||||
logger.info(f"🔍 CROP DEBUG: Frame dimensions: {frame_w}x{frame_h}")
|
||||
logger.info(f"🔍 CROP DEBUG: Original bbox: {bbox}")
|
||||
logger.info(f"🔍 CROP DEBUG: Bbox dimensions: {x2-x1}x{y2-y1}")
|
||||
logger.debug(f"CROP DEBUG: Frame dimensions: {frame_w}x{frame_h}")
|
||||
logger.debug(f"CROP DEBUG: Original bbox: {bbox}")
|
||||
logger.debug(f"CROP DEBUG: Bbox dimensions: {x2-x1}x{y2-y1}")
|
||||
|
||||
# Check if bbox is within frame bounds
|
||||
if x1 < 0 or y1 < 0 or x2 > frame_w or y2 > frame_h:
|
||||
logger.warning(f"🔍 CROP DEBUG: Bbox extends beyond frame! Clipping...")
|
||||
logger.warning(f"CROP DEBUG: Bbox extends beyond frame! Clipping...")
|
||||
x1, y1 = max(0, x1), max(0, y1)
|
||||
x2, y2 = min(frame_w, x2), min(frame_h, y2)
|
||||
logger.info(f"🔍 CROP DEBUG: Clipped bbox: ({x1}, {y1}, {x2}, {y2})")
|
||||
logger.debug(f"CROP DEBUG: Clipped bbox: ({x1}, {y1}, {x2}, {y2})")
|
||||
|
||||
cropped = frame[y1:y2, x1:x2]
|
||||
|
||||
|
@ -81,7 +81,7 @@ def crop_region_by_class(frame, regions_dict, class_name):
|
|||
logger.warning(f"Empty crop for class '{class_name}' with bbox {bbox}")
|
||||
return None
|
||||
|
||||
logger.info(f"🔍 CROP DEBUG: Successful crop shape: {cropped.shape}")
|
||||
logger.debug(f"CROP DEBUG: Successful crop shape: {cropped.shape}")
|
||||
return cropped
|
||||
|
||||
def format_action_context(base_context, additional_context=None):
|
||||
|
@ -764,10 +764,10 @@ def run_detection_with_tracking(frame, node, context=None):
|
|||
for i, detection in enumerate(candidate_detections):
|
||||
logger.debug(f"🏆 Camera {camera_id}: Candidate {i+1}: {detection['class']} conf={detection['confidence']:.3f} track_id={detection['id']}")
|
||||
|
||||
# TEMP DEBUG: Show all candidate detections before selection
|
||||
logger.info(f"🔍 TEMP DEBUG: Found {len(candidate_detections)} candidate detections:")
|
||||
# Show all candidate detections before selection
|
||||
logger.debug(f"Found {len(candidate_detections)} candidate detections:")
|
||||
for i, det in enumerate(candidate_detections):
|
||||
logger.info(f"🔍 TEMP DEBUG: Candidate {i+1}: {det['class']} conf={det['confidence']:.3f} bbox={det['bbox']}")
|
||||
logger.debug(f"Candidate {i+1}: {det['class']} conf={det['confidence']:.3f} bbox={det['bbox']}")
|
||||
|
||||
# Find the single highest confidence detection across all detected classes
|
||||
best_detection = max(candidate_detections, key=lambda x: x["confidence"])
|
||||
|
@ -820,24 +820,24 @@ def run_detection_with_tracking(frame, node, context=None):
|
|||
logger.info(f"✅ Camera {camera_id}: DETECTION COMPLETE - tracking single car: track_id={track_id}, conf={best_detection['confidence']:.3f}")
|
||||
logger.debug(f"📊 Camera {camera_id}: Detection summary: {len(res.boxes)} raw → {len(candidate_detections)} candidates → 1 selected")
|
||||
|
||||
# TEMP DEBUG: Save vehicle crop immediately after yolo detection
|
||||
if node.get("modelId") in ["yolo11n", "yolo11m"] and regions_dict:
|
||||
try:
|
||||
import datetime
|
||||
os.makedirs("temp_debug", exist_ok=True)
|
||||
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:-3]
|
||||
|
||||
for class_name, region_data in regions_dict.items():
|
||||
bbox = region_data['bbox']
|
||||
x1, y1, x2, y2 = bbox
|
||||
cropped = frame[y1:y2, x1:x2]
|
||||
if cropped.size > 0:
|
||||
model_name = node.get("modelId", "yolo")
|
||||
debug_path = f"temp_debug/{model_name}_{class_name}_crop_{timestamp}.jpg"
|
||||
cv2.imwrite(debug_path, cropped)
|
||||
logger.info(f"🚗 TEMP DEBUG: Saved {model_name} {class_name} crop to {debug_path}")
|
||||
except Exception as e:
|
||||
logger.error(f"🚗 TEMP DEBUG: Failed to save {node.get('modelId', 'yolo')} crop: {e}")
|
||||
# Debug: Save vehicle crop for debugging (disabled for production)
|
||||
# if node.get("modelId") in ["yolo11n", "yolo11m"] and regions_dict:
|
||||
# try:
|
||||
# import datetime
|
||||
# os.makedirs("temp_debug", exist_ok=True)
|
||||
# timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:-3]
|
||||
#
|
||||
# for class_name, region_data in regions_dict.items():
|
||||
# bbox = region_data['bbox']
|
||||
# x1, y1, x2, y2 = bbox
|
||||
# cropped = frame[y1:y2, x1:x2]
|
||||
# if cropped.size > 0:
|
||||
# model_name = node.get("modelId", "yolo")
|
||||
# debug_path = f"temp_debug/{model_name}_{class_name}_crop_{timestamp}.jpg"
|
||||
# cv2.imwrite(debug_path, cropped)
|
||||
# logger.debug(f"Saved {model_name} {class_name} crop to {debug_path}")
|
||||
# except Exception as e:
|
||||
# logger.error(f"Failed to save {node.get('modelId', 'yolo')} crop: {e}")
|
||||
|
||||
# Update track-based stability tracking for the single selected car
|
||||
camera_id = context.get("camera_id", "unknown") if context else "unknown"
|
||||
|
@ -1433,48 +1433,45 @@ def run_pipeline(frame, node: dict, return_bbox: bool=False, context=None, valid
|
|||
# Normal detection stage - Using structured detection function
|
||||
all_detections, regions_dict, track_validation_result = run_detection_with_tracking(frame, node, context)
|
||||
|
||||
# TEMP DEBUG: Save only specific crops
|
||||
if regions_dict:
|
||||
try:
|
||||
import datetime
|
||||
os.makedirs("temp_debug", exist_ok=True)
|
||||
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
model_id = node.get("modelId", "unknown")
|
||||
|
||||
# Save vehicle crop from yolo model (any vehicle: car, truck, bus, motorcycle)
|
||||
if model_id in ["yolo11n", "yolo11m"]:
|
||||
# Look for any vehicle class in regions_dict
|
||||
vehicle_classes = ["car", "truck", "bus", "motorcycle"]
|
||||
found_vehicle = None
|
||||
for vehicle_class in vehicle_classes:
|
||||
if vehicle_class in regions_dict:
|
||||
found_vehicle = vehicle_class
|
||||
break
|
||||
|
||||
bbox = regions_dict[found_vehicle]['bbox']
|
||||
x1, y1, x2, y2 = bbox
|
||||
cropped = frame[y1:y2, x1:x2]
|
||||
if cropped.size > 0:
|
||||
debug_path = f"temp_debug/{found_vehicle}_crop_{timestamp}.jpg"
|
||||
cv2.imwrite(debug_path, cropped)
|
||||
logger.info(f"🚗 TEMP DEBUG: Saved {found_vehicle} crop to {debug_path}")
|
||||
else:
|
||||
logger.warning(f"🚗 TEMP DEBUG: Empty {found_vehicle} crop with bbox {bbox}")
|
||||
else:
|
||||
logger.warning(f"🚗 TEMP DEBUG: {model_id} detected but no vehicle classes found. Available: {list(regions_dict.keys())}")
|
||||
|
||||
# Save frontal crop from frontal_detection_v1
|
||||
elif model_id == "frontal_detection_v1" and "frontal" in regions_dict:
|
||||
bbox = regions_dict["frontal"]['bbox']
|
||||
x1, y1, x2, y2 = bbox
|
||||
cropped = frame[y1:y2, x1:x2]
|
||||
if cropped.size > 0:
|
||||
debug_path = f"temp_debug/frontal_crop_{timestamp}.jpg"
|
||||
cv2.imwrite(debug_path, cropped)
|
||||
logger.info(f"🔍 TEMP DEBUG: Saved frontal crop to {debug_path}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"🔍 TEMP DEBUG: Failed to save crops: {e}")
|
||||
# Debug: Save crops for debugging (disabled for production)
|
||||
# if regions_dict:
|
||||
# try:
|
||||
# import datetime
|
||||
# os.makedirs("temp_debug", exist_ok=True)
|
||||
# timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
# model_id = node.get("modelId", "unknown")
|
||||
#
|
||||
# # Save vehicle crop from yolo model (any vehicle: car, truck, bus, motorcycle)
|
||||
# if model_id in ["yolo11n", "yolo11m"]:
|
||||
# # Look for any vehicle class in regions_dict
|
||||
# vehicle_classes = ["car", "truck", "bus", "motorcycle"]
|
||||
# found_vehicle = None
|
||||
# for vehicle_class in vehicle_classes:
|
||||
# if vehicle_class in regions_dict:
|
||||
# found_vehicle = vehicle_class
|
||||
# break
|
||||
#
|
||||
# if found_vehicle:
|
||||
# bbox = regions_dict[found_vehicle]['bbox']
|
||||
# x1, y1, x2, y2 = bbox
|
||||
# cropped = frame[y1:y2, x1:x2]
|
||||
# if cropped.size > 0:
|
||||
# debug_path = f"temp_debug/{found_vehicle}_crop_{timestamp}.jpg"
|
||||
# cv2.imwrite(debug_path, cropped)
|
||||
# logger.debug(f"Saved {found_vehicle} crop to {debug_path}")
|
||||
#
|
||||
# # Save frontal crop from frontal_detection_v1
|
||||
# elif model_id == "frontal_detection_v1" and "frontal" in regions_dict:
|
||||
# bbox = regions_dict["frontal"]['bbox']
|
||||
# x1, y1, x2, y2 = bbox
|
||||
# cropped = frame[y1:y2, x1:x2]
|
||||
# if cropped.size > 0:
|
||||
# debug_path = f"temp_debug/frontal_crop_{timestamp}.jpg"
|
||||
# cv2.imwrite(debug_path, cropped)
|
||||
# logger.debug(f"Saved frontal crop to {debug_path}")
|
||||
#
|
||||
# except Exception as e:
|
||||
# logger.error(f"Failed to save crops: {e}")
|
||||
|
||||
if not all_detections:
|
||||
logger.debug("No detections from structured detection function - sending 'none' detection")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue