-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_classifier.py
54 lines (39 loc) · 1.18 KB
/
live_classifier.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
# import the opencv library
import cv2
import time
import supervision as sv
from ultralytics import YOLO
import yaml
from pathlib import Path
def main():
# define resolution
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# specify the model
model = YOLO("model/best.pt")
classes = yaml.safe_load(Path("data/gripdata.yaml").read_text())
label_dict = classes['names']
# customize the bounding box
box_annotator = sv.BoxAnnotator(
thickness=2,
text_thickness=2,
text_scale=1
)
while True:
ret, frame = cap.read()
result = model(frame, agnostic_nms=True)[0]
detections = sv.Detections.from_yolov8(result)
labels = [label_dict[d] for d in detections.class_id]
frame = box_annotator.annotate(
scene=frame,
detections=detections,
labels=labels
)
cv2.imshow("yolov8", frame)
if (cv2.waitKey(30) == 27): # break with escape key
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()