-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdemo_webcam.py
62 lines (48 loc) · 1.86 KB
/
demo_webcam.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
import argparse
import cv2
from yolo import YOLO
ap = argparse.ArgumentParser()
ap.add_argument('-n', '--network', default="normal", help='Network Type: normal / tiny / prn')
ap.add_argument('-d', '--device', default=0, help='Device to use')
ap.add_argument('-s', '--size', default=416, help='Size for yolo')
ap.add_argument('-c', '--confidence', default=0.5, help='Confidence for yolo')
args = ap.parse_args()
classes = ["good", "bad", "none"]
if args.network == "normal":
print("loading yolov4...")
yolo = YOLO("models/mask-yolov4.cfg", "models/mask-yolov4.weights", classes)
elif args.network == "prn":
print("loading yolov3-tiny-prn...")
yolo = YOLO("models/mask-yolov3-tiny-prn.cfg", "models/mask-yolov3-tiny-prn.weights", classes)
else:
print("loading yolov4-tiny...")
yolo = YOLO("models/mask-yolov4-tiny.cfg", "models/mask-yolov4-tiny.weights", classes)
yolo.size = int(args.size)
yolo.confidence = float(args.confidence)
colors = [(0, 255, 0), (0, 165, 255), (0, 0, 255)]
print("starting webcam...")
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
width, height, inference_time, results = yolo.inference(frame)
for detection in results:
id, name, confidence, x, y, w, h = detection
cx = x + (w / 2)
cy = y + (h / 2)
# draw a bounding box rectangle and label on the image
color = colors[id]
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
text = "%s (%s)" % (name, round(confidence, 2))
cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.5, color, 2)
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")
vc.release()