Spaces:
Sleeping
Sleeping
Update services/overlay_service.py
Browse files- services/overlay_service.py +14 -2
services/overlay_service.py
CHANGED
|
@@ -11,6 +11,16 @@ logging.basicConfig(
|
|
| 11 |
)
|
| 12 |
|
| 13 |
def overlay_boxes(frame: np.ndarray, detected_items: List[Dict[str, Any]]) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Validate inputs
|
| 15 |
if not isinstance(frame, np.ndarray) or frame.size == 0:
|
| 16 |
logging.error("Invalid input frame provided to overlay_service.")
|
|
@@ -21,19 +31,21 @@ def overlay_boxes(frame: np.ndarray, detected_items: List[Dict[str, Any]]) -> np
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
for item in detected_items:
|
|
|
|
| 24 |
if "coordinates" not in item or "label" not in item:
|
| 25 |
logging.warning(f"Skipping item without coordinates or label: {item}")
|
| 26 |
continue
|
| 27 |
|
|
|
|
| 28 |
x_min, y_min, x_max, y_max = item["coordinates"]
|
| 29 |
label = item["label"]
|
| 30 |
|
| 31 |
-
# Define colors
|
| 32 |
type_colors = {
|
| 33 |
"crack": (0, 0, 255), # Blue for cracks
|
| 34 |
"pothole": (255, 0, 0), # Red for potholes
|
| 35 |
"signage": (0, 0, 255), # Blue for signage
|
| 36 |
-
"default": (0, 255, 255) # Yellow
|
| 37 |
}
|
| 38 |
color = type_colors.get(item.get("type", "default"), type_colors["default"])
|
| 39 |
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
def overlay_boxes(frame: np.ndarray, detected_items: List[Dict[str, Any]]) -> np.ndarray:
|
| 14 |
+
"""
|
| 15 |
+
Overlay bounding boxes and labels on the frame for detected items.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
frame (np.ndarray): Input frame in BGR format.
|
| 19 |
+
detected_items (List[Dict[str, Any]]): List of detected items with coordinates and labels.
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
np.ndarray: Frame with overlaid bounding boxes and labels.
|
| 23 |
+
"""
|
| 24 |
# Validate inputs
|
| 25 |
if not isinstance(frame, np.ndarray) or frame.size == 0:
|
| 26 |
logging.error("Invalid input frame provided to overlay_service.")
|
|
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
for item in detected_items:
|
| 34 |
+
# Check for required keys
|
| 35 |
if "coordinates" not in item or "label" not in item:
|
| 36 |
logging.warning(f"Skipping item without coordinates or label: {item}")
|
| 37 |
continue
|
| 38 |
|
| 39 |
+
# Extract coordinates and label
|
| 40 |
x_min, y_min, x_max, y_max = item["coordinates"]
|
| 41 |
label = item["label"]
|
| 42 |
|
| 43 |
+
# Define colors based on detection type
|
| 44 |
type_colors = {
|
| 45 |
"crack": (0, 0, 255), # Blue for cracks
|
| 46 |
"pothole": (255, 0, 0), # Red for potholes
|
| 47 |
"signage": (0, 0, 255), # Blue for signage
|
| 48 |
+
"default": (0, 255, 255) # Yellow
|
| 49 |
}
|
| 50 |
color = type_colors.get(item.get("type", "default"), type_colors["default"])
|
| 51 |
|