-
Notifications
You must be signed in to change notification settings - Fork 24
/
tflite_example.py
executable file
·161 lines (128 loc) · 5.59 KB
/
tflite_example.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
156
157
158
159
160
161
import time
import cv2
import numpy as np
import sys
import os
import tensorflow as tf
from PIL import Image, ImageDraw
def handle_predictions(predictions, confidence=0.6, 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]
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 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 load_coco_names(file_name):
names = {}
with open(file_name) as f:
for id, name in enumerate(f):
names[id] = name
return names
def letter_box_image(image: Image.Image, output_height: int, output_width: int, fill_value)-> np.ndarray:
height_ratio = float(output_height)/image.size[1]
width_ratio = float(output_width)/image.size[0]
fit_ratio = min(width_ratio, height_ratio)
fit_height = int(image.size[1] * fit_ratio)
fit_width = int(image.size[0] * fit_ratio)
fit_image = np.asarray(image.resize((fit_width, fit_height), resample=Image.BILINEAR))
if isinstance(fill_value, int):
fill_value = np.full(fit_image.shape[2], fill_value, fit_image.dtype)
to_return = np.tile(fill_value, (output_height, output_width, 1))
pad_top = int(0.5 * (output_height - fit_height))
pad_left = int(0.5 * (output_width - fit_width))
to_return[pad_top:pad_top+fit_height, pad_left:pad_left+fit_width] = fit_image
return to_return
def draw_boxes(boxes, classes, scores, img, cls_names, detection_size, is_letter_box_image):
draw = ImageDraw.Draw(img)
color = tuple(np.random.randint(0, 256, 3))
for box, score, cls in zip(boxes, scores, classes):
box = convert_to_original_size(box, np.array(detection_size),
np.array(img.size),
is_letter_box_image)
draw.rectangle(box, outline=color)
draw.text(box[:2], '{} {:.2f}%'.format(
cls_names[cls], score * 100), fill=color)
def convert_to_original_size(box, size, original_size, is_letter_box_image):
if is_letter_box_image:
box = box.reshape(2, 2)
box[0, :] = letter_box_pos_to_original_pos(box[0, :], size, original_size)
box[1, :] = letter_box_pos_to_original_pos(box[1, :], size, original_size)
else:
ratio = original_size / size
box = box.reshape(2, 2) * ratio
return list(box.reshape(-1))
def letter_box_pos_to_original_pos(letter_pos, current_size, ori_image_size)-> np.ndarray:
letter_pos = np.asarray(letter_pos, dtype=np.float)
current_size = np.asarray(current_size, dtype=np.float)
ori_image_size = np.asarray(ori_image_size, dtype=np.float)
final_ratio = min(current_size[0]/ori_image_size[0], current_size[1]/ori_image_size[1])
pad = 0.5 * (current_size - final_ratio * ori_image_size)
pad = pad.astype(np.int32)
to_return_pos = (letter_pos - pad) / final_ratio
return to_return_pos
model_path = os.path.join(os.getcwd(), 'yolo_v3.tflite')
interpreter = tf.contrib.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print(output_details)
if input_details[0]['dtype'] == np.float32:
floating_model = True
height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
img = Image.open('example.jpg')
img_resized = letter_box_image(img, height, width, 128)
img_resized = img_resized.astype(np.float32)
interpreter.set_tensor(input_details[0]['index'], np.expand_dims(img_resized, 0))
interpreter.invoke()
predictions = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
boxes, classes, scores = handle_predictions(predictions[0],
confidence=0.3,
iou_threshold=0.5)
class_names = load_coco_names("coco.names")
draw_boxes(boxes, classes, scores, img, class_names, (height, width), True)
img.save("output.jpg")