gaze  0.1.0
Perform gaze tracking with common webcams.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
Public Member Functions | Protected Member Functions | List of all members
gaze::pipeline::HeadPoseEstimation Class Referencefinal

Estimates the head pose from landmarks and source image. More...

#include "gaze/pipeline_steps/head_pose_estimation.h"

Inheritance diagram for gaze::pipeline::HeadPoseEstimation:
gaze::PipelineStep gaze::gui::Visualizeable< widget_type > gaze::gui::VisualizeableBase

Public Member Functions

void process (util::Data &data) override
 
void visualize (util::Data &data) override
 
- Public Member Functions inherited from gaze::PipelineStep
 PipelineStep ()
 
std::string get_name ()
 
virtual ~PipelineStep ()=default
 
- Public Member Functions inherited from gaze::gui::Visualizeable< widget_type >
std::shared_ptr< dlib::drawable > init (dlib::drawable_window &parent, int width, int height, std::string text="No visualization.") final
 
void remove_widget () override
 
- Public Member Functions inherited from gaze::gui::VisualizeableBase
virtual ~VisualizeableBase ()=default
 

Protected Member Functions

cv::Matx33d read_or_set_camera_matrix (const util::Data &data)
 
cv::Mat get_and_maybe_read_distortions (const util::Data &data)
 

Additional Inherited Members

- Protected Attributes inherited from gaze::PipelineStep
int number
 
std::string name
 
- Protected Attributes inherited from gaze::gui::Visualizeable< widget_type >
std::shared_ptr< widget_type > widget
 

Detailed Description

Estimates the head pose from landmarks and source image.

///

Todo:
shoeffner: Write documentation for HeadPoseEstimation.

Definition at line 24 of file head_pose_estimation.h.

Member Function Documentation

cv::Mat gaze::pipeline::HeadPoseEstimation::get_and_maybe_read_distortions ( const util::Data data)
protected

Returns a distortions matrix.

If the distortions matrix is available from the configuration, the configuration value is stored and used. Otherwise, it is assumed to be a zero matrix.

Parameters
dataThe data to read from, if no yaml data is available.
Returns
a distortion matrix.

Definition at line 149 of file head_pose_estimation.cpp.

References CV_64F, gaze::util::get_config(), and cv::Mat::zeros().

Referenced by process().

149  {
150  if (this->has_distortions) {
151  return this->distortions;
152  }
153  YAML::Node camera_config = util::get_config()["meta"]["camera"];
154  if (camera_config["distortion_coefficients"]) {
155  this->distortions = camera_config["distortion_coefficients"].as<cv::Mat>();
156  } else {
157  this->distortions = cv::Mat::zeros(4, 1, CV_64F);
158  }
159  this->has_distortions = true;
160  return this->distortions;
161 }
static MatExpr zeros(int rows, int cols, int type)
YAML::Node get_config()
Definition: config.in.cpp:12
void gaze::pipeline::HeadPoseEstimation::process ( util::Data data)
overridevirtual

Estimates the head pose from face landmarks and the input image.

Parameters
dataThe data object to be updated.
Returns
via data the modified data object.

Implements gaze::PipelineStep.

Definition at line 108 of file head_pose_estimation.cpp.

References get_and_maybe_read_distortions(), gaze::util::Data::head_rotation, gaze::util::Data::head_translation, gaze::util::Data::landmarks, std::vector< _Tp, _Alloc >::push_back(), read_or_set_camera_matrix(), cv::solvePnP(), and cv::SOLVEPNP_ITERATIVE.

108  {
109  if (data.landmarks.num_parts() <= 0) {
110  return;
111  }
112 
113  // Convert dlib::points to cv::Point for solvePNP
114  std::vector<cv::Point2d> im_points;
115  for (auto i : this->index_list) {
116  im_points.push_back(cv::Point2d(data.landmarks.part(i).x(),
117  data.landmarks.part(i).y()));
118  }
119 
120  cv::solvePnP(this->model_points, im_points,
121  this->read_or_set_camera_matrix(data),
122  this->get_and_maybe_read_distortions(data),
123  data.head_rotation, data.head_translation, false, cv::SOLVEPNP_ITERATIVE);
124 }
cv::Mat get_and_maybe_read_distortions(const util::Data &data)
void push_back(const value_type &__x)
cv::Matx33d read_or_set_camera_matrix(const util::Data &data)
SOLVEPNP_ITERATIVE
bool solvePnP(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs, OutputArray rvec, OutputArray tvec, bool useExtrinsicGuess=false, int flags=SOLVEPNP_ITERATIVE)
cv::Matx33d gaze::pipeline::HeadPoseEstimation::read_or_set_camera_matrix ( const util::Data data)
protected

Returns a camera matrix.

If it was not loaded yet, it will be read from gaze.yaml (as created using OpenCV's calibration tool, also check the gaze.default.yaml to see how to integrate it).

If nothing can be found in the configuration, the following matrix is calculated and returned:

\[ \left( \begin{array}{ccc} c & 0 & \frac{c}{2} \\ 0 & c & \frac{r}{2} \\ 0 & 0 & 1 \end{array} \right) \]

Where $c=\mbox{image columns}, r = \mbox{image rows}$.

Parameters
dataThe data to read from, if no yaml data is available.
Returns
a camera matrix.

Definition at line 131 of file head_pose_estimation.cpp.

References cv::Mat::cols, gaze::util::get_config(), cv::Mat::rows, and gaze::util::Data::source_image.

Referenced by process().

132  {
133  if (this->has_camera_matrix) {
134  return this->camera_matrix;
135  }
136  YAML::Node camera_config = util::get_config()["meta"]["camera"];
137  if (camera_config["camera_matrix"]) {
138  this->camera_matrix = camera_config["camera_matrix"].as<cv::Mat>();
139  } else {
140  this->camera_matrix <<
141  data.source_image.cols, 0, data.source_image.cols / 2,
142  0, data.source_image.cols, data.source_image.rows / 2,
143  0, 0, 1;
144  }
145  this->has_camera_matrix = true;
146  return this->camera_matrix;
147 }
YAML::Node get_config()
Definition: config.in.cpp:12
void gaze::pipeline::HeadPoseEstimation::visualize ( util::Data data)
overridevirtual

Visualizes the estimated head pose.

Parameters
dataThe data object to be visualized.

Implements gaze::gui::VisualizeableBase.

Definition at line 126 of file head_pose_estimation.cpp.

References gaze::util::Data::image, and gaze::gui::Visualizeable< widget_type >::widget.

126  {
127  this->widget->set_image(data.image);
128  this->update_overlay(data);
129 }
std::shared_ptr< widget_type > widget

The documentation for this class was generated from the following files: