forked from treger-8105/OCSR-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification_demo(OCSR).py
309 lines (265 loc) · 14.3 KB
/
classification_demo(OCSR).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
#!/usr/bin/env python3
"""
Copyright (C) 2018-2021 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.
"""
from pyxll import xl_func, RTD
import logging as log
import sys
from argparse import ArgumentParser, SUPPRESS
from pathlib import Path
from time import perf_counter
import csv
import cv2
import numpy
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python'))
sys.path.append(str(Path(__file__).resolve().parents[2] / 'common/python/openvino/model_zoo'))
from model_api.models import Classification, OutputTransform
from model_api.performance_metrics import put_highlighted_text, PerformanceMetrics
from model_api.pipelines import get_user_config, AsyncPipeline
from model_api.adapters import create_core, OpenvinoAdapter, RemoteAdapter
import monitors
from images_capture import open_images_capture
from helpers import resolution, log_latency_per_stage
log.basicConfig(format='[ %(levelname)s ] %(message)s', level=log.DEBUG, stream=sys.stdout)
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('--adapter', help='Optional. Specify the model adapter. Default is openvino.',
default='openvino', type=str, choices=('openvino', 'remote'))
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, 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('--labels', help='Optional. Labels mapping file.', default=None, type=str)
common_model_args.add_argument('-topk', help='Optional. Number of top results. Default value is 5. Must be from 1 to 10.', default=5,
type=int, choices=range(1, 11))
infer_args = parser.add_argument_group('Inference options')
infer_args.add_argument('-nireq', '--num_infer_requests', help='Optional. Number of infer requests',
default=0, 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 the output file(s) 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('--output_resolution', default=None, type=resolution,
help='Optional. Specify the maximum output window resolution '
'in (width x height) format. Example: 1280x720. '
'Input frame size used by default.')
io_args.add_argument('-u', '--utilization_monitors', default='', type=str,
help='Optional. List of monitors to show initially.')
input_transform_args = parser.add_argument_group('Input transform options')
input_transform_args.add_argument('--reverse_input_channels', default=False, action='store_true',
help='Optional. Switch the input channels order from '
'BGR to RGB.')
input_transform_args.add_argument('--mean_values', default=None, type=float, nargs=3,
help='Optional. Normalize input by subtracting the mean '
'values per channel. Example: 255.0 255.0 255.0')
input_transform_args.add_argument('--scale_values', default=None, type=float, nargs=3,
help='Optional. Divide input by scale values per channel. '
'Division is applied after mean values subtraction. '
'Example: 255.0 255.0 255.0')
debug_args = parser.add_argument_group('Debug options')
debug_args.add_argument('-r', '--raw_output_message', help='Optional. Output inference results raw values showing.',
default=False, action='store_true')
return parser
def draw_labels(frame, classifications, output_transform):
frame = output_transform.resize(frame)
сlass_label = ""
if classifications:
сlass_label = classifications[0][1]
font_scale = 0.7
label_height = cv2.getTextSize(сlass_label, cv2.FONT_HERSHEY_COMPLEX, font_scale, 2)[0][1]
initial_labels_pos = frame.shape[0] - label_height * (int(1.5 * len(classifications)) + 1)
if (initial_labels_pos < 0):
initial_labels_pos = label_height
log.warning('Too much labels to display on this frame, some will be omitted')
offset_y = initial_labels_pos
header = "Label: Score:"
label_width = cv2.getTextSize(header, cv2.FONT_HERSHEY_COMPLEX, font_scale, 2)[0][0]
put_highlighted_text(frame, header, (frame.shape[1] - label_width, offset_y),
cv2.FONT_HERSHEY_COMPLEX, font_scale, (255, 0, 0), 2)
for idx, сlass_label, score in classifications:
label = '{}. {} {:.2f}'.format(idx, сlass_label, score)
label_width = cv2.getTextSize(label, cv2.FONT_HERSHEY_COMPLEX, font_scale, 2)[0][0]
offset_y += int(label_height * 1.5)
put_highlighted_text(frame, label, (frame.shape[1] - label_width, offset_y),
cv2.FONT_HERSHEY_COMPLEX, font_scale, (255, 0, 0), 2)
return frame
def print_raw_results(classifications, frame_id):
label_max_len = 0
if classifications:
label_max_len = len(max([cl[1] for cl in classifications], key=len))
log.debug(' ------------------- Frame # {} ------------------ '.format(frame_id))
if label_max_len != 0:
log.debug(' Class ID | {:^{width}s}| Confidence '.format('Label', width=label_max_len))
else:
log.debug(' Class ID | Confidence ')
for class_id, class_label, score in classifications:
if class_label != "":
log.debug('{:^9} | {:^{width}s}| {:^10f} '.format(class_id, class_label, score, width=label_max_len))
else:
log.debug('{:^9} | {:^10f} '.format(class_id, score))
with open('List.csv','a') as f:
f.write(class_label)
#ac=str(class_label.count('pineapple'))
#f.write(',1')
f.write(','+str(score))
f.write("\n")
with open('List.csv', newline='') as csvfile:
count = []
rows = csv.reader(csvfile)
for row in rows:
count.append(row[0])
cc=numpy.unique(count).tolist()
for c in cc:
ac=count.count(c)
print(c,ac)
def main():
args = build_argparser().parse_args()
cap = open_images_capture(args.input, args.loop)
delay = int(cap.get_type() in {'VIDEO', 'CAMERA'})
if args.adapter == 'openvino':
plugin_config = get_user_config(args.device, args.num_streams, args.num_threads)
model_adapter = OpenvinoAdapter(create_core(), args.model, device=args.device, plugin_config=plugin_config,
max_num_requests=args.num_infer_requests)
elif args.adapter == 'remote':
log.info('Reading model {}'.format(args.model))
serving_config = {"address": "localhost", "port": 9000}
model_adapter = RemoteAdapter(args.model, serving_config)
config = {
'mean_values': args.mean_values,
'scale_values': args.scale_values,
'reverse_input_channels': args.reverse_input_channels,
'topk': args.topk,
'path_to_labels': args.labels
}
model = Classification(model_adapter, config)
model.log_layers_info()
async_pipeline = AsyncPipeline(model)
next_frame_id = 0
next_frame_id_to_show = 0
metrics = PerformanceMetrics()
render_metrics = PerformanceMetrics()
presenter = None
output_transform = None
video_writer = cv2.VideoWriter()
ESC_KEY = 27
key = -1
while True:
if async_pipeline.callback_exceptions:
raise async_pipeline.callback_exceptions[0]
# Process all completed requests
results = async_pipeline.get_result(next_frame_id_to_show)
if results:
classifications, frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
if args.raw_output_message:
print_raw_results(classifications, next_frame_id_to_show)
presenter.drawGraphs(frame)
rendering_start_time = perf_counter()
frame = draw_labels(frame, classifications, output_transform)
if delay or args.no_show:
render_metrics.update(rendering_start_time)
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)
next_frame_id_to_show += 1
if not args.no_show:
cv2.imshow('Classification Results', frame)
key = cv2.waitKey(delay)
# Quit.
if key in {ord('q'), ord('Q'), ESC_KEY}:
break
presenter.handleKey(key)
continue
if async_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:
output_transform = OutputTransform(frame.shape[:2], args.output_resolution)
if args.output_resolution:
output_resolution = output_transform.new_resolution
else:
output_resolution = (frame.shape[1], frame.shape[0])
presenter = monitors.Presenter(args.utilization_monitors, 55,
(round(output_resolution[0] / 4), round(output_resolution[1] / 8)))
if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'),
cap.fps(), output_resolution):
raise RuntimeError("Can't open video writer")
# Submit for inference
async_pipeline.submit_data(frame, next_frame_id, {'frame': frame, 'start_time': start_time})
next_frame_id += 1
else:
# Wait for empty request
async_pipeline.await_any()
async_pipeline.await_all()
if key not in {ord('q'), ord('Q'), ESC_KEY}:
# Process completed requests
for next_frame_id_to_show in range(next_frame_id_to_show, next_frame_id):
results = async_pipeline.get_result(next_frame_id_to_show)
while results is None:
results = async_pipeline.get_result(next_frame_id_to_show)
classifications, frame_meta = results
frame = frame_meta['frame']
start_time = frame_meta['start_time']
if args.raw_output_message:
print_raw_results(classifications, next_frame_id_to_show)
presenter.drawGraphs(frame)
rendering_start_time = perf_counter()
frame = draw_labels(frame, classifications, output_transform)
if delay or args.no_show:
render_metrics.update(rendering_start_time)
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('Classification Results', frame)
key = cv2.waitKey(delay)
# Quit.
if key in {ord('q'), ord('Q'), ESC_KEY}:
break
presenter.handleKey(key)
if delay or args.no_show:
metrics.log_total()
log_latency_per_stage(cap.reader_metrics.get_latency(),
async_pipeline.preprocess_metrics.get_latency(),
async_pipeline.inference_metrics.get_latency(),
async_pipeline.postprocess_metrics.get_latency(),
render_metrics.get_latency())
for rep in presenter.reportMeans():
log.info(rep)
if __name__ == '__main__':
sys.exit(main() or 0)