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
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// FaceTrackingVid.cpp : Defines the entry point for the console application for tracking faces in videos.
|
|
|
|
|
|
|
|
|
|
// Libraries for landmark detection (includes CLNF and CLM modules)
|
|
|
|
|
#include "LandmarkCoreIncludes.h"
|
|
|
|
|
#include "GazeEstimation.h"
|
|
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
#include <SequenceCapture.h>
|
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
|
// Boost includes
|
|
|
|
|
#include <filesystem.hpp>
|
|
|
|
|
#include <filesystem/fstream.hpp>
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define FATAL_STREAM( stream ) \
|
|
|
|
|
printErrorAndAbort( std::string( "Fatal error: " ) + stream )
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
vector<string> get_arguments(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
vector<string> arguments;
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < argc; ++i)
|
|
|
|
|
{
|
|
|
|
|
arguments.push_back(string(argv[i]));
|
|
|
|
|
}
|
|
|
|
|
return arguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Some globals for tracking timing information for visualisation
|
|
|
|
|
double fps_tracker = -1.0;
|
|
|
|
|
int64 t0 = 0;
|
2017-11-10 18:49:30 +00:00
|
|
|
|
int frame_count = 0;
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
// Visualising the results, TODO move to separate
|
|
|
|
|
void visualise_tracking(cv::Mat& captured_image, const LandmarkDetector::CLNF& face_model, const LandmarkDetector::FaceModelParameters& det_parameters, cv::Point3f gazeDirection0, cv::Point3f gazeDirection1, double fx, double fy, double cx, double cy)
|
2016-04-28 19:40:36 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Drawing the facial landmarks on the face and the bounding box around it if tracking is successful and initialised
|
|
|
|
|
double detection_certainty = face_model.detection_certainty;
|
|
|
|
|
bool detection_success = face_model.detection_success;
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
double visualisation_boundary = 0.4;
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
|
|
// Only draw if the reliability is reasonable, the value is slightly ad-hoc
|
2017-11-10 18:49:30 +00:00
|
|
|
|
if (detection_certainty > visualisation_boundary)
|
2016-04-28 19:40:36 +00:00
|
|
|
|
{
|
|
|
|
|
LandmarkDetector::Draw(captured_image, face_model);
|
|
|
|
|
|
|
|
|
|
double vis_certainty = detection_certainty;
|
|
|
|
|
if (vis_certainty > 1)
|
|
|
|
|
vis_certainty = 1;
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
// Scale from 0 to 1, to allow to indicated by colour how confident we are in the tracking
|
|
|
|
|
vis_certainty = (vis_certainty - visualisation_boundary) / (1 - visualisation_boundary);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
|
|
// A rough heuristic for box around the face width
|
|
|
|
|
int thickness = (int)std::ceil(2.0* ((double)captured_image.cols) / 640.0);
|
|
|
|
|
|
2017-10-24 15:26:08 +00:00
|
|
|
|
cv::Vec6d pose_estimate_to_draw = LandmarkDetector::GetPose(face_model, fx, fy, cx, cy);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
|
|
// Draw it in reddish if uncertain, blueish if certain
|
2017-11-10 18:49:30 +00:00
|
|
|
|
LandmarkDetector::DrawBox(captured_image, pose_estimate_to_draw, cv::Scalar(vis_certainty*255.0, 0, (1 - vis_certainty) * 255), thickness, fx, fy, cx, cy);
|
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
|
if (det_parameters.track_gaze && detection_success && face_model.eye_model)
|
|
|
|
|
{
|
2017-10-26 07:50:15 +00:00
|
|
|
|
GazeAnalysis::DrawGaze(captured_image, face_model, gazeDirection0, gazeDirection1, fx, fy, cx, cy);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
// Work out the framerate TODO
|
2016-04-28 19:40:36 +00:00
|
|
|
|
if (frame_count % 10 == 0)
|
|
|
|
|
{
|
|
|
|
|
double t1 = cv::getTickCount();
|
|
|
|
|
fps_tracker = 10.0 / (double(t1 - t0) / cv::getTickFrequency());
|
|
|
|
|
t0 = t1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write out the framerate on the image before displaying it
|
|
|
|
|
char fpsC[255];
|
|
|
|
|
std::sprintf(fpsC, "%d", (int)fps_tracker);
|
|
|
|
|
string fpsSt("FPS:");
|
|
|
|
|
fpsSt += fpsC;
|
2017-11-10 18:49:30 +00:00
|
|
|
|
cv::putText(captured_image, fpsSt, cv::Point(10, 20), CV_FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255, 0, 0), 1, CV_AA);
|
|
|
|
|
frame_count++;
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
vector<string> arguments = get_arguments(argc, argv);
|
|
|
|
|
|
|
|
|
|
LandmarkDetector::FaceModelParameters det_parameters(arguments);
|
2017-11-10 18:49:30 +00:00
|
|
|
|
det_parameters.track_gaze = true;
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
|
|
// The modules that are being used for tracking
|
|
|
|
|
LandmarkDetector::CLNF clnf_model(det_parameters.model_location);
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
// Open a sequence
|
|
|
|
|
Utilities::SequenceCapture sequence_reader;
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
2017-11-10 18:49:30 +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-10 18:49:30 +00:00
|
|
|
|
// The sequence reader chooses what to open based on command line arguments provided
|
2017-11-10 19:01:43 +00:00
|
|
|
|
if(!sequence_reader.Open(arguments) && sequence_reader.no_input_specified)
|
2016-04-28 19:40:36 +00:00
|
|
|
|
{
|
2017-11-10 18:49:30 +00:00
|
|
|
|
// If that fails, revert to webcam
|
|
|
|
|
INFO_STREAM("No input specified, attempting to open a webcam 0");
|
|
|
|
|
if (!sequence_reader.OpenWebcam(0))
|
|
|
|
|
ERROR_STREAM("Failed to open the webcam");
|
2016-04-28 19:40:36 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-11-10 18:49:30 +00:00
|
|
|
|
ERROR_STREAM("Failed to open a sequence");
|
|
|
|
|
break;
|
2016-04-28 19:40:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
INFO_STREAM("Device or file opened");
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
2017-11-10 19:01:43 +00:00
|
|
|
|
cv::Mat captured_image = sequence_reader.GetNextFrame();
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
INFO_STREAM("Starting tracking");
|
|
|
|
|
while (!captured_image.empty()) // this is not a for loop as we might also be reading from a webcam
|
2016-04-28 19:40:36 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Reading the images
|
2017-11-10 19:01:43 +00:00
|
|
|
|
cv::Mat_<uchar> grayscale_image = sequence_reader.GetGrayFrame();
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
|
|
// The actual facial landmark detection / tracking
|
2017-08-01 21:11:02 +00:00
|
|
|
|
bool detection_success = LandmarkDetector::DetectLandmarksInVideo(grayscale_image, clnf_model, det_parameters);
|
2017-11-10 18:49:30 +00:00
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
|
// Visualising the results
|
|
|
|
|
// Drawing the facial landmarks on the face and the bounding box around it if tracking is successful and initialised
|
|
|
|
|
double detection_certainty = clnf_model.detection_certainty;
|
|
|
|
|
|
|
|
|
|
// Gaze tracking, absolute gaze direction
|
|
|
|
|
cv::Point3f gazeDirection0(0, 0, -1);
|
|
|
|
|
cv::Point3f gazeDirection1(0, 0, -1);
|
|
|
|
|
|
|
|
|
|
if (det_parameters.track_gaze && detection_success && clnf_model.eye_model)
|
|
|
|
|
{
|
2017-11-10 18:49:30 +00:00
|
|
|
|
GazeAnalysis::EstimateGaze(clnf_model, gazeDirection0, sequence_reader.fx, sequence_reader.fy, sequence_reader.cx, sequence_reader.cy, true);
|
|
|
|
|
GazeAnalysis::EstimateGaze(clnf_model, gazeDirection1, sequence_reader.fx, sequence_reader.fy, sequence_reader.cx, sequence_reader.cy, false);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
visualise_tracking(captured_image, clnf_model, det_parameters, gazeDirection0, gazeDirection1, sequence_reader.fx, sequence_reader.fy, sequence_reader.cx, sequence_reader.cy);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
|
|
// detect key presses
|
|
|
|
|
char character_press = cv::waitKey(1);
|
2017-11-10 18:49:30 +00:00
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
|
// restart the tracker
|
2017-11-10 18:49:30 +00:00
|
|
|
|
if (character_press == 'r')
|
2016-04-28 19:40:36 +00:00
|
|
|
|
{
|
|
|
|
|
clnf_model.Reset();
|
|
|
|
|
}
|
|
|
|
|
// quit the application
|
2017-11-10 18:49:30 +00:00
|
|
|
|
else if (character_press == 'q')
|
2016-04-28 19:40:36 +00:00
|
|
|
|
{
|
|
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset the model, for the next video
|
|
|
|
|
clnf_model.Reset();
|
|
|
|
|
|
2017-11-10 18:49:30 +00:00
|
|
|
|
}
|
2016-04-28 19:40:36 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|