-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
37 lines (26 loc) · 1016 Bytes
/
detect.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
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator # ultralytics.yolo.utils.plotting is deprecated
import cv2
# モデル読み込み(https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt)
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while True:
_, img = cap.read()
# BGR to RGB conversion is performed under the hood
# see: https://github.com/ultralytics/ultralytics/issues/2575
results = model.predict(img)
for r in results:
annotator = Annotator(img)
boxes = r.boxes
for box in boxes:
b = box.xyxy[0] # get box coordinates in (left, top, right, bottom) format
c = box.cls
annotator.box_label(b, model.names[int(c)])
img = annotator.result()
cv2.imshow('YOLO V8 Detection', img)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
cap.release()
cv2.destroyAllWindows()