Spaces:
Sleeping
Sleeping
Update services/overlay_service.py
Browse files- services/overlay_service.py +13 -31
services/overlay_service.py
CHANGED
|
@@ -13,15 +13,12 @@ logging.basicConfig(
|
|
| 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
|
| 19 |
-
detected_items
|
| 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.")
|
| 27 |
return frame
|
|
@@ -31,35 +28,20 @@ def overlay_boxes(frame: np.ndarray, detected_items: List[Dict[str, Any]]) -> np
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
for item in detected_items:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
logging.warning(f"Skipping item without coordinates or label: {item}")
|
| 37 |
continue
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
"
|
| 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 |
-
|
| 52 |
-
# Draw bounding box and label
|
| 53 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
| 54 |
-
cv2.putText(
|
| 55 |
-
frame,
|
| 56 |
-
label,
|
| 57 |
-
(x_min, y_min - 10),
|
| 58 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
| 59 |
-
0.6,
|
| 60 |
-
color,
|
| 61 |
-
2
|
| 62 |
-
)
|
| 63 |
|
| 64 |
logging.info(f"Overlaid {len(detected_items)} items on frame.")
|
| 65 |
return frame
|
|
|
|
| 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 |
Args:
|
| 17 |
+
frame: Input frame in BGR format.
|
| 18 |
+
detected_items: List of detected items with coordinates and labels.
|
|
|
|
| 19 |
Returns:
|
| 20 |
np.ndarray: Frame with overlaid bounding boxes and labels.
|
| 21 |
"""
|
|
|
|
| 22 |
if not isinstance(frame, np.ndarray) or frame.size == 0:
|
| 23 |
logging.error("Invalid input frame provided to overlay_service.")
|
| 24 |
return frame
|
|
|
|
| 28 |
|
| 29 |
try:
|
| 30 |
for item in detected_items:
|
| 31 |
+
if "box" not in item or "type" not in item or "severity" not in item:
|
| 32 |
+
logging.warning(f"Skipping item without box, type, or severity: {item}")
|
|
|
|
| 33 |
continue
|
| 34 |
|
| 35 |
+
x_min, y_min, x_max, y_max = map(int, item["box"])
|
| 36 |
+
severity = item["severity"]
|
| 37 |
+
if item["type"] == "crack":
|
| 38 |
+
color = (0, 0, 255) if severity == "Severe" else (0, 255, 255) if severity == "Moderate" else (0, 255, 0) if severity == "Minor" else (255, 165, 0)
|
| 39 |
+
label = f"Crack: {severity}"
|
| 40 |
+
else: # Hole
|
| 41 |
+
color = (255, 0, 0) if severity == "Severe" else (255, 255, 0) if severity == "Moderate" else (0, 128, 0)
|
| 42 |
+
label = f"Hole: {severity}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
| 44 |
+
cv2.putText(frame, label, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
logging.info(f"Overlaid {len(detected_items)} items on frame.")
|
| 47 |
return frame
|