device_tof3d_b.h 6.1 KB

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