void load( std::string model_path ) { eigenfaceRecognizor->load( model_path + "/eigenface.xml"); fisherfaceRecognizor->load( model_path + "/fisherface.xml"); LBPHRecognizor->load( model_path + "/LBPH.xml"); }
int main(int argc, char **argv) { bool try_flip = false; std::string scale_param = "--scale="; size_t scale_param_length = scale_param.length(); std::string cascade_param = "--cascade="; size_t cascade_param_length = cascade_param.length(); std::string nested_cascade_param = "--nested-cascade="; size_t nested_cascade_param_length = nested_cascade_param.length(); std::string try_flip_param = "--try-flip="; size_t try_flip_param_length = try_flip_param.length(); std::string name_input; double scale = 1.0; cv::Mat frame; cv::Mat frame_copy; cv::Mat image; cv::CascadeClassifier cascade; cv::CascadeClassifier nested_cascade; CvCapture *capture = 0; g_face_recognition.getUseHelp(); // Load trained model. g_model = cv::createLBPHFaceRecognizer(); g_model->load(g_save_model_path); std::cout << "*************************" << std::endl << " Load model finished " << std::endl << "*************************" << std::endl << std::endl; // Load command line parameters. for (int i = 1; i < argc; i++) { std::cout << "Processing " << i << " " << argv[i] << std::endl; if (cascade_param.compare(0, cascade_param_length, argv[i], cascade_param_length) == 0) { g_name_cascade.assign(argv[i] + cascade_param_length); std::cout << "Cascade name: " << g_name_cascade << std::endl; } else if (nested_cascade_param.compare(0, nested_cascade_param_length, argv[i], nested_cascade_param_length) == 0) { if (argv[i][nested_cascade_param.length()] == '=') { g_name_nested_cascade.assign(argv[i] + nested_cascade_param.length() + 1); } if (!nested_cascade.load(g_name_nested_cascade)) { std::cerr << "Load nested cascade classifier failed!" << std::endl; } } else if (scale_param.compare(0, scale_param_length, argv[i], scale_param_length) == 0) { if(!sscanf(argv[i] + scale_param.length(), "%lf", &scale) || scale < 1) { scale = 1; } std::cout << "Scale: " << scale << std::endl; } else if (try_flip_param.compare(0, try_flip_param_length, argv[i], try_flip_param_length) == 0) { try_flip = true; std::cout << " Try to flip image horizontally." << std::endl; } else if (argv[i][0] == '-') { std::cerr << "Unknown option %s" << argv[i] << std::endl; } else { name_input.assign(argv[i]); } } if (!cascade.load(g_name_cascade)) { std::cerr << "Load cascade classifier failed!" << std::endl; g_face_recognition.getUseHelp(); return -1; } if (name_input.empty() || (isdigit(name_input.c_str()[0]) && name_input.c_str()[1]) == '\0') { capture = cvCaptureFromCAM(name_input.empty() ? 0 : name_input.c_str()[0] - '0'); int camera = name_input.empty() ? 0 : name_input.c_str()[0] - '0'; if (!capture) { std::cerr << "Capture from CAM " << camera << "doesn't work!" << std::endl; } } else if (name_input.size()) { image = cv::imread(name_input, 1); if (image.empty()) { capture = cvCaptureFromAVI(name_input.c_str()); if (!capture) { std::cerr << "Capture from AVI doesn't work!" << std::endl; } } } else { image = cv::imread("lena.jpg", 1); if (image.empty()) { std::cerr << "Read lena.jpg failed!" << std::endl; } } cvNamedWindow("Face Recognition", 1); if (capture) { std::cout << "In capture..." << std::endl; for (;;) { IplImage *ipl_image = cvQueryFrame(capture); frame = ipl_image; while (frame.dims == 0) { ipl_image = cvQueryFrame(capture); frame = ipl_image; } if (frame.empty()) { break; } if (ipl_image->origin == IPL_ORIGIN_TL) { frame.copyTo(frame_copy); } else { cv::flip(frame, frame_copy, 0); } g_face_recognition.detectAndDraw(frame_copy, cascade, nested_cascade, scale, try_flip); // Press any keys to close the camera and exit. if (cv::waitKey(10) > 0) { goto cleanup; } } cv::waitKey(0); cleanup: cvReleaseCapture(&capture); } else { std::cout << "In image read..." << std::endl; if (!image.empty()) { g_face_recognition.detectAndDraw(image, cascade, nested_cascade, scale, try_flip); cv::waitKey(0); } else if (!name_input.empty()) { FILE *file = fopen(name_input.c_str(), "rt"); if (file) { char buffer[1000 + 1]; while (fgets(buffer, 1000, file)) { int length = (int)strlen(buffer); int key = 0; while (length > 0 && isspace(buffer[length - 1])) { length--; } buffer[length] = '\0'; std::cout << "File " << buffer << std::endl; image = cv::imread(buffer, 1); if (!image.empty()) { g_face_recognition.detectAndDraw(image, cascade, nested_cascade, scale, try_flip); key = cv::waitKey(0); if (key == 27 || key == 'q' || key == 'Q') { break; } } else { std::cerr << "Read image failed!" << std::endl; } } fclose(file); } } } cvDestroyWindow("Face Detection"); return 0; }