device_tof3d.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #pragma once
  2. #include <thread>
  3. #include <boost/chrono/duration.hpp>
  4. #include <opencv2/opencv.hpp>
  5. #include <pcl/point_types.h>
  6. #include <pcl/point_cloud.h>
  7. #include "error_code/error_code.hpp"
  8. #include "VzenseNebula_api.h"
  9. #include "log/log.h"
  10. #include "thread/thread_condition.h"
  11. #include "stream/streamServer.h"
  12. #include "tool/timedlockdata.hpp"
  13. #include "tool/ip.hpp"
  14. #include "pcl/trans_coor.hpp"
  15. #include "pcl/point3D_tool.h"
  16. #include "tof3d_config.pb.h"
  17. #ifdef ENABLE_TENSORRT_DETECT
  18. #include "detect/tensorrt/wheel-detector.h"
  19. #else
  20. #include "detect/onnx/wheel-detector.h"
  21. #endif
  22. class DeviceTof3D {
  23. public:
  24. struct WheelDetectResult {
  25. bool haveCloud = false;
  26. bool segmentOk = false;
  27. bool detectOk = false;
  28. pcl::PointXYZ center;
  29. float confidence = 0;
  30. void reset() {
  31. haveCloud = false;
  32. segmentOk = false;
  33. detectOk = false;
  34. center.x = 0;
  35. center.y = 0;
  36. center.z = 0;
  37. confidence = 0;
  38. }
  39. };
  40. struct CarDetectResult {
  41. float wheels_center_x;
  42. float wheels_center_y;
  43. float width;
  44. float wheel_width;
  45. float length;
  46. float wheel_length;
  47. float front_wheels_theta;
  48. float theta;
  49. float wheel_base;
  50. void reset() {
  51. wheels_center_x = 0;
  52. wheels_center_y = 0;
  53. width = 0;
  54. wheel_width = 0;
  55. length = 0;
  56. wheel_length = 0;
  57. front_wheels_theta = 0;
  58. theta = 0;
  59. wheel_base = 0;
  60. }
  61. };
  62. struct DetectResult {
  63. WheelDetectResult wheel[DeviceAzimuth::MAX];
  64. CarDetectResult car;
  65. void reset() {
  66. for (int i = 0; i < DeviceAzimuth::MAX; i++) {
  67. wheel[i].reset();
  68. }
  69. car.reset();
  70. }
  71. };
  72. struct tof3dVzenseInfo {
  73. VzDeviceHandle handle = nullptr;
  74. bool is_connect = false;
  75. bool is_start_stream = false;
  76. VzDeviceInfo info;
  77. tof3dVzenseEtc etc;
  78. };
  79. using VzEtcMap = std::map<std::string, tof3dVzenseEtc>;
  80. using tof3dVzenseInfoMap = std::map<std::string, tof3dVzenseInfo *>;
  81. public:
  82. static DeviceTof3D *iter() {
  83. static DeviceTof3D *instance = nullptr;
  84. if (instance == nullptr) {
  85. instance = new DeviceTof3D();
  86. }
  87. return instance;
  88. }
  89. ~DeviceTof3D() = default;
  90. // TODO
  91. void run();
  92. Error_manager Init(const VzEtcMap &tp_tof3d, bool search = true);
  93. Error_manager reInit(const VzEtcMap &etc);
  94. Error_manager SearchDevice(const double &time);
  95. Error_manager ConnectDevice(const std::string &ip, bool open_stream = true);
  96. Error_manager ConnectAllDevices(bool open_stream = true);
  97. Error_manager ConnectAllEtcDevices(bool open_stream = true);
  98. Error_manager DisConnectDevice(const std::string &ip);
  99. Error_manager DisConnectAllEtcDevices();
  100. Error_manager DisConnectAllDevices();
  101. Error_manager getDepthFrame(const std::string &ip, VzFrame &depthFrame);
  102. Error_manager getIrFrame(const std::string &ip, VzFrame &irFrame);
  103. Error_manager getDepthAndIrPicture(const std::string &ip, VzFrame &depthFrame, VzFrame &irFrame);
  104. Error_manager getDepthPointCloud(const std::string &ip, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud);
  105. Error_manager
  106. DepthFrame2PointCloud(const std::string &ip, VzFrame &depthFrame, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud);
  107. Error_manager updateTof3dEtc();
  108. Error_manager setTof3dParams(const std::string &ip);
  109. Error_manager getTof3dParams(const std::string &ip);
  110. static Error_manager Frame2Mat(VzFrame &frame, cv::Mat &mat);
  111. protected:
  112. DeviceTof3D() = default;
  113. static Error_manager DepthFrame2Mat(VzFrame &frame, cv::Mat &mat);
  114. static Error_manager IrFrame2Mat(VzFrame &frame, cv::Mat &mat);
  115. void receiveFrameThread(const std::string &ip);
  116. void detectThread();
  117. static void HotPlugStateCallback(const VzDeviceInfo *pInfo, int status, void *contex);
  118. bool drawBox(cv::Mat &mat, cv::Rect &box, cv::Scalar &color, std::string className, float confidence);
  119. Error_manager loadEtc(const VzEtcMap &etc);
  120. void stopWorking();
  121. // // 传入深度图,得到车轮在mat中的坐标
  122. // Error_manager segmentTensorRT(cv::Mat &depth_mat, std::vector<cv::Point> &wheel_cv_cloud);
  123. // 根据传入坐标,分离depth frame的数据点
  124. Error_manager segFrame2CarAndWheel(VzFrame &depth_frame, std::vector<cv::Point> &wheel_cv_cloud,
  125. pcl::PointCloud<pcl::PointXYZ>::Ptr &car_cloud,
  126. pcl::PointCloud<pcl::PointXYZ>::Ptr &wheel_cloud);
  127. // 车轮点云优化
  128. Error_manager WheelCloudOptimize(pcl::PointCloud<pcl::PointXYZ>::Ptr &wheel_cloud, DetectResult &result);
  129. // 车身点云优化
  130. Error_manager CarPoseOptimize(pcl::PointCloud<pcl::PointXYZ>::Ptr &car_cloud, DetectResult &result);
  131. Error_manager grpcVzenseMat(JetStream::ResFrame &frame, const DeviceAzimuth &azimuth, cv::Mat &mat);
  132. Error_manager grpcVzensePointMat(JetStream::ResFrame &frame, const DeviceAzimuth &azimuth, cv::Mat &mat);
  133. Error_manager grpcCloud(JetStream::ResFrame &frame, const DeviceAzimuth &azimuth, pcl::PointCloud<pcl::PointXYZI>::Ptr &cloud);
  134. private:
  135. struct ThreadInfo {
  136. std::thread *t;
  137. Thread_condition *condit;
  138. ThreadInfo() {
  139. t = nullptr;
  140. condit = nullptr;
  141. }
  142. ThreadInfo(std::thread *m_t, Thread_condition *m_condit) : t(m_t), condit(m_condit) {}
  143. };
  144. struct DeviceMat {
  145. cv::Mat depthMat;
  146. cv::Mat pointMat;
  147. DeviceMat() {
  148. depthMat.create(480, 640, CV_16UC1);
  149. pointMat.create(480, 640, CV_32FC4);
  150. }
  151. DeviceMat& operator=(const DeviceMat &p) {
  152. this->depthMat = p.depthMat.clone();
  153. this->pointMat = p.pointMat.clone();
  154. return *this;
  155. }
  156. };
  157. TimedLockData<DeviceMat> m_device_mat[DeviceAzimuth::MAX];
  158. VzReturnStatus m_vz_status = VzRetOthers;
  159. tof3dVzenseInfoMap mp_device_info;
  160. std::map<std::string, ThreadInfo> mp_thread_info;
  161. ThreadInfo detect_thread;
  162. TensorrtWheelDetector *detector;
  163. StreamRpcServer m_grpc_server;
  164. DetectResult m_detect_result_record;
  165. };