-
Notifications
You must be signed in to change notification settings - Fork 212
/
utils.hpp
144 lines (130 loc) · 5.22 KB
/
utils.hpp
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
//*****************************************************************************
// Copyright 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.
//*****************************************************************************
#pragma once
#include <cmath>
#include <string>
#include <utility>
#include <vector>
#include "../../custom_node_interface.h"
#include "opencv2/opencv.hpp"
#define NODE_ASSERT(cond, msg) \
if (!(cond)) { \
std::cout << "[" << __LINE__ << "] Assert: " << msg << std::endl; \
return 1; \
}
template <typename T>
std::vector<T> reorder_to_nhwc(const T* nchwVector, int rows, int cols, int channels) {
std::vector<T> nhwcVector(rows * cols * channels);
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
for (int c = 0; c < channels; ++c) {
nhwcVector[y * channels * cols + x * channels + c] = reinterpret_cast<const T*>(nchwVector)[c * (rows * cols) + y * cols + x];
}
}
}
return std::move(nhwcVector);
}
template <typename T>
std::vector<T> reorder_to_nchw(const T* nhwcVector, int rows, int cols, int channels) {
std::vector<T> nchwVector(rows * cols * channels);
for (int y = 0; y < rows; ++y) {
for (int x = 0; x < cols; ++x) {
for (int c = 0; c < channels; ++c) {
nchwVector[c * (rows * cols) + y * cols + x] = reinterpret_cast<const T*>(nhwcVector)[y * channels * cols + x * channels + c];
}
}
}
return std::move(nchwVector);
}
const cv::Mat nhwc_to_mat(const CustomNodeTensor* input) {
uint64_t height = input->dims[1];
uint64_t width = input->dims[2];
return cv::Mat(height, width, CV_32FC3, input->data);
}
const cv::Mat nchw_to_mat(const CustomNodeTensor* input) {
uint64_t channels = input->dims[1];
uint64_t rows = input->dims[2];
uint64_t cols = input->dims[3];
auto nhwcVector = reorder_to_nhwc<float>((float*)input->data, rows, cols, channels);
cv::Mat image(rows, cols, CV_32FC3);
std::memcpy(image.data, nhwcVector.data(), nhwcVector.size() * sizeof(float));
return image;
}
bool crop_rotate_resize(cv::Mat originalImage, cv::Mat& targetImage, cv::Rect roi, float angle, float originalTextWidth, float originalTextHeight, cv::Size targetShape) {
cv::Mat cropped = originalImage(roi);
cv::Mat rotated;
if (angle != 0.0) {
cv::Mat rotationMatrix = cv::getRotationMatrix2D(cv::Point2f(cropped.size().width / 2, cropped.size().height / 2), angle, 1.0);
cv::warpAffine(cropped, rotated, rotationMatrix, cropped.size());
} else {
rotated = cropped;
}
try {
cv::Mat rotatedSlicedImage;
if (angle != 0.0) {
int sliceOffset = (rotated.size().height - originalTextHeight) / 2;
rotatedSlicedImage = rotated(cv::Rect(0, sliceOffset, rotated.size().width, originalTextHeight));
} else {
rotatedSlicedImage = rotated;
}
cv::resize(rotatedSlicedImage, targetImage, targetShape);
} catch (const cv::Exception& e) {
std::cout << e.what() << std::endl;
return false;
}
return true;
}
cv::Mat apply_grayscale(cv::Mat image) {
cv::Mat grayscaled;
cv::cvtColor(image, grayscaled, cv::COLOR_BGR2GRAY);
return grayscaled;
}
float get_float_parameter(const std::string& name, const struct CustomNodeParam* params, int paramsCount, float defaultValue = 0.0f) {
for (int i = 0; i < paramsCount; i++) {
if (name == params[i].key) {
try {
return std::stof(params[i].value);
} catch (std::invalid_argument& e) {
return defaultValue;
} catch (std::out_of_range& e) {
return defaultValue;
}
}
}
return defaultValue;
}
int get_int_parameter(const std::string& name, const struct CustomNodeParam* params, int paramsCount, int defaultValue = 0) {
for (int i = 0; i < paramsCount; i++) {
if (name == params[i].key) {
try {
return std::stoi(params[i].value);
} catch (std::invalid_argument& e) {
return defaultValue;
} catch (std::out_of_range& e) {
return defaultValue;
}
}
}
return defaultValue;
}
std::string get_string_parameter(const std::string& name, const struct CustomNodeParam* params, int paramsCount, const std::string& defaultValue = "") {
for (int i = 0; i < paramsCount; i++) {
if (name == params[i].key) {
return params[i].value;
}
}
return defaultValue;
}