2022-02-01 21:32:56 +08:00
|
|
|
#ifndef OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP
|
|
|
|
#define OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP
|
|
|
|
|
2022-09-15 18:47:13 +08:00
|
|
|
#include <map>
|
|
|
|
|
2022-02-01 21:32:56 +08:00
|
|
|
#include <opencv2/core.hpp>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// FIXME: It's better to place it somewhere in common.hpp
|
|
|
|
struct OutputDescr {
|
|
|
|
std::vector<int> dims;
|
|
|
|
int precision;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace utils {
|
2022-02-08 00:48:43 +08:00
|
|
|
|
2023-02-15 19:04:14 +08:00
|
|
|
using double_ms_t = std::chrono::duration<double, std::milli>;
|
|
|
|
|
2022-02-08 00:48:43 +08:00
|
|
|
inline void createNDMat(cv::Mat& mat, const std::vector<int>& dims, int depth) {
|
|
|
|
GAPI_Assert(!dims.empty());
|
|
|
|
mat.create(dims, depth);
|
|
|
|
if (dims.size() == 1) {
|
|
|
|
//FIXME: Well-known 1D mat WA
|
|
|
|
mat.dims = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 21:32:56 +08:00
|
|
|
inline void generateRandom(cv::Mat& out) {
|
|
|
|
switch (out.depth()) {
|
|
|
|
case CV_8U:
|
|
|
|
cv::randu(out, 0, 255);
|
|
|
|
break;
|
|
|
|
case CV_32F:
|
|
|
|
cv::randu(out, 0.f, 1.f);
|
|
|
|
break;
|
|
|
|
case CV_16F: {
|
2022-02-08 00:48:43 +08:00
|
|
|
std::vector<int> dims;
|
|
|
|
for (int i = 0; i < out.size.dims(); ++i) {
|
|
|
|
dims.push_back(out.size[i]);
|
|
|
|
}
|
|
|
|
cv::Mat fp32_mat;
|
|
|
|
createNDMat(fp32_mat, dims, CV_32F);
|
2022-02-01 21:32:56 +08:00
|
|
|
cv::randu(fp32_mat, 0.f, 1.f);
|
|
|
|
fp32_mat.convertTo(out, out.type());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw std::logic_error("Unsupported preprocessing depth");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:04:14 +08:00
|
|
|
inline void sleep(std::chrono::microseconds delay) {
|
2022-02-01 21:32:56 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// FIXME: Wrap it to RAII and instance only once.
|
|
|
|
HANDLE timer = CreateWaitableTimer(NULL, true, NULL);
|
|
|
|
if (!timer) {
|
|
|
|
throw std::logic_error("Failed to create timer");
|
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER li;
|
2023-02-15 19:04:14 +08:00
|
|
|
using ns_t = std::chrono::nanoseconds;
|
|
|
|
using ns_100_t = std::chrono::duration<ns_t::rep,
|
|
|
|
std::ratio_multiply<std::ratio<100>, ns_t::period>>;
|
|
|
|
// NB: QuadPart takes portions of 100 nanoseconds.
|
|
|
|
li.QuadPart = -std::chrono::duration_cast<ns_100_t>(delay).count();
|
|
|
|
|
2022-02-01 21:32:56 +08:00
|
|
|
if(!SetWaitableTimer(timer, &li, 0, NULL, NULL, false)){
|
|
|
|
CloseHandle(timer);
|
|
|
|
throw std::logic_error("Failed to set timer");
|
|
|
|
}
|
|
|
|
if (WaitForSingleObject(timer, INFINITE) != WAIT_OBJECT_0) {
|
|
|
|
CloseHandle(timer);
|
|
|
|
throw std::logic_error("Failed to wait timer");
|
|
|
|
}
|
|
|
|
CloseHandle(timer);
|
|
|
|
#else
|
2023-02-15 19:04:14 +08:00
|
|
|
std::this_thread::sleep_for(delay);
|
2022-02-01 21:32:56 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename duration_t>
|
|
|
|
typename duration_t::rep measure(std::function<void()> f) {
|
|
|
|
using namespace std::chrono;
|
|
|
|
auto start = high_resolution_clock::now();
|
|
|
|
f();
|
|
|
|
return duration_cast<duration_t>(
|
|
|
|
high_resolution_clock::now() - start).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename duration_t>
|
|
|
|
typename duration_t::rep timestamp() {
|
|
|
|
using namespace std::chrono;
|
|
|
|
auto now = high_resolution_clock::now();
|
|
|
|
return duration_cast<duration_t>(now.time_since_epoch()).count();
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:04:14 +08:00
|
|
|
inline void busyWait(std::chrono::microseconds delay) {
|
|
|
|
auto start_ts = timestamp<std::chrono::microseconds>();
|
|
|
|
auto end_ts = start_ts;
|
|
|
|
auto time_to_wait = delay.count();
|
|
|
|
|
|
|
|
while (end_ts - start_ts < time_to_wait) {
|
|
|
|
end_ts = timestamp<std::chrono::microseconds>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:47:13 +08:00
|
|
|
template <typename K, typename V>
|
2022-09-16 23:24:13 +08:00
|
|
|
void mergeMapWith(std::map<K, V>& target, const std::map<K, V>& second) {
|
2022-09-15 18:47:13 +08:00
|
|
|
for (auto&& item : second) {
|
|
|
|
auto it = target.find(item.first);
|
|
|
|
if (it != target.end()) {
|
2022-09-16 23:24:13 +08:00
|
|
|
throw std::logic_error("Error: key: " + it->first + " is already in target map");
|
2022-09-15 18:47:13 +08:00
|
|
|
}
|
|
|
|
target.insert(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:04:49 +08:00
|
|
|
template <typename T>
|
|
|
|
double avg(const std::vector<T>& vec) {
|
|
|
|
return std::accumulate(vec.begin(), vec.end(), 0.0) / vec.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T max(const std::vector<T>& vec) {
|
|
|
|
return *std::max_element(vec.begin(), vec.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T min(const std::vector<T>& vec) {
|
|
|
|
return *std::min_element(vec.begin(), vec.end());
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:37:56 +08:00
|
|
|
template <typename T>
|
|
|
|
int64_t ms_to_mcs(T ms) {
|
|
|
|
using namespace std::chrono;
|
|
|
|
return duration_cast<microseconds>(duration<T, std::milli>(ms)).count();
|
|
|
|
}
|
|
|
|
|
2022-02-01 21:32:56 +08:00
|
|
|
} // namespace utils
|
|
|
|
|
|
|
|
#endif // OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP
|