main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include <QCoreApplication>
  2. #include <pcl/io/pcd_io.h>
  3. #include "cnn_segmentation.h"
  4. //#include "detect_lidar_type.h"
  5. #include "object.pb.h"
  6. #include "objectarray.pb.h"
  7. #include "modulecomm.h"
  8. #include "xmlparam.h"
  9. #include "ivfault.h"
  10. #include "ivlog.h"
  11. #include "ivexit.h"
  12. #include "ivversion.h"
  13. #include <thread>
  14. //#include "alog.h"
  15. CNNSegmentation gcnn;
  16. void * gpa;
  17. void * gpdetect;
  18. iv::Ivfault *gfault = nullptr;
  19. iv::Ivlog *givlog = nullptr;
  20. std::thread * gpthread;
  21. std::string gstrinput;
  22. std::string gstroutput;
  23. static iv::lidar::PointXYZ pcltoprotopoint(pcl::PointXYZ x)
  24. {
  25. iv::lidar::PointXYZ lx;
  26. lx.set_x(x.x);
  27. lx.set_y(x.y);
  28. lx.set_z(x.z);
  29. return lx;
  30. }
  31. static void GetLidarObj(std::vector<Obstacle> objvec,iv::lidar::objectarray & lidarobjvec)
  32. {
  33. int i;
  34. givlog->verbose("CNNOBJ","object size is %d",objvec.size());
  35. for(i=0;i<objvec.size();i++)
  36. {
  37. Obstacle x;
  38. iv::lidar::lidarobject lidarobj;
  39. // iv::lidar::object xo;
  40. x = objvec.at(i);
  41. pcl::PointCloud<pcl::PointXYZI> in_cluster = *x.cloud_ptr;
  42. float min_x = std::numeric_limits<float>::max();
  43. float max_x = -std::numeric_limits<float>::max();
  44. float min_y = std::numeric_limits<float>::max();
  45. float max_y = -std::numeric_limits<float>::max();
  46. float min_z = std::numeric_limits<float>::max();
  47. float max_z = -std::numeric_limits<float>::max();
  48. float average_x = 0, average_y = 0, average_z = 0;
  49. float length, width, height;
  50. pcl::PointXYZ min_point;
  51. pcl::PointXYZ max_point;
  52. // pcl::PointXYZ average_point;
  53. pcl::PointXYZ centroid;
  54. centroid.x = 0;
  55. centroid.y = 0;
  56. centroid.z = 0;
  57. for (auto pit = in_cluster.points.begin(); pit != in_cluster.points.end(); ++pit)
  58. {
  59. average_x += pit->x;
  60. average_y += pit->y;
  61. average_z += pit->z;
  62. centroid.x += pit->x;
  63. centroid.y += pit->y;
  64. centroid.z += pit->z;
  65. if (pit->x < min_x)
  66. min_x = pit->x;
  67. if (pit->y < min_y)
  68. min_y = pit->y;
  69. if (pit->z < min_z)
  70. min_z = pit->z;
  71. if (pit->x > max_x)
  72. max_x = pit->x;
  73. if (pit->y > max_y)
  74. max_y = pit->y;
  75. if (pit->z > max_z)
  76. max_z = pit->z;
  77. }
  78. // min, max points
  79. min_point.x = min_x;
  80. min_point.y = min_y;
  81. min_point.z = min_z;
  82. max_point.x = max_x;
  83. max_point.y = max_y;
  84. max_point.z = max_z;
  85. if (in_cluster.points.size() > 0)
  86. {
  87. centroid.x /= in_cluster.points.size();
  88. centroid.y /= in_cluster.points.size();
  89. centroid.z /= in_cluster.points.size();
  90. }
  91. length = max_point.x - min_point.x;
  92. width = max_point.y - min_point.y;
  93. height = max_point.z - min_point.z;
  94. iv::lidar::PointXYZ lx;
  95. iv::lidar::PointXYZ * plx;
  96. lx = pcltoprotopoint(centroid);
  97. plx = lidarobj.mutable_centroid();
  98. plx->CopyFrom(lx);
  99. lx = pcltoprotopoint(min_point);
  100. plx = lidarobj.mutable_min_point();
  101. plx->CopyFrom(lx);
  102. lx = pcltoprotopoint(max_point);
  103. plx = lidarobj.mutable_max_point();
  104. plx->CopyFrom(lx);
  105. // lidarobj.centroid = centroid;
  106. // lidarobj.min_point = min_point;
  107. // lidarobj.max_point = max_point;
  108. lx.set_x(min_point.x + length / 2);
  109. lx.set_y(min_point.y + width / 2);
  110. lx.set_z(min_point.z + height / 2);
  111. plx = lidarobj.mutable_position();
  112. plx->CopyFrom(lx);
  113. // lidarobj.position.x = min_point.x + length / 2;
  114. // lidarobj.position.y = min_point.y + width / 2;
  115. // lidarobj.position.z = min_point.z + height / 2;
  116. iv::lidar::Dimension ld;
  117. iv::lidar::Dimension * pld;
  118. ld.set_x((length < 0) ? -1 * length : length);
  119. ld.set_y((width < 0) ? -1 * width : width);
  120. ld.set_z((height < 0) ? -1 * height : height);
  121. // lidarobj.dimensions.x = ((length < 0) ? -1 * length : length);
  122. // lidarobj.dimensions.y = ((width < 0) ? -1 * width : width);
  123. // lidarobj.dimensions.z = ((height < 0) ? -1 * height : height);
  124. pld = lidarobj.mutable_dimensions();
  125. pld->CopyFrom(ld);
  126. lidarobj.set_mntype(x.meta_type);
  127. lidarobj.set_score(x.score);
  128. lidarobj.set_type_name(x.GetTypeString());
  129. int j;
  130. for(j=0;j<x.meta_type_probs.size();j++)
  131. {
  132. lidarobj.add_type_probs(x.meta_type_probs.at(j));
  133. }
  134. for(j=0;j<x.cloud_ptr->size();j++)
  135. {
  136. iv::lidar::PointXYZI * pcp = lidarobj.add_cloud();
  137. pcl::PointXYZI pp = x.cloud_ptr->at(j);
  138. pcp->set_x(pp.x);
  139. pcp->set_y(pp.y);
  140. pcp->set_z(pp.z);
  141. pcp->set_i(pp.intensity);
  142. }
  143. iv::lidar::lidarobject * po = lidarobjvec.add_obj();
  144. po->CopyFrom(lidarobj);
  145. givlog->verbose("CNNOBJ"," object %d type:%s x:%6.3f y:%6.3f",i,
  146. lidarobj.type_name().data(),lidarobj.position().x(),
  147. lidarobj.position().y());
  148. // lidarobj.mnType = x.meta_type;
  149. // lidarobj.score = x.score;
  150. // lidarobj.type_probs = x.meta_type_probs;
  151. // lidarobj.cloud_ptr = x.cloud_ptr;
  152. // int ndatalen;
  153. // char * strx = lidarobj.serialize(ndatalen);
  154. // lidarobj.unserialize(strx,ndatalen);
  155. // delete strx;
  156. // lidarobjvec.push_back(lidarobj);
  157. }
  158. }
  159. //static char * lidarobjtostr( std::vector<iv::lidar::lidarobject> lidarobjvec,int & ntlen)
  160. //{
  161. // int * nlens = new int[lidarobjvec.size()];
  162. // int i;
  163. // int ntotallen = 2*sizeof(int) + lidarobjvec.size() * sizeof(int);
  164. // for(i=0;i<lidarobjvec.size();i++)
  165. // {
  166. // *(nlens+i) = lidarobjvec.at(i).getserializelen();
  167. // ntotallen = ntotallen + *(nlens + i);
  168. // }
  169. // char * pRtn = new char[ntotallen];
  170. // int npos = 0;
  171. // int nobjsize = lidarobjvec.size();
  172. // memcpy(pRtn,&ntotallen,sizeof(int));npos = npos + sizeof(int);
  173. // memcpy(pRtn+npos,&nobjsize,sizeof(int));npos = npos + sizeof(int);
  174. // for(i=0;i<lidarobjvec.size();i++)
  175. // {
  176. // int nlenvalue = *(nlens + i);
  177. // memcpy(pRtn+npos,&nlenvalue,sizeof(int));
  178. // npos = npos + sizeof(int);
  179. // }
  180. // for(i=0;i<lidarobjvec.size();i++)
  181. // {
  182. // int ndatalen;
  183. // char * strser = lidarobjvec.at(i).serialize(ndatalen);
  184. // memcpy(pRtn+ npos,strser,*(nlens+i));
  185. // npos = npos + *(nlens+i);
  186. // delete strser;
  187. // }
  188. // if(ntotallen != npos)std::cout<<" total len is "<<ntotallen<<" pos is "<<npos<<std::endl;
  189. // delete nlens;
  190. // ntlen = ntotallen;
  191. // return pRtn;
  192. //}
  193. void DectectOnePCD(const pcl::PointCloud<pcl::PointXYZI>::Ptr &pc_ptr)
  194. {
  195. std::cout<<"pcd size is "<<pc_ptr->size()<<std::endl;
  196. pcl::PointIndices valid_idx;
  197. auto &indices = valid_idx.indices;
  198. indices.resize(pc_ptr->size());
  199. std::iota(indices.begin(), indices.end(), 0);
  200. std::vector<Obstacle> objvec;
  201. gcnn.segment(pc_ptr, valid_idx, objvec);
  202. givlog->verbose("obj size is %d", objvec.size());
  203. std::cout<<"obj size is "<<objvec.size()<<std::endl;
  204. // std::vector<iv::lidar::lidarobject> lidarobjvec;
  205. iv::lidar::objectarray lidarobjvec;
  206. GetLidarObj(objvec,lidarobjvec);
  207. double timex = pc_ptr->header.stamp;
  208. timex = timex/1000.0;
  209. lidarobjvec.set_timestamp(pc_ptr->header.stamp);
  210. int ntlen;
  211. std::string out = lidarobjvec.SerializeAsString();
  212. // char * strout = lidarobjtostr(lidarobjvec,ntlen);
  213. iv::modulecomm::ModuleSendMsg(gpdetect,out.data(),out.length());
  214. givlog->verbose("lenth is %d",out.length());
  215. // delete strout;
  216. // int i;
  217. // for(i=0;i<objvec.size();i++)
  218. // {
  219. // Obstacle x;
  220. // x = objvec.at(i);
  221. // std::cout<<"obj "<<i<<" x is "<<x.GetTypeString()<<std::endl;
  222. // iv::lidar::lidarobject y;
  223. // y = lidarobjvec.at(i);
  224. // std::cout<<" "<<" x is"<<y.position.x<<" y is "<<y.position.y<<" len x is "<<y.dimensions.x<<" len y is "<<y.dimensions.y<<std::endl;
  225. // std::cout<<" "<<" type is "<<y.mnType<<std::endl;
  226. // }
  227. }
  228. int gnothavedatatime = 0;
  229. void ListenPointCloud(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  230. {
  231. if(nSize <=16)return;
  232. unsigned int * pHeadSize = (unsigned int *)strdata;
  233. if(*pHeadSize > nSize)
  234. {
  235. givlog->verbose("ListenPointCloud data is small headsize = %d, data size is %d", *pHeadSize, nSize);
  236. std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
  237. }
  238. gnothavedatatime = 0;
  239. QTime xTime;
  240. xTime.start();
  241. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  242. new pcl::PointCloud<pcl::PointXYZI>());
  243. int nNameSize;
  244. nNameSize = *pHeadSize - 4-4-8;
  245. char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
  246. memcpy(strName,(char *)((char *)strdata +4),nNameSize);
  247. point_cloud->header.frame_id = strName;
  248. memcpy(&point_cloud->header.seq,(char *)strdata+4+nNameSize,4);
  249. memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
  250. int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
  251. int i;
  252. pcl::PointXYZI * p;
  253. p = (pcl::PointXYZI *)((char *)strdata + *pHeadSize);
  254. for(i=0;i<nPCount;i++)
  255. {
  256. pcl::PointXYZI xp;
  257. // if((p->x<-30)||(p->x>30)||(p->y>50)||(p->y<-50))
  258. // {
  259. // }
  260. // else
  261. // {
  262. memcpy(&xp,p,sizeof(pcl::PointXYZI));
  263. xp.z = xp.z;
  264. point_cloud->push_back(xp);
  265. // }
  266. p++;
  267. // xp.x = p->x;
  268. // xp.y = p->y;
  269. // xp.z = p->z;
  270. // xp.intensity = p->intensity;
  271. // point_cloud->push_back(xp);
  272. // p++;
  273. }
  274. // std::cout<<"pcl time is "<<xTime.elapsed()<<std::endl;
  275. DectectOnePCD(point_cloud);
  276. std::cout<<"time is "<<(QDateTime::currentMSecsSinceEpoch() % 1000)<<" "<<xTime.elapsed()<<std::endl;
  277. gfault->SetFaultState(0, 0, "ok");
  278. }
  279. bool gbstate = true;
  280. void statethread()
  281. {
  282. int nstate = 0;
  283. int nlaststate = 0;
  284. while(gbstate)
  285. {
  286. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  287. if(gnothavedatatime < 100000) gnothavedatatime++;
  288. if(gnothavedatatime < 100)
  289. {
  290. nstate = 0;
  291. }
  292. if(gnothavedatatime > 1000)
  293. {
  294. nstate = 1;
  295. }
  296. if(gnothavedatatime > 6000)
  297. {
  298. nstate = 2;
  299. }
  300. if(nstate != nlaststate)
  301. {
  302. switch (nstate) {
  303. case 0:
  304. givlog->info("detection_lidar_cnn_segmentation is ok");
  305. gfault->SetFaultState(0,0,"data is ok.");
  306. break;
  307. case 1:
  308. givlog->warn("more than 10 seconds not have lidar pointcloud.");
  309. gfault->SetFaultState(1,1,"more than 10 seconds not have lidar pointcloud.");
  310. break;
  311. case 2:
  312. givlog->error("more than 60 seconds not have lidar pointcloud.");
  313. gfault->SetFaultState(2,2,"more than 60 seconds not have lidar pointcloud.");
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. void exitfunc()
  322. {
  323. gbstate = false;
  324. gpthread->join();
  325. std::cout<<"state thread closed."<<std::endl;
  326. iv::modulecomm::Unregister(gpa);
  327. iv::modulecomm::Unregister(gpdetect);
  328. std::cout<<"exit func complete"<<std::endl;
  329. }
  330. //void testcall(const char * str)
  331. //{
  332. // std::cout<<"call msg is "<<str<<std::endl;
  333. //}
  334. int main(int argc, char *argv[])
  335. {
  336. showversion("detection_lidar_cnn_segmentation");
  337. QCoreApplication a(argc, argv);
  338. gfault = new iv::Ivfault("lidar_cnn_segmentation");
  339. givlog = new iv::Ivlog("lidar_cnn_segmentation");
  340. gfault->SetFaultState(0,0,"cnn initialize.");
  341. \
  342. // ALOG("cnn.");
  343. // ALOG("VALUE = %d",1);
  344. // iv::Logger::Inst().SetCallback(testcall);
  345. // ALOG("cnn.");
  346. // iv::Logger::Inst().setlogfile("/home/nvidia/logger.txt");
  347. // ALOG("CNN");
  348. char * strhome = getenv("HOME");
  349. std::string protofile = strhome ;//
  350. protofile += "/models/lidar/model.prototxt";
  351. std::string weightfile = strhome;
  352. weightfile += "/models/lidar/model.caffemodel";
  353. QString strpath = QCoreApplication::applicationDirPath();
  354. if(argc < 2)
  355. strpath = strpath + "/detection_lidar_cnn_segmentation.xml";
  356. else
  357. strpath = argv[1];
  358. std::cout<<strpath.toStdString()<<std::endl;
  359. iv::xmlparam::Xmlparam xparam(strpath.toStdString());
  360. protofile = xparam.GetParam("prototxt",protofile.data());
  361. weightfile = xparam.GetParam("caffemodel",weightfile.data());
  362. gstrinput = xparam.GetParam("input","lidar_pc");
  363. gstroutput = xparam.GetParam("output","lidar_cnn_dectect");
  364. // std::string protofile = "/home/yuchuli/qt/bq/models/lidar/model.prototxt";
  365. // std::string weightfile = "/home/yuchuli/qt/bq/models/lidar/model.caffemodel";
  366. gcnn.init(protofile,weightfile,60.0,0.6,512,512,false,true,0);
  367. // pcl::PointCloud<pcl::PointXYZI>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZI>);
  368. // pcl::io::loadPCDFile ("/home/yuchuli/test_pcd.pcd", *cloud);
  369. // DectectOnePCD(cloud);
  370. gpa = iv::modulecomm::RegisterRecv(gstrinput.data(),ListenPointCloud);
  371. gpdetect = iv::modulecomm::RegisterSend(gstroutput.data(),10000000,1);
  372. gpthread = new std::thread(statethread);
  373. iv::ivexit::RegIVExitCall(exitfunc);
  374. return a.exec();
  375. }