-
Notifications
You must be signed in to change notification settings - Fork 18
/
Utilities.cpp
94 lines (80 loc) · 2.9 KB
/
Utilities.cpp
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
// ---------------------------------------------------------------------
// Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// ---------------------------------------------------------------------
#include <fstream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <sstream>
#include <string>
#include <vector>
#include "Utilities.hpp"
namespace Utils
{
void AddText(const cv::Mat& img, const std::string& msg)
{
int font_face = cv::FONT_HERSHEY_SIMPLEX;
double font_scale = 2;
int thickness = 3;
int baseline = 0;
cv::Size text_size = cv::getTextSize(msg, font_face, font_scale, thickness, &baseline);
cv::Point text_position((img.cols - text_size.width) / 2, text_size.height + 10);
cv::rectangle(img, text_position + cv::Point(0, baseline),
text_position + cv::Point(text_size.width, -text_size.height), cv::Scalar(255, 255, 255), cv::FILLED);
cv::putText(img, msg, text_position, font_face, font_scale, cv::Scalar(0, 0, 0), thickness);
}
std::vector<float> LoadImageFile(const std::string& image_path, uint32_t input_image_height, uint32_t input_image_width)
{
cv::Mat image = cv::imread(image_path);
if (image.empty())
{
std::ostringstream msg;
msg << "Input image not found at " << image_path << ".\n Please provide full path to input image.";
throw std::runtime_error(msg.str());
}
cvtColor(image, image, cv::COLOR_BGR2RGB);
resize(image, image, cv::Size(input_image_height, input_image_width));
image = image.reshape(1, 1);
std::vector<float> vec;
image.convertTo(vec, CV_32FC1, 1. / 255);
std::vector<float> output;
output.reserve(vec.size());
// Transposing channel
for (size_t ch = 0; ch < 3; ++ch)
{
for (size_t i = ch; i < vec.size(); i += 3)
{
output.push_back(vec[i]);
}
}
return output;
}
std::string RemoveFirstWord(const std::string& input)
{
size_t pos = input.find(' '); // Find the first space
if (pos == std::string::npos)
{
return ""; // If no space is found, return an empty string
}
return input.substr(pos + 1); // Return the substring after the first space
}
std::unordered_map<int, std::string> ReadLabelFile(std::string filepath, const size_t label_size)
{
std::fstream label_file(filepath, std::ios::in);
if (!label_file.is_open())
{
throw std::runtime_error("Error: File not found or unable to open the file.");
}
std::unordered_map<int, std::string> label_table;
label_table.reserve(label_size);
int i = 0;
std::string line;
while (std::getline(label_file, line))
{
// Remove the synset ID as it is not the actual class label
label_table.emplace(i++, RemoveFirstWord(line));
}
return label_table;
}
} // namespace Utils