open source pkg v1
This commit is contained in:
255
pkg/OpenFace/exe/FaceLandmarkImg/FaceLandmarkImg.cpp
Normal file
255
pkg/OpenFace/exe/FaceLandmarkImg/FaceLandmarkImg.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright (C) 2017, Carnegie Mellon University and University of Cambridge,
|
||||
// all rights reserved.
|
||||
//
|
||||
// ACADEMIC OR NON-PROFIT ORGANIZATION NONCOMMERCIAL RESEARCH USE ONLY
|
||||
//
|
||||
// BY USING OR DOWNLOADING THE SOFTWARE, YOU ARE AGREEING TO THE TERMS OF THIS LICENSE AGREEMENT.
|
||||
// IF YOU DO NOT AGREE WITH THESE TERMS, YOU MAY NOT USE OR DOWNLOAD THE SOFTWARE.
|
||||
//
|
||||
// License can be found in OpenFace-license.txt
|
||||
|
||||
// * Any publications arising from the use of this software, including but
|
||||
// not limited to academic journal and conference publications, technical
|
||||
// reports and manuals, must cite at least one of the following works:
|
||||
//
|
||||
// OpenFace 2.0: Facial Behavior Analysis Toolkit
|
||||
// Tadas Baltrušaitis, Amir Zadeh, Yao Chong Lim, and Louis-Philippe Morency
|
||||
// in IEEE International Conference on Automatic Face and Gesture Recognition, 2018
|
||||
//
|
||||
// Convolutional experts constrained local model for facial landmark detection.
|
||||
// A. Zadeh, T. Baltrušaitis, and Louis-Philippe Morency,
|
||||
// in Computer Vision and Pattern Recognition Workshops, 2017.
|
||||
//
|
||||
// Rendering of Eyes for Eye-Shape Registration and Gaze Estimation
|
||||
// Erroll Wood, Tadas Baltrušaitis, Xucong Zhang, Yusuke Sugano, Peter Robinson, and Andreas Bulling
|
||||
// in IEEE International. Conference on Computer Vision (ICCV), 2015
|
||||
//
|
||||
// Cross-dataset learning and person-specific normalisation for automatic Action Unit detection
|
||||
// Tadas Baltrušaitis, Marwa Mahmoud, and Peter Robinson
|
||||
// in Facial Expression Recognition and Analysis Challenge,
|
||||
// IEEE International Conference on Automatic Face and Gesture Recognition, 2015
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// FaceLandmarkImg.cpp : Defines the entry point for the console application for detecting landmarks in images.
|
||||
|
||||
// dlib
|
||||
#include <dlib/image_processing/frontal_face_detector.h>
|
||||
|
||||
#include "LandmarkCoreIncludes.h"
|
||||
|
||||
#include <FaceAnalyser.h>
|
||||
#include <GazeEstimation.h>
|
||||
|
||||
#include <ImageCapture.h>
|
||||
#include <Visualizer.h>
|
||||
#include <VisualizationUtils.h>
|
||||
#include <RecorderOpenFace.h>
|
||||
#include <RecorderOpenFaceParameters.h>
|
||||
|
||||
|
||||
#ifndef CONFIG_DIR
|
||||
#define CONFIG_DIR "~"
|
||||
#endif
|
||||
|
||||
std::vector<std::string> get_arguments(int argc, char **argv)
|
||||
{
|
||||
|
||||
std::vector<std::string> arguments;
|
||||
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
arguments.push_back(std::string(argv[i]));
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
//Convert arguments to more convenient vector form
|
||||
std::vector<std::string> arguments = get_arguments(argc, argv);
|
||||
|
||||
// no arguments: output usage
|
||||
if (arguments.size() == 1)
|
||||
{
|
||||
std::cout << "For command line arguments see:" << std::endl;
|
||||
std::cout << " https://github.com/TadasBaltrusaitis/OpenFace/wiki/Command-line-arguments";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Prepare for image reading
|
||||
Utilities::ImageCapture image_reader;
|
||||
|
||||
// The sequence reader chooses what to open based on command line arguments provided
|
||||
if (!image_reader.Open(arguments))
|
||||
{
|
||||
std::cout << "Could not open any images" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Load the models if images found
|
||||
LandmarkDetector::FaceModelParameters det_parameters(arguments);
|
||||
|
||||
// The modules that are being used for tracking
|
||||
std::cout << "Loading the model" << std::endl;
|
||||
LandmarkDetector::CLNF face_model(det_parameters.model_location);
|
||||
|
||||
if (!face_model.loaded_successfully)
|
||||
{
|
||||
std::cout << "ERROR: Could not load the landmark detector" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Model loaded" << std::endl;
|
||||
|
||||
// Load facial feature extractor and AU analyser (make sure it is static)
|
||||
FaceAnalysis::FaceAnalyserParameters face_analysis_params(arguments);
|
||||
face_analysis_params.OptimizeForImages();
|
||||
FaceAnalysis::FaceAnalyser face_analyser(face_analysis_params);
|
||||
|
||||
// If bounding boxes not provided, use a face detector
|
||||
cv::CascadeClassifier classifier(det_parameters.haar_face_detector_location);
|
||||
dlib::frontal_face_detector face_detector_hog = dlib::get_frontal_face_detector();
|
||||
LandmarkDetector::FaceDetectorMTCNN face_detector_mtcnn(det_parameters.mtcnn_face_detector_location);
|
||||
|
||||
// If can't find MTCNN face detector, default to HOG one
|
||||
if (det_parameters.curr_face_detector == LandmarkDetector::FaceModelParameters::MTCNN_DETECTOR && face_detector_mtcnn.empty())
|
||||
{
|
||||
std::cout << "INFO: defaulting to HOG-SVM face detector" << std::endl;
|
||||
det_parameters.curr_face_detector = LandmarkDetector::FaceModelParameters::HOG_SVM_DETECTOR;
|
||||
}
|
||||
|
||||
// A utility for visualizing the results
|
||||
Utilities::Visualizer visualizer(arguments);
|
||||
|
||||
cv::Mat rgb_image;
|
||||
|
||||
rgb_image = image_reader.GetNextImage();
|
||||
|
||||
if (!face_model.eye_model)
|
||||
{
|
||||
std::cout << "WARNING: no eye model found" << std::endl;
|
||||
}
|
||||
|
||||
if (face_analyser.GetAUClassNames().size() == 0 && face_analyser.GetAUClassNames().size() == 0)
|
||||
{
|
||||
std::cout << "WARNING: no Action Unit models found" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Starting tracking" << std::endl;
|
||||
while (!rgb_image.empty())
|
||||
{
|
||||
|
||||
Utilities::RecorderOpenFaceParameters recording_params(arguments, false, false,
|
||||
image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy);
|
||||
|
||||
if (!face_model.eye_model)
|
||||
{
|
||||
recording_params.setOutputGaze(false);
|
||||
}
|
||||
Utilities::RecorderOpenFace open_face_rec(image_reader.name, recording_params, arguments);
|
||||
|
||||
visualizer.SetImage(rgb_image, image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy);
|
||||
|
||||
// Making sure the image is in uchar grayscale (some face detectors use RGB, landmark detector uses grayscale)
|
||||
cv::Mat_<uchar> grayscale_image = image_reader.GetGrayFrame();
|
||||
|
||||
// Detect faces in an image
|
||||
std::vector<cv::Rect_<float> > face_detections;
|
||||
|
||||
if (image_reader.has_bounding_boxes)
|
||||
{
|
||||
face_detections = image_reader.GetBoundingBoxes();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (det_parameters.curr_face_detector == LandmarkDetector::FaceModelParameters::HOG_SVM_DETECTOR)
|
||||
{
|
||||
std::vector<float> confidences;
|
||||
LandmarkDetector::DetectFacesHOG(face_detections, grayscale_image, face_detector_hog, confidences);
|
||||
}
|
||||
else if (det_parameters.curr_face_detector == LandmarkDetector::FaceModelParameters::HAAR_DETECTOR)
|
||||
{
|
||||
LandmarkDetector::DetectFaces(face_detections, grayscale_image, classifier);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::vector<float> confidences;
|
||||
LandmarkDetector::DetectFacesMTCNN(face_detections, rgb_image, face_detector_mtcnn, confidences);
|
||||
}
|
||||
}
|
||||
|
||||
// Detect landmarks around detected faces
|
||||
int face_det = 0;
|
||||
// perform landmark detection for every face detected
|
||||
for (size_t face = 0; face < face_detections.size(); ++face)
|
||||
{
|
||||
|
||||
// if there are multiple detections go through them
|
||||
bool success = LandmarkDetector::DetectLandmarksInImage(rgb_image, face_detections[face], face_model, det_parameters, grayscale_image);
|
||||
|
||||
// Estimate head pose and eye gaze
|
||||
cv::Vec6d pose_estimate = LandmarkDetector::GetPose(face_model, image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy);
|
||||
|
||||
// Gaze tracking, absolute gaze direction
|
||||
cv::Point3f gaze_direction0(0, 0, -1);
|
||||
cv::Point3f gaze_direction1(0, 0, -1);
|
||||
cv::Vec2f gaze_angle(0, 0);
|
||||
|
||||
if (face_model.eye_model)
|
||||
{
|
||||
GazeAnalysis::EstimateGaze(face_model, gaze_direction0, image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy, true);
|
||||
GazeAnalysis::EstimateGaze(face_model, gaze_direction1, image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy, false);
|
||||
gaze_angle = GazeAnalysis::GetGazeAngle(gaze_direction0, gaze_direction1);
|
||||
}
|
||||
|
||||
cv::Mat sim_warped_img;
|
||||
cv::Mat_<double> hog_descriptor; int num_hog_rows = 0, num_hog_cols = 0;
|
||||
|
||||
// Perform AU detection and HOG feature extraction, as this can be expensive only compute it if needed by output or visualization
|
||||
if (recording_params.outputAlignedFaces() || recording_params.outputHOG() || recording_params.outputAUs() || visualizer.vis_align || visualizer.vis_hog)
|
||||
{
|
||||
face_analyser.PredictStaticAUsAndComputeFeatures(rgb_image, face_model.detected_landmarks);
|
||||
face_analyser.GetLatestAlignedFace(sim_warped_img);
|
||||
face_analyser.GetLatestHOG(hog_descriptor, num_hog_rows, num_hog_cols);
|
||||
}
|
||||
|
||||
// Displaying the tracking visualizations
|
||||
visualizer.SetObservationFaceAlign(sim_warped_img);
|
||||
visualizer.SetObservationHOG(hog_descriptor, num_hog_rows, num_hog_cols);
|
||||
visualizer.SetObservationLandmarks(face_model.detected_landmarks, 1.0, face_model.GetVisibilities()); // Set confidence to high to make sure we always visualize
|
||||
visualizer.SetObservationPose(pose_estimate, 1.0);
|
||||
visualizer.SetObservationGaze(gaze_direction0, gaze_direction1, LandmarkDetector::CalculateAllEyeLandmarks(face_model), LandmarkDetector::Calculate3DEyeLandmarks(face_model, image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy), face_model.detection_certainty);
|
||||
visualizer.SetObservationActionUnits(face_analyser.GetCurrentAUsReg(), face_analyser.GetCurrentAUsClass());
|
||||
|
||||
// Setting up the recorder output
|
||||
open_face_rec.SetObservationHOG(face_model.detection_success, hog_descriptor, num_hog_rows, num_hog_cols, 31); // The number of channels in HOG is fixed at the moment, as using FHOG
|
||||
open_face_rec.SetObservationActionUnits(face_analyser.GetCurrentAUsReg(), face_analyser.GetCurrentAUsClass());
|
||||
open_face_rec.SetObservationLandmarks(face_model.detected_landmarks, face_model.GetShape(image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy),
|
||||
face_model.params_global, face_model.params_local, face_model.detection_certainty, face_model.detection_success);
|
||||
open_face_rec.SetObservationPose(pose_estimate);
|
||||
open_face_rec.SetObservationGaze(gaze_direction0, gaze_direction1, gaze_angle, LandmarkDetector::CalculateAllEyeLandmarks(face_model), LandmarkDetector::Calculate3DEyeLandmarks(face_model, image_reader.fx, image_reader.fy, image_reader.cx, image_reader.cy));
|
||||
open_face_rec.SetObservationFaceAlign(sim_warped_img);
|
||||
open_face_rec.SetObservationFaceID(face);
|
||||
open_face_rec.WriteObservation();
|
||||
|
||||
}
|
||||
if (face_detections.size() > 0)
|
||||
{
|
||||
visualizer.ShowObservation();
|
||||
}
|
||||
|
||||
open_face_rec.SetObservationVisualization(visualizer.GetVisImage());
|
||||
open_face_rec.WriteObservationTracked();
|
||||
|
||||
open_face_rec.Close();
|
||||
|
||||
// Grabbing the next frame in the sequence
|
||||
rgb_image = image_reader.GetNextImage();
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user