main.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <iostream>
  4. #include "pointpillars.h"
  5. #include <iostream>
  6. #include <pcl/point_cloud.h>
  7. #include <pcl/point_types.h>
  8. #include <pcl/io/io.h>
  9. #include <pcl/io/pcd_io.h>
  10. #include "xmlparam.h"
  11. #include "modulecomm.h"
  12. #include "ivfault.h"
  13. #include "ivlog.h"
  14. #include "ivexit.h"
  15. #include "ivversion.h"
  16. #include <thread>
  17. #include "objectarray.pb.h"
  18. //#include "ivbacktrace.h"
  19. #include "Tracking.hpp"
  20. #include "roiaware_pool3d.h"
  21. #include "Eigen/Dense"
  22. iv::Ivfault *gfault = nullptr;
  23. iv::Ivlog *givlog = nullptr;
  24. std::thread * gpthread;
  25. PointPillars * pPillars = nullptr ;
  26. void * gpa;
  27. void * gpdetect;
  28. int gnothavedatatime = 0;
  29. const int kNumPointFeature = 5;
  30. const int kOutputNumBoxFeature = 7;
  31. std::string gstrinput;
  32. std::string gstroutput;
  33. Eigen::Matrix3d rotation_matrix;
  34. Eigen::Vector3d trans_matrix;
  35. TrackerSettings settings;
  36. CTracker tracker(settings);
  37. bool m_isTrackerInitialized = false;
  38. #include <random>
  39. ////////////////////用于nms////////////////////
  40. #include<opencv2/opencv.hpp>
  41. #define rad2Angle(rad) ((rad) * 180.0 / M_PI)
  42. const float smallinbig_threshold = 0.8;
  43. const float distance_threshold = 0.1;
  44. const float secondnms_threshold = 0.1;
  45. typedef struct {
  46. cv::RotatedRect box;
  47. std::vector<float> detection;
  48. std::vector<float> bbox_pts;
  49. int label;
  50. float score;
  51. }BBOX3D;
  52. //将检测结果转为RotatedRect以便于nms计算
  53. bool GetRotatedRect(std::vector<float> &out_detections,std::vector<int> &out_labels,
  54. std::vector<float> &out_scores, std::vector<BBOX3D> &results)
  55. {
  56. int obj_size = out_detections.size()/kOutputNumBoxFeature;
  57. if(out_labels.size()==obj_size && out_scores.size()==obj_size)
  58. {
  59. for(int i=0;i<obj_size;i++)
  60. {
  61. BBOX3D result;
  62. result.box = cv::RotatedRect(cv::Point2f(out_detections.at(i*7),out_detections.at(i*7+1)),
  63. cv::Size2f(out_detections.at(i*7+3),out_detections.at(i*7+4)),
  64. rad2Angle(out_detections.at(i*7+6)));
  65. for(int j=0;j<kOutputNumBoxFeature;j++)
  66. result.detection.push_back(out_detections.at(i*7+j));
  67. result.label = out_labels.at(i);
  68. result.score = out_scores.at(i);
  69. results.push_back(result);
  70. }
  71. return true;
  72. }
  73. else
  74. {
  75. std::cout<<"the size of detections labels scores is not equal !!!"<<std::endl;
  76. return false;
  77. }
  78. }
  79. bool sort_score(BBOX3D box1,BBOX3D box2)
  80. {
  81. return (box1.score > box2.score);
  82. }
  83. //计算两个旋转矩形的IOU
  84. float calcIOU(cv::RotatedRect rect1, cv::RotatedRect rect2)
  85. {
  86. float areaRect1 = rect1.size.width * rect1.size.height;
  87. float areaRect2 = rect2.size.width * rect2.size.height;
  88. vector<cv::Point2f> vertices;
  89. int intersectionType = cv::rotatedRectangleIntersection(rect1, rect2, vertices);
  90. if (vertices.size()==0)
  91. return 0.0;
  92. else{
  93. vector<cv::Point2f> order_pts;
  94. cv::convexHull(cv::Mat(vertices), order_pts, true);
  95. double area = cv::contourArea(order_pts);
  96. float inner = (float) (area / (areaRect1 + areaRect2 - area + 0.0001));
  97. //排除小框完全在大框里面的case
  98. float areaMin = (areaRect1 < areaRect2)?areaRect1:areaRect2;
  99. float innerMin = (float)(area / (areaMin + 0.0001));
  100. if(innerMin > smallinbig_threshold)
  101. inner = innerMin;
  102. return inner;
  103. }
  104. }
  105. //计算两个点的欧式距离
  106. float calcdistance(cv::Point2f center1, cv::Point2f center2)
  107. {
  108. float distance = sqrt((center1.x-center2.x)*(center1.x-center2.x)+
  109. (center1.y-center2.y)*(center1.y-center2.y));
  110. return distance;
  111. }
  112. //nms
  113. void nms(std::vector<BBOX3D> &vec_boxs,float threshold,std::vector<BBOX3D> &results)
  114. {
  115. std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
  116. while(vec_boxs.size() > 0)
  117. {
  118. results.push_back(vec_boxs[0]);
  119. vec_boxs.erase(vec_boxs.begin());
  120. for (auto it = vec_boxs.begin(); it != vec_boxs.end();)
  121. {
  122. float iou_value =calcIOU(results.back().box,(*it).box);
  123. float distance_value = calcdistance(results.back().box.center,(*it).box.center);
  124. if ((iou_value > threshold) || (distance_value<distance_threshold))
  125. it = vec_boxs.erase(it);
  126. else it++;
  127. }
  128. // std::cout<<"results: "<<results.back().detection.at(0)<<" "<<results.back().detection.at(1)<<
  129. // " "<<results.back().detection.at(2)<<std::endl;
  130. }
  131. }
  132. void GetLidarObj(std::vector<BBOX3D> &results,iv::lidar::objectarray & lidarobjvec)
  133. {
  134. int i;
  135. int obj_size = results.size();
  136. // givlog->verbose("OBJ","object size is %d",obj_size);;
  137. for(i=0;i<obj_size;i++)
  138. {
  139. iv::lidar::lidarobject lidarobj;
  140. if (results.at(i).score < 0.10) {
  141. std::cout<<"///////: "<<results.at(i).score<<std::endl;
  142. continue;
  143. }
  144. std::cout<<" label: "<<results.at(i).label<<" score: "<<results.at(i).score<<std::endl;
  145. if (results.at(i).label == 6) continue;
  146. vector<float>out_detection = results.at(i).detection;
  147. lidarobj.set_tyaw(out_detection.at(6));
  148. iv::lidar::PointXYZ centroid;
  149. iv::lidar::PointXYZ * _centroid;
  150. centroid.set_x(out_detection.at(0));
  151. centroid.set_y(out_detection.at(1));
  152. centroid.set_z(out_detection.at(2));
  153. _centroid = lidarobj.mutable_centroid();
  154. _centroid->CopyFrom(centroid);
  155. iv::lidar::PointXYZ min_point;
  156. iv::lidar::PointXYZ * _min_point;
  157. min_point.set_x(0);
  158. min_point.set_y(0);
  159. min_point.set_z(0);
  160. _min_point = lidarobj.mutable_min_point();
  161. _min_point->CopyFrom(min_point);
  162. iv::lidar::PointXYZ max_point;
  163. iv::lidar::PointXYZ * _max_point;
  164. max_point.set_x(0);
  165. max_point.set_y(0);
  166. max_point.set_z(0);
  167. _max_point = lidarobj.mutable_max_point();
  168. _max_point->CopyFrom(max_point);
  169. iv::lidar::PointXYZ position;
  170. iv::lidar::PointXYZ * _position;
  171. position.set_x(out_detection.at(0));
  172. position.set_y(out_detection.at(1));
  173. position.set_z(out_detection.at(2));
  174. _position = lidarobj.mutable_position();
  175. _position->CopyFrom(position);
  176. lidarobj.set_mntype(results.at(i).label);
  177. // std::cout<<" "<<position.x()<<", "<<position.y()<<" "<<results.at(i).label<<" "<<results.at(i).score<<std::endl;
  178. // label 2 5
  179. if(results.at(i).label==2){
  180. lidarobj.set_mntype(5);
  181. }else if(results.at(i).label==5){
  182. lidarobj.set_mntype(2);
  183. }
  184. lidarobj.set_score(results.at(i).score);
  185. lidarobj.add_type_probs(results.at(i).score);
  186. iv::lidar::PointXYZI point_cloud;
  187. iv::lidar::PointXYZI * _point_cloud;
  188. point_cloud.set_x(out_detection.at(0));
  189. point_cloud.set_y(out_detection.at(1));
  190. point_cloud.set_z(out_detection.at(2));
  191. point_cloud.set_i(results.at(i).label);
  192. _point_cloud = lidarobj.add_cloud();
  193. _point_cloud->CopyFrom(point_cloud);
  194. iv::lidar::Dimension ld;
  195. iv::lidar::Dimension * pld;
  196. ld.set_x(out_detection.at(3));
  197. ld.set_y(out_detection.at(4));
  198. ld.set_z(out_detection.at(5));
  199. pld = lidarobj.mutable_dimensions();
  200. pld->CopyFrom(ld);
  201. // std::cout<<"x y z: "<<out_detection.at(0)<<" "<<out_detection.at(1)<<" "<<
  202. // out_detection.at(2)<<" "<<out_detection.at(3)<<" "<< out_detection.at(4)<<" "
  203. // <<out_detection.at(5)<<" "<<out_detection.at(6)<<std::endl;
  204. iv::lidar::lidarobject * po = lidarobjvec.add_obj();
  205. po->CopyFrom(lidarobj);
  206. }
  207. // std::cout<<"the lidarobjvec: "<<lidarobjvec.obj_size()<<std::endl;
  208. }
  209. ////////////////////用于nms////////////////////
  210. ////////////////////用于获得3dbbox中点云个数////////////////////
  211. #if 0
  212. inline void lidar_to_local_coords_cpu(float shift_x, float shift_y, float rot_angle, float &local_x, float &local_y){
  213. float cosa = cos(-rot_angle), sina = sin(-rot_angle);
  214. local_x = shift_x * cosa + shift_y * (-sina);
  215. local_y = shift_x * sina + shift_y * cosa;
  216. }
  217. inline int check_pt_in_box3d_cpu(const float *pt, std::vector<float> &box3d, float &local_x, float &local_y){
  218. // param pt: (x, y, z)
  219. // param box3d: [x, y, z, dx, dy, dz, heading], (x, y, z) is the box center
  220. const float MARGIN = 1e-2;
  221. float x = pt[0], y = pt[1], z = pt[2];
  222. float cx = box3d[0], cy = box3d[1], cz = box3d[2];
  223. float dx = box3d[3], dy = box3d[4], dz = box3d[5], rz = box3d[6];
  224. if (fabsf(z - cz) > dz / 2.0) return 0;
  225. lidar_to_local_coords_cpu(x - cx, y - cy, rz, local_x, local_y);
  226. float in_flag = (fabs(local_x) < dx / 2.0 + MARGIN) & (fabs(local_y) < dy / 2.0 + MARGIN);
  227. return in_flag;
  228. }
  229. int points_in_boxes_cpu(std::vector<BBOX3D>& boxes, float* pts_lidar,
  230. int pts_num, std::vector<std::vector<int>>& pts_indices){
  231. std::vector<std::vector<float>> pts_bboxes;
  232. int boxes_num = boxes.size();
  233. float local_x = 0, local_y = 0;
  234. for (int i = 0; i < boxes_num; i++){
  235. std::vector<float>pts_bbox;
  236. for (int j = 0; j < pts_num; j++){
  237. int cur_in_flag = check_pt_in_box3d_cpu(pts_lidar + j * 5, boxes[i].detection, local_x, local_y);
  238. pts_indices[i][j] = cur_in_flag;
  239. if(cur_in_flag)
  240. {
  241. pts_bbox.push_back(pts_lidar[j*5+0]);
  242. pts_bbox.push_back(pts_lidar[j*5+1]);
  243. pts_bbox.push_back(pts_lidar[j*5+2]);
  244. pts_bbox.push_back(pts_lidar[j*5+3]);
  245. pts_bbox.push_back(pts_lidar[j*5+4]);
  246. }
  247. }
  248. pts_bboxes.push_back(pts_bbox);
  249. std::cout<<"the size of points: "<<i<<" : "<<pts_bbox.size() / 5 <<std::endl;
  250. pts_bbox.clear();
  251. }
  252. return 1;
  253. }
  254. #endif
  255. ////////////////////用于获得3dbbox中点云个数////////////////////
  256. void PclToArray(
  257. const pcl::PointCloud<pcl::PointXYZI>::Ptr& in_pcl_pc_ptr,
  258. float* out_points_array, const float normalizing_factor) {
  259. for (size_t i = 0; i < in_pcl_pc_ptr->size(); ++i) {
  260. pcl::PointXYZI point = in_pcl_pc_ptr->at(i);
  261. out_points_array[i * 4 + 0] = point.x;
  262. out_points_array[i * 4 + 1] = point.y;
  263. out_points_array[i * 4 + 2] = point.z;
  264. out_points_array[i * 4 + 3] =
  265. static_cast<float>(point.intensity / normalizing_factor);
  266. }
  267. }
  268. void PclXYZITToArray(
  269. const pcl::PointCloud<pcl::PointXYZI>::Ptr& in_pcl_pc_ptr,
  270. float* out_points_array, const float normalizing_factor) {
  271. /////shuffle the index array/////
  272. bool shuffle = true;
  273. int point_num = in_pcl_pc_ptr->size();
  274. std::vector<int>indices(point_num);
  275. std::iota(indices.begin(),indices.end(),0);
  276. if(shuffle)
  277. {
  278. // unsigned seed = 0;
  279. // std::shuffle(indices.begin(),indices.end(),std::default_random_engine(seed));
  280. std::random_device rd;
  281. std::mt19937 g(rd());
  282. std::shuffle(indices.begin(),indices.end(),g);
  283. }
  284. /////shuffle the index array/////
  285. for (size_t i = 0; i < in_pcl_pc_ptr->size(); ++i) {
  286. //pcl::PointXYZI point = in_pcl_pc_ptr->at(i);
  287. int indice = indices[i];
  288. pcl::PointXYZI point = in_pcl_pc_ptr->at(indice);
  289. Eigen::Vector3d new_point, old_point;
  290. old_point<<point.x, point.y, point.z;
  291. new_point = rotation_matrix * (old_point) + trans_matrix;
  292. out_points_array[i * 5 + 0] = new_point[0];
  293. out_points_array[i * 5 + 1] = new_point[1];
  294. out_points_array[i * 5 + 2] = new_point[2];
  295. out_points_array[i * 5 + 3] =
  296. static_cast<float>(point.intensity / normalizing_factor);
  297. out_points_array[i * 5 + 4] = 0;
  298. }
  299. }
  300. void GetLidarObj(std::vector<float> out_detections,std::vector<int> out_labels,
  301. std::vector<float> out_scores,iv::lidar::objectarray & lidarobjvec)
  302. {
  303. int i;
  304. int obj_size = out_detections.size()/kOutputNumBoxFeature;
  305. // givlog->verbose("OBJ","object size is %d",obj_size);
  306. for(i=0;i<obj_size;i++)
  307. {
  308. iv::lidar::lidarobject lidarobj;
  309. if (out_scores.at(i) < 0.10) continue;
  310. lidarobj.set_tyaw(out_detections.at(i*7+6));
  311. iv::lidar::PointXYZ centroid;
  312. iv::lidar::PointXYZ * _centroid;
  313. centroid.set_x(out_detections.at(i*7));
  314. centroid.set_y(out_detections.at(i*7+1));
  315. centroid.set_z(out_detections.at(i*7+2));
  316. _centroid = lidarobj.mutable_centroid();
  317. _centroid->CopyFrom(centroid);
  318. iv::lidar::PointXYZ min_point;
  319. iv::lidar::PointXYZ * _min_point;
  320. min_point.set_x(0);
  321. min_point.set_y(0);
  322. min_point.set_z(0);
  323. _min_point = lidarobj.mutable_min_point();
  324. _min_point->CopyFrom(min_point);
  325. iv::lidar::PointXYZ max_point;
  326. iv::lidar::PointXYZ * _max_point;
  327. max_point.set_x(0);
  328. max_point.set_y(0);
  329. max_point.set_z(0);
  330. _max_point = lidarobj.mutable_max_point();
  331. _max_point->CopyFrom(max_point);
  332. iv::lidar::PointXYZ position;
  333. iv::lidar::PointXYZ * _position;
  334. position.set_x(out_detections.at(i*7));
  335. position.set_y(out_detections.at(i*7+1));
  336. position.set_z(out_detections.at(i*7+2));
  337. _position = lidarobj.mutable_position();
  338. _position->CopyFrom(position);
  339. lidarobj.set_mntype(out_labels.at(i));
  340. std::cout<<" "<<position.y()<<" "<<out_labels.at(i)<<std::endl;
  341. // label 2 5
  342. if(out_labels.at(i)==2){
  343. lidarobj.set_mntype(5);
  344. }else if(out_labels.at(i)==5){
  345. lidarobj.set_mntype(2);
  346. }
  347. lidarobj.set_score(out_scores.at(i));
  348. lidarobj.add_type_probs(out_scores.at(i));
  349. iv::lidar::PointXYZI point_cloud;
  350. iv::lidar::PointXYZI * _point_cloud;
  351. point_cloud.set_x(out_detections.at(i*7));
  352. point_cloud.set_y(out_detections.at(i*7+1));
  353. point_cloud.set_z(out_detections.at(i*7+2));
  354. point_cloud.set_i(0);
  355. _point_cloud = lidarobj.add_cloud();
  356. _point_cloud->CopyFrom(point_cloud);
  357. iv::lidar::Dimension ld;
  358. iv::lidar::Dimension * pld;
  359. ld.set_x(out_detections.at(i*7+3));// w
  360. ld.set_y(out_detections.at(i*7+4));// l
  361. ld.set_z(out_detections.at(i*7+5));// h
  362. pld = lidarobj.mutable_dimensions();
  363. pld->CopyFrom(ld);
  364. // std::cout<<"x y z : "<<out_detections.at(i*7+3)<<" "<< out_detections.at(i*7+4)<<" "<<out_detections.at(i*7+5)<<std::endl;
  365. iv::lidar::lidarobject * po = lidarobjvec.add_obj();
  366. po->CopyFrom(lidarobj);
  367. }
  368. }
  369. void DectectOnePCD(const pcl::PointCloud<pcl::PointXYZI>::Ptr &pc_ptr)
  370. {
  371. std::shared_ptr<float> points_array_ptr = std::shared_ptr<float>(new float[pc_ptr->size() * kNumPointFeature]);
  372. // float* points_array = new float[pc_ptr->size() * kNumPointFeature];
  373. PclXYZITToArray(pc_ptr, points_array_ptr.get(), 1.0);
  374. int in_num_points = pc_ptr->width;
  375. std::vector<float> out_detections;
  376. std::vector<int> out_labels;
  377. std::vector<float> out_scores;
  378. QTime xTime;
  379. xTime.start();
  380. auto startTime = std::chrono::high_resolution_clock::now();
  381. cudaDeviceSynchronize();
  382. pPillars->DoInference(points_array_ptr.get(), in_num_points, &out_detections, &out_labels , &out_scores);
  383. cudaDeviceSynchronize();
  384. auto endTime = std::chrono::high_resolution_clock::now();
  385. double inferenceDuration = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count()/1000000.0;
  386. // std::cout<< "inferenceDuration Time: " << inferenceDuration << " ms"<< std::endl;
  387. // int BoxFeature = 7;
  388. // int num_objects = out_detections.size() / BoxFeature;
  389. // givlog->verbose("obj size is %d", num_objects);
  390. // std::cout<<"obj size is "<<num_objects<<std::endl;
  391. // iv::lidar::objectarray lidarobjvec;
  392. // GetLidarObj(out_detections,out_labels,out_scores,lidarobjvec);
  393. //////////nms//////////
  394. //startTime = std::chrono::high_resolution_clock::now();
  395. std::vector<BBOX3D>results_rect;
  396. GetRotatedRect(out_detections,out_labels,out_scores,results_rect);
  397. // std::cout<<"results_rect size: "<<results_rect.size()<<std::endl;
  398. std::vector<BBOX3D>results_bbox;
  399. nms(results_rect,secondnms_threshold,results_bbox);
  400. // std::cout<<"results_bbox size: "<<results_bbox.size()<<std::endl;
  401. //get lidar points in 3Dbbox in cpu
  402. // startTime = std::chrono::high_resolution_clock::now();
  403. // //get lidar points in 3Dbbox
  404. // int boxes_num = results_bbox.size();
  405. // std::vector<std::vector<int>>pts_indices(boxes_num, vector<int>(in_num_points, 0));
  406. // points_in_boxes_cpu(results_bbox,points_array_ptr.get(),in_num_points,pts_indices);
  407. // endTime = std::chrono::high_resolution_clock::now();
  408. // double nmsDuration = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count()/1000000.0;
  409. // std::cout <<"3DBoxDuration Time : "<<nmsDuration<< endl;
  410. //get lidar points in 3Dbbox in gpu
  411. startTime = std::chrono::high_resolution_clock::now();
  412. float* dev_points;
  413. GPU_CHECK(cudaMalloc(reinterpret_cast<void**>(&dev_points),
  414. in_num_points * 5 * sizeof(float))); // in_num_points , 5
  415. GPU_CHECK(cudaMemset(dev_points, 0, in_num_points * 5 * sizeof(float)));
  416. GPU_CHECK(cudaMemcpy(dev_points, points_array_ptr.get(),
  417. in_num_points * 5 * sizeof(float),
  418. cudaMemcpyHostToDevice));
  419. int* box_idx_of_points_gpu;
  420. GPU_CHECK(cudaMalloc(reinterpret_cast<void**>(&box_idx_of_points_gpu),
  421. in_num_points * sizeof(int))); // in_num_points , 5
  422. GPU_CHECK(cudaMemset(box_idx_of_points_gpu, -1, in_num_points * sizeof(float)));
  423. int boxes_num = results_bbox.size();
  424. float *boxes_cpu = new float[boxes_num*7];
  425. for(int i=0;i<boxes_num;i++)
  426. {
  427. for(int j=0;j<7;j++)
  428. *(boxes_cpu + (i*7+j)) = results_bbox[i].detection[j];
  429. }
  430. float *boxes_gpu;
  431. GPU_CHECK(cudaMalloc(reinterpret_cast<void**>(&boxes_gpu),
  432. boxes_num * 7 * sizeof(float))); // in_num_points , 5
  433. GPU_CHECK(cudaMemset(boxes_gpu, 0, boxes_num * 7 * sizeof(float)));
  434. GPU_CHECK(cudaMemcpy(boxes_gpu, boxes_cpu,boxes_num * 7 * sizeof(float),
  435. cudaMemcpyHostToDevice));
  436. int batch_size = 1;
  437. points_in_boxes_launcher(batch_size,boxes_num,in_num_points,boxes_gpu,dev_points,box_idx_of_points_gpu);
  438. int* box_idx_of_points_cpu = new int[in_num_points]();
  439. cudaMemcpy(box_idx_of_points_cpu, box_idx_of_points_gpu, in_num_points * sizeof(int), cudaMemcpyDeviceToHost);
  440. //vector<int> box_idx_of_points(box_idx_of_points_cpu,box_idx_of_points_cpu+in_num_points);
  441. //cv::Mat image=cv::Mat::zeros(1200,1200,CV_8UC3);
  442. //存储bbox的点云
  443. for(int i=0; i < in_num_points; i++)
  444. {
  445. for(int j=0; j<boxes_num; j++)
  446. {
  447. if (box_idx_of_points_cpu[i] == j)
  448. {
  449. for(int idx=0; idx<5; idx++)
  450. results_bbox[j].bbox_pts.push_back(points_array_ptr.get()[i*5+idx]);
  451. // int x = int(points_array_ptr.get()[i*5]*10+600);
  452. // int y = int(points_array_ptr.get()[i*5+1]*10+600);
  453. // cv::circle(image,cv::Point(x,y),2,cv::Scalar(0,0,255));
  454. }
  455. }
  456. }
  457. // for(int j=0; j<boxes_num; j++)
  458. // {
  459. // std::cout<<"num points in bbox: "<<results_bbox[j].bbox_pts.size()/5<<std::endl;
  460. // }
  461. endTime = std::chrono::high_resolution_clock::now();
  462. double gpuDuration = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count()/1000000.0;
  463. // std::cout <<"3DBoxDuration_gpu Time : "<<gpuDuration<< endl;
  464. //endTime = std::chrono::high_resolution_clock::now();
  465. //double nmsDuration = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count()/1000000.0;
  466. //std::cout <<"nmsDuration Time : "<<(double)(ends - start)/ CLOCKS_PER_SEC*1000 << endl;
  467. iv::lidar::objectarray lidarobjvec;
  468. GetLidarObj(results_bbox,lidarobjvec);
  469. //////////nms//////////
  470. double timex = pc_ptr->header.stamp;
  471. timex = timex/1000.0;
  472. lidarobjvec.set_timestamp(pc_ptr->header.stamp);
  473. //--------------------------------------------- init tracker -------------------------------------------------
  474. // if (!m_isTrackerInitialized)
  475. // {
  476. // m_isTrackerInitialized = InitTracker(tracker);
  477. // if (!m_isTrackerInitialized)
  478. // {
  479. // std::cerr << "Tracker initialize error!!!" << std::endl;
  480. // }
  481. // }
  482. // iv::lidar::objectarray trackedobjvec = Tracking(lidarobjvec, tracker);
  483. //-------------------------------------------- end tracking --------------------------------------------------
  484. int ntlen;
  485. std::string out = lidarobjvec.SerializeAsString();
  486. iv::modulecomm::ModuleSendMsg(gpdetect,out.data(),out.length());
  487. delete box_idx_of_points_cpu;
  488. GPU_CHECK(cudaFree(dev_points));
  489. GPU_CHECK(cudaFree(box_idx_of_points_gpu));
  490. GPU_CHECK(cudaFree(boxes_gpu));
  491. // givlog->verbose("lenth is %d",out.length());
  492. }
  493. void ListenPointCloud(const char *strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  494. {
  495. // std::cout<<" is ok ------------ "<<std::endl;
  496. if(nSize <=16)return;
  497. unsigned int * pHeadSize = (unsigned int *)strdata;
  498. if(*pHeadSize > nSize)
  499. {
  500. givlog->verbose("ListenPointCloud data is small headsize = %d, data size is %d", *pHeadSize, nSize);
  501. std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
  502. }
  503. gnothavedatatime = 0;
  504. QTime xTime;
  505. xTime.start();
  506. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  507. new pcl::PointCloud<pcl::PointXYZI>());
  508. int nNameSize;
  509. nNameSize = *pHeadSize - 4-4-8;
  510. char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
  511. std::shared_ptr<char> str_ptr;
  512. str_ptr.reset(strName);
  513. memcpy(strName,(char *)((char *)strdata +4),nNameSize);
  514. point_cloud->header.frame_id = strName;
  515. memcpy(&point_cloud->header.seq,(char *)strdata+4+nNameSize,4);
  516. memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
  517. int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
  518. int i;
  519. pcl::PointXYZI * p;
  520. p = (pcl::PointXYZI *)((char *)strdata + *pHeadSize);
  521. for(i=0;i<nPCount;i++)
  522. {
  523. pcl::PointXYZI xp;
  524. memcpy(&xp,p,sizeof(pcl::PointXYZI));
  525. xp.z = xp.z;p++;
  526. if (((abs(xp.x)<0.5)&&(xp.y<0.5 && xp.y > -1.0)) || xp.z > 3.0 ) continue;
  527. point_cloud->push_back(xp);
  528. }
  529. DectectOnePCD(point_cloud);
  530. std::cout<<"time is "<<(QDateTime::currentMSecsSinceEpoch() % 1000)<<" "<<xTime.elapsed()<<std::endl;
  531. gfault->SetFaultState(0, 0, "ok");
  532. }
  533. bool gbstate = true;
  534. void statethread()
  535. {
  536. int nstate = 0;
  537. int nlaststate = 0;
  538. while (gbstate)
  539. {
  540. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  541. if(gnothavedatatime < 100000) gnothavedatatime++;
  542. if (gnothavedatatime < 100){
  543. nstate = 0;
  544. }
  545. if (gnothavedatatime > 1000)
  546. {
  547. nstate = 1;
  548. }
  549. if (gnothavedatatime > 6000)
  550. {
  551. nstate = 2;
  552. }
  553. if (nstate != nlaststate) {
  554. switch (nstate) {
  555. case 0:
  556. givlog->info("detection_lidar_pointpillar is ok");
  557. gfault->SetFaultState(0,0,"data is ok.");
  558. break;
  559. case 1:
  560. givlog->info(" more than 10 seconds not have lidar pointcloud.");
  561. gfault->SetFaultState(1,1,"more than 10 seconds not have lidar pointcloud.");
  562. break;
  563. case 2:
  564. givlog->info(" more than 60 seconds not have lidar pointcloud.");
  565. gfault->SetFaultState(2,2, "more than 60 seconds not have lidar pointcloud.");
  566. break;
  567. default:
  568. break;
  569. }
  570. }
  571. }
  572. }
  573. void exitfunc()
  574. {
  575. gbstate = false;
  576. gpthread->join();
  577. std::cout<<" state thread closed."<<std::endl;
  578. iv::modulecomm::Unregister(gpa);
  579. iv::modulecomm::Unregister(gpdetect);
  580. std::cout<<"exit func complete"<<std::endl;
  581. }
  582. #include <QFile>
  583. bool trtisexist(std::string strpfe,std::string strbackbone)
  584. {
  585. QFile xFile;
  586. xFile.setFileName(strpfe.data());
  587. if(xFile.exists() == false)
  588. {
  589. return false;
  590. }
  591. xFile.setFileName(strbackbone.data());
  592. if(xFile.exists() == false)
  593. {
  594. return false;
  595. }
  596. return true;
  597. }
  598. int main(int argc, char *argv[])
  599. {
  600. QCoreApplication a(argc, argv);
  601. // RegisterIVBackTrace();
  602. tracker.setSettings(settings);
  603. gfault = new iv::Ivfault("lidar_pointpillar");
  604. givlog = new iv::Ivlog("lidar_pointpillar");
  605. gfault->SetFaultState(0,0,"pointpillar initialize. ");
  606. char * strhome = getenv("HOME");
  607. std::string pfe_file = strhome;
  608. pfe_file += "/models/lidar/cbgs_pp_multihead_pfe.onnx";
  609. std::string pfe_trt_file = pfe_file.substr(0, pfe_file.find(".")) + ".trt";
  610. std::string backbone_file = strhome;
  611. backbone_file += "/models/lidar/cbgs_pp_multihead_backbone.onnx";
  612. std::string backbone_trt_file = backbone_file.substr(0, backbone_file.find(".")) + ".trt";
  613. bool btrtexist = trtisexist(pfe_trt_file,backbone_trt_file);
  614. QString strpath = QCoreApplication::applicationDirPath();
  615. std::string pp_config = strpath.toStdString() ;
  616. pp_config += "/cfgs/cbgs_pp_multihead.yaml";
  617. if (argc < 2)
  618. strpath = strpath + "/detection_lidar_pointpillar.xml";
  619. else
  620. strpath = argv[1];
  621. std::cout<<pp_config<<std::endl;
  622. iv::xmlparam::Xmlparam xparam(strpath.toStdString());
  623. pfe_file = xparam.GetParam("pfe_file",pfe_file.data());
  624. backbone_file = xparam.GetParam("backbone_file",backbone_file.data());
  625. gstrinput = xparam.GetParam("input","lidar_pc");
  626. gstroutput = xparam.GetParam("output","lidar_pointpillar");
  627. if(btrtexist == false)
  628. {
  629. std::cout<<"use onnx model."<<std::endl;
  630. pPillars = new PointPillars(
  631. 0.15,
  632. 0.10,
  633. true,
  634. pfe_file,
  635. backbone_file,
  636. pp_config
  637. );
  638. }
  639. else
  640. {
  641. std::cout<<"use trt model."<<std::endl;
  642. pPillars = new PointPillars(
  643. 0.17,
  644. 0.10,
  645. false,
  646. pfe_trt_file,
  647. backbone_trt_file,
  648. pp_config
  649. );
  650. }
  651. std::cout<<"PointPillars Init OK."<<std::endl;
  652. Eigen::AngleAxisd r_z ( 0.37/180.0*M_PI, Eigen::Vector3d ( 0,0,1 ) ); //沿 Z 轴旋转 yaw +
  653. Eigen::AngleAxisd r_y ( 7.45/180.0*M_PI, Eigen::Vector3d ( 0,1,0 ) ); //沿 Y 轴旋转 roll +
  654. Eigen::AngleAxisd r_x ( -2.16/180.0*M_PI, Eigen::Vector3d ( 1,0,0 ) ); //沿 X 轴旋转 pitch -
  655. Eigen::Quaterniond q_zyx = r_z*r_y*r_x; //ZYX旋转顺序(绕旋转后的轴接着旋转)
  656. // 四元数-->>旋转矩阵
  657. rotation_matrix = q_zyx.toRotationMatrix();
  658. trans_matrix << 0, 1.04, 0.35;//x,y,z
  659. gpa = iv::modulecomm::RegisterRecv(gstrinput.data(),ListenPointCloud);
  660. gpdetect = iv::modulecomm::RegisterSend(gstroutput.data(), 10000000,1);
  661. gpthread = new std::thread(statethread);
  662. iv::ivexit::RegIVExitCall(exitfunc);
  663. return a.exec();
  664. }