main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. iv::Ivfault *gfault = nullptr;
  21. iv::Ivlog *givlog = nullptr;
  22. std::thread * gpthread;
  23. PointPillars * pPillars = nullptr ;
  24. void * gpa;
  25. void * gpdetect;
  26. int gnothavedatatime = 0;
  27. const int kNumPointFeature = 5;
  28. const int kOutputNumBoxFeature = 7;
  29. std::string gstrinput;
  30. std::string gstroutput;
  31. TrackerSettings settings;
  32. CTracker tracker(settings);
  33. bool m_isTrackerInitialized = false;
  34. void PclToArray(
  35. const pcl::PointCloud<pcl::PointXYZI>::Ptr& in_pcl_pc_ptr,
  36. float* out_points_array, const float normalizing_factor) {
  37. for (size_t i = 0; i < in_pcl_pc_ptr->size(); ++i) {
  38. pcl::PointXYZI point = in_pcl_pc_ptr->at(i);
  39. out_points_array[i * 4 + 0] = point.x;
  40. out_points_array[i * 4 + 1] = point.y;
  41. out_points_array[i * 4 + 2] = point.z;
  42. out_points_array[i * 4 + 3] =
  43. static_cast<float>(point.intensity / normalizing_factor);
  44. }
  45. }
  46. void PclXYZITToArray(
  47. const pcl::PointCloud<pcl::PointXYZI>::Ptr& in_pcl_pc_ptr,
  48. float* out_points_array, const float normalizing_factor) {
  49. for (size_t i = 0; i < in_pcl_pc_ptr->size(); ++i) {
  50. pcl::PointXYZI point = in_pcl_pc_ptr->at(i);
  51. out_points_array[i * 5 + 0] = point.x;
  52. out_points_array[i * 5 + 1] = point.y;
  53. out_points_array[i * 5 + 2] = point.z;
  54. out_points_array[i * 5 + 3] =
  55. static_cast<float>(point.intensity / normalizing_factor);
  56. out_points_array[i * 5 + 4] = 0;
  57. }
  58. }
  59. void GetLidarObj(std::vector<float> out_detections,std::vector<int> out_labels,
  60. std::vector<float> out_scores,iv::lidar::objectarray & lidarobjvec)
  61. {
  62. int i;
  63. int obj_size = out_detections.size()/kOutputNumBoxFeature;
  64. // givlog->verbose("OBJ","object size is %d",obj_size);
  65. for(i=0;i<obj_size;i++)
  66. {
  67. iv::lidar::lidarobject lidarobj;
  68. if (out_scores.at(i) < 0.10) continue;
  69. lidarobj.set_tyaw(out_detections.at(i*7+6));
  70. iv::lidar::PointXYZ centroid;
  71. iv::lidar::PointXYZ * _centroid;
  72. centroid.set_x(out_detections.at(i*7));
  73. centroid.set_y(out_detections.at(i*7+1));
  74. centroid.set_z(out_detections.at(i*7+2));
  75. _centroid = lidarobj.mutable_centroid();
  76. _centroid->CopyFrom(centroid);
  77. iv::lidar::PointXYZ min_point;
  78. iv::lidar::PointXYZ * _min_point;
  79. min_point.set_x(0);
  80. min_point.set_y(0);
  81. min_point.set_z(0);
  82. _min_point = lidarobj.mutable_min_point();
  83. _min_point->CopyFrom(min_point);
  84. iv::lidar::PointXYZ max_point;
  85. iv::lidar::PointXYZ * _max_point;
  86. max_point.set_x(0);
  87. max_point.set_y(0);
  88. max_point.set_z(0);
  89. _max_point = lidarobj.mutable_max_point();
  90. _max_point->CopyFrom(max_point);
  91. iv::lidar::PointXYZ position;
  92. iv::lidar::PointXYZ * _position;
  93. position.set_x(out_detections.at(i*7));
  94. position.set_y(out_detections.at(i*7+1));
  95. position.set_z(out_detections.at(i*7+2));
  96. _position = lidarobj.mutable_position();
  97. _position->CopyFrom(position);
  98. lidarobj.set_mntype(out_labels.at(i));
  99. // label 2 8
  100. if(out_labels.at(i)==2){
  101. lidarobj.set_mntype(8);
  102. }else if(out_labels.at(i)==8){
  103. lidarobj.set_mntype(2);
  104. }
  105. lidarobj.set_score(out_scores.at(i));
  106. lidarobj.add_type_probs(out_scores.at(i));
  107. iv::lidar::PointXYZI point_cloud;
  108. iv::lidar::PointXYZI * _point_cloud;
  109. point_cloud.set_x(out_detections.at(i*7));
  110. point_cloud.set_y(out_detections.at(i*7+1));
  111. point_cloud.set_z(out_detections.at(i*7+2));
  112. point_cloud.set_i(0);
  113. _point_cloud = lidarobj.add_cloud();
  114. _point_cloud->CopyFrom(point_cloud);
  115. iv::lidar::Dimension ld;
  116. iv::lidar::Dimension * pld;
  117. ld.set_x(out_detections.at(i*7+3));// w
  118. ld.set_y(out_detections.at(i*7+4));// l
  119. ld.set_z(out_detections.at(i*7+5));// h
  120. pld = lidarobj.mutable_dimensions();
  121. pld->CopyFrom(ld);
  122. // 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;
  123. iv::lidar::lidarobject * po = lidarobjvec.add_obj();
  124. po->CopyFrom(lidarobj);
  125. }
  126. }
  127. void DectectOnePCD(const pcl::PointCloud<pcl::PointXYZI>::Ptr &pc_ptr)
  128. {
  129. std::shared_ptr<float> points_array_ptr = std::shared_ptr<float>(new float[pc_ptr->size() * kNumPointFeature]);
  130. // float* points_array = new float[pc_ptr->size() * kNumPointFeature];
  131. PclXYZITToArray(pc_ptr, points_array_ptr.get(), 1.0);
  132. int in_num_points = pc_ptr->width;
  133. std::vector<float> out_detections;
  134. std::vector<int> out_labels;
  135. std::vector<float> out_scores;
  136. QTime xTime;
  137. xTime.start();
  138. cudaDeviceSynchronize();
  139. pPillars->DoInference(points_array_ptr.get(), in_num_points, &out_detections, &out_labels , &out_scores);
  140. cudaDeviceSynchronize();
  141. // givlog->verbose("obj size is %d", num_objects);
  142. // std::cout<<"obj size is "<<num_objects<<std::endl;
  143. // std::vector<iv::lidar::lidarobject> lidarobjvec;
  144. iv::lidar::objectarray lidarobjvec;
  145. GetLidarObj(out_detections,out_labels,out_scores,lidarobjvec);
  146. double timex = pc_ptr->header.stamp;
  147. timex = timex/1000.0;
  148. lidarobjvec.set_timestamp(pc_ptr->header.stamp);
  149. //--------------------------------------------- init tracker -------------------------------------------------
  150. if (!m_isTrackerInitialized)
  151. {
  152. m_isTrackerInitialized = InitTracker(tracker);
  153. if (!m_isTrackerInitialized)
  154. {
  155. std::cerr << "Tracker initialize error!!!" << std::endl;
  156. }
  157. }
  158. iv::lidar::objectarray trackedobjvec = Tracking(lidarobjvec, tracker);
  159. // std::<<"track end"<<std::endl;
  160. // -------------------------------------------- end tracking --------------------------------------------------
  161. int ntlen;
  162. std::string out = trackedobjvec.SerializeAsString();
  163. iv::modulecomm::ModuleSendMsg(gpdetect,out.data(),out.length());
  164. // givlog->verbose("lenth is %d",out.length());
  165. }
  166. void ListenPointCloud(const char *strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  167. {
  168. // std::cout<<" is ok ------------ "<<std::endl;
  169. if(nSize <=16)return;
  170. unsigned int * pHeadSize = (unsigned int *)strdata;
  171. if(*pHeadSize > nSize)
  172. {
  173. givlog->verbose("ListenPointCloud data is small headsize = %d, data size is %d", *pHeadSize, nSize);
  174. std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
  175. }
  176. gnothavedatatime = 0;
  177. QTime xTime;
  178. xTime.start();
  179. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  180. new pcl::PointCloud<pcl::PointXYZI>());
  181. int nNameSize;
  182. nNameSize = *pHeadSize - 4-4-8;
  183. char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
  184. std::shared_ptr<char> str_ptr;
  185. str_ptr.reset(strName);
  186. memcpy(strName,(char *)((char *)strdata +4),nNameSize);
  187. point_cloud->header.frame_id = strName;
  188. memcpy(&point_cloud->header.seq,(char *)strdata+4+nNameSize,4);
  189. memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
  190. int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
  191. int i;
  192. pcl::PointXYZI * p;
  193. p = (pcl::PointXYZI *)((char *)strdata + *pHeadSize);
  194. for(i=0;i<nPCount;i++)
  195. {
  196. pcl::PointXYZI xp;
  197. memcpy(&xp,p,sizeof(pcl::PointXYZI));
  198. xp.z = xp.z;
  199. point_cloud->push_back(xp);
  200. p++;
  201. }
  202. DectectOnePCD(point_cloud);
  203. std::cout<<"time is "<<(QDateTime::currentMSecsSinceEpoch() % 1000)<<" "<<xTime.elapsed()<<std::endl;
  204. gfault->SetFaultState(0, 0, "ok");
  205. }
  206. bool gbstate = true;
  207. void statethread()
  208. {
  209. int nstate = 0;
  210. int nlaststate = 0;
  211. while (gbstate)
  212. {
  213. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  214. if(gnothavedatatime < 100000) gnothavedatatime++;
  215. if (gnothavedatatime < 100){
  216. nstate = 0;
  217. }
  218. if (gnothavedatatime > 1000)
  219. {
  220. nstate = 1;
  221. }
  222. if (gnothavedatatime > 6000)
  223. {
  224. nstate = 2;
  225. }
  226. if (nstate != nlaststate) {
  227. switch (nstate) {
  228. case 0:
  229. givlog->info("detection_lidar_pointpillar is ok");
  230. gfault->SetFaultState(0,0,"data is ok.");
  231. break;
  232. case 1:
  233. givlog->info(" more than 10 seconds not have lidar pointcloud.");
  234. gfault->SetFaultState(1,1,"more than 10 seconds not have lidar pointcloud.");
  235. break;
  236. case 2:
  237. givlog->info(" more than 60 seconds not have lidar pointcloud.");
  238. gfault->SetFaultState(2,2, "more than 60 seconds not have lidar pointcloud.");
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. void exitfunc()
  247. {
  248. gbstate = false;
  249. gpthread->join();
  250. std::cout<<" state thread closed."<<std::endl;
  251. iv::modulecomm::Unregister(gpa);
  252. iv::modulecomm::Unregister(gpdetect);
  253. std::cout<<"exit func complete"<<std::endl;
  254. }
  255. #include <QFile>
  256. bool trtisexist(std::string strpfe,std::string strbackbone)
  257. {
  258. QFile xFile;
  259. xFile.setFileName(strpfe.data());
  260. if(xFile.exists() == false)
  261. {
  262. return false;
  263. }
  264. xFile.setFileName(strbackbone.data());
  265. if(xFile.exists() == false)
  266. {
  267. return false;
  268. }
  269. return true;
  270. }
  271. int main(int argc, char *argv[])
  272. {
  273. QCoreApplication a(argc, argv);
  274. // RegisterIVBackTrace();
  275. tracker.setSettings(settings);
  276. gfault = new iv::Ivfault("lidar_pointpillar");
  277. givlog = new iv::Ivlog("lidar_pointpillar");
  278. gfault->SetFaultState(0,0,"pointpillar initialize. ");
  279. char * strhome = getenv("HOME");
  280. std::string pfe_file = strhome;
  281. pfe_file += "/models/lidar/cbgs_pp_multihead_pfe.onnx";
  282. std::string pfe_trt_file = pfe_file.substr(0, pfe_file.find(".")) + ".trt";
  283. std::string backbone_file = strhome;
  284. backbone_file += "/models/lidar/cbgs_pp_multihead_backbone.onnx";
  285. std::string backbone_trt_file = backbone_file.substr(0, backbone_file.find(".")) + ".trt";
  286. bool btrtexist = trtisexist(pfe_trt_file,backbone_trt_file);
  287. QString strpath = QCoreApplication::applicationDirPath();
  288. std::string pp_config = strpath.toStdString() ;
  289. pp_config += "/cfgs/cbgs_pp_multihead.yaml";
  290. if (argc < 2)
  291. strpath = strpath + "/detection_lidar_pointpillar.xml";
  292. else
  293. strpath = argv[1];
  294. std::cout<<pp_config<<std::endl;
  295. iv::xmlparam::Xmlparam xparam(strpath.toStdString());
  296. pfe_file = xparam.GetParam("pfe_file",pfe_file.data());
  297. backbone_file = xparam.GetParam("backbone_file",backbone_file.data());
  298. gstrinput = xparam.GetParam("input","lidar_pc");
  299. gstroutput = xparam.GetParam("output","lidar_pointpillar");
  300. if(btrtexist == false)
  301. {
  302. std::cout<<"use onnx model."<<std::endl;
  303. pPillars = new PointPillars(
  304. 0.4,
  305. 0.2,
  306. true,
  307. pfe_file,
  308. backbone_file,
  309. pp_config
  310. );
  311. }
  312. else
  313. {
  314. std::cout<<"use trt model."<<std::endl;
  315. pPillars = new PointPillars(
  316. 0.1,
  317. 0.2,
  318. false,
  319. pfe_trt_file,
  320. backbone_trt_file,
  321. pp_config
  322. );
  323. }
  324. std::cout<<"PointPillars Init OK."<<std::endl;
  325. gpa = iv::modulecomm::RegisterRecv(gstrinput.data(),ListenPointCloud);
  326. gpdetect = iv::modulecomm::RegisterSend(gstroutput.data(), 10000000,1);
  327. gpthread = new std::thread(statethread);
  328. iv::ivexit::RegIVExitCall(exitfunc);
  329. return a.exec();
  330. }