Skip to content

Commit

Permalink
Merge pull request #3435 from ivikhrev/fix-code-style-model-api-demos
Browse files Browse the repository at this point in the history
Fix code style for cpp model api demos
  • Loading branch information
Wovchena authored Apr 1, 2022
2 parents 06b1d1d + fce7ed3 commit 3109f65
Show file tree
Hide file tree
Showing 69 changed files with 2,795 additions and 1,970 deletions.
78 changes: 46 additions & 32 deletions demos/classification_benchmark_demo/cpp/grid_mat.hpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// Copyright (C) 2018-2019 Intel Corporation
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <algorithm>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <queue>

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>

#include <monitors/presenter.h>
#include <utils/ocv_common.hpp>

#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>

enum class PredictionResult { Correct,
Incorrect,
Unknown };
enum class PredictionResult { Correct, Incorrect, Unknown };

class GridMat {
public:
Expand All @@ -27,9 +25,8 @@ class GridMat {
explicit GridMat(Presenter& presenter,
const cv::Size maxDisp = cv::Size{1920, 1080},
const cv::Size aspectRatio = cv::Size{16, 9},
double targetFPS = 60
):
currSourceId{0} {
double targetFPS = 60)
: currSourceId{0} {
cv::Size size(static_cast<int>(std::round(sqrt(1. * targetFPS * aspectRatio.width / aspectRatio.height))),
static_cast<int>(std::round(sqrt(1. * targetFPS * aspectRatio.height / aspectRatio.width))));
if (size.width == 0 || size.height == 0) {
Expand All @@ -45,12 +42,13 @@ class GridMat {
}

outImg.create((cellSize.height * size.height) + presenter.graphSize.height,
cellSize.width * size.width, CV_8UC3);
cellSize.width * size.width,
CV_8UC3);
outImg.setTo(0);

textSize = cv::getTextSize("", fontType, fontScale, thickness, &baseline);
accuracyMessageSize = cv::getTextSize("Accuracy (top 0): 0.000", fontType, fontScale, thickness, &baseline);
testMessageSize = cv::getTextSize(testMessage, fontType, fontScale, thickness, &baseline);
testMessageSize = cv::getTextSize(GridMat::testMessage, fontType, fontScale, thickness, &baseline);
}

void textUpdate(PerformanceMetrics& metrics,
Expand All @@ -60,28 +58,37 @@ class GridMat {
bool isFpsTest,
bool showAccuracy,
Presenter& presenter) {
rectangle(outImg,
{0, 0}, {outImg.cols, presenter.graphSize.height},
cv::Scalar(0, 0, 0), cv::FILLED);
rectangle(outImg, {0, 0}, {outImg.cols, presenter.graphSize.height}, cv::Scalar(0, 0, 0), cv::FILLED);

presenter.drawGraphs(outImg);

metrics.update(lastRequestStartTime, outImg, cv::Point(textPadding, textSize.height + textPadding),
fontType, fontScale, cv::Scalar(255, 100, 100), thickness);
metrics.update(lastRequestStartTime,
outImg,
cv::Point(textPadding, textSize.height + textPadding),
fontType,
fontScale,
cv::Scalar(255, 100, 100),
thickness);

if (showAccuracy) {
cv::putText(outImg,
cv::format("Accuracy (top %d): %.3f", nTop, accuracy),
cv::Point(outImg.cols - accuracyMessageSize.width - textPadding, textSize.height + textPadding),
fontType, fontScale, cv::Scalar(255, 255, 255), thickness);
fontType,
fontScale,
cv::Scalar(255, 255, 255),
thickness);
}

if (isFpsTest) {
cv::putText(outImg,
testMessage,
cv::Point(outImg.cols - testMessageSize.width - textPadding,
(textSize.height + textPadding) * 2),
fontType, fontScale, cv::Scalar(50, 50, 255), thickness);
cv::putText(
outImg,
GridMat::testMessage,
cv::Point(outImg.cols - testMessageSize.width - textPadding, (textSize.height + textPadding) * 2),
fontType,
fontScale,
cv::Scalar(50, 50, 255),
thickness);
}
}

Expand All @@ -94,21 +101,28 @@ class GridMat {
cv::Scalar textColor;
switch (predictionResul) {
case PredictionResult::Correct:
textColor = cv::Scalar(75, 255, 75); break; // green
textColor = cv::Scalar(75, 255, 75); // green
break;
case PredictionResult::Incorrect:
textColor = cv::Scalar(50, 50, 255); break; // red
textColor = cv::Scalar(50, 50, 255); // red
break;
case PredictionResult::Unknown:
textColor = cv::Scalar(200, 10, 10); break; // blue
textColor = cv::Scalar(200, 10, 10); // blue
break;
default:
throw std::runtime_error("Undefined type of prediction result");
}
int labelThickness = cellSize.width / 20;
cv::Size labelTextSize = cv::getTextSize(label, fontType, 1, 2, &baseline);
double labelFontScale = static_cast<double>(cellSize.width - 2 * labelThickness) / labelTextSize.width;
cv::resize(mat, prevImg, cellSize);
putHighlightedText(prevImg, label,
cv::Point(labelThickness, cellSize.height - labelThickness - labelTextSize.height),
fontType, labelFontScale, textColor, 2);
putHighlightedText(prevImg,
label,
cv::Point(labelThickness, cellSize.height - labelThickness - labelTextSize.height),
fontType,
labelFontScale,
textColor,
2);
cv::Mat cell = outImg(cv::Rect(points[currSourceId], cellSize));
prevImg.copyTo(cell);
cv::rectangle(cell, {0, 0}, {cell.cols, cell.rows}, {255, 50, 50}, labelThickness); // draw a border
Expand All @@ -129,11 +143,11 @@ class GridMat {
static constexpr double fontScale = 1.5;
static const int thickness = 2;
static const int textPadding = 10;
static const std::string testMessage;
static constexpr const char testMessage[] = "Testing, please wait...";
int baseline;
cv::Size textSize;
cv::Size accuracyMessageSize;
cv::Size testMessageSize;
};

const std::string GridMat::testMessage = "Testing, please wait...";
constexpr const char GridMat::testMessage[];
89 changes: 57 additions & 32 deletions demos/classification_benchmark_demo/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@
// SPDX-License-Identifier: Apache-2.0
//

#include <stddef.h>

#include <algorithm>
#include <chrono>
#include <exception>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <ratio>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>

#include <gflags/gflags.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <openvino/openvino.hpp>

#include <models/classification_model.h>
#include <models/input_data.h>
#include <models/model_base.h>
#include <models/results.h>
#include <monitors/presenter.h>
#include <pipelines/async_pipeline.h>
#include <pipelines/metadata.h>

#include <utils/args_helper.hpp>
#include <utils/common.hpp>
#include <utils/ocv_common.hpp>
#include <utils/config_factory.h>
#include <utils/performance_metrics.hpp>
#include <utils/slog.hpp>

Expand Down Expand Up @@ -88,7 +101,7 @@ static void showUsage() {
std::cout << " -u " << utilization_monitors_message << std::endl;
}

bool ParseAndCheckCommandLine(int argc, char *argv[]) {
bool ParseAndCheckCommandLine(int argc, char* argv[]) {
// ---------------------------Parsing and validation of input args--------------------------------------
gflags::ParseCommandLineNonHelpFlags(&argc, &argv, true);
if (FLAGS_h) {
Expand Down Expand Up @@ -119,7 +132,7 @@ cv::Mat centerSquareCrop(const cv::Mat& image) {
return image(cv::Rect(0, (image.rows - image.cols) / 2, image.cols, image.cols));
}

int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
try {
PerformanceMetrics metrics, readerMetrics, renderMetrics;

Expand All @@ -132,7 +145,8 @@ int main(int argc, char *argv[]) {
std::vector<std::string> imageNames;
std::vector<cv::Mat> inputImages;
parseInputFilesArguments(imageNames);
if (imageNames.empty()) throw std::runtime_error("No images provided");
if (imageNames.empty())
throw std::runtime_error("No images provided");
std::sort(imageNames.begin(), imageNames.end());
for (size_t i = 0; i < imageNames.size(); i++) {
const std::string& name = imageNames[i];
Expand Down Expand Up @@ -194,18 +208,20 @@ int main(int argc, char *argv[]) {

//------------------------------ Running routines ----------------------------------------------
std::vector<std::string> labels = ClassificationModel::loadLabels(FLAGS_labels);
for (const auto & classIndex : classIndices) {
for (const auto& classIndex : classIndices) {
if (classIndex >= labels.size()) {
throw std::runtime_error("Class index " + std::to_string(classIndex)
+ " is outside the range supported by the model.");
}
throw std::runtime_error("Class index " + std::to_string(classIndex) +
" is outside the range supported by the model.");
}
}

slog::info << ov::get_openvino_version() << slog::endl;
ov::Core core;

AsyncPipeline pipeline(std::unique_ptr<ModelBase>(new ClassificationModel(FLAGS_m, FLAGS_nt, FLAGS_auto_resize, labels, FLAGS_layout)),
ConfigFactory::getUserConfig(FLAGS_d, FLAGS_nireq, FLAGS_nstreams, FLAGS_nthreads), core);
AsyncPipeline pipeline(std::unique_ptr<ModelBase>(
new ClassificationModel(FLAGS_m, FLAGS_nt, FLAGS_auto_resize, labels, FLAGS_layout)),
ConfigFactory::getUserConfig(FLAGS_d, FLAGS_nireq, FLAGS_nstreams, FLAGS_nthreads),
core);

Presenter presenter(FLAGS_u, 0);
int width;
Expand Down Expand Up @@ -238,9 +254,11 @@ int main(int argc, char *argv[]) {
if (isTestMode && elapsedSeconds >= testDuration) {
isTestMode = false;
typedef std::chrono::duration<double, std::chrono::seconds::period> Sec;
gridMat = GridMat(presenter, cv::Size(width, height), cv::Size(16, 9),
(framesNum - framesNumOnCalculationStart) / std::chrono::duration_cast<Sec>(
fpsCalculationDuration).count());
gridMat = GridMat(presenter,
cv::Size(width, height),
cv::Size(16, 9),
(framesNum - framesNumOnCalculationStart) /
std::chrono::duration_cast<Sec>(fpsCalculationDuration).count());
metrics = PerformanceMetrics();
startTime = std::chrono::steady_clock::now();
framesNum = 0;
Expand All @@ -252,14 +270,17 @@ int main(int argc, char *argv[]) {
auto imageStartTime = std::chrono::steady_clock::now();

pipeline.submitData(ImageInputData(inputImages[nextImageIndex]),
std::make_shared<ClassificationImageMetaData>(inputImages[nextImageIndex], imageStartTime, classIndices[nextImageIndex]));
std::make_shared<ClassificationImageMetaData>(inputImages[nextImageIndex],
imageStartTime,
classIndices[nextImageIndex]));
nextImageIndex++;
if (nextImageIndex == imageNames.size()) {
nextImageIndex = 0;
}
}

//--- Waiting for free input slot or output data available. Function will return immediately if any of them are available.
//--- Waiting for free input slot or output data available. Function will return immediately if any of them
// are available.
pipeline.waitForData(false);

//--- Checking for results and rendering data if it's ready
Expand All @@ -269,8 +290,8 @@ int main(int argc, char *argv[]) {
if (!classificationResult.metaData) {
throw std::invalid_argument("Renderer: metadata is null");
}
const ClassificationImageMetaData& classificationImageMetaData
= classificationResult.metaData->asRef<const ClassificationImageMetaData>();
const ClassificationImageMetaData& classificationImageMetaData =
classificationResult.metaData->asRef<const ClassificationImageMetaData>();

auto outputImg = classificationImageMetaData.img;

Expand All @@ -295,8 +316,13 @@ int main(int argc, char *argv[]) {
framesNum++;
gridMat.updateMat(outputImg, label, predictionResult);
accuracy = static_cast<double>(correctPredictionsCount) / framesNum;
gridMat.textUpdate(metrics, classificationResult.metaData->asRef<ImageMetaData>().timeStamp, accuracy, FLAGS_nt, isTestMode,
!FLAGS_gt.empty(), presenter);
gridMat.textUpdate(metrics,
classificationResult.metaData->asRef<ImageMetaData>().timeStamp,
accuracy,
FLAGS_nt,
isTestMode,
!FLAGS_gt.empty(),
presenter);
renderMetrics.update(renderingStart);
elapsedSeconds = std::chrono::steady_clock::now() - startTime;
if (!FLAGS_no_show) {
Expand All @@ -305,17 +331,16 @@ int main(int argc, char *argv[]) {
int key = cv::waitKey(1);
if (27 == key || 'q' == key || 'Q' == key) { // Esc
keepRunning = false;
}
else if (32 == key || 'r' == key || 'R' == key) { // press space or r to restart testing if needed
} else if (32 == key || 'r' == key ||
'R' == key) { // press space or r to restart testing if needed
isTestMode = true;
framesNum = 0;
framesNumOnCalculationStart = 0;
correctPredictionsCount = 0;
accuracy = 0;
elapsedSeconds = std::chrono::steady_clock::duration(0);
startTime = std::chrono::steady_clock::now();
}
else {
} else {
presenter.handleKey(key);
}
}
Expand All @@ -328,16 +353,16 @@ int main(int argc, char *argv[]) {

slog::info << "Metrics report:" << slog::endl;
metrics.logTotal();
logLatencyPerStage(readerMetrics.getTotal().latency, pipeline.getPreprocessMetrics().getTotal().latency,
pipeline.getInferenceMetircs().getTotal().latency, pipeline.getPostprocessMetrics().getTotal().latency,
renderMetrics.getTotal().latency);
logLatencyPerStage(readerMetrics.getTotal().latency,
pipeline.getPreprocessMetrics().getTotal().latency,
pipeline.getInferenceMetircs().getTotal().latency,
pipeline.getPostprocessMetrics().getTotal().latency,
renderMetrics.getTotal().latency);
slog::info << presenter.reportMeans() << slog::endl;
}
catch (const std::exception& error) {
} catch (const std::exception& error) {
slog::err << error.what() << slog::endl;
return 1;
}
catch (...) {
} catch (...) {
slog::err << "Unknown/internal exception happened." << slog::endl;
return 1;
}
Expand Down
Loading

0 comments on commit 3109f65

Please sign in to comment.