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


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Fri Aug 28 2015 10:24:12