lokesh341 commited on
Commit
4b1f1b4
·
1 Parent(s): b379351

Update services/operations_maintenance/pothole_detection.py

Browse files
services/operations_maintenance/pothole_detection.py CHANGED
@@ -3,15 +3,20 @@ import numpy as np
3
  from ultralytics import YOLO
4
  import os
5
  import logging
 
6
 
 
7
  logging.basicConfig(
8
  filename="app.log",
9
  level=logging.INFO,
10
  format="%(asctime)s - %(levelname)s - %(message)s"
11
  )
12
 
 
13
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
14
  MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8m.pt"))
 
 
15
  try:
16
  model = YOLO(MODEL_PATH)
17
  logging.info("Loaded YOLOv8m model for pothole detection.")
@@ -19,18 +24,37 @@ except Exception as e:
19
  logging.error(f"Failed to load YOLOv8m model: {str(e)}")
20
  model = None
21
 
22
- def process_potholes(frame):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  if model is None:
24
  logging.error("YOLO model not loaded. Skipping pothole detection.")
25
  return [], frame
26
 
27
  try:
28
- results = model(frame)
 
 
29
  except Exception as e:
30
  logging.error(f"Error during YOLO inference: {str(e)}")
31
  return [], frame
32
 
33
- detections = []
34
  line_counter = 1
35
 
36
  for r in results:
@@ -55,8 +79,15 @@ def process_potholes(frame):
55
 
56
  color = (255, 127, 80) # Coral (red + orange mix)
57
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
58
- cv2.putText(frame, detection_label, (x_min, y_min - 10),
59
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
 
 
 
 
 
 
 
60
 
61
  line_counter += 1
62
 
 
3
  from ultralytics import YOLO
4
  import os
5
  import logging
6
+ from typing import Tuple, List, Dict, Any
7
 
8
+ # Configure logging
9
  logging.basicConfig(
10
  filename="app.log",
11
  level=logging.INFO,
12
  format="%(asctime)s - %(levelname)s - %(message)s"
13
  )
14
 
15
+ # Define base directory and model path
16
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
17
  MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8m.pt"))
18
+
19
+ # Initialize YOLO model
20
  try:
21
  model = YOLO(MODEL_PATH)
22
  logging.info("Loaded YOLOv8m model for pothole detection.")
 
24
  logging.error(f"Failed to load YOLOv8m model: {str(e)}")
25
  model = None
26
 
27
+ def process_potholes(frame: np.ndarray) -> Tuple[List[Dict[str, Any]], np.ndarray]:
28
+ """
29
+ Detect potholes in a video frame using YOLOv8m.
30
+
31
+ Args:
32
+ frame (np.ndarray): Input frame in BGR format.
33
+
34
+ Returns:
35
+ Tuple[List[Dict[str, Any]], np.ndarray]: A tuple containing:
36
+ - List of detected potholes.
37
+ - Annotated frame with bounding boxes and labels.
38
+ """
39
+ # Validate input frame
40
+ if not isinstance(frame, np.ndarray) or frame.size == 0:
41
+ logging.error("Invalid input frame provided to pothole_detection.")
42
+ return [], frame
43
+
44
+ # Check if model is loaded
45
  if model is None:
46
  logging.error("YOLO model not loaded. Skipping pothole detection.")
47
  return [], frame
48
 
49
  try:
50
+ # Perform YOLO inference
51
+ results = model(frame, verbose=False)
52
+ logging.debug("Completed YOLO inference for pothole detection.")
53
  except Exception as e:
54
  logging.error(f"Error during YOLO inference: {str(e)}")
55
  return [], frame
56
 
57
+ detections: List[Dict[str, Any]] = []
58
  line_counter = 1
59
 
60
  for r in results:
 
79
 
80
  color = (255, 127, 80) # Coral (red + orange mix)
81
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
82
+ cv2.putText(
83
+ frame,
84
+ detection_label,
85
+ (x_min, y_min - 10),
86
+ cv2.FONT_HERSHEY_SIMPLEX,
87
+ 0.6,
88
+ color,
89
+ 2
90
+ )
91
 
92
  line_counter += 1
93