Spaces:
Sleeping
Sleeping
File size: 1,909 Bytes
eb0e26a b098c89 7a73535 b098c89 7a73535 b098c89 7a73535 eb0e26a b098c89 7a73535 b098c89 7a73535 b098c89 7a73535 fb723a9 7a73535 fb723a9 7a73535 b098c89 7a73535 b098c89 7a73535 b098c89 27d8552 7a73535 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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 |