-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartCountDetection.py
147 lines (103 loc) · 5.51 KB
/
PartCountDetection.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import cv2
import numpy as np
from openvino.inference_engine import IECore
from Classes import Detection
class PartCountDetection():
PROB_THRESHOLD = 0.2
MAX_DETECTIONS = 5
ANCHOR_BOXES = np.array([[0.573, 0.677], [1.87, 2.06], [3.34, 5.47], [7.88, 3.53], [9.77, 9.17]])
IOU_THRESHOLD = 0.5
def __init__(self, inference_engine, device_name):
model_path = 'PartCountDetection/model'
model_xml = model_path + '.xml'
model_bin = model_path + '.bin'
model_labels = model_path + '.labels'
with open(model_labels, 'r') as io:
self.labels = [x.split(sep=' ', maxsplit=1)[-1].strip() for x in io]
network = inference_engine.read_network(model=model_xml, weights=model_bin)
network.batch_size = 1
self.input_blob = next(iter(network.inputs))
self.output_blob = next(iter(network.outputs))
n, c, h, w = network.inputs[self.input_blob].shape
self.exec_network = inference_engine.load_network(network=network, device_name=device_name)
self.images = np.ndarray(shape=(n, c, h, w))
def Infer(self, image):
self.images[0] = self._preprocess(image)
result = self.exec_network.infer(inputs={self.input_blob: self.images})
outputs = result[self.output_blob]
return self._postprocess(outputs)
def _preprocess(self, image):
resized_image = cv2.resize(image, (416, 416), interpolation=cv2.INTER_AREA)
input_image = resized_image.transpose((2, 0, 1))
return input_image
def _postprocess(self, outputs):
outputs = np.squeeze(outputs).transpose((1,2,0)).astype(np.float32)
boxes, class_probs = self._extract_bounding_boxes(outputs)
max_probs = np.amax(class_probs, axis=1)
index, = np.where(max_probs > self.PROB_THRESHOLD)
index = index[(-max_probs[index]).argsort()]
selected_boxes, selected_classes, selected_probs = self._non_maximum_suppression(
boxes[index], class_probs[index])
predictions = list()
for i in range(len(selected_boxes)):
label = self.labels[selected_classes[i]]
probability = selected_probs[i] * 100
left = round(float(selected_boxes[i][0]), 8)
top = round(float(selected_boxes[i][1]), 8)
width = round(float(selected_boxes[i][2]), 8)
height = round(float(selected_boxes[i][3]), 3)
prediction = Detection(label, probability, left, top, width, height)
predictions.append(prediction)
return predictions
def _extract_bounding_boxes(self, output):
num_anchor = self.ANCHOR_BOXES.shape[0]
height, width, channels = output.shape
num_class = int(channels / num_anchor) - 5
outputs = output.reshape((height, width, num_anchor, -1))
x = (self._logistic(outputs[..., 0]) + np.arange(width)[np.newaxis, :, np.newaxis]) / width
y = (self._logistic(outputs[..., 1]) + np.arange(height)[:, np.newaxis, np.newaxis]) / height
w = np.exp(outputs[..., 2]) * self.ANCHOR_BOXES[:, 0][np.newaxis, np.newaxis, :] / width
h = np.exp(outputs[..., 3]) * self.ANCHOR_BOXES[:, 1][np.newaxis, np.newaxis, :] / height
x = x - w / 2
y = y - h / 2
boxes = np.stack((x, y, w, h), axis=-1).reshape(-1, 4)
objectness = self._logistic(outputs[..., 4])
class_probs = outputs[..., 5:]
class_probs = np.exp(class_probs - np.amax(class_probs, axis=3)[..., np.newaxis])
class_probs = class_probs / np.sum(class_probs, axis=3)[..., np.newaxis] * objectness[..., np.newaxis]
class_probs = class_probs.reshape(-1, num_class)
return (boxes, class_probs)
def _logistic(self, x):
return np.where(x > 0, 1 / (1 + np.exp(-x)), np.exp(x) / (1 + np.exp(x)))
def _non_maximum_suppression(self, boxes, class_probs):
max_detections = min(self.MAX_DETECTIONS, len(boxes))
max_probs = np.amax(class_probs, axis=1)
max_classes = np.argmax(class_probs, axis=1)
areas = boxes[:, 2] * boxes[:, 3]
selected_boxes = []
selected_classes = []
selected_probs = []
while len(selected_boxes) < max_detections:
i = np.argmax(max_probs)
if max_probs[i] < self.PROB_THRESHOLD:
break
selected_boxes.append(boxes[i])
selected_classes.append(max_classes[i])
selected_probs.append(max_probs[i])
box = boxes[i]
other_indices = np.concatenate((np.arange(i), np.arange(i + 1, len(boxes))))
other_boxes = boxes[other_indices]
x1 = np.maximum(box[0], other_boxes[:, 0])
y1 = np.maximum(box[1], other_boxes[:, 1])
x2 = np.minimum(box[0] + box[2], other_boxes[:, 0] + other_boxes[:, 2])
y2 = np.minimum(box[1] + box[3], other_boxes[:, 1] + other_boxes[:, 3])
w = np.maximum(0, x2 - x1)
h = np.maximum(0, y2 - y1)
overlap_area = w * h
iou = overlap_area / (areas[i] + areas[other_indices] - overlap_area)
overlapping_indices = other_indices[np.where(iou > self.IOU_THRESHOLD)[0]]
overlapping_indices = np.append(overlapping_indices, i)
class_probs[overlapping_indices, max_classes[i]] = 0
max_probs[overlapping_indices] = np.amax(class_probs[overlapping_indices], axis=1)
max_classes[overlapping_indices] = np.argmax(class_probs[overlapping_indices], axis=1)
return selected_boxes, selected_classes, selected_probs