Spaces:
Sleeping
Sleeping
| import cv2 | |
| import numpy as np | |
| import logging | |
| from typing import List, Dict, Any | |
| # Configure logging | |
| logging.basicConfig( | |
| filename="app.log", | |
| level=logging.INFO, | |
| format="%(asctime)s - %(levelname)s - %(message)s" | |
| ) | |
| def overlay_boxes(frame: np.ndarray, detected_items: List[Dict[str, Any]]) -> np.ndarray: | |
| # Validate inputs | |
| if not isinstance(frame, np.ndarray) or frame.size == 0: | |
| logging.error("Invalid input frame provided to overlay_service.") | |
| return frame | |
| if not isinstance(detected_items, list): | |
| logging.error("Detected items must be a list.") | |
| return frame | |
| try: | |
| for item in detected_items: | |
| if "coordinates" not in item or "label" not in item: | |
| logging.warning(f"Skipping item without coordinates or label: {item}") | |
| continue | |
| x_min, y_min, x_max, y_max = item["coordinates"] | |
| label = item["label"] | |
| # Define colors for Operations Maintenance | |
| type_colors = { | |
| "crack": (0, 0, 255), # Blue for cracks | |
| "pothole": (255, 0, 0), # Red for potholes | |
| "signage": (0, 0, 255), # Blue for signage | |
| "default": (0, 255, 255) # Yellow as fallback | |
| } | |
| color = type_colors.get(item.get("type", "default"), type_colors["default"]) | |
| # Draw bounding box and label | |
| cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2) | |
| cv2.putText( | |
| frame, | |
| label, | |
| (x_min, y_min - 10), | |
| cv2.FONT_HERSHEY_SIMPLEX, | |
| 0.6, | |
| color, | |
| 2 | |
| ) | |
| logging.info(f"Overlaid {len(detected_items)} items on frame.") | |
| return frame | |
| except Exception as e: | |
| logging.error(f"Error overlaying boxes: {str(e)}") | |
| return frame |