YoloDetector.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef YOLO_DETECTOR__HH
  2. #define YOLO_DETECTOR__HH
  3. #include "opencv/highgui.h"
  4. #include "opencv2/opencv.hpp"
  5. #include <pcl/point_types.h>
  6. #include <pcl/common/common.h>
  7. #include "yolo_v2_class.hpp"
  8. #include "../error_code/error_code.h"
  9. #define YOLO_API_USED 1
  10. typedef struct YOLO_BOX
  11. {
  12. float x, y, w, h;
  13. }YoloBox;
  14. /*
  15. * yolo v2网络识别类, 输入图像大小为416*416.
  16. */
  17. class YoloDetector
  18. {
  19. public:
  20. YoloDetector(std::string cfg,std::string weights,float minx,float maxx,float miny,float maxy,float freq);
  21. //设置输入点云的边界,识别的时候根据边界投影到图像
  22. //输入边界的长款比必须是1:1,否则会出现图像扭曲
  23. //freq:表示多长一个像素点
  24. Error_manager set_region(float minx, float maxx, float miny, float maxy,float freq);
  25. virtual ~YoloDetector();
  26. //识别函数, 将点云数据转换成图像格式保存,该函数已经过期(第一版算法,点云由两个雷达扫描)
  27. //l:左边雷达扫描数据; r:右边雷达扫描数据
  28. //save_dir:中间数据保存路径
  29. Error_manager detect(cv::Mat& l, cv::Mat& r, std::vector<YoloBox>& boxes,std::string save_dir);
  30. //将点云投影成图像, 识别车辆外接矩形框
  31. Error_manager detect(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in, std::vector<YoloBox>& boxes, std::string save_dir);
  32. protected:
  33. //释放图像内存
  34. void free_img(image_t img);
  35. //opencv图像类型转换成 image_t 格式,并调整大小
  36. std::shared_ptr<image_t> mat_to_image_resize(cv::Mat mat) const
  37. {
  38. if (mat.data == NULL) return std::shared_ptr<image_t>(NULL);
  39. cv::Mat det_mat;
  40. cv::resize(mat, det_mat, cv::Size(m_pDetector->get_net_width(), m_pDetector->get_net_height()));
  41. return mat_to_image(det_mat);
  42. }
  43. static std::shared_ptr<image_t> mat_to_image(cv::Mat img_src)
  44. {
  45. cv::Mat img;
  46. cv::cvtColor(img_src, img, cv::COLOR_RGB2BGR);
  47. std::shared_ptr<image_t> image_ptr(new image_t, [](image_t *img) { if(img->data) free(img->data); delete img; });
  48. std::shared_ptr<IplImage> ipl_small = std::make_shared<IplImage>(img);
  49. *image_ptr = ipl_to_image(ipl_small.get());
  50. return image_ptr;
  51. }
  52. static image_t ipl_to_image(IplImage* src)
  53. {
  54. unsigned char *data = (unsigned char *)src->imageData;
  55. int h = src->height;
  56. int w = src->width;
  57. int c = src->nChannels;
  58. int step = src->widthStep;
  59. image_t out = make_image_custom(w, h, c);
  60. int count = 0;
  61. for (int k = 0; k < c; ++k) {
  62. for (int i = 0; i < h; ++i) {
  63. int i_step = i*step;
  64. for (int j = 0; j < w; ++j) {
  65. out.data[count++] = data[i_step + j*c + k] / 255.;
  66. }
  67. }
  68. }
  69. return out;
  70. }
  71. static image_t make_empty_image(int w, int h, int c)
  72. {
  73. image_t out;
  74. out.data = 0;
  75. out.h = h;
  76. out.w = w;
  77. out.c = c;
  78. return out;
  79. }
  80. static image_t make_image_custom(int w, int h, int c)
  81. {
  82. image_t out = make_empty_image(w, h, c);
  83. out.data = (float *)calloc(h*w*c, sizeof(float));
  84. return out;
  85. }
  86. protected:
  87. Detector* m_pDetector;
  88. float m_minx;
  89. float m_maxx;
  90. float m_miny;
  91. float m_maxy;
  92. float m_freq;
  93. };
  94. #endif