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
00013 bool valid = true;
00014 if (normalize)
00015 {
00016 valid = fn->normalizeFace(img, xyz, norm_size, dm);
00017
00018
00019
00020
00021
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
00037 if (normalize)
00038 {
00039 valid = fn->normalizeFace(img, norm_size);
00040
00041
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
00067 std::string method_str;
00068 bool use_xyz = true;
00069 bool normalizer = false;
00070 if (argc == 1)
00071 {
00072 method_str = "EIGEN";
00073
00074 normalizer = false;
00075 }
00076
00077 else if (argc == 2)
00078 {
00079 method_str = argv[1];
00080
00081 normalizer = false;
00082 }
00083
00084 else if (argc == 3)
00085 {
00086 method_str = argv[1];
00087
00088 normalizer = false;
00089 }
00090
00091 else if (argc == 3)
00092 {
00093 method_str = argv[1];
00094
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
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
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
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 std::cout << "SSA test configuration:" << std::endl;
00179
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
00184
00185
00186
00187
00188
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
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
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
00216 probe_file_xyz_vec.push_back(probe_file_xyz);
00217
00218 }
00219 }
00220
00221
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
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
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
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
00299 }
00300 valid = true;
00301 cv::Mat dm;
00302 if (use_xyz)
00303 valid = preprocess(img, xyz, fn, normalizer, norm_size, dm);
00304
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
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
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
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
00376
00377
00378
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
00413 std::cout << "loaded" << std::endl;
00414 EFF->trainModel(img_vec, label_vec, ss_dim);
00415
00416
00417
00418
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
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
00432
00433
00434
00435
00436
00437
00438
00439
00440
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
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00464
00465
00466
00467
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
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499 EFF->saveModel(rpath);
00500 return 0;
00501 }