-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolo_test.py
129 lines (116 loc) · 2.06 KB
/
yolo_test.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from ultralytics import YOLO
import cv2
# start webcam
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
# model
model = YOLO('yolov8m.pt')
# object classes
classNames = ["person"
,"bicycle"
,"car"
,"motorcycle"
,"airplane"
,"bus"
,"train"
,"truck"
,"boat"
,"traffic light"
,"fire hydrant"
,"stop sign"
,"parking meter"
,"bench"
,"bird"
,"cat"
,"dog"
,"horse"
,"sheep"
,"cow"
,"elephant"
,"bear"
,"zebra"
,"giraffe"
,"backpack"
,"umbrella"
,"handbag"
,"tie"
,"suitcase"
,"frisbee"
,"skis"
,'snowboard'
,'sports ball'
,'kite'
,'baseball bat'
,'baseball glove'
,'skateboard'
,'surfboard'
,'tennis racket'
,'bottle'
,'wine glass'
,'cup'
,'fork'
,'knife'
,'spoon'
,'bowl'
,'banana'
,'apple'
,'sandwich'
,'orange'
,'broccoli'
,'carrot'
,'hot dog'
,'pizza'
,'donut'
,'cake'
,'chair'
,'couch'
,'potted plant'
,'bed'
,'dining table'
,'toilet'
,'tv'
,'laptop'
,'mouse'
,'remote'
,'keyboard'
,'cell phone'
,'microwave'
,'oven'
,'toaster'
,'sink'
,'refrigerator'
,'book'
,'clock'
,'vase'
,'scissors'
,'teddy bear'
,"hair drier",
"toothbrush"]
while True:
success, img = cap.read()
results = model(img, stream=True)
# coordinates
for r in results:
boxes = r.boxes
for box in boxes:
# bounding box
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
# put box in cam
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
# class name
cls = int(box.cls[0])
# object details
org = [x1, y1]
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color = (255, 0, 0)
thickness = 2
# place label
cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)
cv2.imshow('Webcam', img)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()