face_recognizer_algorithms_test.cpp
Go to the documentation of this file.
00001 #include <cob_people_detection/face_recognizer_algorithms.h>
00002 #include <cob_people_detection/face_normalizer.h>
00003 #include <opencv2/opencv.hpp>
00004 #include <iostream>
00005 #include <fstream>
00006 #include <sys/time.h>
00007 #include <boost/timer.hpp>
00008 #include <boost/filesystem.hpp>
00009 
00010 bool preprocess(cv::Mat& img, cv::Mat& xyz, FaceNormalizer* fn, bool normalize, cv::Size& norm_size, cv::Mat& dm)
00011 {
00012         //cv::Size norm_size=cv::Size(120,120);
00013         bool valid = true;
00014         if (normalize)
00015         {
00016                 valid = fn->normalizeFace(img, xyz, norm_size, dm);
00017 
00018                 // dm.convertTo(dm,CV_8UC1);
00019                 // cv::equalizeHist(dm,dm);
00020                 // cv::imshow("normalized",dm);
00021                 // cv::waitKey(5);
00022 
00023         }
00024         else
00025         {
00026                 cv::resize(img, img, norm_size);
00027         }
00028 
00029         dm.convertTo(dm, CV_64FC1);
00030         img.convertTo(img, CV_64FC1);
00031         return valid;
00032 }
00033 bool preprocess(cv::Mat& img, FaceNormalizer* fn, bool normalize, cv::Size& norm_size)
00034 {
00035         bool valid = true;
00036         //cv::Size norm_size=cv::Size(120,120);
00037         if (normalize)
00038         {
00039                 valid = fn->normalizeFace(img, norm_size);
00040                 //cv::imshow("normalized",img);
00041                 //cv::waitKey(5);
00042 
00043         }
00044         else
00045         {
00046                 cv::resize(img, img, norm_size);
00047         }
00048 
00049         img.convertTo(img, CV_64FC1);
00050         return valid;
00051 }
00052 
00053 int main(int argc, const char *argv[])
00054 {
00055 
00056         FaceNormalizer::FNConfig config;
00057         config.eq_ill = true;
00058         config.align = false;
00059         config.resize = true;
00060         config.cvt2gray = true;
00061         config.extreme_illumination_condtions = false;
00062 
00063         FaceNormalizer* fn = new FaceNormalizer();
00064         fn->init(config);
00065 
00066         // parse input arguments from command line
00067         std::string method_str;//,classifier_str;
00068         bool use_xyz = true;
00069         bool normalizer = false;
00070         if (argc == 1)
00071         {
00072                 method_str = "EIGEN";
00073                 //classifier_str="";
00074                 normalizer = false;
00075         }
00076 
00077         else if (argc == 2)
00078         {
00079                 method_str = argv[1];
00080                 //classifier_str="KNN";
00081                 normalizer = false;
00082         }
00083 
00084         else if (argc == 3)
00085         {
00086                 method_str = argv[1];
00087                 //classifier_str=argv[2];
00088                 normalizer = false;
00089         }
00090 
00091         else if (argc == 3)
00092         {
00093                 method_str = argv[1];
00094                 //classifier_str=argv[2];
00095 
00096                 if (std::strcmp(argv[2], "0") == 0)
00097                         normalizer = false;
00098                 if (std::strcmp(argv[2], "1") == 0)
00099                         normalizer = true;
00100 
00101                 if (std::strcmp(argv[3], "0") == 0)
00102                         use_xyz = false;
00103                 if (std::strcmp(argv[3], "1") == 0)
00104                         use_xyz = true;
00105         }
00106 
00107         else if (argc == 4)
00108         {
00109                 method_str = argv[1];
00110                 //classifier_str=argv[2];
00111                 if (std::strcmp(argv[2], "0") == 0)
00112                         normalizer = false;
00113                 if (std::strcmp(argv[2], "1") == 0)
00114                         normalizer = true;
00115 
00116                 if (std::strcmp(argv[3], "0") == 0)
00117                         use_xyz = false;
00118                 if (std::strcmp(argv[3], "1") == 0)
00119                         use_xyz = true;
00120         }
00121 
00122         //  Configure input params for subspace analysis
00123 
00124         ipa_PeopleDetector::Method method;
00125         ipa_PeopleDetector::Classifier classifier;
00126 
00127         if (!method_str.compare("FISHER"))
00128         {
00129                 std::cout << "FISHER" << std::endl;
00130                 method = ipa_PeopleDetector::METH_FISHER;
00131         }
00132         else if (!method_str.compare("EIGEN"))
00133         {
00134                 std::cout << "EIGEN" << std::endl;
00135                 method = ipa_PeopleDetector::METH_EIGEN;
00136         }
00137         else if (!method_str.compare("LDA2D"))
00138         {
00139                 std::cout << "LDA2D" << std::endl;
00140                 method = ipa_PeopleDetector::METH_LDA2D;
00141         }
00142         else if (!method_str.compare("PCA2D"))
00143         {
00144                 std::cout << "PCA2D" << std::endl;
00145                 method = ipa_PeopleDetector::METH_PCA2D;
00146         }
00147         else
00148         {
00149                 std::cout << "ERROR: invalid method - use FISHER or EIGEN" << std::endl;
00150         }
00151 
00152         // if(!classifier_str.compare("KNN"))
00153         // {
00154         //   std::cout<<"KNN"<<std::endl;
00155         //   classifier = ipa_PeopleDetector::CLASS_KNN;
00156         // }
00157         // else if(!classifier_str.compare("DIFFS"))
00158         // {
00159         //   std::cout<<"DIFFS"<<std::endl;
00160         //   classifier = ipa_PeopleDetector::CLASS_DIFS;
00161         // }
00162         // else if(!classifier_str.compare("SVM"))
00163         // {
00164         //   std::cout<<"SVM"<<std::endl;
00165         //   classifier = ipa_PeopleDetector::CLASS_SVM;
00166         // }
00167         // else if(!classifier_str.compare("RF"))
00168         // {
00169         //   std::cout<<"RF"<<std::endl;
00170         //   classifier = ipa_PeopleDetector::CLASS_RF;
00171         // }
00172         // else
00173         // {
00174         //   std::cout<<"ERROR: invalid classifier - use KNN or DIFFS or SVM"<<std::endl;
00175         // }
00176 
00177 
00178         std::cout << "SSA test configuration:" << std::endl;
00179         //std::cout<<"classifier: "<<classifier_str<<std::endl;
00180         std::cout << "method: " << method_str << std::endl;
00181         std::cout << "normalizing: " << normalizer << std::endl;
00182         std::cout << "use xyz: " << use_xyz << std::endl;
00183         //HOME
00184         //std::string training_set_path=     "/home/tom/git/care-o-bot/cob_people_perception/ipa_PeopleDetector/debug/eval/eval_tool_files/training_set_list";
00185         //std::string training_set_xyz_path= "/home/tom/git/care-o-bot/cob_people_perception/ipa_PeopleDetector/debug/eval/eval_tool_files/training_set_xyz_list";
00186         //std::string probe_file_path=       "/home/tom/git/care-o-bot/cob_people_perception/ipa_PeopleDetector/debug/eval/eval_tool_files/probe_file_list";
00187         //std::string probe_file_xyz_path=   "/home/tom/git/care-o-bot/cob_people_perception/ipa_PeopleDetector/debug/eval/eval_tool_files/probe_file_xyz_list";
00188         //IPA
00189         std::string training_set_path = "/share/goa-tz/people_detection/eval/eval_tool_files/training_set_list";
00190         std::string training_set_xyz_path = "/share/goa-tz/people_detection/eval/eval_tool_files/training_set_xyz_list";
00191         std::string probe_file_path = "/share/goa-tz/people_detection/eval/eval_tool_files/probe_file_list";
00192         std::string probe_file_xyz_path = "/share/goa-tz/people_detection/eval/eval_tool_files/probe_file_xyz_list";
00193 
00194         //read probe file
00195         std::ifstream probe_file_stream(probe_file_path.c_str());
00196 
00197         std::string probe_file;
00198         std::vector < std::string > probe_file_vec;
00199 
00200         while (probe_file_stream >> probe_file)
00201         {
00202                 //std::cout<<probe_file<<std::endl;
00203                 probe_file_vec.push_back(probe_file);
00204 
00205         }
00206 
00207         std::vector < std::string > probe_file_xyz_vec;
00208         if (use_xyz)
00209         {
00210                 std::ifstream probe_file_xyz_stream(probe_file_xyz_path.c_str());
00211                 std::string probe_file_xyz;
00212 
00213                 while (probe_file_xyz_stream >> probe_file_xyz)
00214                 {
00215                         //std::cout<<probe_file<<std::endl;
00216                         probe_file_xyz_vec.push_back(probe_file_xyz);
00217 
00218                 }
00219         }
00220 
00221         // read training set
00222         std::ifstream in_file(training_set_path.c_str());
00223         std::string img_file;
00224 
00225         int label = 0;
00226         std::vector < std::string > in_vec;
00227         std::vector<int> label_vec;
00228 
00229         while (in_file >> img_file)
00230         {
00231                 if (std::strcmp(img_file.c_str(), "$$") == 0)
00232                 {
00233 
00234                         label++;
00235                 }
00236                 else
00237                 {
00238                         in_vec.push_back(img_file);
00239                         label_vec.push_back(label);
00240                 }
00241 
00242         }
00243 
00244         std::vector < std::string > in_vec_xyz;
00245         if (use_xyz)
00246         {
00247                 // read xyz data
00248                 std::ifstream in_file_xyz(training_set_xyz_path.c_str());
00249                 std::string xml_file;
00250 
00251                 while (in_file_xyz >> xml_file)
00252                 {
00253                         if (!std::strcmp(xml_file.c_str(), "$$") == 0)
00254                         {
00255                                 in_vec_xyz.push_back(xml_file);
00256                         }
00257 
00258                 }
00259         }
00260 
00261         if ((use_xyz == true) && (in_vec.size() != in_vec_xyz.size() || probe_file_vec.size() != probe_file_xyz_vec.size()))
00262         {
00263                 use_xyz = false;
00264                 std::cerr << "Error - not for every image 3d information could be loaded - ignoring 3d information\n";
00265         }
00266 
00267         int num_classes = label;
00268         cv::Size norm_size;
00269         double aspect_ratio = 1;
00270         // load training images
00271         std::vector<cv::Mat> img_vec;
00272         std::vector<cv::Mat> dm_vec;
00273 
00274         std::string invalid_path = "/share/goa-tz/people_detection/eval/eval_tool_files/nrm_failed";
00275         //std::string invalid_path="/home/tom/git/care-o-bot/cob_people_perception/ipa_PeopleDetector/eval/eval_tool_files/nrm_failed";
00276         std::ofstream os_inv(invalid_path.c_str());
00277         bool valid;
00278         for (int i = 0; i < in_vec.size(); i++)
00279         {
00280                 cv::Mat img;
00281                 cv::Mat xyz;
00282                 if (use_xyz)
00283                 {
00284                         cv::FileStorage fs(in_vec_xyz[i], FileStorage::READ);
00285                         fs["depth"] >> xyz;
00286                         fs["color"] >> img;
00287                         fs.release();
00288                 }
00289                 else
00290                 {
00291                         img = cv::imread(in_vec[i], 0);
00292                 }
00293 
00294                 if (i == 0)
00295                 {
00296                         aspect_ratio = double(img.cols) / double(img.rows);
00297                         norm_size = cv::Size(round(160 * aspect_ratio), 160);
00298                         //norm_size=cv::Size(img.rows,img.cols);
00299                 }
00300                 valid = true;
00301                 cv::Mat dm;
00302                 if (use_xyz)
00303                         valid = preprocess(img, xyz, fn, normalizer, norm_size, dm);
00304                 //if(use_xyz)valid=preprocess(img,fn,normalizer,norm_size);
00305                 if (!use_xyz)
00306                         valid = preprocess(img, fn, normalizer, norm_size);
00307 
00308                 img_vec.push_back(img);
00309                 if (use_xyz)
00310                         dm_vec.push_back(dm);
00311 
00312                 if (!valid)
00313                 {
00314                         os_inv << in_vec[i] << "\n";
00315                 }
00316 
00317         }
00318 
00319         // load test images
00320         std::vector<cv::Mat> probe_mat_vec;
00321         std::vector<cv::Mat> probe_dm_vec;
00322         for (int i = 0; i < probe_file_vec.size(); i++)
00323         {
00324                 std::stringstream ostr, nstr;
00325                 nstr << "/share/goa-tz/people_detection/eval/picdump/";
00326                 //nstr<<"/home/tom/git/care-o-bot/cob_people_perception/ipa_PeopleDetector/debug/eval/picdump/";
00327                 ostr << nstr.str().c_str() << i << "_orig" << ".jpg";
00328 
00329                 cv::Mat probe_xyz, probe_img;
00330                 if (use_xyz)
00331                 {
00332                         cv::FileStorage fs(probe_file_xyz_vec[i], FileStorage::READ);
00333                         fs["depth"] >> probe_xyz;
00334                         fs["color"] >> probe_img;
00335                         fs.release();
00336                 }
00337                 else
00338                 {
00339                         probe_img = cv::imread(probe_file_vec[i], 0);
00340                 }
00341 
00342                 cv::imwrite(ostr.str().c_str(), probe_img);
00343 
00344                 valid = true;
00345                 cv::Mat dm;
00346                 if (use_xyz)
00347                         valid = preprocess(probe_img, probe_xyz, fn, normalizer, norm_size, dm);
00348                 if (!use_xyz)
00349                         valid = preprocess(probe_img, fn, normalizer, norm_size);
00350 
00351                 cv::Mat oimg;
00352                 probe_img.convertTo(oimg, CV_8UC1);
00353                 //cv::equalizeHist(oimg,oimg);
00354                 nstr << i << "_norm" << ".jpg";
00355                 cv::imwrite(nstr.str().c_str(), oimg);
00356 
00357                 probe_mat_vec.push_back(probe_img);
00358                 if (use_xyz)
00359                         probe_dm_vec.push_back(dm);
00360 
00361                 if (!valid)
00362                 {
00363                         os_inv << probe_file_vec[i] << "\n";
00364                 }
00365 
00366         }
00367         os_inv.close();
00368 
00369         std::cout << "Size Training Set= " << img_vec.size() << std::endl;
00370         std::cout << "Size Test Set= " << probe_file_vec.size() << std::endl;
00371 
00372         int ss_dim = 5;
00373 
00374         ipa_PeopleDetector::FaceRecognizerBaseClass* EFF;
00375         // calculate Model
00376 
00377         // timeval t1,t2,t3,t4;
00378         // gettimeofday(&t1,NULL);
00379 
00380         switch (method)
00381         {
00382         case ipa_PeopleDetector::METH_EIGEN:
00383         {
00384                 EFF = new ipa_PeopleDetector::FaceRecognizer_Eigenfaces();
00385                 break;
00386         }
00387         case ipa_PeopleDetector::METH_FISHER:
00388         {
00389                 EFF = new ipa_PeopleDetector::FaceRecognizer_Fisherfaces();
00390                 break;
00391         }
00392         case ipa_PeopleDetector::METH_PCA2D:
00393         {
00394                 EFF = new ipa_PeopleDetector::FaceRecognizer_PCA2D();
00395                 break;
00396         }
00397         case ipa_PeopleDetector::METH_LDA2D:
00398         {
00399                 EFF = new ipa_PeopleDetector::FaceRecognizer_LDA2D();
00400                 break;
00401         }
00402         default:
00403         {
00404                 EFF = new ipa_PeopleDetector::FaceRecognizer_Eigenfaces();
00405                 break;
00406         }
00407         }
00408         std::cout << EFF->trained_ << std::endl;
00409         boost::timer t;
00410 
00411         boost::filesystem::path rpath = "/home/goa-tz/.ros/test.xml";
00412         //EFF->loadModel(rpath);
00413         std::cout << "loaded" << std::endl;
00414         EFF->trainModel(img_vec, label_vec, ss_dim);
00415         //EFF->activate_unknown_treshold();
00416         //gettimeofday(&t2,NULL);jj
00417 
00418         //open output file
00419         std::string path = "/share/goa-tz/people_detection/eval/eval_tool_files/classification_labels";
00420         std::string probabilities_path = "/share/goa-tz/people_detection/eval/eval_tool_files/classification_probabilities";
00421         std::string timing_path = "/share/goa-tz/people_detection/eval/eval_tool_files/timing";
00422         //std::string path = "/home/tom/git/care-o-bot/cob_people_perception/ipa_PeopleDetector/debug/eval/eval_tool_files/classified_output";
00423         std::ofstream os(path.c_str());
00424         std::ofstream probabilities_os(probabilities_path.c_str());
00425         std::ofstream timing_os(timing_path.c_str(), std::ofstream::app);
00426 
00427         timing_os << t.elapsed() << ",";
00428         std::cout << ">>>>>>>>>>training time = " << t.elapsed() << std::endl;
00429 
00430         std::cout << "EFF model computed" << std::endl;
00431         //EFF->loadModelFromFile("/share/goa-tz/people_detection/debug/rdata.xml",true);
00432 
00433 
00434         //if(use_xyz)
00435         //{
00436         //EFF_depth->trainModel(dm_vec,label_vec,ss_dim,method,true,false);
00437         //}
00438 
00439 
00440         //restart timer
00441         t.restart();
00442         for (int i = 0; i < probe_mat_vec.size(); i++)
00443         {
00444                 cv::Mat probe = probe_mat_vec[i];
00445                 int c_EFF;
00446                 cv::Mat coeff_EFF;
00447                 double DFFS_EFF;
00448                 cv::Mat probabilities;
00449                 EFF->classifyImage(probe, c_EFF, probabilities);
00450 
00451                 //c_EFF=model->predict(probe);
00452                 //std::cout<<"RGB CLASS"<<c_EFF<<std::endl;
00453 
00454                 //  //For use with depth data
00455                 //  int c_EFF_dm;
00456                 //  cv::Mat coeff_EFF_dm;
00457                 //  double DFFS_EFF_dm;
00458                 //  //if(use_xyz)
00459                 //  //{
00460                 //  cv::Mat probe_dm = probe_dm_vec[i];
00461                 //EFF_depth->projectToSubspace(probe_dm,coeff_EFF_dm,DFFS_EFF_dm);
00462                 //EFF_depth->classify(coeff_EFF_dm,classifier,c_EFF_dm);
00464 
00465                 //}
00466 
00467                 //Output to classified file
00468                 os << c_EFF << "\n";
00469                 probabilities_os << probabilities << "\n";
00470         }
00471         std::cout << ">>>>>>>>>>recognition time = " << t.elapsed() << std::endl;
00472         timing_os << t.elapsed() << std::endl;
00473 
00474         os.close();
00475         std::cout << "EFF classified" << std::endl;
00476 
00477         cv::Mat m1_evec, m1_eval, m1_avg, m1_pmd;
00478 
00479         //EFF->getModel(m1_evec,m1_eval,m1_avg,m1_pmd);
00480         //EFF->saveModel("/share/goa-tz/people_detection/debug/test.xml");
00481 
00482 
00483         //m2->loadModel(m1_evec,m1_eval,m1_avg,m1_pmd,label_vec,false);
00484         //m2->loadModelFromFile("/share/goa-tz/people_detection/debug/rdata.xml",true);
00485 
00486         //double m2_dffs;
00487         //cv::Mat m2_coeff;
00488         //int m2_c;
00489         //cv::Mat probe=probe_mat_vec[0];
00490         //m2->projectToSubspace(probe,m2_coeff,m2_dffs);
00491         //m2->classify(m2_coeff,classifier,m2_c);
00492 
00493 
00494         // The following line predicts the label of a given
00495         // test image:
00496         //int predictedLabel = model->predict(testSample);
00497 
00498 
00499         EFF->saveModel(rpath);
00500         return 0;
00501 }


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Mon May 6 2019 02:32:06