-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdetect.py
259 lines (188 loc) · 8.2 KB
/
detect.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
Project: Custom Object Detection
Author: Jitesh Saini
Github: https://github.com/jiteshsaini
website: https://helloworld.co.in
This code is built using the help of examples provided by the following resources:-
https://coral.ai/examples/
https://www.tensorflow.org/lite/examples
This is an example script demonstrating Object Detection using custom model trained using Tensorflow's Model Maker.
This script can be used for both custom models created by Model Maker tool or Pretrained Models.
"""
import time
import numpy as np
from PIL import Image
import tflite_runtime.interpreter as tflite
import os
import cv2
cap = cv2.VideoCapture(0)
edgetpu='0' # make it '1' if Coral Accelerator is attached and use model with 'edgetpu' name
#====custom model and label files==================
'''
model_dir = 'models/custom'
model = 'custom_detection_model.tflite'
#model = 'custom_detection_model_edgetpu.tflite'
label = 'custom_labels.txt'
'''
#=================================================
#====pretrained model and label files==================
model_dir = 'models/pretrained'
model='mobilenet_ssd_v2_coco_quant_postprocess.tflite'
#model='mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite'
#model='efficientdet_lite0.tflite'
#model='efficientdet_lite0_edgetpu.tflite'
label = 'coco_labels.txt'
#=================================================
model_path=os.path.join(model_dir,model)
label_path=os.path.join(model_dir,label)
#--------------------object detection--------------------------------------------------
def detect_objects(interpreter, image, score_threshold=0.3, top_k=6):
"""Returns list of detected objects."""
set_input_tensor(interpreter, image)
#interpreter.invoke()
invoke_interpreter(interpreter)
global model_dir
if (model_dir=='models/pretrained'):
# for pre-trained models
boxes = get_output_tensor(interpreter, 0)
class_ids = get_output_tensor(interpreter, 1)
scores = get_output_tensor(interpreter, 2)
count = int(get_output_tensor(interpreter, 3))
else:
# for custom models made by Model Maker
scores = get_output_tensor(interpreter, 0)
boxes = get_output_tensor(interpreter, 1)
count = int(get_output_tensor(interpreter, 2))
class_ids = get_output_tensor(interpreter, 3)
def make(i):
ymin, xmin, ymax, xmax = boxes[i]
return Object(
id=int(class_ids[i]),
score=scores[i],
bbox=BBox(xmin=np.maximum(0.0, xmin),
ymin=np.maximum(0.0, ymin),
xmax=np.minimum(1.0, xmax),
ymax=np.minimum(1.0, ymax)))
return [make(i) for i in range(top_k) if scores[i] >= score_threshold]
import collections
Object = collections.namedtuple('Object', ['id', 'score', 'bbox'])
class BBox(collections.namedtuple('BBox', ['xmin', 'ymin', 'xmax', 'ymax'])):
"""Bounding box.
Represents a rectangle which sides are either vertical or horizontal, parallel
to the x or y axis.
"""
__slots__ = ()
#--------------------------------------------------------------------
#----------Loading Labels----------------------------------------------------
import re
def load_labels(path):
"""Loads the labels file. Supports files with or without index numbers."""
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
labels = {}
for row_number, content in enumerate(lines):
pair = re.split(r'[:\s]+', content.strip(), maxsplit=1)
if len(pair) == 2 and pair[0].strip().isdigit():
labels[int(pair[0])] = pair[1].strip()
else:
labels[row_number] = pair[0].strip()
return labels
#--------------------------------------------------------------------------
#------Making Interpreter---------------------------------------------------------
import platform
EDGETPU_SHARED_LIB = {
'Linux': 'libedgetpu.so.1',
'Darwin': 'libedgetpu.1.dylib',
'Windows': 'edgetpu.dll'
}[platform.system()]
def make_interpreter(path, edgetpu):
print (path,edgetpu)
if(edgetpu=='0'):
interpreter = tflite.Interpreter(model_path=path)
else:
path, *device = path.split('@')
interpreter = tflite.Interpreter(model_path=path,experimental_delegates=[tflite.load_delegate(EDGETPU_SHARED_LIB,{'device': device[0]} if device else {})])
print('Loading Model: {} '.format(path))
return interpreter
#--------------------------------------------------------------------------
def input_image_size(interpreter):
"""Returns input image size as (width, height, channels) tuple."""
_, height, width, channels = interpreter.get_input_details()[0]['shape']
return width, height, channels
def set_input_tensor(interpreter, image):
"""Sets the input tensor."""
image = image.resize((input_image_size(interpreter)[0:2]), resample=Image.NEAREST)
#input_tensor(interpreter)[:, :] = image
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image
def get_output_tensor(interpreter, index):
"""Returns the output tensor at the given index."""
output_details = interpreter.get_output_details()[index]
#print(output_details)
tensor = np.squeeze(interpreter.get_tensor(output_details['index']))
return tensor
def invoke_interpreter(interpreter):
global inference_time_ms
t1=time.time()
interpreter.invoke()
inference_time_ms = (time.time() - t1) * 1000
print("****Inference time = ", inference_time_ms)
#--------------------------------------------------------------------------
#--------Image Overlay---------------------------------------------------------
def overlay_text_detection(objs, labels, cv2_im, fps):
height, width, channels = cv2_im.shape
font=cv2.FONT_HERSHEY_SIMPLEX
for obj in objs:
x0, y0, x1, y1 = list(obj.bbox)
x0, y0, x1, y1 = int(x0*width), int(y0*height), int(x1*width), int(y1*height)
percent = int(100 * obj.score)
if (percent>=60):
box_color, text_color, thickness=(0,255,0), (0,0,0),2
elif (percent<60 and percent>40):
box_color, text_color, thickness=(0,0,255), (0,0,0),2
else:
box_color, text_color, thickness=(255,0,0), (0,0,0),1
text3 = '{}% {}'.format(percent, labels.get(obj.id, obj.id))
print(text3)
try:
cv2_im = cv2.rectangle(cv2_im, (x0, y0), (x1, y1), box_color, thickness)
cv2_im = cv2.rectangle(cv2_im, (x0,y1-10), (x1, y1+10), (255,255,255), -1)
cv2_im = cv2.putText(cv2_im, text3, (x0, y1),font, 0.6, text_color, thickness)
except:
#log_error()
pass
global model, inference_time_ms
str1="FPS: " + str(fps)
cv2_im = cv2.putText(cv2_im, str1, (width-180, height-55),font, 0.7, (255, 0, 0), 2)
str2="Inference: " + str(round(inference_time_ms,1)) + " ms"
cv2_im = cv2.putText(cv2_im, str2, (width-240, height-25),font, 0.7, (255, 0, 0), 2)
cv2_im = cv2.rectangle(cv2_im, (0,height-20), (width, height), (0,0,0), -1)
cv2_im = cv2.putText(cv2_im, model, (10, height-5),font, 0.6, (0, 255, 0), 2)
return cv2_im
#--------------------------------------------------------------------------
def main():
interpreter = make_interpreter(model_path, edgetpu)
interpreter.allocate_tensors()
labels = load_labels(label_path)
fps=1
while True:
start_time=time.time()
ret, frame = cap.read()
if not ret:
break
cv2_im = frame
#cv2_im = cv2.flip(cv2_im, 0) #vertical flip
#cv2_im = cv2.flip(cv2_im, 1) #horzontal flip
cv2_im_rgb = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB)
image = Image.fromarray(cv2_im_rgb)
results = detect_objects(interpreter, image)
cv2_im = overlay_text_detection(results, labels, cv2_im, fps)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.imshow('Detect Objects', cv2_im)
elapsed_ms = (time.time() - start_time) * 1000
fps=round(1000/elapsed_ms,1)
print("--------fps: ",fps,"---------------")
if __name__ == '__main__':
main()