Spaces:
Runtime error
Runtime error
Update services/overlay_service.py
Browse files- services/overlay_service.py +41 -7
services/overlay_service.py
CHANGED
@@ -1,31 +1,65 @@
|
|
1 |
import cv2
|
|
|
2 |
import logging
|
|
|
3 |
|
|
|
4 |
logging.basicConfig(
|
5 |
filename="app.log",
|
6 |
level=logging.INFO,
|
7 |
format="%(asctime)s - %(levelname)s - %(message)s"
|
8 |
)
|
9 |
|
10 |
-
def overlay_boxes(frame, detected_items):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
try:
|
12 |
for item in detected_items:
|
|
|
13 |
if "coordinates" not in item or "label" not in item:
|
|
|
14 |
continue
|
|
|
|
|
15 |
x_min, y_min, x_max, y_max = item["coordinates"]
|
16 |
label = item["label"]
|
17 |
|
|
|
18 |
type_colors = {
|
19 |
-
"crack": (147, 112, 219), # Light purple (green + pink mix
|
20 |
"pothole": (255, 127, 80), # Coral (red + orange mix)
|
21 |
-
"signage": (0, 191, 255),
|
22 |
-
"default": (0, 255, 255)
|
23 |
}
|
24 |
-
color = type_colors.get(item.get("type", ""), type_colors["default"])
|
25 |
|
|
|
26 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
27 |
-
cv2.putText(
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
logging.info(f"Overlaid {len(detected_items)} items on frame.")
|
31 |
return frame
|
|
|
1 |
import cv2
|
2 |
+
import numpy as np
|
3 |
import logging
|
4 |
+
from typing import List, Dict, Any
|
5 |
|
6 |
+
# Configure logging
|
7 |
logging.basicConfig(
|
8 |
filename="app.log",
|
9 |
level=logging.INFO,
|
10 |
format="%(asctime)s - %(levelname)s - %(message)s"
|
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.")
|
27 |
+
return frame
|
28 |
+
if not isinstance(detected_items, list):
|
29 |
+
logging.error("Detected items must be a list.")
|
30 |
+
return frame
|
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": (147, 112, 219), # Light purple (green + pink mix)
|
46 |
"pothole": (255, 127, 80), # Coral (red + orange mix)
|
47 |
+
"signage": (0, 191, 255), # DeepSkyBlue
|
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
|