data_buf_lable.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include <iostream>
  3. #include <map>
  4. #include <chrono>
  5. #include <thread>
  6. #include <mutex>
  7. #include <opencv2/opencv.hpp>
  8. #include "proto/def.grpc.pb.h"
  9. template <class Type>
  10. class lockFunc {
  11. public:
  12. template<class Func, class ... Args>
  13. int lock_exe(Func func, Args&&... args) {
  14. lock.lock();
  15. bool ret = ((Type* )this->*func)(args...);
  16. lock.unlock();
  17. return ret;
  18. }
  19. template<class Func, class ... Args>
  20. int lock_exe(Func func, Type* p, Args&&... args) {
  21. lock.lock();
  22. bool ret = (p->*func)(args...);
  23. lock.unlock();
  24. return ret;
  25. }
  26. private:
  27. std::mutex lock;
  28. };
  29. class MatDataBuffer : public lockFunc<MatDataBuffer>{
  30. public:
  31. static MatDataBuffer *iter() {
  32. static MatDataBuffer *instance = nullptr;
  33. if (instance == nullptr) {
  34. instance = new MatDataBuffer();
  35. }
  36. return instance;
  37. }
  38. bool set(int azimuth, int lable, cv::Mat &mat) {
  39. if (azimuth >= DeviceAzimuth::DEVICE_AZIMUTH_MAX) {
  40. return false;
  41. }
  42. if (data[azimuth].size() > 10) {
  43. data[azimuth].erase(data[azimuth].begin());
  44. }
  45. data[azimuth].emplace_back(lable, mat);
  46. return true;
  47. }
  48. bool get(int azimuth, int lable, cv::Mat &mat, double timeout = 10) {
  49. if (azimuth >= DeviceAzimuth::DEVICE_AZIMUTH_MAX) {
  50. LOG(INFO) << "azimuth: " << azimuth << " < " << DeviceAzimuth::DEVICE_AZIMUTH_MAX;
  51. return false;
  52. }
  53. for (auto & iter : data[azimuth]) {
  54. if (iter.lable != lable) {
  55. //LOG(INFO) << lable << " != " << iter.lable;
  56. continue;
  57. } else {
  58. std::chrono::duration<double> cost = std::chrono::steady_clock::now() - iter.t;
  59. if (cost.count() > timeout) {
  60. //LOG(INFO) << "data is too old : " << cost.count() << "s";
  61. return false;
  62. } else {
  63. mat = iter.mat.clone();
  64. return true;
  65. }
  66. }
  67. }
  68. //(INFO) << "Not found data: " << lable << " in device " << azimuth;
  69. return false;
  70. }
  71. bool setIr(int lable, cv::Mat &mat) {
  72. if (ir.size() > 10) {
  73. ir.erase(ir.begin());
  74. }
  75. ir.emplace_back(lable, mat);
  76. return true;
  77. }
  78. bool getIr(int lable, cv::Mat &mat, double timeout = 10) {
  79. for (auto & iter : ir) {
  80. if (iter.lable != lable) {
  81. //LOG(INFO) << lable << " != " << iter.lable;
  82. continue;
  83. } else {
  84. std::chrono::duration<double> cost = std::chrono::steady_clock::now() - iter.t;
  85. if (cost.count() > timeout) {
  86. //LOG(INFO) << "data is too old : " << cost.count() << "s";
  87. return false;
  88. } else {
  89. mat = iter.mat.clone();
  90. return true;
  91. }
  92. }
  93. }
  94. //(INFO) << "Not found data: " << lable << " in device " << azimuth;
  95. return false;
  96. }
  97. private:
  98. struct DataBufferInfo {
  99. std::chrono::steady_clock::time_point t;
  100. int lable;
  101. cv::Mat mat;
  102. DataBufferInfo(int lable_, cv::Mat &mat_) : lable(lable_), mat(mat_.clone()) {
  103. t = std::chrono::steady_clock::now();
  104. }
  105. };
  106. private:
  107. std::vector<DataBufferInfo> data[DeviceAzimuth::DEVICE_AZIMUTH_MAX];
  108. std::vector<DataBufferInfo> ir;
  109. };
  110. class LabelYoloDataBuffer : public lockFunc<LabelYoloDataBuffer> {
  111. public:
  112. static LabelYoloDataBuffer *iter() {
  113. static LabelYoloDataBuffer *instance = nullptr;
  114. if (instance == nullptr) {
  115. instance = new LabelYoloDataBuffer();
  116. }
  117. return instance;
  118. }
  119. bool set(JetStream::LabelYolo &yolo) {
  120. if (data.size() > 10) {
  121. data.pop();
  122. }
  123. data.push(yolo);
  124. return true;
  125. }
  126. bool get(JetStream::LabelYolo &yolo, double timeout = 10) {
  127. if (data.empty()) {
  128. return false;
  129. }
  130. yolo = data.front();
  131. data.pop();
  132. return true;
  133. }
  134. private:
  135. std::queue<JetStream::LabelYolo> data;
  136. };