ssa_test.cpp
Go to the documentation of this file.
00001 #include<cob_people_detection/subspace_analysis.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 
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 = "FISHER";
00073                 classifier_str = "SVM";
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 == 4)
00092         {
00093                 method_str = argv[1];
00094                 classifier_str = argv[2];
00095 
00096                 if (std::strcmp(argv[3], "0") == 0)
00097                         normalizer = false;
00098                 if (std::strcmp(argv[3], "1") == 0)
00099                         normalizer = true;
00100 
00101                 if (std::strcmp(argv[4], "0") == 0)
00102                         use_xyz = false;
00103                 if (std::strcmp(argv[4], "1") == 0)
00104                         use_xyz = true;
00105         }
00106 
00107         else if (argc == 5)
00108         {
00109                 method_str = argv[1];
00110                 classifier_str = argv[2];
00111                 if (std::strcmp(argv[3], "0") == 0)
00112                         normalizer = false;
00113                 if (std::strcmp(argv[3], "1") == 0)
00114                         normalizer = true;
00115 
00116                 if (std::strcmp(argv[4], "0") == 0)
00117                         use_xyz = false;
00118                 if (std::strcmp(argv[4], "1") == 0)
00119                         use_xyz = true;
00120         }
00121 
00122         //  Configure input params for subspace analysis
00123 
00124         SubspaceAnalysis::Method method;
00125         SubspaceAnalysis::Classifier classifier;
00126 
00127         if (!method_str.compare("FISHER"))
00128         {
00129                 std::cout << "FISHER" << std::endl;
00130                 method = SubspaceAnalysis::METH_FISHER;
00131         }
00132         else if (!method_str.compare("EIGEN"))
00133         {
00134                 std::cout << "EIGEN" << std::endl;
00135                 method = SubspaceAnalysis::METH_EIGEN;
00136         }
00137         else if (!method_str.compare("LDA2D"))
00138         {
00139                 std::cout << "LDA2D" << std::endl;
00140                 method = SubspaceAnalysis::METH_LDA2D;
00141         }
00142         else if (!method_str.compare("PCA2D"))
00143         {
00144                 std::cout << "PCA2D" << std::endl;
00145                 method = SubspaceAnalysis::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 = SubspaceAnalysis::CLASS_KNN;
00156         }
00157         else if (!classifier_str.compare("DIFFS"))
00158         {
00159                 std::cout << "DIFFS" << std::endl;
00160                 classifier = SubspaceAnalysis::CLASS_DIFS;
00161         }
00162         else if (!classifier_str.compare("SVM"))
00163         {
00164                 std::cout << "SVM" << std::endl;
00165                 classifier = SubspaceAnalysis::CLASS_SVM;
00166         }
00167         else if (!classifier_str.compare("RF"))
00168         {
00169                 std::cout << "RF" << std::endl;
00170                 classifier = SubspaceAnalysis::CLASS_RF;
00171         }
00172         else
00173         {
00174                 std::cout << "ERROR: invalid classifier - use KNN or DIFFS or SVM" << std::endl;
00175         }
00176 
00177         std::cout << "SSA test configuration:" << std::endl;
00178         std::cout << "classifier: " << classifier_str << std::endl;
00179         std::cout << "method: " << method_str << std::endl;
00180         std::cout << "normalizing: " << normalizer << std::endl;
00181         std::cout << "use xyz: " << use_xyz << std::endl;
00182         //HOME
00183         //std::string training_set_path=     "/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/eval_tool_files/training_set_list";
00184         //std::string training_set_xyz_path= "/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/eval_tool_files/training_set_xyz_list";
00185         //std::string probe_file_path=       "/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/eval_tool_files/probe_file_list";
00186         //std::string probe_file_xyz_path=   "/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/eval_tool_files/probe_file_xyz_list";
00187         //IPA
00188         std::string training_set_path = "/share/goa-tz/people_detection/eval/eval_tool_files/training_set_list";
00189         std::string training_set_xyz_path = "/share/goa-tz/people_detection/eval/eval_tool_files/training_set_xyz_list";
00190         std::string probe_file_path = "/share/goa-tz/people_detection/eval/eval_tool_files/probe_file_list";
00191         std::string probe_file_xyz_path = "/share/goa-tz/people_detection/eval/eval_tool_files/probe_file_xyz_list";
00192 
00193         //read probe file
00194         std::ifstream probe_file_stream(probe_file_path.c_str());
00195 
00196         std::string probe_file;
00197         std::vector < std::string > probe_file_vec;
00198 
00199         while (probe_file_stream >> probe_file)
00200         {
00201                 //std::cout<<probe_file<<std::endl;
00202                 probe_file_vec.push_back(probe_file);
00203 
00204         }
00205 
00206         std::vector < std::string > probe_file_xyz_vec;
00207         if (use_xyz)
00208         {
00209                 std::ifstream probe_file_xyz_stream(probe_file_xyz_path.c_str());
00210                 std::string probe_file_xyz;
00211 
00212                 while (probe_file_xyz_stream >> probe_file_xyz)
00213                 {
00214                         //std::cout<<probe_file<<std::endl;
00215                         probe_file_xyz_vec.push_back(probe_file_xyz);
00216 
00217                 }
00218         }
00219 
00220         // read training set
00221         std::ifstream in_file(training_set_path.c_str());
00222         std::string img_file;
00223 
00224         int label = 0;
00225         std::vector < std::string > in_vec;
00226         std::vector<int> label_vec;
00227 
00228         while (in_file >> img_file)
00229         {
00230                 if (std::strcmp(img_file.c_str(), "$$") == 0)
00231                 {
00232 
00233                         label++;
00234                 }
00235                 else
00236                 {
00237                         in_vec.push_back(img_file);
00238                         label_vec.push_back(label);
00239                 }
00240 
00241         }
00242 
00243         std::vector < std::string > in_vec_xyz;
00244         if (use_xyz)
00245         {
00246                 // read xyz data
00247                 std::ifstream in_file_xyz(training_set_xyz_path.c_str());
00248                 std::string xml_file;
00249 
00250                 while (in_file_xyz >> xml_file)
00251                 {
00252                         if (!std::strcmp(xml_file.c_str(), "$$") == 0)
00253                         {
00254                                 in_vec_xyz.push_back(xml_file);
00255                         }
00256 
00257                 }
00258         }
00259 
00260         if ((use_xyz == true) && (in_vec.size() != in_vec_xyz.size() || probe_file_vec.size() != probe_file_xyz_vec.size()))
00261         {
00262                 use_xyz = false;
00263                 std::cerr << "Error - not for every image 3d information could be loaded - ignoring 3d information\n";
00264         }
00265 
00266         int num_classes = label;
00267         cv::Size norm_size;
00268         double aspect_ratio = 1;
00269         // load training images
00270         std::vector<cv::Mat> img_vec;
00271         std::vector<cv::Mat> dm_vec;
00272 
00273         std::string invalid_path = "/share/goa-tz/people_detection/eval/eval_tool_files/nrm_failed";
00274         //std::string invalid_path="/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/eval/eval_tool_files/nrm_failed";
00275         std::ofstream os_inv(invalid_path.c_str());
00276         bool valid;
00277         for (int i = 0; i < in_vec.size(); i++)
00278         {
00279                 cv::Mat img;
00280                 cv::Mat xyz;
00281                 if (use_xyz)
00282                 {
00283                         cv::FileStorage fs(in_vec_xyz[i], FileStorage::READ);
00284                         fs["depth"] >> xyz;
00285                         fs["color"] >> img;
00286                         fs.release();
00287                 }
00288                 else
00289                 {
00290                         img = cv::imread(in_vec[i], 0);
00291                 }
00292 
00293                 if (i == 0)
00294                 {
00295                         aspect_ratio = double(img.cols) / double(img.rows);
00296                         norm_size = cv::Size(round(160 * aspect_ratio), 160);
00297                         //norm_size=cv::Size(img.rows,img.cols);
00298                 }
00299                 valid = true;
00300                 cv::Mat dm;
00301                 if (use_xyz)
00302                         valid = preprocess(img, xyz, fn, normalizer, norm_size, dm);
00303                 //if(use_xyz)valid=preprocess(img,fn,normalizer,norm_size);
00304                 if (!use_xyz)
00305                         valid = preprocess(img, fn, normalizer, norm_size);
00306 
00307                 img_vec.push_back(img);
00308                 if (use_xyz)
00309                         dm_vec.push_back(dm);
00310 
00311                 if (!valid)
00312                 {
00313                         os_inv << in_vec[i] << "\n";
00314                 }
00315 
00316         }
00317 
00318         // load test images
00319         std::vector<cv::Mat> probe_mat_vec;
00320         std::vector<cv::Mat> probe_dm_vec;
00321         for (int i = 0; i < probe_file_vec.size(); i++)
00322         {
00323                 std::stringstream ostr, nstr;
00324                 nstr << "/share/goa-tz/people_detection/eval/picdump/";
00325                 //nstr<<"/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/picdump/";
00326                 ostr << nstr.str().c_str() << i << "_orig" << ".jpg";
00327 
00328                 cv::Mat probe_xyz, probe_img;
00329                 if (use_xyz)
00330                 {
00331                         cv::FileStorage fs(probe_file_xyz_vec[i], FileStorage::READ);
00332                         fs["depth"] >> probe_xyz;
00333                         fs["color"] >> probe_img;
00334                         fs.release();
00335                 }
00336                 else
00337                 {
00338                         probe_img = cv::imread(probe_file_vec[i], 0);
00339                 }
00340 
00341                 cv::imwrite(ostr.str().c_str(), probe_img);
00342 
00343                 valid = true;
00344                 cv::Mat dm;
00345                 if (use_xyz)
00346                         valid = preprocess(probe_img, probe_xyz, fn, normalizer, norm_size, dm);
00347                 if (!use_xyz)
00348                         valid = preprocess(probe_img, fn, normalizer, norm_size);
00349 
00350                 cv::Mat oimg;
00351                 probe_img.convertTo(oimg, CV_8UC1);
00352                 //cv::equalizeHist(oimg,oimg);
00353                 nstr << i << "_norm" << ".jpg";
00354                 cv::imwrite(nstr.str().c_str(), oimg);
00355 
00356                 probe_mat_vec.push_back(probe_img);
00357                 if (use_xyz)
00358                         probe_dm_vec.push_back(dm);
00359 
00360                 if (!valid)
00361                 {
00362                         os_inv << probe_file_vec[i] << "\n";
00363                 }
00364 
00365         }
00366         os_inv.close();
00367 
00368         std::cout << "Size Training Set= " << img_vec.size() << std::endl;
00369         std::cout << "Size Test Set= " << probe_file_vec.size() << std::endl;
00370 
00371         int ss_dim = num_classes;
00372 
00373         SubspaceAnalysis::FaceRecognizer* EFF = new SubspaceAnalysis::FaceRecognizer();
00374         // calculate Model
00375 
00376         // timeval t1,t2,t3,t4;
00377         // gettimeofday(&t1,NULL);
00378         boost::timer t;
00379         EFF->trainModel(img_vec, label_vec, ss_dim, method, true, false);
00380         //gettimeofday(&t2,NULL);jj
00381 
00382         //open output file
00383         std::string path = "/share/goa-tz/people_detection/eval/eval_tool_files/classification_labels";
00384         std::string probabilities_path = "/share/goa-tz/people_detection/eval/eval_tool_files/classification_probabilities";
00385         std::string timing_path = "/share/goa-tz/people_detection/eval/eval_tool_files/timing";
00386         //std::string path = "/home/tom/git/care-o-bot/cob_people_perception/cob_people_detection/debug/eval/eval_tool_files/classified_output";
00387         std::ofstream os(path.c_str());
00388         std::ofstream probabilities_os(probabilities_path.c_str());
00389         std::ofstream timing_os(timing_path.c_str(), std::ofstream::app);
00390 
00391         timing_os << t.elapsed() << ",";
00392         std::cout << ">>>>>>>>>>training time = " << t.elapsed() << std::endl;
00393 
00394         std::cout << "EFF model computed" << std::endl;
00395         //EFF->loadModelFromFile("/share/goa-tz/people_detection/debug/rdata.xml",true);
00396 
00397 
00398         SubspaceAnalysis::FaceRecognizer* EFF_depth = new SubspaceAnalysis::FaceRecognizer();
00399         //if(use_xyz)
00400         //{
00401         //EFF_depth->trainModel(dm_vec,label_vec,ss_dim,method,true,false);
00402         //}
00403 
00404 
00405         //restart timer
00406         t.restart();
00407         for (int i = 0; i < probe_mat_vec.size(); i++)
00408         {
00409                 cv::Mat probe = probe_mat_vec[i];
00410                 int c_EFF;
00411                 cv::Mat coeff_EFF;
00412                 double DFFS_EFF;
00413                 cv::Mat probabilities;
00414                 if (method == SubspaceAnalysis::METH_LDA2D || method == SubspaceAnalysis::METH_PCA2D)
00415                 {
00416                         std::vector<cv::Mat> coeff_EFF_vec;
00417                         EFF->projectToSubspace2D(probe, coeff_EFF_vec, DFFS_EFF);
00418                         EFF->classify(coeff_EFF_vec[0], classifier, c_EFF, probabilities);
00419                 }
00420                 else
00421                 {
00422                         EFF->projectToSubspace(probe, coeff_EFF, DFFS_EFF);
00423                         EFF->classify(coeff_EFF, classifier, c_EFF, probabilities);
00424                 }
00425 
00426                 //c_EFF=model->predict(probe);
00427                 //std::cout<<"RGB CLASS"<<c_EFF<<std::endl;
00428 
00429                 //For use with depth data
00430                 int c_EFF_dm;
00431                 cv::Mat coeff_EFF_dm;
00432                 double DFFS_EFF_dm;
00433                 //if(use_xyz)
00434                 //{
00435                 cv::Mat probe_dm = probe_dm_vec[i];
00436                 //EFF_depth->projectToSubspace(probe_dm,coeff_EFF_dm,DFFS_EFF_dm);
00437                 //EFF_depth->classify(coeff_EFF_dm,classifier,c_EFF_dm);
00439 
00440                 //}
00441 
00442                 //Output to classified file
00443                 os << c_EFF << "\n";
00444                 probabilities_os << probabilities << "\n";
00445         }
00446         std::cout << ">>>>>>>>>>recognition time = " << t.elapsed() << std::endl;
00447         timing_os << t.elapsed() << std::endl;
00448 
00449         os.close();
00450         std::cout << "EFF classified" << std::endl;
00451 
00452         cv::Mat m1_evec, m1_eval, m1_avg, m1_pmd;
00453 
00454         //EFF->getModel(m1_evec,m1_eval,m1_avg,m1_pmd);
00455         //EFF->saveModel("/share/goa-tz/people_detection/debug/test.xml");
00456 
00457 
00458         //SubspaceAnalysis::FaceRecognizer* m2=new SubspaceAnalysis::FaceRecognizer();
00459 
00460 
00461         //m2->loadModel(m1_evec,m1_eval,m1_avg,m1_pmd,label_vec,false);
00462         //m2->loadModelFromFile("/share/goa-tz/people_detection/debug/rdata.xml",true);
00463 
00464         //double m2_dffs;
00465         //cv::Mat m2_coeff;
00466         //int m2_c;
00467         //cv::Mat probe=probe_mat_vec[0];
00468         //m2->projectToSubspace(probe,m2_coeff,m2_dffs);
00469         //m2->classify(m2_coeff,classifier,m2_c);
00470 
00471 
00472         // The following line predicts the label of a given
00473         // test image:
00474         //int predictedLabel = model->predict(testSample);
00475 
00476 
00477         return 0;
00478 }


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