lokesh341 commited on
Commit
c240caa
·
1 Parent(s): 505898d

Update services/detection_service.py

Browse files
Files changed (1) hide show
  1. services/detection_service.py +32 -18
services/detection_service.py CHANGED
@@ -1,24 +1,44 @@
1
- # services/detection_service.py
2
  import cv2
3
  import numpy as np
4
  from ultralytics import YOLO
5
  import os
 
6
 
7
- # Load YOLOv8m model
 
 
 
 
 
 
 
8
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
9
- MODEL_PATH = os.path.join(BASE_DIR, "../models/yolov8m.pt")
10
- model = YOLO(MODEL_PATH)
 
 
 
 
 
11
 
12
- def detect_objects(frame):
13
  """
14
- Detect objects in a frame using YOLOv8m.
15
  Args:
16
  frame: Input frame (numpy array)
17
  Returns:
18
- dict: Detection results with numbered labels
19
- numpy array: Annotated frame
20
  """
21
- results = model(frame)
 
 
 
 
 
 
 
 
22
 
23
  detections = []
24
  line_counter = 1 # Initialize counter for numbered labels
@@ -36,24 +56,18 @@ def detect_objects(frame):
36
  # Add numbered label
37
  detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
38
  detections.append({
 
39
  "label": detection_label,
40
  "confidence": conf,
41
  "coordinates": [x_min, y_min, x_max, y_max]
42
  })
43
 
44
  # Draw bounding box and label
45
- color = (0, 255, 0) # Green for generic objects
46
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
47
  cv2.putText(frame, detection_label, (x_min, y_min - 10),
48
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
49
 
50
  line_counter += 1
51
 
52
- return {"detections": detections, "frame": frame}
53
-
54
- def process_frame(frame):
55
- """
56
- Wrapper function for integration with app.py.
57
- """
58
- result = detect_objects(frame)
59
- return result["detections"], result["frame"]
 
 
1
  import cv2
2
  import numpy as np
3
  from ultralytics import YOLO
4
  import os
5
+ import logging
6
 
7
+ # Setup logging
8
+ logging.basicConfig(
9
+ filename="app.log",
10
+ level=logging.INFO,
11
+ format="%(asctime)s - %(levelname)s - %(message)s"
12
+ )
13
+
14
+ # Load YOLOv8m model for generic detection
15
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
16
+ MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../models/yolov8m.pt"))
17
+ try:
18
+ model = YOLO(MODEL_PATH)
19
+ logging.info("Loaded YOLOv8m model for generic detection.")
20
+ except Exception as e:
21
+ logging.error(f"Failed to load YOLOv8m model: {str(e)}")
22
+ model = None
23
 
24
+ def process_frame(frame):
25
  """
26
+ Process a frame for generic object detection using YOLOv8m.
27
  Args:
28
  frame: Input frame (numpy array)
29
  Returns:
30
+ list: List of detected objects
31
+ numpy array: Annotated frame with numbered labels
32
  """
33
+ if model is None:
34
+ logging.error("YOLO model not loaded. Skipping generic detection.")
35
+ return [], frame
36
+
37
+ try:
38
+ results = model(frame)
39
+ except Exception as e:
40
+ logging.error(f"Error during YOLO inference: {str(e)}")
41
+ return [], frame
42
 
43
  detections = []
44
  line_counter = 1 # Initialize counter for numbered labels
 
56
  # Add numbered label
57
  detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
58
  detections.append({
59
+ "type": label,
60
  "label": detection_label,
61
  "confidence": conf,
62
  "coordinates": [x_min, y_min, x_max, y_max]
63
  })
64
 
65
  # Draw bounding box and label
66
+ color = (0, 255, 255) # Yellow for generic detections
67
  cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
68
  cv2.putText(frame, detection_label, (x_min, y_min - 10),
69
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
70
 
71
  line_counter += 1
72
 
73
+ return detections, frame