forked from naisy/realtime_object_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detection_nms_v2.py
415 lines (385 loc) · 17.2 KB
/
detection_nms_v2.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import numpy as np
from tf_utils import visualization_utils_cv2 as vis_util
from lib.session_worker import SessionWorker
from lib.load_graph_nms_v2 import LoadFrozenGraph
from lib.load_label_map import LoadLabelMap
from lib.mpvariable import MPVariable
from lib.mpvisualizeworker import MPVisualizeWorker, visualization
from lib.mpio import start_sender
import time
import cv2
import tensorflow as tf
import os
import sys
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY2:
import Queue
elif PY3:
import queue as Queue
class NMSV2():
def __init__(self):
return
def start(self, cfg):
""" """ """ """ """ """ """ """ """ """ """
GET CONFIG
""" """ """ """ """ """ """ """ """ """ """
FORCE_GPU_COMPATIBLE = cfg['force_gpu_compatible']
SAVE_TO_FILE = cfg['save_to_file']
VISUALIZE = cfg['visualize']
VIS_WORKER = cfg['vis_worker']
VIS_TEXT = cfg['vis_text']
MAX_FRAMES = cfg['max_frames']
WIDTH = cfg['width']
HEIGHT = cfg['height']
FPS_INTERVAL = cfg['fps_interval']
DET_INTERVAL = cfg['det_interval']
DET_TH = cfg['det_th']
SPLIT_MODEL = cfg['split_model']
LOG_DEVICE = cfg['log_device']
ALLOW_MEMORY_GROWTH = cfg['allow_memory_growth']
SPLIT_SHAPE = cfg['split_shape']
DEBUG_MODE = cfg['debug_mode']
LABEL_PATH = cfg['label_path']
NUM_CLASSES = cfg['num_classes']
SRC_FROM = cfg['src_from']
CAMERA = 0
MOVIE = 1
IMAGE = 2
if SRC_FROM == 'camera':
SRC_FROM = CAMERA
VIDEO_INPUT = cfg['camera_input']
elif SRC_FROM == 'movie':
SRC_FROM = MOVIE
VIDEO_INPUT = cfg['movie_input']
elif SRC_FROM == 'image':
SRC_FROM = IMAGE
VIDEO_INPUT = cfg['image_input']
""" """
""" """ """ """ """ """ """ """ """ """ """
LOAD FROZEN_GRAPH
""" """ """ """ """ """ """ """ """ """ """
load_frozen_graph = LoadFrozenGraph(cfg)
graph = load_frozen_graph.load_graph()
""" """
""" """ """ """ """ """ """ """ """ """ """
LOAD LABEL MAP
""" """ """ """ """ """ """ """ """ """ """
llm = LoadLabelMap()
category_index = llm.load_label_map(cfg)
""" """
""" """ """ """ """ """ """ """ """ """ """
PREPARE TF CONFIG OPTION
""" """ """ """ """ """ """ """ """ """ """
# Session Config: allow seperate GPU/CPU adressing and limit memory allocation
config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=LOG_DEVICE)
config.gpu_options.allow_growth = ALLOW_MEMORY_GROWTH
config.gpu_options.force_gpu_compatible = FORCE_GPU_COMPATIBLE
#config.gpu_options.per_process_gpu_memory_fraction = 0.01 # 80MB memory is enough to run on TX2
""" """
""" """ """ """ """ """ """ """ """ """ """
PREPARE GRAPH I/O TO VARIABLE
""" """ """ """ """ """ """ """ """ """ """
# Define Input and Ouput tensors
image_tensor = graph.get_tensor_by_name('image_tensor:0')
detection_boxes = graph.get_tensor_by_name('detection_boxes:0')
detection_scores = graph.get_tensor_by_name('detection_scores:0')
detection_classes = graph.get_tensor_by_name('detection_classes:0')
num_detections = graph.get_tensor_by_name('num_detections:0')
if SPLIT_MODEL:
SPLIT_TARGET_NAME = ['Postprocessor/Slice',
'Postprocessor/ExpandDims_1',
'Postprocessor/stack_1'
]
split_out = []
split_in = []
for stn in SPLIT_TARGET_NAME:
split_out += [graph.get_tensor_by_name(stn+':0')]
split_in += [graph.get_tensor_by_name(stn+'_1:0')]
""" """
""" """ """ """ """ """ """ """ """ """ """
START WORKER THREAD
""" """ """ """ """ """ """ """ """ """ """
# gpu_worker uses in split_model and non-split_model
gpu_tag = 'GPU'
cpu_tag = 'CPU'
gpu_worker = SessionWorker(gpu_tag, graph, config)
if SPLIT_MODEL:
gpu_opts = split_out
cpu_worker = SessionWorker(cpu_tag, graph, config)
cpu_opts = [detection_boxes, detection_scores, detection_classes, num_detections]
else:
gpu_opts = [detection_boxes, detection_scores, detection_classes, num_detections]
""" """
"""
START VISUALIZE WORKER
"""
if VISUALIZE and VIS_WORKER:
q_out = Queue.Queue()
vis_worker = MPVisualizeWorker(cfg, MPVariable.vis_in_con)
""" """ """ """ """ """ """ """ """ """ """
START SENDER THREAD
""" """ """ """ """ """ """ """ """ """ """
start_sender(MPVariable.det_out_con, q_out)
proc_frame_counter = 0
vis_proc_time = 0
""" """ """ """ """ """ """ """ """ """ """
WAIT UNTIL THE FIRST DUMMY IMAGE DONE
""" """ """ """ """ """ """ """ """ """ """
print('Loading...')
sleep_interval = 0.1
"""
PUT DUMMY DATA INTO GPU WORKER
"""
gpu_feeds = {image_tensor: [np.zeros((300, 300, 3))]}
gpu_extras = {}
gpu_worker.put_sess_queue(gpu_opts, gpu_feeds, gpu_extras)
if SPLIT_MODEL:
"""
PUT DUMMY DATA INTO CPU WORKER
"""
cpu_feeds = {split_in[0]: np.zeros((1, SPLIT_SHAPE, NUM_CLASSES)),
split_in[1]: np.zeros((1, SPLIT_SHAPE, 1, 4)),
split_in[2]: [[0., 0., 1., 1.]]}
cpu_extras = {}
cpu_worker.put_sess_queue(cpu_opts, cpu_feeds, cpu_extras)
"""
WAIT UNTIL JIT-COMPILE DONE
"""
while True:
g = gpu_worker.get_result_queue()
if g is None:
time.sleep(sleep_interval)
else:
break
if SPLIT_MODEL:
while True:
c = cpu_worker.get_result_queue()
if c is None:
time.sleep(sleep_interval)
else:
break
""" """
""" """ """ """ """ """ """ """ """ """ """
START CAMERA
""" """ """ """ """ """ """ """ """ """ """
if SRC_FROM == CAMERA:
from lib.webcam import WebcamVideoStream as VideoReader
elif SRC_FROM == MOVIE:
from lib.video import VideoReader
elif SRC_FROM == IMAGE:
from lib.image import ImageReader as VideoReader
video_reader = VideoReader()
if SRC_FROM == IMAGE:
video_reader.start(VIDEO_INPUT, save_to_file=SAVE_TO_FILE)
else: # CAMERA, MOVIE
video_reader.start(VIDEO_INPUT, WIDTH, HEIGHT, save_to_file=SAVE_TO_FILE)
frame_cols, frame_rows = video_reader.getSize()
""" STATISTICS FONT """
fontScale = frame_rows/1000.0
if fontScale < 0.4:
fontScale = 0.4
fontThickness = 1 + int(fontScale)
fontFace = cv2.FONT_HERSHEY_SIMPLEX
if SRC_FROM == MOVIE:
dir_path, filename = os.path.split(VIDEO_INPUT)
filepath_prefix = filename
elif SRC_FROM == CAMERA:
filepath_prefix = 'frame'
""" """
""" """ """ """ """ """ """ """ """ """ """
DETECTION LOOP
""" """ """ """ """ """ """ """ """ """ """
print('Starting Detection')
sleep_interval = 0.005
top_in_time = None
frame_in_processing_counter = 0
try:
if not video_reader.running:
raise IOError(("Input src error."))
while MPVariable.running.value:
if top_in_time is None:
top_in_time = time.time()
"""
SPRIT/NON-SPLIT MODEL CAMERA TO WORKER
"""
if video_reader.running:
if gpu_worker.is_sess_empty(): # must need for speed
cap_in_time = time.time()
if SRC_FROM == IMAGE:
frame, filepath = video_reader.read()
if frame is not None:
frame_in_processing_counter += 1
else:
frame = video_reader.read()
if frame is not None:
filepath = filepath_prefix+'_'+str(proc_frame_counter)+'.png'
frame_in_processing_counter += 1
if frame is not None:
image_expanded = np.expand_dims(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), axis=0) # np.expand_dims is faster than []
#image_expanded = np.expand_dims(frame, axis=0) # BGR image for input. Of couse, bad accuracy in RGB trained model, but speed up.
cap_out_time = time.time()
# put new queue
gpu_feeds = {image_tensor: image_expanded}
gpu_extras = {'image':frame, 'top_in_time':top_in_time, 'cap_in_time':cap_in_time, 'cap_out_time':cap_out_time, 'filepath': filepath} # always image draw.
gpu_worker.put_sess_queue(gpu_opts, gpu_feeds, gpu_extras)
elif frame_in_processing_counter <= 0:
MPVariable.running.value = False
break
g = gpu_worker.get_result_queue()
if SPLIT_MODEL:
# if g is None: gpu thread has no output queue. ok skip, let's check cpu thread.
if g is not None:
# gpu thread has output queue.
result_slice_out, extras = g['results'], g['extras']
if cpu_worker.is_sess_empty():
# When cpu thread has no next queue, put new queue.
# else, drop gpu queue.
cpu_feeds = {}
for i in range(len(result_slice_out)):
cpu_feeds.update({split_in[i]:result_slice_out[i]})
cpu_extras = extras
cpu_worker.put_sess_queue(cpu_opts, cpu_feeds, cpu_extras)
else:
# else: cpu thread is busy. don't put new queue. let's check cpu result queue.
frame_in_processing_counter -= 1
# check cpu thread.
q = cpu_worker.get_result_queue()
else:
"""
NON-SPLIT MODEL
"""
q = g
if q is None:
"""
SPLIT/NON-SPLIT MODEL
"""
# detection is not complete yet. ok nothing to do.
time.sleep(sleep_interval)
continue
frame_in_processing_counter -= 1
boxes, scores, classes, num, extras = q['results'][0], q['results'][1], q['results'][2], q['results'][3], q['extras']
boxes, scores, classes = np.squeeze(boxes), np.squeeze(scores), np.squeeze(classes)
det_out_time = time.time()
"""
ALWAYS BOX DRAW ON IMAGE
"""
vis_in_time = time.time()
image = extras['image']
if SRC_FROM == IMAGE:
filepath = extras['filepath']
frame_rows, frame_cols = image.shape[:2]
""" STATISTICS FONT """
fontScale = frame_rows/1000.0
if fontScale < 0.4:
fontScale = 0.4
fontThickness = 1 + int(fontScale)
else:
filepath = extras['filepath']
image = visualization(category_index, image, boxes, scores, classes, DEBUG_MODE, VIS_TEXT, FPS_INTERVAL,
fontFace=fontFace, fontScale=fontScale, fontThickness=fontThickness)
"""
VISUALIZATION
"""
if VISUALIZE:
if (MPVariable.vis_skip_rate.value == 0) or (proc_frame_counter % MPVariable.vis_skip_rate.value < 1):
if VIS_WORKER:
q_out.put({'image':image, 'vis_in_time':vis_in_time})
else:
"""
SHOW
"""
cv2.imshow("Object Detection", image)
# Press q to quit
if cv2.waitKey(1) & 0xFF == 113: #ord('q'):
break
MPVariable.vis_frame_counter.value += 1
vis_out_time = time.time()
"""
PROCESSING TIME
"""
vis_proc_time = vis_out_time - vis_in_time
MPVariable.vis_proc_time.value += vis_proc_time
else:
"""
NO VISUALIZE
"""
for box, score, _class in zip(boxes, scores, classes):
if proc_frame_counter % DET_INTERVAL == 0 and score > DET_TH:
label = category_index[_class]['name']
print("label: {}\nscore: {}\nbox: {}".format(label, score, box))
vis_out_time = time.time()
"""
PROCESSING TIME
"""
vis_proc_time = vis_out_time - vis_in_time
if SAVE_TO_FILE:
if SRC_FROM == IMAGE:
video_reader.save(image, filepath)
else:
video_reader.save(image)
proc_frame_counter += 1
if proc_frame_counter > 100000:
proc_frame_counter = 0
"""
PROCESSING TIME
"""
top_in_time = extras['top_in_time']
cap_proc_time = extras['cap_out_time'] - extras['cap_in_time']
gpu_proc_time = extras[gpu_tag+'_out_time'] - extras[gpu_tag+'_in_time']
if SPLIT_MODEL:
cpu_proc_time = extras[cpu_tag+'_out_time'] - extras[cpu_tag+'_in_time']
else:
cpu_proc_time = 0
lost_proc_time = det_out_time - top_in_time - cap_proc_time - gpu_proc_time - cpu_proc_time
total_proc_time = det_out_time - top_in_time
MPVariable.cap_proc_time.value += cap_proc_time
MPVariable.gpu_proc_time.value += gpu_proc_time
MPVariable.cpu_proc_time.value += cpu_proc_time
MPVariable.lost_proc_time.value += lost_proc_time
MPVariable.total_proc_time.value += total_proc_time
if DEBUG_MODE:
if SPLIT_MODEL:
sys.stdout.write('snapshot FPS:{: ^5.1f} total:{: ^10.5f} cap:{: ^10.5f} gpu:{: ^10.5f} cpu:{: ^10.5f} lost:{: ^10.5f} | vis:{: ^10.5f}\n'.format(
MPVariable.fps.value, total_proc_time, cap_proc_time, gpu_proc_time, cpu_proc_time, lost_proc_time, vis_proc_time))
else:
sys.stdout.write('snapshot FPS:{: ^5.1f} total:{: ^10.5f} cap:{: ^10.5f} gpu:{: ^10.5f} lost:{: ^10.5f} | vis:{: ^10.5f}\n'.format(
MPVariable.fps.value, total_proc_time, cap_proc_time, gpu_proc_time, lost_proc_time, vis_proc_time))
"""
EXIT WITHOUT GUI
"""
if not VISUALIZE and MAX_FRAMES > 0:
if proc_frame_counter >= MAX_FRAMES:
MPVariable.running.value = False
break
"""
CHANGE SLEEP INTERVAL
"""
if MPVariable.frame_counter.value == 0 and MPVariable.fps.value > 0:
sleep_interval = 0.1 / MPVariable.fps.value
MPVariable.sleep_interval.value = sleep_interval
MPVariable.frame_counter.value += 1
top_in_time = None
"""
END while
"""
except KeyboardInterrupt:
pass
except:
import traceback
traceback.print_exc()
finally:
""" """ """ """ """ """ """ """ """ """ """
CLOSE
""" """ """ """ """ """ """ """ """ """ """
if VISUALIZE and VIS_WORKER:
q_out.put(None)
MPVariable.running.value = False
gpu_worker.stop()
if SPLIT_MODEL:
cpu_worker.stop()
video_reader.stop()
if VISUALIZE:
cv2.destroyAllWindows()
""" """
return