lokesh341 commited on
Commit
2c426db
·
1 Parent(s): edb10ef

Update services/road_safety/barrier_check.py

Browse files
Files changed (1) hide show
  1. services/road_safety/barrier_check.py +30 -19
services/road_safety/barrier_check.py CHANGED
@@ -1,26 +1,38 @@
1
  import cv2
2
  import numpy as np
 
3
  from ultralytics import YOLO
4
  import os
 
 
 
 
 
 
 
5
 
6
- # Load YOLOv8m model
7
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
8
  MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8m.pt"))
9
- model = YOLO(MODEL_PATH)
 
 
 
 
 
10
 
11
  def process_barriers(frame):
12
- """
13
- Detect road barriers in a frame.
14
- Args:
15
- frame: Input frame (numpy array)
16
- Returns:
17
- list: List of detected barriers
18
- numpy array: Annotated frame with numbered labels
19
- """
20
- results = model(frame)
21
 
22
  detections = []
23
- line_counter = 1 # Initialize counter for numbered labels
24
 
25
  for r in results:
26
  for box in r.boxes:
@@ -29,12 +41,11 @@ def process_barriers(frame):
29
  continue
30
  cls = int(box.cls[0])
31
  label = model.names[cls]
32
- if label != "barrier": # Assuming "barrier" class exists
33
  continue
34
- xyxy = box.xyxy[0].cpu().numpy()
35
- x_min, y_min, x_max, y_max = map(int, xyxy)
36
 
37
- # Add numbered label
38
  detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
39
  detections.append({
40
  "type": label,
@@ -43,12 +54,12 @@ def process_barriers(frame):
43
  "coordinates": [x_min, y_min, x_max, y_max]
44
  })
45
 
46
- # Draw bounding box and label
47
- color = (255, 215, 0) # Gold for barriers
48
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
49
  cv2.putText(frame, detection_label, (x_min, y_min - 10),
50
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
51
 
52
  line_counter += 1
53
 
 
54
  return detections, frame
 
1
  import cv2
2
  import numpy as np
3
+ import numpy as np
4
  from ultralytics import YOLO
5
  import os
6
+ import logging
7
+
8
+ logging.basicConfig(
9
+ filename="app.log",
10
+ level=logging.INFO,
11
+ format="%(asctime)s - %(levelname)s - %(message)s"
12
+ )
13
 
 
14
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
15
  MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8m.pt"))
16
+ try:
17
+ model = YOLO(MODEL_PATH)
18
+ logging.info("Loaded YOLOv8m model for barrier check.")
19
+ except Exception as e:
20
+ logging.error(f"Failed to load YOLOv8m model: {str(e)}")
21
+ model = None
22
 
23
  def process_barriers(frame):
24
+ if model is None:
25
+ logging.error("YOLO model not loaded. Skipping barrier check.")
26
+ return [], frame
27
+
28
+ try:
29
+ results = model(frame)
30
+ except Exception as e:
31
+ logging.error(f"Error during YOLO inference: {str(e)}")
32
+ return [], frame
33
 
34
  detections = []
35
+ line_counter = 1
36
 
37
  for r in results:
38
  for box in r.boxes:
 
41
  continue
42
  cls = int(box.cls[0])
43
  label = model.names[cls]
44
+ if label != "barrier":
45
  continue
46
+ xyxy = box.xyxy[0].cpu().numpy().astype(int)
47
+ x_min, y_min, x_max, y_max = xyxy
48
 
 
49
  detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
50
  detections.append({
51
  "type": label,
 
54
  "coordinates": [x_min, y_min, x_max, y_max]
55
  })
56
 
57
+ color = (0, 0, 255) # Blue as requested
 
58
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
59
  cv2.putText(frame, detection_label, (x_min, y_min - 10),
60
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
61
 
62
  line_counter += 1
63
 
64
+ logging.info(f"Detected {len(detections)} barriers in road_safety.")
65
  return detections, frame