-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyolo-webcam.py
More file actions
48 lines (37 loc) · 1.87 KB
/
yolo-webcam.py
File metadata and controls
48 lines (37 loc) · 1.87 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
39
40
41
42
43
44
45
46
47
48
from ultralytics import YOLO
import cv2
import cvzone
import math
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']
cap = cv2.VideoCapture(0)
cap.set(3, 720)
cap.set(4, 500)
model = YOLO('yolo-weights/yolov8n.pt')
while True:
ret, img = cap.read()
results = model(img, stream=True)
for result in results:
boxes = result.boxes
for box in boxes:
#Bounding box
x1, y1, x2, y2 = box.xyxy[0].int().tolist()
# cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
w, h = x2-x1, y2-y1
# cvzone.cornerRect(img, (x1, y1, w, h), 30,1, 1, (0, 255, 0), (21, 23, 148))
cvzone.cornerRect(img, (x1, y1, w, h))
conf = math.ceil(box.conf[0]*100)
cls = int(box.cls[0])
#Show confidence on image
cvzone.putTextRect(img, f'{classNames[cls]}: {conf}%', (max(0,x1), max(40,y1)))
cv2.imshow('Image', img)
cv2.waitKey(1)
# cap.release()
# cv2.destroyAllWindows()