2
2
// SPDX-License-Identifier: Apache-2.0
3
3
//
4
4
5
+ #include < stddef.h>
6
+
7
+ #include < algorithm>
5
8
#include < chrono>
9
+ #include < exception>
6
10
#include < fstream>
7
11
#include < iostream>
8
12
#include < limits>
13
+ #include < map>
14
+ #include < memory>
15
+ #include < ratio>
16
+ #include < stdexcept>
9
17
#include < string>
18
+ #include < utility>
10
19
#include < vector>
11
20
12
21
#include < gflags/gflags.h>
13
- #include < opencv2/opencv.hpp>
22
+ #include < opencv2/core.hpp>
23
+ #include < opencv2/highgui.hpp>
24
+ #include < opencv2/imgcodecs.hpp>
14
25
#include < openvino/openvino.hpp>
15
26
16
27
#include < models/classification_model.h>
28
+ #include < models/input_data.h>
29
+ #include < models/model_base.h>
17
30
#include < models/results.h>
31
+ #include < monitors/presenter.h>
18
32
#include < pipelines/async_pipeline.h>
19
33
#include < pipelines/metadata.h>
20
-
21
34
#include < utils/args_helper.hpp>
22
35
#include < utils/common.hpp>
23
- #include < utils/ocv_common.hpp >
36
+ #include < utils/config_factory.h >
24
37
#include < utils/performance_metrics.hpp>
25
38
#include < utils/slog.hpp>
26
39
@@ -88,7 +101,7 @@ static void showUsage() {
88
101
std::cout << " -u " << utilization_monitors_message << std::endl;
89
102
}
90
103
91
- bool ParseAndCheckCommandLine (int argc, char * argv[]) {
104
+ bool ParseAndCheckCommandLine (int argc, char * argv[]) {
92
105
// ---------------------------Parsing and validation of input args--------------------------------------
93
106
gflags::ParseCommandLineNonHelpFlags (&argc, &argv, true );
94
107
if (FLAGS_h) {
@@ -119,7 +132,7 @@ cv::Mat centerSquareCrop(const cv::Mat& image) {
119
132
return image (cv::Rect (0 , (image.rows - image.cols ) / 2 , image.cols , image.cols ));
120
133
}
121
134
122
- int main (int argc, char * argv[]) {
135
+ int main (int argc, char * argv[]) {
123
136
try {
124
137
PerformanceMetrics metrics, readerMetrics, renderMetrics;
125
138
@@ -132,7 +145,8 @@ int main(int argc, char *argv[]) {
132
145
std::vector<std::string> imageNames;
133
146
std::vector<cv::Mat> inputImages;
134
147
parseInputFilesArguments (imageNames);
135
- if (imageNames.empty ()) throw std::runtime_error (" No images provided" );
148
+ if (imageNames.empty ())
149
+ throw std::runtime_error (" No images provided" );
136
150
std::sort (imageNames.begin (), imageNames.end ());
137
151
for (size_t i = 0 ; i < imageNames.size (); i++) {
138
152
const std::string& name = imageNames[i];
@@ -194,18 +208,20 @@ int main(int argc, char *argv[]) {
194
208
195
209
// ------------------------------ Running routines ----------------------------------------------
196
210
std::vector<std::string> labels = ClassificationModel::loadLabels (FLAGS_labels);
197
- for (const auto & classIndex : classIndices) {
211
+ for (const auto & classIndex : classIndices) {
198
212
if (classIndex >= labels.size ()) {
199
- throw std::runtime_error (" Class index " + std::to_string (classIndex)
200
- + " is outside the range supported by the model." );
201
- }
213
+ throw std::runtime_error (" Class index " + std::to_string (classIndex) +
214
+ " is outside the range supported by the model." );
215
+ }
202
216
}
203
217
204
218
slog::info << ov::get_openvino_version () << slog::endl;
205
219
ov::Core core;
206
220
207
- AsyncPipeline pipeline (std::unique_ptr<ModelBase>(new ClassificationModel (FLAGS_m, FLAGS_nt, FLAGS_auto_resize, labels, FLAGS_layout)),
208
- ConfigFactory::getUserConfig (FLAGS_d, FLAGS_nireq, FLAGS_nstreams, FLAGS_nthreads), core);
221
+ AsyncPipeline pipeline (std::unique_ptr<ModelBase>(
222
+ new ClassificationModel (FLAGS_m, FLAGS_nt, FLAGS_auto_resize, labels, FLAGS_layout)),
223
+ ConfigFactory::getUserConfig (FLAGS_d, FLAGS_nireq, FLAGS_nstreams, FLAGS_nthreads),
224
+ core);
209
225
210
226
Presenter presenter (FLAGS_u, 0 );
211
227
int width;
@@ -238,9 +254,11 @@ int main(int argc, char *argv[]) {
238
254
if (isTestMode && elapsedSeconds >= testDuration) {
239
255
isTestMode = false ;
240
256
typedef std::chrono::duration<double , std::chrono::seconds::period> Sec;
241
- gridMat = GridMat (presenter, cv::Size (width, height), cv::Size (16 , 9 ),
242
- (framesNum - framesNumOnCalculationStart) / std::chrono::duration_cast<Sec>(
243
- fpsCalculationDuration).count ());
257
+ gridMat = GridMat (presenter,
258
+ cv::Size (width, height),
259
+ cv::Size (16 , 9 ),
260
+ (framesNum - framesNumOnCalculationStart) /
261
+ std::chrono::duration_cast<Sec>(fpsCalculationDuration).count ());
244
262
metrics = PerformanceMetrics ();
245
263
startTime = std::chrono::steady_clock::now ();
246
264
framesNum = 0 ;
@@ -252,14 +270,17 @@ int main(int argc, char *argv[]) {
252
270
auto imageStartTime = std::chrono::steady_clock::now ();
253
271
254
272
pipeline.submitData (ImageInputData (inputImages[nextImageIndex]),
255
- std::make_shared<ClassificationImageMetaData>(inputImages[nextImageIndex], imageStartTime, classIndices[nextImageIndex]));
273
+ std::make_shared<ClassificationImageMetaData>(inputImages[nextImageIndex],
274
+ imageStartTime,
275
+ classIndices[nextImageIndex]));
256
276
nextImageIndex++;
257
277
if (nextImageIndex == imageNames.size ()) {
258
278
nextImageIndex = 0 ;
259
279
}
260
280
}
261
281
262
- // --- Waiting for free input slot or output data available. Function will return immediately if any of them are available.
282
+ // --- Waiting for free input slot or output data available. Function will return immediately if any of them
283
+ // are available.
263
284
pipeline.waitForData (false );
264
285
265
286
// --- Checking for results and rendering data if it's ready
@@ -269,8 +290,8 @@ int main(int argc, char *argv[]) {
269
290
if (!classificationResult.metaData ) {
270
291
throw std::invalid_argument (" Renderer: metadata is null" );
271
292
}
272
- const ClassificationImageMetaData& classificationImageMetaData
273
- = classificationResult.metaData ->asRef <const ClassificationImageMetaData>();
293
+ const ClassificationImageMetaData& classificationImageMetaData =
294
+ classificationResult.metaData ->asRef <const ClassificationImageMetaData>();
274
295
275
296
auto outputImg = classificationImageMetaData.img ;
276
297
@@ -295,8 +316,13 @@ int main(int argc, char *argv[]) {
295
316
framesNum++;
296
317
gridMat.updateMat (outputImg, label, predictionResult);
297
318
accuracy = static_cast <double >(correctPredictionsCount) / framesNum;
298
- gridMat.textUpdate (metrics, classificationResult.metaData ->asRef <ImageMetaData>().timeStamp , accuracy, FLAGS_nt, isTestMode,
299
- !FLAGS_gt.empty (), presenter);
319
+ gridMat.textUpdate (metrics,
320
+ classificationResult.metaData ->asRef <ImageMetaData>().timeStamp ,
321
+ accuracy,
322
+ FLAGS_nt,
323
+ isTestMode,
324
+ !FLAGS_gt.empty (),
325
+ presenter);
300
326
renderMetrics.update (renderingStart);
301
327
elapsedSeconds = std::chrono::steady_clock::now () - startTime;
302
328
if (!FLAGS_no_show) {
@@ -305,17 +331,16 @@ int main(int argc, char *argv[]) {
305
331
int key = cv::waitKey (1 );
306
332
if (27 == key || ' q' == key || ' Q' == key) { // Esc
307
333
keepRunning = false ;
308
- }
309
- else if ( 32 == key || ' r ' == key || ' R' == key) { // press space or r to restart testing if needed
334
+ } else if ( 32 == key || ' r ' == key ||
335
+ ' R' == key) { // press space or r to restart testing if needed
310
336
isTestMode = true ;
311
337
framesNum = 0 ;
312
338
framesNumOnCalculationStart = 0 ;
313
339
correctPredictionsCount = 0 ;
314
340
accuracy = 0 ;
315
341
elapsedSeconds = std::chrono::steady_clock::duration (0 );
316
342
startTime = std::chrono::steady_clock::now ();
317
- }
318
- else {
343
+ } else {
319
344
presenter.handleKey (key);
320
345
}
321
346
}
@@ -328,16 +353,16 @@ int main(int argc, char *argv[]) {
328
353
329
354
slog::info << " Metrics report:" << slog::endl;
330
355
metrics.logTotal ();
331
- logLatencyPerStage (readerMetrics.getTotal ().latency , pipeline.getPreprocessMetrics ().getTotal ().latency ,
332
- pipeline.getInferenceMetircs ().getTotal ().latency , pipeline.getPostprocessMetrics ().getTotal ().latency ,
333
- renderMetrics.getTotal ().latency );
356
+ logLatencyPerStage (readerMetrics.getTotal ().latency ,
357
+ pipeline.getPreprocessMetrics ().getTotal ().latency ,
358
+ pipeline.getInferenceMetircs ().getTotal ().latency ,
359
+ pipeline.getPostprocessMetrics ().getTotal ().latency ,
360
+ renderMetrics.getTotal ().latency );
334
361
slog::info << presenter.reportMeans () << slog::endl;
335
- }
336
- catch (const std::exception& error) {
362
+ } catch (const std::exception& error) {
337
363
slog::err << error.what () << slog::endl;
338
364
return 1 ;
339
- }
340
- catch (...) {
365
+ } catch (...) {
341
366
slog::err << " Unknown/internal exception happened." << slog::endl;
342
367
return 1 ;
343
368
}
0 commit comments