Spaces:
Sleeping
Sleeping
Update services/overlay_service.py
Browse files- services/overlay_service.py +35 -21
services/overlay_service.py
CHANGED
@@ -1,31 +1,45 @@
|
|
1 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
def overlay_boxes(frame, detected_items):
|
4 |
"""
|
5 |
-
|
6 |
Args:
|
7 |
frame: Input frame (numpy array)
|
8 |
-
detected_items: List of
|
9 |
Returns:
|
10 |
numpy array: Frame with overlays
|
11 |
"""
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
31 |
-
return frame
|
|
|
1 |
import cv2
|
2 |
+
import logging
|
3 |
+
|
4 |
+
# Setup logging
|
5 |
+
logging.basicConfig(
|
6 |
+
filename="app.log",
|
7 |
+
level=logging.INFO,
|
8 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
9 |
+
)
|
10 |
|
11 |
def overlay_boxes(frame, detected_items):
|
12 |
"""
|
13 |
+
Overlay bounding boxes and numbered labels on the frame for detected items.
|
14 |
Args:
|
15 |
frame: Input frame (numpy array)
|
16 |
+
detected_items: List of detected items (dicts with type, label, coordinates, etc.)
|
17 |
Returns:
|
18 |
numpy array: Frame with overlays
|
19 |
"""
|
20 |
+
try:
|
21 |
+
for item in detected_items:
|
22 |
+
if "coordinates" not in item or "label" not in item:
|
23 |
+
continue
|
24 |
+
x_min, y_min, x_max, y_max = item["coordinates"]
|
25 |
+
label = item["label"]
|
26 |
+
|
27 |
+
# Determine color based on type
|
28 |
+
type_colors = {
|
29 |
+
"crack": (255, 0, 0), # Red for cracks
|
30 |
+
"pothole": (255, 165, 0), # Orange for potholes
|
31 |
+
"signage": (0, 191, 255), # DeepSkyBlue for signage
|
32 |
+
"default": (0, 255, 255) # Yellow for others
|
33 |
+
}
|
34 |
+
color = type_colors.get(item.get("type", "default"), type_colors["default"])
|
35 |
+
|
36 |
+
# Draw bounding box and label
|
37 |
+
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
38 |
+
cv2.putText(frame, label, (x_min, y_min - 10),
|
39 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
40 |
|
41 |
+
logging.info(f"Overlaid {len(detected_items)} items on frame.")
|
42 |
+
return frame
|
43 |
+
except Exception as e:
|
44 |
+
logging.error(f"Error overlaying boxes: {str(e)}")
|
45 |
+
return frame
|
|
|
|