forked from openvinotoolkit/open_model_zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmentation_demo.py
executable file
·267 lines (218 loc) · 10.4 KB
/
segmentation_demo.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
#!/usr/bin/env python3
"""
Copyright (C) 2018-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import logging
import sys
from argparse import ArgumentParser, SUPPRESS
from pathlib import Path
from time import perf_counter
import cv2
import numpy as np
from openvino.inference_engine import IECore
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python'))
from models import SegmentationModel
import monitors
from pipelines import AsyncPipeline
from images_capture import open_images_capture
from performance_metrics import PerformanceMetrics
logging.basicConfig(format='[ %(levelname)s ] %(message)s', level=logging.INFO, stream=sys.stdout)
log = logging.getLogger()
class Visualizer(object):
pascal_voc_palette = [
(0, 0, 0),
(128, 0, 0),
(0, 128, 0),
(128, 128, 0),
(0, 0, 128),
(128, 0, 128),
(0, 128, 128),
(128, 128, 128),
(64, 0, 0),
(192, 0, 0),
(64, 128, 0),
(192, 128, 0),
(64, 0, 128),
(192, 0, 128),
(64, 128, 128),
(192, 128, 128),
(0, 64, 0),
(128, 64, 0),
(0, 192, 0),
(128, 192, 0),
(0, 64, 128)
]
def __init__(self, colors_path=None):
if colors_path:
self.color_palette = self.get_palette_from_file(colors_path)
else:
self.color_palette = self.pascal_voc_palette
self.color_map = self.create_color_map()
def get_palette_from_file(self, colors_path):
with open(colors_path, 'r') as file:
colors = []
for line in file.readlines():
values = line[line.index('(')+1:line.index(')')].split(',')
colors.append([int(v.strip()) for v in values])
return colors
def create_color_map(self):
classes = np.array(self.color_palette, dtype=np.uint8)[:, ::-1] # RGB to BGR
color_map = np.zeros((256, 1, 3), dtype=np.uint8)
classes_num = len(classes)
color_map[:classes_num, 0, :] = classes
color_map[classes_num:, 0, :] = np.random.uniform(0, 255, size=(256-classes_num, 3))
return color_map
def apply_color_map(self, input):
input_3d = cv2.merge([input, input, input])
return cv2.LUT(input_3d, self.color_map)
def overlay_masks(self, frame, objects):
# Visualizing result data over source image
return np.floor_divide(frame, 2) + np.floor_divide(self.apply_color_map(objects), 2)
def build_argparser():
parser = ArgumentParser(add_help=False)
args = parser.add_argument_group('Options')
args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
args.add_argument('-m', '--model', help='Required. Path to an .xml file with a trained model.',
required=True, type=Path)
args.add_argument('-i', '--input', required=True,
help='Required. An input to process. The input must be a single image, '
'a folder of images, video file or camera id.')
args.add_argument('-d', '--device', default='CPU', type=str,
help='Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is '
'acceptable. The demo will look for a suitable plugin for device specified. '
'Default value is CPU.')
common_model_args = parser.add_argument_group('Common model options')
common_model_args.add_argument('-c', '--colors', type=Path,
help='Optional. Path to a text file containing colors for classes.')
infer_args = parser.add_argument_group('Inference options')
infer_args.add_argument('-nireq', '--num_infer_requests', help='Optional. Number of infer requests.',
default=1, type=int)
infer_args.add_argument('-nstreams', '--num_streams',
help='Optional. Number of streams to use for inference on the CPU or/and GPU in throughput '
'mode (for HETERO and MULTI device cases use format '
'<device1>:<nstreams1>,<device2>:<nstreams2> or just <nstreams>).',
default='', type=str)
infer_args.add_argument('-nthreads', '--num_threads', default=None, type=int,
help='Optional. Number of threads to use for inference on CPU (including HETERO cases).')
io_args = parser.add_argument_group('Input/output options')
io_args.add_argument('--loop', default=False, action='store_true',
help='Optional. Enable reading the input in a loop.')
io_args.add_argument('-o', '--output', required=False,
help='Optional. Name of output to save.')
io_args.add_argument('-limit', '--output_limit', required=False, default=1000, type=int,
help='Optional. Number of frames to store in output. '
'If 0 is set, all frames are stored.')
io_args.add_argument('--no_show', help="Optional. Don't show output.", action='store_true')
io_args.add_argument('-u', '--utilization_monitors', default='', type=str,
help='Optional. List of monitors to show initially.')
return parser
def get_plugin_configs(device, num_streams, num_threads):
config_user_specified = {}
devices_nstreams = {}
if num_streams:
devices_nstreams = {device: num_streams for device in ['CPU', 'GPU'] if device in device} \
if num_streams.isdigit() \
else dict(device.split(':', 1) for device in num_streams.split(','))
if 'CPU' in device:
if num_threads is not None:
config_user_specified['CPU_THREADS_NUM'] = str(num_threads)
if 'CPU' in devices_nstreams:
config_user_specified['CPU_THROUGHPUT_STREAMS'] = devices_nstreams['CPU'] \
if int(devices_nstreams['CPU']) > 0 \
else 'CPU_THROUGHPUT_AUTO'
if 'GPU' in device:
if 'GPU' in devices_nstreams:
config_user_specified['GPU_THROUGHPUT_STREAMS'] = devices_nstreams['GPU'] \
if int(devices_nstreams['GPU']) > 0 \
else 'GPU_THROUGHPUT_AUTO'
return config_user_specified
def main():
metrics = PerformanceMetrics()
args = build_argparser().parse_args()
log.info('Initializing Inference Engine...')
ie = IECore()
plugin_config = get_plugin_configs(args.device, args.num_streams, args.num_threads)
log.info('Loading network...')
model = SegmentationModel(ie, args.model)
pipeline = AsyncPipeline(ie, model, plugin_config, device=args.device, max_num_requests=args.num_infer_requests)
cap = open_images_capture(args.input, args.loop)
next_frame_id = 0
next_frame_id_to_show = 0
log.info('Starting inference...')
print("To close the application, press 'CTRL+C' here or switch to the output window and press ESC key")
visualizer = Visualizer(args.colors)
presenter = None
video_writer = cv2.VideoWriter()
while True:
if pipeline.is_ready():
# Get new image/frame
start_time = perf_counter()
frame = cap.read()
if frame is None:
if next_frame_id == 0:
raise ValueError("Can't read an image from the input")
break
if next_frame_id == 0:
presenter = monitors.Presenter(args.utilization_monitors, 55,
(round(frame.shape[1] / 4), round(frame.shape[0] / 8)))
if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'),
cap.fps(), (frame.shape[1], frame.shape[0])):
raise RuntimeError("Can't open video writer")
# Submit for inference
pipeline.submit_data(frame, next_frame_id, {'frame': frame, 'start_time': start_time})
next_frame_id += 1
else:
# Wait for empty request
pipeline.await_any()
if pipeline.callback_exceptions:
raise pipeline.callback_exceptions[0]
# Process all completed requests
results = pipeline.get_result(next_frame_id_to_show)
if results:
objects, frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
frame = visualizer.overlay_masks(frame, objects)
presenter.drawGraphs(frame)
metrics.update(start_time, frame)
if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
if not args.no_show:
cv2.imshow('Segmentation Results', frame)
key = cv2.waitKey(1)
if key == 27 or key == 'q' or key == 'Q':
break
presenter.handleKey(key)
next_frame_id_to_show += 1
pipeline.await_all()
# Process completed requests
while pipeline.has_completed_request():
results = pipeline.get_result(next_frame_id_to_show)
if results:
objects, frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
frame = visualizer.overlay_masks(frame, objects)
presenter.drawGraphs(frame)
metrics.update(start_time, frame)
if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
if not args.no_show:
cv2.imshow('Segmentation Results', frame)
key = cv2.waitKey(1)
next_frame_id_to_show += 1
else:
break
metrics.print_total()
print(presenter.reportMeans())
if __name__ == '__main__':
sys.exit(main() or 0)