2016-04-28 19:40:36 +00:00
///////////////////////////////////////////////////////////////////////////////
2017-05-09 01:36:23 +00:00
// Copyright (C) 2017, Carnegie Mellon University and University of Cambridge,
2016-04-28 19:40:36 +00:00
// all rights reserved.
//
2017-05-09 01:36:23 +00:00
// ACADEMIC OR NON-PROFIT ORGANIZATION NONCOMMERCIAL RESEARCH USE ONLY
2016-04-28 19:40:36 +00:00
//
2017-05-09 01:36:23 +00:00
// 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
2016-04-28 19:40:36 +00:00
// * 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: an open source facial behavior analysis toolkit
2017-03-05 20:49:01 +00:00
// Tadas Baltrušaitis, Peter Robinson, and Louis-Philippe Morency
2016-04-28 19:40:36 +00:00
// in IEEE Winter Conference on Applications of Computer Vision, 2016
//
// Rendering of Eyes for Eye-Shape Registration and Gaze Estimation
2017-03-05 20:49:01 +00:00
// Erroll Wood, Tadas Baltrušaitis, Xucong Zhang, Yusuke Sugano, Peter Robinson, and Andreas Bulling
2016-04-28 19:40:36 +00:00
// in IEEE International. Conference on Computer Vision (ICCV), 2015
//
// Cross-dataset learning and person-speci?c normalisation for automatic Action Unit detection
2017-03-05 20:49:01 +00:00
// Tadas Baltrušaitis, Marwa Mahmoud, and Peter Robinson
2016-04-28 19:40:36 +00:00
// in Facial Expression Recognition and Analysis Challenge,
// IEEE International Conference on Automatic Face and Gesture Recognition, 2015
//
// Constrained Local Neural Fields for robust facial landmark detection in the wild.
2017-03-05 20:49:01 +00:00
// Tadas Baltrušaitis, Peter Robinson, and Louis-Philippe Morency.
2016-04-28 19:40:36 +00:00
// in IEEE Int. Conference on Computer Vision Workshops, 300 Faces in-the-Wild Challenge, 2013.
//
///////////////////////////////////////////////////////////////////////////////
// FeatureExtraction.cpp : Defines the entry point for the feature extraction console application.
// System includes
# include <fstream>
# include <sstream>
// OpenCV includes
# include <opencv2/videoio/videoio.hpp> // Video write
# include <opencv2/videoio/videoio_c.h> // Video write
# include <opencv2/imgproc.hpp>
# include <opencv2/highgui/highgui.hpp>
// Boost includes
# include <filesystem.hpp>
# include <filesystem/fstream.hpp>
# include <boost/algorithm/string.hpp>
// Local includes
# include "LandmarkCoreIncludes.h"
# include <Face_utils.h>
# include <FaceAnalyser.h>
# include <GazeEstimation.h>
2017-11-01 20:29:57 +00:00
# include <RecorderOpenFace.h>
2017-11-03 16:40:07 +00:00
# include <RecorderOpenFaceParameters.h>
2017-11-08 21:35:27 +00:00
# include <SequenceCapture.h>
2017-11-11 11:57:57 +00:00
# include <Visualizer.h>
2017-11-13 09:07:52 +00:00
# include <VisualizationUtils.h>
2016-04-28 19:40:36 +00:00
2016-12-29 23:40:03 +00:00
# ifndef CONFIG_DIR
# define CONFIG_DIR "~"
# endif
2016-04-28 19:40:36 +00:00
# define INFO_STREAM( stream ) \
std : : cout < < stream < < std : : endl
# define WARN_STREAM( stream ) \
std : : cout < < " Warning: " < < stream < < std : : endl
# define ERROR_STREAM( stream ) \
std : : cout < < " Error: " < < stream < < std : : endl
static void printErrorAndAbort ( const std : : string & error )
{
std : : cout < < error < < std : : endl ;
}
# define FATAL_STREAM( stream ) \
printErrorAndAbort ( std : : string ( " Fatal error: " ) + stream )
using namespace std ;
vector < string > get_arguments ( int argc , char * * argv )
{
vector < string > arguments ;
// First argument is reserved for the name of the executable
for ( int i = 0 ; i < argc ; + + i )
{
arguments . push_back ( string ( argv [ i ] ) ) ;
}
return arguments ;
}
2016-05-19 15:51:32 +00:00
int main ( int argc , char * * argv )
2016-04-28 19:40:36 +00:00
{
2016-05-19 15:51:32 +00:00
vector < string > arguments = get_arguments ( argc , argv ) ;
2016-04-28 19:40:36 +00:00
2017-10-23 16:58:35 +00:00
// Load the modules that are being used for tracking and face analysis
// Load face landmark detector
LandmarkDetector : : FaceModelParameters det_parameters ( arguments ) ;
// Always track gaze in feature extraction
LandmarkDetector : : CLNF face_model ( det_parameters . model_location ) ;
2016-04-28 19:40:36 +00:00
2017-10-23 16:58:35 +00:00
// Load facial feature extractor and AU analyser
FaceAnalysis : : FaceAnalyserParameters face_analysis_params ( arguments ) ;
FaceAnalysis : : FaceAnalyser face_analyser ( face_analysis_params ) ;
2017-03-08 16:46:50 +00:00
2017-11-09 18:25:49 +00:00
Utilities : : SequenceCapture sequence_reader ;
2017-11-08 21:35:27 +00:00
2017-11-11 11:57:57 +00:00
// A utility for visualizing the results
Utilities : : Visualizer visualizer ( arguments ) ;
2017-11-13 09:07:52 +00:00
// Tracking FPS for visualization
Utilities : : FpsTracker fps_tracker ;
fps_tracker . AddFrame ( ) ;
2017-11-08 21:35:27 +00:00
while ( true ) // this is not a for loop as we might also be reading from a webcam
2016-04-28 19:40:36 +00:00
{
2017-11-05 08:24:42 +00:00
2017-11-09 18:25:49 +00:00
// The sequence reader chooses what to open based on command line arguments provided
if ( ! sequence_reader . Open ( arguments ) )
2017-11-08 21:35:27 +00:00
break ;
2017-11-05 08:24:42 +00:00
2017-11-09 18:25:49 +00:00
INFO_STREAM ( " Device or file opened " ) ;
2017-11-08 21:35:27 +00:00
2017-11-24 17:16:15 +00:00
if ( sequence_reader . IsWebcam ( ) )
{
INFO_STREAM ( " WARNING: using a webcam in feature extraction, Action Unit predictions will not be as accurate in real-time webcam mode " ) ;
}
2016-04-28 19:40:36 +00:00
cv : : Mat captured_image ;
2017-11-08 21:35:27 +00:00
2017-11-09 18:25:49 +00:00
Utilities : : RecorderOpenFaceParameters recording_params ( arguments , true , sequence_reader . fps ) ;
2017-11-09 18:37:26 +00:00
Utilities : : RecorderOpenFace open_face_rec ( sequence_reader . name , recording_params , arguments ) ;
2016-04-28 19:40:36 +00:00
2017-11-16 09:00:47 +00:00
if ( recording_params . outputGaze ( ) & & ! face_model . eye_model )
cout < < " WARNING: no eye model defined, but outputting gaze " < < endl ;
2017-11-09 18:25:49 +00:00
captured_image = sequence_reader . GetNextFrame ( ) ;
2016-04-28 19:40:36 +00:00
2017-11-09 19:56:16 +00:00
// For reporting progress
double reported_completion = 0 ;
2017-11-05 08:24:42 +00:00
INFO_STREAM ( " Starting tracking " ) ;
while ( ! captured_image . empty ( ) )
{
2016-04-28 19:40:36 +00:00
2017-11-09 18:25:49 +00:00
// Converting to grayscale
cv : : Mat_ < uchar > grayscale_image = sequence_reader . GetGrayFrame ( ) ;
2017-11-05 08:24:42 +00:00
2016-04-28 19:40:36 +00:00
// The actual facial landmark detection / tracking
2017-11-09 18:25:49 +00:00
bool detection_success = LandmarkDetector : : DetectLandmarksInVideo ( grayscale_image , face_model , det_parameters ) ;
2017-11-05 08:24:42 +00:00
2016-04-28 19:40:36 +00:00
// Gaze tracking, absolute gaze direction
2017-11-11 11:57:57 +00:00
cv : : Point3f gazeDirection0 ( 0 , 0 , - 1 ) ; cv : : Point3f gazeDirection1 ( 0 , 0 , - 1 ) ; cv : : Vec2d gazeAngle ( 0 , 0 ) ;
2016-04-28 19:40:36 +00:00
2017-11-16 09:00:47 +00:00
if ( detection_success & & face_model . eye_model )
2016-04-28 19:40:36 +00:00
{
2017-11-09 18:37:26 +00:00
GazeAnalysis : : EstimateGaze ( face_model , gazeDirection0 , sequence_reader . fx , sequence_reader . fy , sequence_reader . cx , sequence_reader . cy , true ) ;
GazeAnalysis : : EstimateGaze ( face_model , gazeDirection1 , sequence_reader . fx , sequence_reader . fy , sequence_reader . cx , sequence_reader . cy , false ) ;
2017-10-26 07:50:15 +00:00
gazeAngle = GazeAnalysis : : GetGazeAngle ( gazeDirection0 , gazeDirection1 ) ;
2016-04-28 19:40:36 +00:00
}
// Do face alignment
cv : : Mat sim_warped_img ;
2017-11-11 11:57:57 +00:00
cv : : Mat_ < double > hog_descriptor ; int num_hog_rows = 0 , num_hog_cols = 0 ;
2016-04-28 19:40:36 +00:00
2017-11-11 11:57:57 +00:00
// Perform AU detection and HOG feature extraction, as this can be expensive only compute it if needed by output or visualization
2017-11-11 21:13:29 +00:00
if ( recording_params . outputAlignedFaces ( ) | | recording_params . outputHOG ( ) | | recording_params . outputAUs ( ) | | visualizer . vis_align | | visualizer . vis_hog )
2016-04-28 19:40:36 +00:00
{
2017-11-24 17:16:15 +00:00
face_analyser . AddNextFrame ( captured_image , face_model . detected_landmarks , face_model . detection_success , sequence_reader . time_stamp , sequence_reader . IsWebcam ( ) ) ;
2016-04-28 19:40:36 +00:00
face_analyser . GetLatestAlignedFace ( sim_warped_img ) ;
2017-11-11 11:57:57 +00:00
face_analyser . GetLatestHOG ( hog_descriptor , num_hog_rows , num_hog_cols ) ;
2016-04-28 19:40:36 +00:00
}
// Work out the pose of the head from the tracked model
2017-11-09 18:37:26 +00:00
cv : : Vec6d pose_estimate = LandmarkDetector : : GetPose ( face_model , sequence_reader . fx , sequence_reader . fy , sequence_reader . cx , sequence_reader . cy ) ;
2016-04-28 19:40:36 +00:00
2017-11-13 09:07:52 +00:00
// Keeping track of FPS
fps_tracker . AddFrame ( ) ;
2017-11-11 11:57:57 +00:00
// Displaying the tracking visualizations
visualizer . SetImage ( captured_image , sequence_reader . fx , sequence_reader . fy , sequence_reader . cx , sequence_reader . cy ) ;
visualizer . SetObservationFaceAlign ( sim_warped_img ) ;
visualizer . SetObservationHOG ( hog_descriptor , num_hog_rows , num_hog_cols ) ;
visualizer . SetObservationLandmarks ( face_model . detected_landmarks , face_model . detection_certainty , detection_success ) ;
2017-11-12 10:55:47 +00:00
visualizer . SetObservationPose ( pose_estimate , face_model . detection_certainty ) ;
2017-11-13 17:30:19 +00:00
visualizer . SetObservationGaze ( gazeDirection0 , gazeDirection1 , LandmarkDetector : : CalculateAllEyeLandmarks ( face_model ) , LandmarkDetector : : Calculate3DEyeLandmarks ( face_model , sequence_reader . fx , sequence_reader . fy , sequence_reader . cx , sequence_reader . cy ) , face_model . detection_certainty ) ;
2017-11-13 09:07:52 +00:00
visualizer . SetFps ( fps_tracker . GetFPS ( ) ) ;
2017-11-22 17:53:15 +00:00
// detect key presses
char character_press = visualizer . ShowObservation ( ) ;
// quit processing the current sequence (useful when in Webcam mode)
if ( character_press = = ' q ' )
{
break ;
}
2017-11-11 11:57:57 +00:00
2017-11-03 09:04:00 +00:00
// Setting up the recorder output
2017-11-05 08:14:01 +00:00
open_face_rec . SetObservationHOG ( 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
2017-11-11 11:57:57 +00:00
open_face_rec . SetObservationVisualization ( visualizer . GetVisImage ( ) ) ;
2017-11-05 08:14:01 +00:00
open_face_rec . SetObservationActionUnits ( face_analyser . GetCurrentAUsReg ( ) , face_analyser . GetCurrentAUsClass ( ) ) ;
2017-11-09 19:56:16 +00:00
open_face_rec . SetObservationLandmarks ( face_model . detected_landmarks , face_model . GetShape ( sequence_reader . fx , sequence_reader . fy , sequence_reader . cx , sequence_reader . cy ) ,
face_model . params_global , face_model . params_local , face_model . detection_certainty , detection_success ) ;
2017-11-05 08:14:01 +00:00
open_face_rec . SetObservationPose ( pose_estimate ) ;
2017-11-12 13:26:51 +00:00
open_face_rec . SetObservationGaze ( gazeDirection0 , gazeDirection1 , gazeAngle , LandmarkDetector : : CalculateAllEyeLandmarks ( face_model ) , LandmarkDetector : : Calculate3DEyeLandmarks ( face_model , sequence_reader . fx , sequence_reader . fy , sequence_reader . cx , sequence_reader . cy ) ) ;
2017-11-09 19:56:16 +00:00
open_face_rec . SetObservationTimestamp ( sequence_reader . time_stamp ) ;
2017-11-09 21:28:35 +00:00
open_face_rec . SetObservationFaceAlign ( sim_warped_img ) ;
2017-11-05 08:14:01 +00:00
open_face_rec . WriteObservation ( ) ;
2017-11-11 11:57:57 +00:00
2017-11-10 08:57:34 +00:00
// Reporting progress
2017-11-09 19:56:16 +00:00
if ( sequence_reader . GetProgress ( ) > = reported_completion / 10.0 )
2016-04-28 19:40:36 +00:00
{
2017-11-09 19:56:16 +00:00
cout < < reported_completion * 10 < < " % " ;
2017-11-24 17:16:15 +00:00
if ( reported_completion = = 10 )
{
cout < < endl ;
}
2017-11-09 19:56:16 +00:00
reported_completion = reported_completion + 1 ;
2016-04-28 19:40:36 +00:00
}
2017-11-11 11:57:57 +00:00
// Grabbing the next frame in the sequence
captured_image = sequence_reader . GetNextFrame ( ) ;
2016-04-28 19:40:36 +00:00
}
2017-11-05 08:14:01 +00:00
open_face_rec . Close ( ) ;
2016-04-28 19:40:36 +00:00
2017-11-09 19:56:16 +00:00
if ( recording_params . outputAUs ( ) )
2016-04-28 19:40:36 +00:00
{
2017-11-11 11:57:57 +00:00
INFO_STREAM ( " Postprocessing the Action Unit predictions " ) ;
2017-11-05 09:21:55 +00:00
face_analyser . PostprocessOutputFile ( open_face_rec . GetCSVFile ( ) ) ;
2016-04-28 19:40:36 +00:00
}
2017-11-05 09:21:55 +00:00
2016-04-28 19:40:36 +00:00
// Reset the models for the next video
face_analyser . Reset ( ) ;
face_model . Reset ( ) ;
}
return 0 ;
}