-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolo_utils.py
93 lines (54 loc) · 2.67 KB
/
yolo_utils.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
84
85
86
87
88
89
90
91
92
93
import numpy as np
import argparse
import cv2 as cv
import subprocess
import time
import os
def show_image(img):
cv.imshow("Image", img)
cv.waitKey(0)
def draw_labels_and_boxes(img, boxes, confidences, classids, idxs, colors, labels):
if len(idxs) > 0:
for i in idxs.flatten():
x, y = boxes[i][0], boxes[i][1]
w, h = boxes[i][2], boxes[i][3]
color = [int(c) for c in colors[classids[i]]]
cv.rectangle(img, (x, y), (x+w, y+h), color, 2)
text = "{}: {:4f}".format(labels[classids[i]], confidences[i])
cv.putText(img, text, (x, y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
return img
def generate_boxes_confidences_classids(outs, height, width, tconf):
boxes = []
confidences = []
classids = []
for out in outs:
for detection in out:
scores = detection[5:]
classid = np.argmax(scores)
confidence = scores[classid]
if confidence > tconf:
box = detection[0:4] * np.array([width, height, width, height])
centerX, centerY, bwidth, bheight = box.astype('int')
x = int(centerX - (bwidth / 2))
y = int(centerY - (bheight / 2))
boxes.append([x, y, int(bwidth), int(bheight)])
confidences.append(float(confidence))
classids.append(classid)
return boxes, confidences, classids
def infer_image(net, layer_names, height, width, img, colors, labels, FLAGS,
boxes=None, confidences=None, classids=None, idxs=None, infer=True):
if infer:
blob = cv.dnn.blobFromImage(img, 1 / 255.0, (416, 416),
swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
outs = net.forward(layer_names)
end = time.time()
if FLAGS.show_time:
print ("[INFO] YOLOv3 took {:6f} seconds".format(end - start))
boxes, confidences, classids = generate_boxes_confidences_classids(outs, height, width, FLAGS.confidence)
idxs = cv.dnn.NMSBoxes(boxes, confidences, FLAGS.confidence, FLAGS.threshold)
if boxes is None or confidences is None or idxs is None or classids is None:
raise '[ERROR] Required variables are set to None before drawing boxes on images.'
img = draw_labels_and_boxes(img, boxes, confidences, classids, idxs, colors, labels)
return img, boxes, confidences, classids, idxs