13 #include <opencv2/core.hpp>
14 #include <opencv2/imgproc.hpp>
16 #include <monitors/presenter.h>
21 enum class PredictionResult { Correct, Incorrect, Unknown };
28 const cv::Size maxDisp = cv::Size{1920, 1080},
29 const cv::Size aspectRatio = cv::Size{16, 9},
30 double targetFPS = 60)
32 cv::Size size(
static_cast<int>(std::round(sqrt(1. * targetFPS * aspectRatio.width / aspectRatio.height))),
33 static_cast<int>(std::round(sqrt(1. * targetFPS * aspectRatio.height / aspectRatio.width))));
34 if (size.width == 0 || size.height == 0) {
37 int minCellSize = std::min(maxDisp.width / size.width, maxDisp.height / size.height);
38 cellSize = cv::Size(minCellSize, minCellSize);
40 for (
int i = 0; i < size.height; i++) {
41 for (
int j = 0; j < size.width; j++) {
42 points.emplace_back(cellSize.width * j, presenter.graphSize.height + cellSize.height * i);
46 outImg.create((cellSize.height * size.height) + presenter.graphSize.height,
47 cellSize.width * size.width,
51 textSize = cv::getTextSize(
"", fontType, fontScale, thickness, &baseline);
52 accuracyMessageSize = cv::getTextSize(
"Accuracy (top 0): 0.000", fontType, fontScale, thickness, &baseline);
53 testMessageSize = cv::getTextSize(ClassificationGridMat::testMessage, fontType, fontScale, thickness, &baseline);
57 PerformanceMetrics::TimePoint lastRequestStartTime,
63 rectangle(outImg, {0, 0}, {outImg.cols, presenter.graphSize.height}, cv::Scalar(0, 0, 0), cv::FILLED);
65 presenter.drawGraphs(outImg);
67 metrics.update(lastRequestStartTime,
69 cv::Point(textPadding, textSize.height + textPadding),
72 cv::Scalar(255, 100, 100),
77 cv::format(
"Accuracy (top %d): %.3f", nTop, accuracy),
78 cv::Point(outImg.cols - accuracyMessageSize.width - textPadding, textSize.height + textPadding),
81 cv::Scalar(255, 255, 255),
88 ClassificationGridMat::testMessage,
89 cv::Point(outImg.cols - testMessageSize.width - textPadding, (textSize.height + textPadding) * 2),
92 cv::Scalar(50, 50, 255),
97 void updateMat(
const cv::Mat& mat,
const std::string& label, PredictionResult predictionResul) {
98 if (!prevImg.empty()) {
99 size_t prevSourceId = currSourceId - 1;
100 prevSourceId = std::min(prevSourceId, points.size() - 1);
101 prevImg.copyTo(outImg(cv::Rect(points[prevSourceId], cellSize)));
103 cv::Scalar textColor;
104 switch (predictionResul) {
105 case PredictionResult::Correct:
106 textColor = cv::Scalar(75, 255, 75);
108 case PredictionResult::Incorrect:
109 textColor = cv::Scalar(50, 50, 255);
111 case PredictionResult::Unknown:
112 textColor = cv::Scalar(200, 10, 10);
115 throw std::runtime_error(
"Undefined type of prediction result");
117 int labelThickness = cellSize.width / 20;
118 cv::Size labelTextSize = cv::getTextSize(label, fontType, 1, 2, &baseline);
119 double labelFontScale =
static_cast<double>(cellSize.width - 2 * labelThickness) / labelTextSize.width;
120 cv::resize(mat, prevImg, cellSize);
123 cv::Point(labelThickness, cellSize.height - labelThickness - labelTextSize.height),
128 cv::Mat cell = outImg(cv::Rect(points[currSourceId], cellSize));
129 prevImg.copyTo(cell);
130 cv::rectangle(cell, {0, 0}, {cell.cols, cell.rows}, {255, 50, 50}, labelThickness);
132 if (currSourceId == points.size() - 1) {
143 std::vector<cv::Point> points;
144 static const int fontType = cv::FONT_HERSHEY_PLAIN;
145 static constexpr
double fontScale = 1.5;
146 static const int thickness = 2;
147 static const int textPadding = 10;
148 static constexpr
const char testMessage[] =
"Testing, please wait...";
151 cv::Size accuracyMessageSize;
152 cv::Size testMessageSize;
155 constexpr
const char ClassificationGridMat::testMessage[];
Definition: classification_grid_mat.hpp:23
Definition: presenter.h:19
a header file with common samples functionality using OpenCV
void putHighlightedText(const cv::Mat &frame, const std::string &message, cv::Point position, int fontFace, double fontScale, cv::Scalar color, int thickness)
Puts text message on the frame, highlights the text with a white border to make it distinguishable fr...
Definition: ocv_common.hpp:200