locate_sample.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Created by zx on 2019/12/30.
  3. //
  4. /*
  5. * locater 模块测试例子
  6. */
  7. #include <fcntl.h>
  8. #include "../locate/locate_task.h"
  9. #include "../locate/locater.h"
  10. #include <glog/logging.h>
  11. #include <google/protobuf/io/coded_stream.h>
  12. #include <google/protobuf/io/zero_copy_stream_impl.h>
  13. #include <google/protobuf/text_format.h>
  14. using google::protobuf::io::FileInputStream;
  15. using google::protobuf::io::FileOutputStream;
  16. using google::protobuf::io::ZeroCopyInputStream;
  17. using google::protobuf::io::CodedInputStream;
  18. using google::protobuf::io::ZeroCopyOutputStream;
  19. using google::protobuf::io::CodedOutputStream;
  20. using google::protobuf::Message;
  21. #include <sys/stat.h>
  22. #include <dirent.h>
  23. void init_glog()
  24. {
  25. FLAGS_max_log_size = 100;
  26. FLAGS_logbufsecs = 0;
  27. google::InitGoogleLogging("LidarMeasurement");
  28. google::SetStderrLogging(google::INFO);
  29. google::InstallFailureSignalHandler();
  30. FLAGS_colorlogtostderr = true; // Set log color
  31. FLAGS_logbufsecs = 0; // Set log output speed(s)
  32. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  33. FLAGS_stop_logging_if_full_disk = true;
  34. }
  35. void list_dir( const char * dir_name,std::vector<std::string>& files)
  36. {
  37. if( 0 == dir_name )
  38. {
  39. std::cout<<" dir_name is null ! "<<std::endl;
  40. return;
  41. }
  42. struct stat s;
  43. lstat( dir_name , &s );
  44. if( ! S_ISDIR( s.st_mode ) )
  45. {
  46. return;
  47. }
  48. struct dirent * filename;
  49. DIR * dir;
  50. dir = opendir( dir_name );
  51. if( NULL == dir )
  52. {
  53. return;
  54. }
  55. int iName=0;
  56. while( ( filename = readdir(dir) ) != NULL )
  57. {
  58. if( strcmp( filename->d_name , "." ) == 0 ||
  59. strcmp( filename->d_name , "..") == 0)
  60. continue;
  61. char wholePath[128] = {0};
  62. sprintf(wholePath, "%s", filename->d_name);
  63. files.push_back(wholePath);
  64. }
  65. }
  66. bool read_proto_param(std::string path, ::google::protobuf::Message& param)
  67. {
  68. int fd = open(path.c_str(), O_RDONLY);
  69. if (fd == -1) return false;
  70. FileInputStream* input = new FileInputStream(fd);
  71. bool success = google::protobuf::TextFormat::Parse(input, &param);
  72. delete input;
  73. close(fd);
  74. return success;
  75. }
  76. bool string2point(std::string str,pcl::PointXYZ& point)
  77. {
  78. std::istringstream iss;
  79. iss.str(str);
  80. float value;
  81. float data[3]={0};
  82. for(int i=0;i<3;++i)
  83. {
  84. if(iss>>value)
  85. {
  86. data[i]=value;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93. point.x=data[0];
  94. point.y=data[1];
  95. point.z=data[2];
  96. return true;
  97. }
  98. pcl::PointCloud<pcl::PointXYZ>::Ptr ReadTxtCloud(std::string file)
  99. {
  100. std::ifstream fin(file.c_str());
  101. const int line_length=64;
  102. char str[line_length]={0};
  103. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  104. while(fin.getline(str,line_length))
  105. {
  106. pcl::PointXYZ point;
  107. if(string2point(str,point))
  108. {
  109. cloud->push_back(point);
  110. }
  111. }
  112. return cloud;
  113. }
  114. int main(int argc,char* argv[])
  115. {
  116. init_glog();
  117. std::string input_file="balck_suv";
  118. std::string out_path="./test";
  119. if(argc>=2)
  120. {
  121. input_file=argv[1];
  122. }
  123. if(argc>=3)
  124. {
  125. out_path=argv[2];
  126. }
  127. Error_manager code;
  128. Locater* locater=new Locater();
  129. Measure::LocateParameter parameter;
  130. if(false==read_proto_param("./setting/locate.prototxt",parameter))
  131. {
  132. std::cout<<" read parameter failed..."<<std::endl;
  133. }
  134. code=locater->init(parameter);
  135. if(code!=SUCCESS)
  136. {
  137. LOG(ERROR)<<code.to_string();
  138. }
  139. Locate_task* task=new Locate_task();
  140. std::string cloud_path="/home/zx/data/samples/src_txt/";
  141. cloud_path+=input_file;
  142. std::vector<std::string> files;
  143. list_dir(cloud_path.c_str(),files);
  144. int count=files.size();
  145. int correct_size=0;
  146. for(int i=0;i<files.size();++i) {
  147. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
  148. std::string t_file=cloud_path+"/"+files[i];
  149. cloud = ReadTxtCloud(t_file);
  150. std::cout << " input file: " << t_file << std::endl;
  151. code = task->set_locate_cloud(cloud);
  152. if (code != SUCCESS) {
  153. LOG(ERROR) << code.to_string();
  154. return 0;
  155. }
  156. task->set_save_path(out_path);
  157. code = locater->execute_task(task, 5);
  158. if (code == SUCCESS) {
  159. LOG(INFO) << " LOCATE SUCCESS";
  160. correct_size++;
  161. }
  162. else {
  163. LOG(ERROR) << code.to_string();
  164. //移动文件
  165. usleep(300);
  166. std::string src="./test/cnn3d.txt";
  167. std::string dst="./error/";
  168. dst+=files[i];
  169. std::string name="mv "+src+" "+dst;
  170. const char *des_name=name.c_str();
  171. system(des_name); //调用系统命令
  172. usleep(200);
  173. }
  174. }
  175. if(count>0)
  176. {
  177. std::cout<<std::endl<<" acc : "<<correct_size<<"/"<<count<<
  178. " percent:"<<float(correct_size)/float(count)<<std::endl;
  179. }
  180. return 0;
  181. }