-
Notifications
You must be signed in to change notification settings - Fork 3
/
yolo_inference.py
56 lines (45 loc) · 1.26 KB
/
yolo_inference.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import cv2
from ultralytics import YOLO
import supervision as sv
import numpy as np
def initialize_camera(camera_index=0):
cap = cv2.VideoCapture(camera_index)
return cap
def load_yolov8_model(model_path="yolov8n.pt"):
model = YOLO(model_path)
return model
def process_frame(frame, model, box_annotator):
result = model(frame, agnostic_nms=True)[0]
detections = sv.Detections.from_yolov8(result)
labels = [
f"{model.model.names[class_id]} {confidence:0.2f}"
for _, confidence, class_id, _
in detections
]
annotated_frame = box_annotator.annotate(
scene=frame,
detections=detections,
labels=labels
)
return annotated_frame
def main():
cap = initialize_camera()
model = load_yolov8_model()
box_annotator = sv.BoxAnnotator(
thickness=1,
text_thickness=1,
text_scale=1
)
while True:
ret, frame = cap.read()
if not ret:
print("Failed to capture frame")
break
annotated_frame = process_frame(frame, model, box_annotator)
cv2.imshow("yolov8", annotated_frame)
if cv2.waitKey(20) == 27:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()