utils.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP
  2. #define OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP
  3. #include <map>
  4. #include <opencv2/core.hpp>
  5. #if defined(_WIN32)
  6. #include <windows.h>
  7. #endif
  8. // FIXME: It's better to place it somewhere in common.hpp
  9. struct OutputDescr {
  10. std::vector<int> dims;
  11. int precision;
  12. };
  13. namespace utils {
  14. using double_ms_t = std::chrono::duration<double, std::milli>;
  15. inline void createNDMat(cv::Mat& mat, const std::vector<int>& dims, int depth) {
  16. GAPI_Assert(!dims.empty());
  17. mat.create(dims, depth);
  18. if (dims.size() == 1) {
  19. //FIXME: Well-known 1D mat WA
  20. mat.dims = 1;
  21. }
  22. }
  23. inline void generateRandom(cv::Mat& out) {
  24. switch (out.depth()) {
  25. case CV_8U:
  26. cv::randu(out, 0, 255);
  27. break;
  28. case CV_32F:
  29. cv::randu(out, 0.f, 1.f);
  30. break;
  31. case CV_16F: {
  32. std::vector<int> dims;
  33. for (int i = 0; i < out.size.dims(); ++i) {
  34. dims.push_back(out.size[i]);
  35. }
  36. cv::Mat fp32_mat;
  37. createNDMat(fp32_mat, dims, CV_32F);
  38. cv::randu(fp32_mat, 0.f, 1.f);
  39. fp32_mat.convertTo(out, out.type());
  40. break;
  41. }
  42. default:
  43. throw std::logic_error("Unsupported preprocessing depth");
  44. }
  45. }
  46. inline void sleep(std::chrono::microseconds delay) {
  47. #if defined(_WIN32)
  48. // FIXME: Wrap it to RAII and instance only once.
  49. HANDLE timer = CreateWaitableTimer(NULL, true, NULL);
  50. if (!timer) {
  51. throw std::logic_error("Failed to create timer");
  52. }
  53. LARGE_INTEGER li;
  54. using ns_t = std::chrono::nanoseconds;
  55. using ns_100_t = std::chrono::duration<ns_t::rep,
  56. std::ratio_multiply<std::ratio<100>, ns_t::period>>;
  57. // NB: QuadPart takes portions of 100 nanoseconds.
  58. li.QuadPart = -std::chrono::duration_cast<ns_100_t>(delay).count();
  59. if(!SetWaitableTimer(timer, &li, 0, NULL, NULL, false)){
  60. CloseHandle(timer);
  61. throw std::logic_error("Failed to set timer");
  62. }
  63. if (WaitForSingleObject(timer, INFINITE) != WAIT_OBJECT_0) {
  64. CloseHandle(timer);
  65. throw std::logic_error("Failed to wait timer");
  66. }
  67. CloseHandle(timer);
  68. #else
  69. std::this_thread::sleep_for(delay);
  70. #endif
  71. }
  72. template <typename duration_t>
  73. typename duration_t::rep measure(std::function<void()> f) {
  74. using namespace std::chrono;
  75. auto start = high_resolution_clock::now();
  76. f();
  77. return duration_cast<duration_t>(
  78. high_resolution_clock::now() - start).count();
  79. }
  80. template <typename duration_t>
  81. typename duration_t::rep timestamp() {
  82. using namespace std::chrono;
  83. auto now = high_resolution_clock::now();
  84. return duration_cast<duration_t>(now.time_since_epoch()).count();
  85. }
  86. inline void busyWait(std::chrono::microseconds delay) {
  87. auto start_ts = timestamp<std::chrono::microseconds>();
  88. auto end_ts = start_ts;
  89. auto time_to_wait = delay.count();
  90. while (end_ts - start_ts < time_to_wait) {
  91. end_ts = timestamp<std::chrono::microseconds>();
  92. }
  93. }
  94. template <typename K, typename V>
  95. void mergeMapWith(std::map<K, V>& target, const std::map<K, V>& second) {
  96. for (auto&& item : second) {
  97. auto it = target.find(item.first);
  98. if (it != target.end()) {
  99. throw std::logic_error("Error: key: " + it->first + " is already in target map");
  100. }
  101. target.insert(item);
  102. }
  103. }
  104. template <typename T>
  105. double avg(const std::vector<T>& vec) {
  106. return std::accumulate(vec.begin(), vec.end(), 0.0) / vec.size();
  107. }
  108. template <typename T>
  109. T max(const std::vector<T>& vec) {
  110. return *std::max_element(vec.begin(), vec.end());
  111. }
  112. template <typename T>
  113. T min(const std::vector<T>& vec) {
  114. return *std::min_element(vec.begin(), vec.end());
  115. }
  116. template <typename T>
  117. int64_t ms_to_mcs(T ms) {
  118. using namespace std::chrono;
  119. return duration_cast<microseconds>(duration<T, std::milli>(ms)).count();
  120. }
  121. } // namespace utils
  122. #endif // OPENCV_GAPI_PIPELINE_MODELING_TOOL_UTILS_HPP