device_tof3d.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "tof3d_config.pb.h"
  14. #ifdef ENABLE_TENSORRT_DETECT
  15. #include "detect/tensorrt/wheel-detector.h"
  16. #else
  17. #include "detect/onnx/wheel-detector.h"
  18. #endif
  19. class DeviceTof3D {
  20. public:
  21. struct DetectResult {
  22. };
  23. struct tof3dVzenseInfo {
  24. VzDeviceHandle handle = nullptr;
  25. bool is_connect = false;
  26. bool is_start_stream = false;
  27. VzDeviceInfo info;
  28. tof3dVzenseEtc etc;
  29. };
  30. using VzEtcMap = std::map<std::string, tof3dVzenseEtc>;
  31. using tof3dVzenseInfoMap = std::map<std::string, tof3dVzenseInfo *>;
  32. public:
  33. static DeviceTof3D *iter() {
  34. static DeviceTof3D *instance = nullptr;
  35. if (instance == nullptr) {
  36. instance = new DeviceTof3D();
  37. }
  38. return instance;
  39. }
  40. ~DeviceTof3D() = default;
  41. // TODO
  42. void run();
  43. Error_manager Init(const VzEtcMap &tp_tof3d, bool search = true);
  44. Error_manager reInit(const VzEtcMap &etc);
  45. Error_manager SearchDevice(const double &time);
  46. Error_manager ConnectDevice(const std::string &ip, bool open_stream = true);
  47. Error_manager ConnectAllDevices(bool open_stream = true);
  48. Error_manager ConnectAllEtcDevices(bool open_stream = true);
  49. Error_manager DisConnectDevice(const std::string &ip);
  50. Error_manager DisConnectAllEtcDevices();
  51. Error_manager DisConnectAllDevices();
  52. Error_manager getDepthFrame(const std::string &ip, VzFrame &depthFrame);
  53. Error_manager getIrFrame(const std::string &ip, VzFrame &irFrame);
  54. Error_manager getDepthAndIrPicture(const std::string &ip, VzFrame &depthFrame, VzFrame &irFrame);
  55. Error_manager getDepthPointCloud(const std::string &ip, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud);
  56. Error_manager
  57. DepthFrame2PointCloud(const std::string &ip, VzFrame &depthFrame, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud);
  58. Error_manager updateTof3dEtc();
  59. Error_manager setTof3dParams(const std::string &ip);
  60. Error_manager getTof3dParams(const std::string &ip);
  61. static Error_manager Frame2Mat(VzFrame &frame, cv::Mat &mat);
  62. protected:
  63. DeviceTof3D() = default;
  64. static Error_manager DepthFrame2Mat(VzFrame &frame, cv::Mat &mat);
  65. static Error_manager IrFrame2Mat(VzFrame &frame, cv::Mat &mat);
  66. void receiveFrameThread(const std::string &ip);
  67. void detectThread();
  68. static void HotPlugStateCallback(const VzDeviceInfo *pInfo, int status, void *contex);
  69. bool drawBox(cv::Mat &mat, cv::Rect &box, cv::Scalar &color, std::string className, float confidence);
  70. Error_manager loadEtc(const VzEtcMap &etc);
  71. void stopWorking();
  72. // // 传入深度图,得到车轮在mat中的坐标
  73. // Error_manager segmentTensorRT(cv::Mat &depth_mat, std::vector<cv::Point> &wheel_cv_cloud);
  74. // 根据传入坐标,分离depth frame的数据点
  75. Error_manager segFrame2CarAndWheel(VzFrame &depth_frame, std::vector<cv::Point> &wheel_cv_cloud,
  76. pcl::PointCloud<pcl::PointXYZ>::Ptr &car_cloud,
  77. pcl::PointCloud<pcl::PointXYZ>::Ptr &wheel_cloud);
  78. // 车轮点云优化
  79. Error_manager WheelCloudOptimize(pcl::PointCloud<pcl::PointXYZ>::Ptr &wheel_cloud);
  80. // 车身点云优化
  81. Error_manager CarPoseOptimize(pcl::PointCloud<pcl::PointXYZ>::Ptr &car_cloud);
  82. Error_manager grpcVzenseMat(JetStream::ResFrame &frame, const DeviceAzimuth &azimuth, cv::Mat &mat);
  83. Error_manager grpcVzensePointMat(JetStream::ResFrame &frame, const DeviceAzimuth &azimuth, cv::Mat &mat);
  84. Error_manager grpcCloud(JetStream::ResFrame &frame, const DeviceAzimuth &azimuth, pcl::PointCloud<pcl::PointXYZI>::Ptr &cloud);
  85. private:
  86. struct ThreadInfo {
  87. std::thread *t;
  88. Thread_condition *condit;
  89. ThreadInfo() {
  90. t = nullptr;
  91. condit = nullptr;
  92. }
  93. ThreadInfo(std::thread *m_t, Thread_condition *m_condit) : t(m_t), condit(m_condit) {}
  94. };
  95. struct DeviceMat {
  96. bool empty;
  97. cv::Mat depthMat;
  98. cv::Mat pointMat;
  99. DeviceMat() {
  100. empty = true;
  101. depthMat.create(480, 640, CV_16UC1);
  102. pointMat.create(480, 640, CV_32FC4);
  103. }
  104. DeviceMat& operator=(const DeviceMat &p) {
  105. this->depthMat = p.depthMat.clone();
  106. this->pointMat = p.pointMat.clone();
  107. return *this;
  108. }
  109. };
  110. TimedLockData<DeviceMat> m_device_mat[DeviceAzimuth::MAX];
  111. VzReturnStatus m_vz_status = VzRetOthers;
  112. tof3dVzenseInfoMap mp_device_info;
  113. std::map<std::string, ThreadInfo> mp_thread_info;
  114. ThreadInfo detect_thread;
  115. TensorrtWheelDetector *detector;
  116. StreamRpcServer m_grpc_server;
  117. };