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