-
Notifications
You must be signed in to change notification settings - Fork 62
/
demo.py
83 lines (64 loc) · 2.71 KB
/
demo.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
import argparse
import glob
import os
import cv2
from yolo import YOLO
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--images', default="images", help='Path to images or image file')
ap.add_argument('-n', '--network', default="normal", choices=["normal", "tiny", "prn", "v4-tiny"],
help='Network Type')
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.25, help='Confidence for yolo')
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("extracting tags for each image...")
if args.images.endswith(".txt"):
with open(args.images, "r") as myfile:
lines = myfile.readlines()
files = map(lambda x: os.path.join(os.path.dirname(args.images), x.strip()), lines)
else:
files = sorted(glob.glob("%s/*.jpg" % args.images))
conf_sum = 0
detection_count = 0
for file in files:
print(file)
mat = cv2.imread(file)
width, height, inference_time, results = yolo.inference(mat)
print("%s in %s seconds: %s classes found!" %
(os.path.basename(file), round(inference_time, 2), len(results)))
output = []
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', 848, 640)
for detection in results:
id, name, confidence, x, y, w, h = detection
cx = x + (w / 2)
cy = y + (h / 2)
conf_sum += confidence
detection_count += 1
# draw a bounding box rectangle and label on the image
color = (255, 0, 255)
cv2.rectangle(mat, (x, y), (x + w, y + h), color, 1)
text = "%s (%s)" % (name, round(confidence, 2))
cv2.putText(mat, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.25, color, 1)
print("%s with %s confidence" % (name, round(confidence, 2)))
# cv2.imwrite("export.jpg", mat)
# show the output image
cv2.imshow('image', mat)
cv2.waitKey(0)
print("AVG Confidence: %s Count: %s" % (round(conf_sum / detection_count, 2), detection_count))
cv2.destroyAllWindows()