-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_detector.py
More file actions
38 lines (32 loc) · 1.34 KB
/
Copy pathobject_detector.py
File metadata and controls
38 lines (32 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from ultralytics import YOLO
import cv2
from typing import List, Tuple
class VideoObjectDetector:
def __init__(self, model_path: str, class_labels: List[str]):
self.model = YOLO(model_path)
self.classes = class_labels
self.box_color = (0, 255, 0)
self.text_color = (255, 255, 0)
self.font = cv2.FONT_HERSHEY_DUPLEX
def process_frame(self, frame):
detections = self.model(frame, stream=True)
for result in detections:
self._draw_predictions(frame, result.boxes)
return frame
def _draw_predictions(self, frame, boxes):
for box in boxes:
self._draw_box(frame, box)
self._draw_class(frame, box)
def _get_box_coordinates(self, box) -> Tuple[int, int, int, int]:
x1, y1, x2, y2 = map(int, box.xyxy[0])
return x1, y1, x2, y2
def _draw_box(self, frame, box):
x1, y1, x2, y2 = self._get_box_coordinates(box)
cv2.rectangle(frame, (x1, y1), (x2, y2), self.box_color, 2)
def _draw_class(self, frame, box):
x1, y1, _, _ = self._get_box_coordinates(box)
class_id = int(box.cls[0])
if 0 <= class_id < len(self.classes):
label = f"{self.classes[class_id].upper()}"
cv2.putText(frame, label, (x1, y1-5), self.font, 0.7,
self.text_color, 1, cv2.LINE_AA)