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
00014 bool valid = true;
00015 if (normalize)
00016 {
00017 valid = fn->normalizeFace(img, xyz, norm_size, dm);
00018
00019
00020
00021
00022
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
00038 if (normalize)
00039 {
00040 valid = fn->normalizeFace(img, norm_size);
00041
00042
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
00068 std::string method_str;
00069 bool use_xyz = true;
00070 bool normalizer = false;
00071 if (argc == 1)
00072 {
00073 method_str = "EIGEN";
00074
00075 normalizer = false;
00076 }
00077
00078 else if (argc == 2)
00079 {
00080 method_str = argv[1];
00081
00082 normalizer = false;
00083 }
00084
00085 else if (argc == 3)
00086 {
00087 method_str = argv[1];
00088
00089 normalizer = false;
00090 }
00091
00092 else if (argc == 3)
00093 {
00094 method_str = argv[1];
00095
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
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
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
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 std::cout << "SSA test configuration:" << std::endl;
00180
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
00185
00186
00187
00188
00189
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
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
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
00217 probe_file_xyz_vec.push_back(probe_file_xyz);
00218
00219 }
00220 }
00221
00222
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
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
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
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
00300 }
00301 valid = true;
00302 cv::Mat dm;
00303 if (use_xyz)
00304 valid = preprocess(img, xyz, fn, normalizer, norm_size, dm);
00305
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
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
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
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
00377
00378
00379
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
00414 std::cout << "loaded" << std::endl;
00415 EFF->trainModel(img_vec, label_vec, ss_dim);
00416
00417
00418
00419
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
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
00433
00434
00435
00436
00437
00438
00439
00440
00441
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
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00465
00466
00467
00468
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
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 EFF->saveModel(rpath);
00501 return 0;
00502 }