/* This file was part of GSoC Project: Facemark API for OpenCV Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc Student: Laksono Kurnianggoro Mentor: Delia Passalacqua */ /*---------------------------------------------- * Usage: * facemark_demo_lbf [test_files] * * Example: * facemark_demo_lbf ../face_cascade.xml ../LBF.model ../images_train.txt ../points_train.txt ../test.txt * * Notes: * the user should provides the list of training images_train * accompanied by their corresponding landmarks location in separated files. * example of contents for images_train.txt: * ../trainset/image_0001.png * ../trainset/image_0002.png * example of contents for points_train.txt: * ../trainset/image_0001.pts * ../trainset/image_0002.pts * where the image_xxxx.pts contains the position of each face landmark. * example of the contents: * version: 1 * n_points: 68 * { * 115.167660 220.807529 * 116.164839 245.721357 * 120.208690 270.389841 * ... * } * example of the dataset is available at https://ibug.doc.ic.ac.uk/download/annotations/ibug.zip *--------------------------------------------------*/ #include #include #include #include #include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/face.hpp" using namespace std; using namespace cv; using namespace cv::face; static bool myDetector( InputArray image, OutputArray roi, CascadeClassifier *face_detector); static bool parseArguments(int argc, char** argv, String & cascade, String & model, String & images, String & annotations, String & testImages ); int main(int argc, char** argv) { String cascade_path,model_path,images_path, annotations_path, test_images_path; if(!parseArguments(argc, argv, cascade_path,model_path,images_path, annotations_path, test_images_path)) return -1; /*create the facemark instance*/ FacemarkLBF::Params params; params.model_filename = model_path; params.cascade_face = cascade_path; Ptr facemark = FacemarkLBF::create(params); CascadeClassifier face_cascade; face_cascade.load(params.cascade_face.c_str()); facemark->setFaceDetector((FN_FaceDetector)myDetector, &face_cascade); /*Loads the dataset*/ std::vector images_train; std::vector landmarks_train; loadDatasetList(images_path,annotations_path,images_train,landmarks_train); Mat image; std::vector facial_points; for(size_t i=0;iaddTrainingSample(image, facial_points); } /*train the Algorithm*/ facemark->training(); /*test using some images*/ String testFiles(images_path), testPts(annotations_path); if(!test_images_path.empty()){ testFiles = test_images_path; testPts = test_images_path; //unused } std::vector images; std::vector facePoints; loadDatasetList(testFiles, testPts, images, facePoints); std::vector rects; CascadeClassifier cc(params.cascade_face.c_str()); for(size_t i=0;i > landmarks; cout<getFaces(img, rects); facemark->fit(img, rects, landmarks); for(size_t j=0;j0){ cout< 1) cvtColor(image, gray, COLOR_BGR2GRAY); else gray = image.getMat().clone(); equalizeHist(gray, gray); std::vector faces_; face_cascade->detectMultiScale(gray, faces_, 1.4, 2, CASCADE_SCALE_IMAGE, Size(30, 30)); Mat(faces_).copyTo(faces); return true; } bool parseArguments(int argc, char** argv, String & cascade, String & model, String & images, String & annotations, String & test_images ){ const String keys = "{ @c cascade | | (required) path to the face cascade xml file fo the face detector }" "{ @i images | | (required) path of a text file contains the list of paths to all training images}" "{ @a annotations | | (required) Path of a text file contains the list of paths to all annotations files}" "{ @m model | | (required) path to save the trained model }" "{ t test-images | | Path of a text file contains the list of paths to the test images}" "{ help h usage ? | | facemark_demo_lbf -cascade -images -annotations -model [-t] \n" " example: facemark_demo_lbf ../face_cascade.xml ../images_train.txt ../points_train.txt ../lbf.model}" ; CommandLineParser parser(argc, argv,keys); parser.about("hello"); if (parser.has("help")){ parser.printMessage(); return false; } cascade = String(parser.get("cascade")); model = String(parser.get("model")); images = String(parser.get("images")); annotations = String(parser.get("annotations")); test_images = String(parser.get("t")); cout<<"cascade : "<