-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
156 lines (110 loc) · 4.71 KB
/
predict.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
148
149
150
151
152
153
154
155
import cv2
import numpy as np
from yolo_head import yolo_head
def predict(model, orig, config, confidence=0.5, iou_threshold=0.5):
image, image_data = preprocess_image(orig, model_image_size=(config['width'], config['height']))
boxes, classes, scores = handle_predictions(model.predict([image_data]),
confidence=confidence,
iou_threshold=iou_threshold)
boxes2= draw_boxes(image, boxes, classes, scores, config)
return [boxes2,classes,scores]
def predict_with_yolo_head(model, orig, config, confidence=0.2, iou_threshold=0.2):
image, image_data = preprocess_image(orig, model_image_size=(config['width'], config['height']))
predictions = yolo_head(model.predict([image_data]), num_classes=80,
input_dims=(config['width'], config['height']))
boxes, classes, scores = handle_predictions(predictions,
confidence=confidence,
iou_threshold=iou_threshold)
draw_boxes(image, boxes, classes, scores, config)
return np.array(image)
def handle_predictions(predictions, confidence=0.5, iou_threshold=0.5):
boxes = predictions[:, :, :4]
box_confidences = np.expand_dims(predictions[:, :, 4], -1)
box_class_probs = predictions[:, :, 5:]
box_scores = box_confidences * box_class_probs
box_classes = np.argmax(box_scores, axis=-1)
box_class_scores = np.max(box_scores, axis=-1)
pos = np.where(box_class_scores >= confidence)
boxes = boxes[pos]
classes = box_classes[pos]
scores = box_class_scores[pos]
# Boxes, Classes and Scores returned from NMS
n_boxes, n_classes, n_scores = nms_boxes(boxes, classes, scores, iou_threshold)
if n_boxes:
boxes = np.concatenate(n_boxes)
classes = np.concatenate(n_classes)
scores = np.concatenate(n_scores)
return boxes, classes, scores
else:
return None, None, None
def preprocess_image(img_arr, model_image_size):
image = img_arr.astype('uint8')
resized_image = cv2.resize(image, tuple(reversed(model_image_size)), cv2.INTER_AREA)
image_data = resized_image.astype('float32')
image_data /= 255.
image_data = np.expand_dims(image_data, 0) # Add batch dimension.
return image, image_data
def nms_boxes(boxes, classes, scores, iou_threshold):
nboxes, nclasses, nscores = [], [], []
for c in set(classes):
inds = np.where(classes == c)
b = boxes[inds]
c = classes[inds]
s = scores[inds]
x = b[:, 0]
y = b[:, 1]
w = b[:, 2]
h = b[:, 3]
areas = w * h
order = s.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x[i], x[order[1:]])
yy1 = np.maximum(y[i], y[order[1:]])
xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])
yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])
w1 = np.maximum(0.0, xx2 - xx1 + 1)
h1 = np.maximum(0.0, yy2 - yy1 + 1)
inter = w1 * h1
ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= iou_threshold)[0]
order = order[inds + 1]
keep = np.array(keep)
nboxes.append(b[keep])
nclasses.append(c[keep])
nscores.append(s[keep])
return nboxes, nclasses, nscores
def draw_label(image, text, color, coords):
font = cv2.FONT_HERSHEY_PLAIN
font_scale = 1.
(text_width, text_height) = cv2.getTextSize(text, font, fontScale=font_scale, thickness=1)[0]
padding = 5
rect_height = text_height + padding * 2
rect_width = text_width + padding * 2
(x, y) = coords
cv2.rectangle(image, (x, y), (x + rect_width, y - rect_height), color, cv2.FILLED)
cv2.putText(image, text, (x + padding, y - text_height + padding), font,
fontScale=font_scale,
color=(255, 255, 255),
lineType=cv2.LINE_AA)
return image
def draw_boxes(image, boxes, classes, scores, config):
height, width = image.shape[:2]
boxesp=[]
labels = config['labels']
colors = config['colors']
ratio_x = width / config['width']
ratio_y = height / config['height']
if boxes is not None:
for box, cls, score in zip(boxes, classes, scores):
x, y, w, h = box
# Rescale box coordinates
x1 = int(x * ratio_x)
y1 = int(y * ratio_y)
x2 = int((x + w) * ratio_x)
y2 = int((y + h) * ratio_y)
box=[x1,y1,x2,y2]
boxesp.append(box)
return boxesp