lokesh341 commited on
Commit
7a73535
·
1 Parent(s): 14e412f

Update services/overlay_service.py

Browse files
Files changed (1) hide show
  1. 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
- Add overlays (bounding boxes and labels) to a frame.
6
  Args:
7
  frame: Input frame (numpy array)
8
- detected_items: List of detection dictionaries
9
  Returns:
10
  numpy array: Frame with overlays
11
  """
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
- # Choose color based on type
18
- if item["type"] == "crack":
19
- color = (255, 0, 0) # Red for cracks
20
- elif item["type"] == "pothole":
21
- color = (255, 165, 0) # Orange for potholes
22
- else:
23
- color = (0, 255, 0) # Green for other objects
 
 
 
 
 
 
 
 
24
 
25
- cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
26
- # Add severity if present
27
- if "severity" in item:
28
- label += f" - Severity: {item['severity'].capitalize()}"
29
- cv2.putText(frame, label, (x_min, y_min - 10),
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