YoloDetector.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "YoloDetector.h"
  2. #include <glog/logging.h>
  3. void YoloDetector::free_img(image_t m)
  4. {
  5. if (m.data)
  6. {
  7. free(m.data);
  8. }
  9. }
  10. Error_manager YoloDetector::set_region(float minx, float maxx, float miny, float maxy,float freq)
  11. {
  12. if(m_maxx<=m_minx || m_maxy<=m_miny || m_freq<=0)
  13. {
  14. char description[255]={0};
  15. sprintf(description,"set yolo parameter l:%.3f r:%.3f t:%.3f b:%.3f",minx,maxx,miny,maxy);
  16. return Error_manager(LOCATER_YOLO_PARAMETER_INVALID,NORMAL,description);
  17. }
  18. m_minx = minx;
  19. m_maxx = maxx;
  20. m_miny = miny;
  21. m_maxy = maxy;
  22. m_freq = freq;
  23. return SUCCESS;
  24. }
  25. YoloDetector::YoloDetector(std::string cfg, std::string weights, float minx, float maxx, float miny, float maxy,float freq)
  26. {
  27. #if YOLO_API_USED
  28. m_pDetector = new Detector(cfg, weights);
  29. m_minx = minx;
  30. m_maxx = maxx;
  31. m_miny = miny;
  32. m_maxy = maxy;
  33. m_freq = freq;
  34. //LOG(INFO) << "\tyolo minx:" << m_minx << ",maxx:" << m_maxx << ",miny:" << m_miny << ",maxy:" << m_maxy;
  35. #endif
  36. }
  37. YoloDetector::~YoloDetector()
  38. {
  39. #if YOLO_API_USED
  40. if (m_pDetector)
  41. {
  42. delete m_pDetector;
  43. m_pDetector = NULL;
  44. }
  45. #endif
  46. }
  47. Error_manager YoloDetector::detect(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in, std::vector<YoloBox>& boxes, std::string save_dir)
  48. {
  49. boxes.clear();
  50. #if YOLO_API_USED
  51. /*int w = int((m_maxx - m_minx) / m_freq);
  52. int h = int((m_maxy - m_miny) / m_freq);*/
  53. int w = 416;
  54. int h = 208;
  55. cv::Mat img = cv::Mat::zeros(cv::Size(w, h * 2), CV_8UC1);
  56. for (int i = 0; i < cloud_in->size(); ++i)
  57. {
  58. pcl::PointXYZ point = cloud_in->points[i];
  59. float x = point.x;
  60. float y = point.y;
  61. float z = point.z;
  62. if (x >= m_minx&&x < m_maxx&&y >= m_miny&&y < m_maxy&&z < 2000.&&z>50.)
  63. {
  64. int c = int((x - m_minx) / (m_maxx-m_minx)*w);
  65. int r = int((y - m_miny) / (m_maxy-m_miny)*h);
  66. int gray = int(z / 2000. * 256);
  67. if (gray > img.at<uchar>(r, c))
  68. img.at<uchar>(r, c) = gray;
  69. }
  70. }
  71. if (m_pDetector)
  72. {
  73. boxes.clear();
  74. cv::cvtColor(img,img,CV_GRAY2RGB);
  75. std::shared_ptr<image_t> img_t=mat_to_image_resize(img);
  76. std::vector<bbox_t> rects=m_pDetector->detect(*img_t, 0.5);
  77. //free_img(*img_t);
  78. cv::Mat rgb=img.clone();
  79. float ext = 0.0;
  80. for (int i = 0; i < rects.size(); ++i)
  81. {
  82. /*bbox_t box = rects[i];
  83. bbox_t box_t = box;
  84. float len = std::min(box.w*m_freq, box.h*m_freq);
  85. box_t.x = int(box.x*m_freq + m_minx - ext*len);
  86. box_t.y = int(box.y*m_freq + m_miny - ext*len);
  87. box_t.w = int(box.w*m_freq*(1.0+2*ext));
  88. box_t.h = int(box.h*m_freq*(1.0+2*ext));*/
  89. bbox_t box = rects[i];
  90. YoloBox box_y;
  91. box_y.x = (float(box.x) / float(w)*(m_maxx - m_minx) + m_minx);
  92. box_y.y = (float(box.y) /float(h)*(m_maxy - m_miny) + m_miny);
  93. box_y.w = (float(box.w)/float(w)*(m_maxx - m_minx));
  94. box_y.h = (float(box.h)/float(h)*(m_maxy - m_miny));
  95. //LOG(WARNING) << "\tyolo minx:" << m_minx << ",maxx:" << m_maxx << ",miny:" << m_miny << ",maxy:" << m_maxy;
  96. //LOG(WARNING) << "box " << i << " x:" << box.x << " ,y:" << box.y << " ,w:" << box.w << " ,h:" << box.h;
  97. //LOG(INFO) << "yolo box " << i << " x:" << box_y.x << " ,y:" << box_y.y << " ,w:" << box_y.w << " ,h:" << box_y.h;
  98. boxes.push_back(box_y);
  99. //����boxes
  100. cv::rectangle(rgb, cv::Point(box.x, box.y),
  101. cv::Point(box.x + box.w, box.y + box.h), cv::Scalar(0, 255, 0), 1);
  102. }
  103. char num[32] = { 0 };
  104. sprintf(num, " size=%d", rects.size());
  105. cv::putText(rgb, num, cv::Point(50, 50), 1, 1, cv::Scalar(0, 255, 0));
  106. cv::imwrite(save_dir + "/yolo.jpg", rgb);
  107. if (boxes.size() > 0)
  108. return SUCCESS;
  109. else
  110. return Error_manager(LOCATER_YOLO_DETECT_NO_TARGET,NORMAL,"yolo detect no car");
  111. }
  112. #endif
  113. return Error_manager(LOCATER_YOLO_DETECT_FAILED,NORMAL,"YOLO detect failed");
  114. }
  115. Error_manager YoloDetector::detect(cv::Mat& l, cv::Mat& r, std::vector<YoloBox>& boxes,std::string save_dir)
  116. {
  117. #if YOLO_API_USED
  118. int w = int((m_maxx - m_minx) / m_freq);
  119. int h = int((m_maxy - m_miny) / m_freq);
  120. cv::Mat img = cv::Mat::zeros(cv::Size(w, h*2), CV_8UC1);
  121. for (int i = 0; i < l.rows; ++i)
  122. {
  123. for (int j = 0; j < l.cols; ++j)
  124. {
  125. cv::Vec3f vec_l = l.at<cv::Vec3f>(i, j);
  126. cv::Vec3f vec_r = r.at<cv::Vec3f>(i, j);
  127. float x = vec_l[0];
  128. float y = vec_l[1];
  129. float z = vec_l[2];
  130. if (x >= m_minx&&x < m_maxx&&y >= m_miny&&y < m_maxy&&z < 2000.)
  131. {
  132. int c = int((x - m_minx) / m_freq);
  133. int r = int((y - m_miny) / m_freq);
  134. int gray = int(z / 2000. * 256);
  135. if (gray > img.at<uchar>(r, c))
  136. img.at<uchar>(r, c) = gray;
  137. }
  138. x = vec_r[0];
  139. y = vec_r[1];
  140. z = vec_r[2];
  141. if (x >= m_minx&&x < m_maxx&&y >= m_miny&&y < m_maxy&&z < 2000.)
  142. {
  143. int c = int((x - m_minx) / m_freq);
  144. int r = int((y - m_miny) / m_freq);
  145. int gray = int(z / 2000. * 256);
  146. if (gray > img.at<uchar>(r, c))
  147. img.at<uchar>(r, c) = gray;
  148. }
  149. }
  150. }
  151. if (m_pDetector)
  152. {
  153. boxes.clear();
  154. std::shared_ptr<image_t> img_t=mat_to_image_resize(img);
  155. std::vector<bbox_t> rects=m_pDetector->detect(*img_t, 0.5);
  156. free_img(*img_t);
  157. cv::Mat rgb;
  158. cv::cvtColor(img, rgb, CV_GRAY2RGB);
  159. for (int i = 0; i < rects.size(); ++i)
  160. {
  161. bbox_t box = rects[i];
  162. YoloBox box_t;
  163. box_t.x = (box.x*m_freq+m_minx);
  164. box_t.y = (box.y*m_freq+m_miny);
  165. box_t.w = (box.w*m_freq);
  166. box_t.h = (box.h*m_freq);
  167. boxes.push_back(box_t);
  168. //����boxes
  169. cv::rectangle(rgb, cv::Point(box.x, box.y ),
  170. cv::Point(box.x+box.w, box.y + box.h), cv::Scalar(0, 255, 0), 1);
  171. }
  172. char num[32] = { 0 };
  173. sprintf(num, " size=%d", rects.size());
  174. cv::putText(rgb, num, cv::Point(50, 50), 1, 1, cv::Scalar(0, 255, 0));
  175. cv::imwrite(save_dir + "/yolo.jpg", rgb(cv::Rect(0,0,rgb.cols,rgb.rows/2)));
  176. return SUCCESS;
  177. }
  178. #endif
  179. return Error_manager(LOCATER_YOLO_DETECT_FAILED,NORMAL,"yolo api not define,(no code)");
  180. }