-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcam.py
More file actions
30 lines (25 loc) · 768 Bytes
/
webcam.py
File metadata and controls
30 lines (25 loc) · 768 Bytes
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
from algorithm.object_detector import YOLOv7
from utils.detections import draw
import json
import cv2
yolov7 = YOLOv7()
yolov7.load('coco.weights', classes='coco.yaml', device='cpu') # use 'gpu' for CUDA GPU inference
webcam = cv2.VideoCapture(0)
if webcam.isOpened() == False:
print('[!] error opening the webcam')
try:
while webcam.isOpened():
ret, frame = webcam.read()
if ret == True:
detections = yolov7.detect(frame)
detected_frame = draw(frame, detections)
print(json.dumps(detections, indent=4))
cv2.imshow('webcam', detected_frame)
cv2.waitKey(1)
else:
break
except KeyboardInterrupt:
pass
webcam.release()
print('[+] webcam closed')
yolov7.unload()