2016-04-28 21:40:36 +02:00
///////////////////////////////////////////////////////////////////////////////
2017-05-09 03:36:23 +02:00
// Copyright (C) 2017, Carnegie Mellon University and University of Cambridge,
2016-04-28 21:40:36 +02:00
// all rights reserved.
//
2017-05-09 03:36:23 +02:00
// 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
2016-04-28 21:40:36 +02: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
// Tadas Baltru<72> aitis, Peter Robinson, and Louis-Philippe Morency
// in IEEE Winter Conference on Applications of Computer Vision, 2016
//
// Rendering of Eyes for Eye-Shape Registration and Gaze Estimation
// Erroll Wood, Tadas Baltru<72> aitis, Xucong Zhang, Yusuke Sugano, Peter Robinson, and Andreas Bulling
// in IEEE International. Conference on Computer Vision (ICCV), 2015
//
// Cross-dataset learning and person-speci?c normalisation for automatic Action Unit detection
// Tadas Baltru<72> aitis, Marwa Mahmoud, and Peter Robinson
// 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.
// Tadas Baltru<72> aitis, Peter Robinson, and Louis-Philippe Morency.
// in IEEE Int. Conference on Computer Vision Workshops, 300 Faces in-the-Wild Challenge, 2013.
//
///////////////////////////////////////////////////////////////////////////////
# ifndef __FACEANALYSER_h_
# define __FACEANALYSER_h_
2017-05-18 23:04:38 +02:00
// STL includes
2016-04-28 21:40:36 +02:00
# include <string>
# include <vector>
2017-05-18 23:04:38 +02:00
# include <map>
2016-04-28 21:40:36 +02:00
2017-05-18 23:04:38 +02:00
// OpenCV includes
2016-04-28 21:40:36 +02:00
# include <opencv2/core/core.hpp>
2017-05-18 23:04:38 +02:00
// Local includes
# include "SVR_dynamic_lin_regressors.h"
# include "SVR_static_lin_regressors.h"
# include "SVM_static_lin.h"
# include "SVM_dynamic_lin.h"
# include "PDM.h"
# include "FaceAnalyserParameters.h"
2016-04-28 21:40:36 +02:00
namespace FaceAnalysis
{
class FaceAnalyser {
public :
enum RegressorType { SVR_appearance_static_linear = 0 , SVR_appearance_dynamic_linear = 1 , SVR_dynamic_geom_linear = 2 , SVR_combined_linear = 3 , SVM_linear_stat = 4 , SVM_linear_dyn = 5 , SVR_linear_static_seg = 6 , SVR_linear_dynamic_seg = 7 } ;
2017-05-18 23:04:38 +02:00
// Constructor for FaceAnalyser using the parameters structure
FaceAnalyser ( const FaceAnalysis : : FaceAnalyserParameters & face_analyser_params ) ;
2016-04-28 21:40:36 +02:00
2017-05-18 23:04:38 +02:00
void AddNextFrame ( const cv : : Mat & frame , const cv : : Mat_ < double > & detected_landmarks , bool success , double timestamp_seconds , bool online = false , bool visualise = true ) ;
2016-04-28 21:40:36 +02:00
cv : : Mat GetLatestHOGDescriptorVisualisation ( ) ;
double GetCurrentTimeSeconds ( ) ;
// Grab the current predictions about AUs from the face analyser
std : : vector < std : : pair < std : : string , double > > GetCurrentAUsClass ( ) const ; // AU presence
std : : vector < std : : pair < std : : string , double > > GetCurrentAUsReg ( ) const ; // AU intensity
std : : vector < std : : pair < std : : string , double > > GetCurrentAUsCombined ( ) const ; // Both presense and intensity
2016-06-14 23:55:16 +02:00
// A standalone call for predicting AUs from a static image, the first element in the pair represents occurence the second intensity
// This call is useful for detecting action units in images
2017-05-18 23:04:38 +02:00
std : : pair < std : : vector < std : : pair < std : : string , double > > , std : : vector < std : : pair < std : : string , double > > > PredictStaticAUs ( const cv : : Mat & frame , const cv : : Mat_ < double > & detected_landmarks , bool visualise = true ) ;
2016-06-14 23:55:16 +02:00
2016-04-28 21:40:36 +02:00
void Reset ( ) ;
void GetLatestHOG ( cv : : Mat_ < double > & hog_descriptor , int & num_rows , int & num_cols ) ;
void GetLatestAlignedFace ( cv : : Mat & image ) ;
void GetLatestNeutralHOG ( cv : : Mat_ < double > & hog_descriptor , int & num_rows , int & num_cols ) ;
cv : : Mat_ < int > GetTriangulation ( ) ;
void GetGeomDescriptor ( cv : : Mat_ < double > & geom_desc ) ;
// Grab the names of AUs being predicted
std : : vector < std : : string > GetAUClassNames ( ) const ; // Presence
std : : vector < std : : string > GetAURegNames ( ) const ; // Intensity
2016-06-03 15:33:04 +02:00
// Identify if models are static or dynamic (useful for correction and shifting)
std : : vector < bool > GetDynamicAUClass ( ) const ; // Presence
2017-05-18 23:04:38 +02:00
std : : vector < std : : pair < std : : string , bool > > GetDynamicAUReg ( ) const ; // Intensity
2016-06-03 15:33:04 +02:00
2017-05-18 23:04:38 +02:00
void ExtractAllPredictionsOfflineReg ( std : : vector < std : : pair < std : : string , std : : vector < double > > > & au_predictions ,
std : : vector < double > & confidences , std : : vector < bool > & successes , std : : vector < double > & timestamps , bool dynamic ) ;
void ExtractAllPredictionsOfflineClass ( std : : vector < std : : pair < std : : string , std : : vector < double > > > & au_predictions ,
std : : vector < double > & confidences , std : : vector < bool > & successes , std : : vector < double > & timestamps , bool dynamic ) ;
2016-04-28 21:40:36 +02:00
2016-12-09 16:06:04 +01:00
// Helper function for post-processing AU output files
2017-05-18 23:04:38 +02:00
void FaceAnalyser : : PostprocessOutputFile ( std : : string output_file ) ;
2016-12-09 16:06:04 +01:00
2016-04-28 21:40:36 +02:00
private :
2017-05-18 23:04:38 +02:00
// Point distribution model coddesponding to the current Face Analyser
FaceAnalysis : : PDM pdm ;
2016-04-28 21:40:36 +02:00
// Where the predictions are kept
std : : vector < std : : pair < std : : string , double > > AU_predictions_reg ;
std : : vector < std : : pair < std : : string , double > > AU_predictions_class ;
std : : vector < std : : pair < std : : string , double > > AU_predictions_combined ;
// Keeping track of AU predictions over time (useful for post-processing)
2017-05-18 23:04:38 +02:00
std : : vector < double > timestamps ;
std : : map < std : : string , std : : vector < double > > AU_predictions_reg_all_hist ;
std : : map < std : : string , std : : vector < double > > AU_predictions_class_all_hist ;
2016-04-28 21:40:36 +02:00
std : : vector < bool > valid_preds ;
int frames_tracking ;
2017-05-18 23:04:38 +02:00
// Is the AU model dynamic
bool dynamic ;
2016-04-28 21:40:36 +02:00
// Cache of intermediate images
2016-12-05 23:28:39 +01:00
cv : : Mat aligned_face_for_au ;
cv : : Mat aligned_face_for_output ;
2016-04-28 21:40:36 +02:00
cv : : Mat hog_descriptor_visualisation ;
2017-05-18 23:04:38 +02:00
bool out_grayscale ;
2016-04-28 21:40:36 +02:00
// Private members to be used for predictions
// The HOG descriptor of the last frame
cv : : Mat_ < double > hog_desc_frame ;
int num_hog_rows ;
int num_hog_cols ;
// Keep a running median of the hog descriptors and a aligned images
cv : : Mat_ < double > hog_desc_median ;
cv : : Mat_ < double > face_image_median ;
// Use histograms for quick (but approximate) median computation
// Use the same for
2017-05-18 23:04:38 +02:00
std : : vector < cv : : Mat_ < unsigned int > > hog_desc_hist ;
2016-04-28 21:40:36 +02:00
// This is not being used at the moment as it is a bit slow
2017-05-18 23:04:38 +02:00
std : : vector < cv : : Mat_ < unsigned int > > face_image_hist ;
std : : vector < int > face_image_hist_sum ;
2016-04-28 21:40:36 +02:00
2017-05-18 23:04:38 +02:00
std : : vector < cv : : Vec3d > head_orientations ;
2016-04-28 21:40:36 +02:00
int num_bins_hog ;
double min_val_hog ;
double max_val_hog ;
2017-05-18 23:04:38 +02:00
std : : vector < int > hog_hist_sum ;
2016-04-28 21:40:36 +02:00
int view_used ;
// The geometry descriptor (rigid followed by non-rigid shape parameters from CLNF)
cv : : Mat_ < double > geom_descriptor_frame ;
cv : : Mat_ < double > geom_descriptor_median ;
int geom_hist_sum ;
cv : : Mat_ < unsigned int > geom_desc_hist ;
int num_bins_geom ;
double min_val_geom ;
double max_val_geom ;
// Using the bounding box of previous analysed frame to determine if a reset is needed
cv : : Rect_ < double > face_bounding_box ;
// The AU predictions internally
std : : vector < std : : pair < std : : string , double > > PredictCurrentAUs ( int view ) ;
std : : vector < std : : pair < std : : string , double > > PredictCurrentAUsClass ( int view ) ;
// special step for online (rather than offline AU prediction)
2017-05-18 23:04:38 +02:00
std : : vector < std : : pair < std : : string , double > > CorrectOnlineAUs ( std : : vector < std : : pair < std : : string , double > > predictions_orig , int view , bool dyn_shift = false , bool dyn_scale = false , bool update_track = true , bool clip_values = false ) ;
void Read ( std : : string model_loc ) ;
2016-04-28 21:40:36 +02:00
void ReadAU ( std : : string au_location ) ;
2017-05-18 23:04:38 +02:00
void ReadRegressor ( std : : string fname , const std : : vector < std : : string > & au_names ) ;
2016-04-28 21:40:36 +02:00
// A utility function for keeping track of approximate running medians used for AU and emotion inference using a set of histograms (the histograms are evenly spaced from min_val to max_val)
// Descriptor has to be a row vector
// TODO this duplicates some other code
void UpdateRunningMedian ( cv : : Mat_ < unsigned int > & histogram , int & hist_sum , cv : : Mat_ < double > & median , const cv : : Mat_ < double > & descriptor , bool update , int num_bins , double min_val , double max_val ) ;
void ExtractMedian ( cv : : Mat_ < unsigned int > & histogram , int hist_count , cv : : Mat_ < double > & median , int num_bins , double min_val , double max_val ) ;
// The linear SVR regressors
SVR_static_lin_regressors AU_SVR_static_appearance_lin_regressors ;
SVR_dynamic_lin_regressors AU_SVR_dynamic_appearance_lin_regressors ;
// The linear SVM classifiers
SVM_static_lin AU_SVM_static_appearance_lin ;
SVM_dynamic_lin AU_SVM_dynamic_appearance_lin ;
// The AUs predicted by the model are not always 0 calibrated to a person. That is they don't always predict 0 for a neutral expression
// Keeping track of the predictions we can correct for this, by assuming that at least "ratio" of frames are neutral and subtract that value of prediction, only perform the correction after min_frames
2017-05-18 23:04:38 +02:00
void UpdatePredictionTrack ( cv : : Mat_ < unsigned int > & prediction_corr_histogram , int & prediction_correction_count ,
std : : vector < double > & correction , const std : : vector < std : : pair < std : : string , double > > & predictions , double ratio = 0.25 , int num_bins = 200 , double min_val = - 3 , double max_val = 5 , int min_frames = 10 ) ;
void GetSampleHist ( cv : : Mat_ < unsigned int > & prediction_corr_histogram , int prediction_correction_count ,
std : : vector < double > & sample , double ratio , int num_bins = 200 , double min_val = 0 , double max_val = 5 ) ;
2016-04-28 21:40:36 +02:00
2016-05-24 22:51:18 +02:00
void PostprocessPredictions ( ) ;
2016-04-28 21:40:36 +02:00
2017-05-18 23:04:38 +02:00
std : : vector < cv : : Mat_ < unsigned int > > au_prediction_correction_histogram ;
std : : vector < int > au_prediction_correction_count ;
2016-04-28 21:40:36 +02:00
// Some dynamic scaling (the logic is that before the extreme versions of expression or emotion are shown,
// it is hard to tell the boundaries, this allows us to scale the model to the most extreme seen)
// They have to be view specific
2017-05-18 23:04:38 +02:00
std : : vector < std : : vector < double > > dyn_scaling ;
2016-04-28 21:40:36 +02:00
// Keeping track of predictions for summary stats
cv : : Mat_ < double > AU_prediction_track ;
cv : : Mat_ < double > geom_desc_track ;
double current_time_seconds ;
// Used for face alignment
cv : : Mat_ < int > triangulation ;
2017-05-18 23:04:38 +02:00
double align_scale_au ;
int align_width_au ;
int align_height_au ;
double align_scale_out ;
int align_width_out ;
int align_height_out ;
2016-05-24 22:51:18 +02:00
// Useful placeholder for renormalizing the initial frames of shorter videos
int max_init_frames = 3000 ;
2017-05-18 23:04:38 +02:00
std : : vector < cv : : Mat_ < double > > hog_desc_frames_init ;
std : : vector < cv : : Mat_ < double > > geom_descriptor_frames_init ;
std : : vector < int > views ;
2016-05-24 22:51:18 +02:00
bool postprocessed = false ;
int frames_tracking_succ = 0 ;
2016-04-28 21:40:36 +02:00
} ;
//===========================================================================
}
# endif