int run_video_mode(const Configuration &cfg, const CommandLineArgument<std::string> &image_argument, const CommandLineArgument<std::string> &landmarks_argument) { FaceTracker *tracker = LoadFaceTracker(cfg.model_pathname.c_str()); FaceTrackerParams *tracker_params = LoadFaceTrackerParams(cfg.params_pathname.c_str()); assert(tracker); assert(tracker_params); cv::VideoCapture input(image_argument->c_str()); if (!input.isOpened()) throw make_runtime_error("Unable to open video file '%s'", image_argument->c_str()); cv::Mat image; std::vector<char> pathname_buffer; pathname_buffer.resize(1000); input >> image; int frame_number = 1; while ((image.rows > 0) && (image.cols > 0)) { if (cfg.verbose) { printf(" Frame number %d\r", frame_number); fflush(stdout); } cv::Mat_<uint8_t> gray_image; if (image.type() == cv::DataType<cv::Vec<uint8_t,3> >::type) cv::cvtColor(image, gray_image, CV_BGR2GRAY); else if (image.type() == cv::DataType<uint8_t>::type) gray_image = image; else throw make_runtime_error("Do not know how to convert video frame to a grayscale image."); int result = tracker->Track(gray_image, tracker_params); std::vector<cv::Point_<double> > shape; std::vector<cv::Point3_<double> > shape3D; Pose pose; if (result >= cfg.tracking_threshold) { shape = tracker->getShape(); shape3D = tracker->get3DShape(); pose = tracker->getPose(); } else { tracker->Reset(); } if (!have_argument_p(landmarks_argument)) { display_data(cfg, image, shape, pose); } else if (shape.size() > 0) { snprintf(pathname_buffer.data(), pathname_buffer.size(), landmarks_argument->c_str(), frame_number); if (cfg.save_3d_points) save_points3(pathname_buffer.data(), shape3D); else save_points(pathname_buffer.data(), shape); if (cfg.verbose) display_data(cfg, image, shape, pose); } else if (cfg.verbose) { display_data(cfg, image, shape, pose); } input >> image; frame_number++; } delete tracker; delete tracker_params; return 0; }
// Helpers int run_lists_mode(const Configuration &cfg, const CommandLineArgument<std::string> &image_argument, const CommandLineArgument<std::string> &landmarks_argument) { FaceTracker * tracker = LoadFaceTracker(cfg.model_pathname.c_str()); FaceTrackerParams *tracker_params = LoadFaceTrackerParams(cfg.params_pathname.c_str()); std::list<std::string> image_pathnames = read_list(image_argument->c_str()); std::list<std::string> landmark_pathnames; if (have_argument_p(landmarks_argument)) { landmark_pathnames = read_list(landmarks_argument->c_str()); if (landmark_pathnames.size() != image_pathnames.size()) throw make_runtime_error("Number of pathnames in list '%s' does not match the number in '%s'", image_argument->c_str(), landmarks_argument->c_str()); } std::list<std::string>::const_iterator image_it = image_pathnames.begin(); std::list<std::string>::const_iterator landmarks_it = landmark_pathnames.begin(); const int number_of_images = image_pathnames.size(); int current_image_index = 1; for (; image_it != image_pathnames.end(); image_it++) { if (cfg.verbose) { printf(" Image %d/%d\r", current_image_index, number_of_images); fflush(stdout); } current_image_index++; cv::Mat image; cv::Mat_<uint8_t> gray_image = load_grayscale_image(image_it->c_str(), &image); int result = tracker->NewFrame(gray_image, tracker_params); std::vector<cv::Point_<double> > shape; std::vector<cv::Point3_<double> > shape3D; Pose pose; if (result >= cfg.tracking_threshold) { shape = tracker->getShape(); shape3D = tracker->get3DShape(); pose = tracker->getPose(); } else { tracker->Reset(); } if (!have_argument_p(landmarks_argument)) { display_data(cfg, image, shape, pose); } else if (shape.size() > 0) { if (cfg.save_3d_points) save_points3(landmarks_it->c_str(), shape3D); else save_points(landmarks_it->c_str(), shape); if (cfg.verbose) display_data(cfg, image, shape, pose); } else if (cfg.verbose) { display_data(cfg, image, shape, pose); } if (have_argument_p(landmarks_argument)) landmarks_it++; } delete tracker; delete tracker_params; return 0; }