device_tof3d.h 3.9 KB

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