-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
66 lines (45 loc) · 2.21 KB
/
util.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
import numpy as np
import cv2
def NMS(boxes, class_ids, confidences, overlapThresh = 0.5):
boxes = np.asarray(boxes)
class_ids = np.asarray(class_ids)
confidences = np.asarray(confidences)
# Return empty lists, if no boxes given
if len(boxes) == 0:
return [], [], []
x1 = boxes[:, 0] - (boxes[:, 2] / 2) # x coordinate of the top-left corner
y1 = boxes[:, 1] - (boxes[:, 3] / 2) # y coordinate of the top-left corner
x2 = boxes[:, 0] + (boxes[:, 2] / 2) # x coordinate of the bottom-right corner
y2 = boxes[:, 1] + (boxes[:, 3] / 2) # y coordinate of the bottom-right corner
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
indices = np.arange(len(x1))
for i, box in enumerate(boxes):
# Create temporary indices
temp_indices = indices[indices != i]
# Find out the coordinates of the intersection box
xx1 = np.maximum(box[0] - (box[2] / 2), boxes[temp_indices, 0] - (boxes[temp_indices, 2] / 2))
yy1 = np.maximum(box[1] - (box[3] / 2), boxes[temp_indices, 1] - (boxes[temp_indices, 3] / 2))
xx2 = np.minimum(box[0] + (box[2] / 2), boxes[temp_indices, 0] + (boxes[temp_indices, 2] / 2))
yy2 = np.minimum(box[1] + (box[3] / 2), boxes[temp_indices, 1] + (boxes[temp_indices, 3] / 2))
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
# compute the ratio of overlap
overlap = (w * h) / areas[temp_indices]
# if overlapping greater than our threshold, remove the bounding box
if np.any(overlap) > overlapThresh:
indices = indices[indices != i]
# return only the boxes at the remaining indices
return boxes[indices], class_ids[indices], confidences[indices]
def get_outputs(net):
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
outs = net.forward(output_layers)
outs = [c for out in outs for c in out if c[4] > 0.1]
return outs
def draw(bbox, img):
xc, yc, w, h = bbox
img = cv2.rectangle(img,
(xc - int(w / 2), yc - int(h / 2)),
(xc + int(w / 2), yc + int(h / 2)),
(0, 255, 0), 20)
return img