fixbug/pympta

This commit is contained in:
Pongsatorn 2025-09-01 11:59:47 +07:00
parent c01f449d0d
commit 2f52374ff4

View file

@ -62,18 +62,18 @@ def crop_region_by_class(frame, regions_dict, class_name):
bbox = regions_dict[class_name]['bbox'] bbox = regions_dict[class_name]['bbox']
x1, y1, x2, y2 = 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] frame_h, frame_w = frame.shape[:2]
logger.info(f"🔍 CROP DEBUG: Frame dimensions: {frame_w}x{frame_h}") logger.debug(f"CROP DEBUG: Frame dimensions: {frame_w}x{frame_h}")
logger.info(f"🔍 CROP DEBUG: Original bbox: {bbox}") logger.debug(f"CROP DEBUG: Original bbox: {bbox}")
logger.info(f"🔍 CROP DEBUG: Bbox dimensions: {x2-x1}x{y2-y1}") logger.debug(f"CROP DEBUG: Bbox dimensions: {x2-x1}x{y2-y1}")
# Check if bbox is within frame bounds # Check if bbox is within frame bounds
if x1 < 0 or y1 < 0 or x2 > frame_w or y2 > frame_h: 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) x1, y1 = max(0, x1), max(0, y1)
x2, y2 = min(frame_w, x2), min(frame_h, y2) 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] 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}") logger.warning(f"Empty crop for class '{class_name}' with bbox {bbox}")
return None return None
logger.info(f"🔍 CROP DEBUG: Successful crop shape: {cropped.shape}") logger.debug(f"CROP DEBUG: Successful crop shape: {cropped.shape}")
return cropped return cropped
def format_action_context(base_context, additional_context=None): 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): 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']}") 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 # Show all candidate detections before selection
logger.info(f"🔍 TEMP DEBUG: Found {len(candidate_detections)} candidate detections:") logger.debug(f"Found {len(candidate_detections)} candidate detections:")
for i, det in enumerate(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 # Find the single highest confidence detection across all detected classes
best_detection = max(candidate_detections, key=lambda x: x["confidence"]) 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.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") 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 # Debug: Save vehicle crop for debugging (disabled for production)
if node.get("modelId") in ["yolo11n", "yolo11m"] and regions_dict: # if node.get("modelId") in ["yolo11n", "yolo11m"] and regions_dict:
try: # try:
import datetime # import datetime
os.makedirs("temp_debug", exist_ok=True) # os.makedirs("temp_debug", exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:-3] # timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:-3]
#
for class_name, region_data in regions_dict.items(): # for class_name, region_data in regions_dict.items():
bbox = region_data['bbox'] # bbox = region_data['bbox']
x1, y1, x2, y2 = bbox # x1, y1, x2, y2 = bbox
cropped = frame[y1:y2, x1:x2] # cropped = frame[y1:y2, x1:x2]
if cropped.size > 0: # if cropped.size > 0:
model_name = node.get("modelId", "yolo") # model_name = node.get("modelId", "yolo")
debug_path = f"temp_debug/{model_name}_{class_name}_crop_{timestamp}.jpg" # debug_path = f"temp_debug/{model_name}_{class_name}_crop_{timestamp}.jpg"
cv2.imwrite(debug_path, cropped) # cv2.imwrite(debug_path, cropped)
logger.info(f"🚗 TEMP DEBUG: Saved {model_name} {class_name} crop to {debug_path}") # logger.debug(f"Saved {model_name} {class_name} crop to {debug_path}")
except Exception as e: # except Exception as e:
logger.error(f"🚗 TEMP DEBUG: Failed to save {node.get('modelId', 'yolo')} crop: {e}") # logger.error(f"Failed to save {node.get('modelId', 'yolo')} crop: {e}")
# Update track-based stability tracking for the single selected car # Update track-based stability tracking for the single selected car
camera_id = context.get("camera_id", "unknown") if context else "unknown" 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 # Normal detection stage - Using structured detection function
all_detections, regions_dict, track_validation_result = run_detection_with_tracking(frame, node, context) all_detections, regions_dict, track_validation_result = run_detection_with_tracking(frame, node, context)
# TEMP DEBUG: Save only specific crops # Debug: Save crops for debugging (disabled for production)
if regions_dict: # if regions_dict:
try: # try:
import datetime # import datetime
os.makedirs("temp_debug", exist_ok=True) # os.makedirs("temp_debug", exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") # timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
model_id = node.get("modelId", "unknown") # model_id = node.get("modelId", "unknown")
#
# Save vehicle crop from yolo model (any vehicle: car, truck, bus, motorcycle) # # Save vehicle crop from yolo model (any vehicle: car, truck, bus, motorcycle)
if model_id in ["yolo11n", "yolo11m"]: # if model_id in ["yolo11n", "yolo11m"]:
# Look for any vehicle class in regions_dict # # Look for any vehicle class in regions_dict
vehicle_classes = ["car", "truck", "bus", "motorcycle"] # vehicle_classes = ["car", "truck", "bus", "motorcycle"]
found_vehicle = None # found_vehicle = None
for vehicle_class in vehicle_classes: # for vehicle_class in vehicle_classes:
if vehicle_class in regions_dict: # if vehicle_class in regions_dict:
found_vehicle = vehicle_class # found_vehicle = vehicle_class
break # break
#
bbox = regions_dict[found_vehicle]['bbox'] # if found_vehicle:
x1, y1, x2, y2 = bbox # bbox = regions_dict[found_vehicle]['bbox']
cropped = frame[y1:y2, x1:x2] # x1, y1, x2, y2 = bbox
if cropped.size > 0: # cropped = frame[y1:y2, x1:x2]
debug_path = f"temp_debug/{found_vehicle}_crop_{timestamp}.jpg" # if cropped.size > 0:
cv2.imwrite(debug_path, cropped) # debug_path = f"temp_debug/{found_vehicle}_crop_{timestamp}.jpg"
logger.info(f"🚗 TEMP DEBUG: Saved {found_vehicle} crop to {debug_path}") # cv2.imwrite(debug_path, cropped)
else: # logger.debug(f"Saved {found_vehicle} crop to {debug_path}")
logger.warning(f"🚗 TEMP DEBUG: Empty {found_vehicle} crop with bbox {bbox}") #
else: # # Save frontal crop from frontal_detection_v1
logger.warning(f"🚗 TEMP DEBUG: {model_id} detected but no vehicle classes found. Available: {list(regions_dict.keys())}") # elif model_id == "frontal_detection_v1" and "frontal" in regions_dict:
# bbox = regions_dict["frontal"]['bbox']
# Save frontal crop from frontal_detection_v1 # x1, y1, x2, y2 = bbox
elif model_id == "frontal_detection_v1" and "frontal" in regions_dict: # cropped = frame[y1:y2, x1:x2]
bbox = regions_dict["frontal"]['bbox'] # if cropped.size > 0:
x1, y1, x2, y2 = bbox # debug_path = f"temp_debug/frontal_crop_{timestamp}.jpg"
cropped = frame[y1:y2, x1:x2] # cv2.imwrite(debug_path, cropped)
if cropped.size > 0: # logger.debug(f"Saved frontal crop to {debug_path}")
debug_path = f"temp_debug/frontal_crop_{timestamp}.jpg" #
cv2.imwrite(debug_path, cropped) # except Exception as e:
logger.info(f"🔍 TEMP DEBUG: Saved frontal crop to {debug_path}") # logger.error(f"Failed to save crops: {e}")
except Exception as e:
logger.error(f"🔍 TEMP DEBUG: Failed to save crops: {e}")
if not all_detections: if not all_detections:
logger.debug("No detections from structured detection function - sending 'none' detection") logger.debug("No detections from structured detection function - sending 'none' detection")