-
Notifications
You must be signed in to change notification settings - Fork 62
/
demo_webcam.py
76 lines (60 loc) · 2.47 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
import cv2
from yolo import YOLO
ap = argparse.ArgumentParser()
ap.add_argument('-n', '--network', default="normal", choices=["normal", "tiny", "prn", "v4-tiny"],
help='Network Type')
ap.add_argument('-d', '--device', type=int, default=0, help='Device to use')
ap.add_argument('-s', '--size', default=416, help='Size for yolo')
ap.add_argument('-c', '--confidence', default=0.2, help='Confidence for yolo')
ap.add_argument('-nh', '--hands', default=-1, help='Total number of hands to be detected per frame (-1 for all)')
args = ap.parse_args()
if args.network == "normal":
print("loading yolo...")
yolo = YOLO("models/cross-hands.cfg", "models/cross-hands.weights", ["hand"])
elif args.network == "prn":
print("loading yolo-tiny-prn...")
yolo = YOLO("models/cross-hands-tiny-prn.cfg", "models/cross-hands-tiny-prn.weights", ["hand"])
elif args.network == "v4-tiny":
print("loading yolov4-tiny-prn...")
yolo = YOLO("models/cross-hands-yolov4-tiny.cfg", "models/cross-hands-yolov4-tiny.weights", ["hand"])
else:
print("loading yolo-tiny...")
yolo = YOLO("models/cross-hands-tiny.cfg", "models/cross-hands-tiny.weights", ["hand"])
yolo.size = int(args.size)
yolo.confidence = float(args.confidence)
print("starting webcam...")
cv2.namedWindow("preview")
vc = cv2.VideoCapture(args.device)
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)
# display fps
cv2.putText(frame, f'{round(1/inference_time,2)} FPS', (15,15), cv2.FONT_HERSHEY_SIMPLEX,0.5, (0,255,255), 2)
# sort by confidence
results.sort(key=lambda x: x[2])
# how many hands should be shown
hand_count = len(results)
if args.hands != -1:
hand_count = int(args.hands)
# display hands
for detection in results[:hand_count]:
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 = (0, 255, 255)
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()