gaze  0.1.0
Perform gaze tracking with common webcams.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
gaze_tracker.cpp
1 #include "gaze/gaze_tracker.h"
2 
3 #include <iostream>
4 #include <string>
5 #include <utility>
6 
7 #include "opencv2/core.hpp"
8 #include "yaml-cpp/yaml.h"
9 
10 #include "gaze/gui/debug_window.h"
11 #include "gaze/pipeline.h"
12 #include "gaze/pipeline_step.h"
13 #include "gaze/pipeline_steps.h"
14 #include "gaze/util/config.h"
15 
16 
17 namespace gaze {
18 
20  bool debug)
21  : debug(debug),
22  subject_id(subject_id) {
23  this->initialized = false;
24  this->init(subject_id, debug);
25 }
26 
27 GazeTracker::~GazeTracker() {
28  if (this->initialized) {
29  delete this->pipeline;
30  for (PipelineStep* step : this->pipeline_steps) {
31  delete step;
32  }
33  }
34 }
35 
37  if (!this->initialized) {
38  std::cerr << "[GazeTracker] Not initialized (calibrate())." << std::endl;
39  return;
40  }
41  std::cout << "[GT Stub] Calibrating." << std::endl;
42 }
43 
45  if (!this->initialized) {
46  std::cerr << "[GazeTracker] Not initialized (get_current_gaze_point())." <<
47  std::endl;
48  return std::pair<int, int>(-1, -1);
49  }
50  int x =
51  static_cast<int>(this->pipeline->get_data().estimated_gaze_point(0));
52  int y =
53  static_cast<int>(this->pipeline->get_data().estimated_gaze_point(1));
54  return std::pair<int, int>(x, y);
55 }
56 
57 void GazeTracker::init(const std::string subject_id,
58  const bool debug) {
59  if (this->initialized) {
60  return;
61  }
62  this->debug = debug;
63  this->subject_id = subject_id;
64  this->init_pipeline(subject_id);
65  if (this->debug) {
66  gui::open_debug_window(this->pipeline);
67  }
68  this->initialized = true;
69 }
70 
71 void GazeTracker::init_pipeline(const std::string subject_id) {
72  if (this->initialized) {
73  return;
74  }
75  YAML::Node config = util::get_config()["pipeline"];
76 
77  for (YAML::Node step_config : config) {
78  std::string type = step_config["type"].as<std::string>();
79  PipelineStep* step;
80  if (!type.compare("EyeLike")) {
81  step = new pipeline::EyeLike();
82  } else if (!type.compare("FaceLandmarks")) {
83  step = new pipeline::FaceLandmarks();
84  } else if (!type.compare("GazePointCalculation")) {
85  step = new pipeline::GazePointCalculation();
86  } else if (!type.compare("GazeCapture")) {
87  step = new pipeline::GazeCapture();
88  } else if (!type.compare("HeadPoseEstimation")) {
89  step = new pipeline::HeadPoseEstimation();
90  } else if (!type.compare("PupilLocalization")) {
91  step = new pipeline::PupilLocalization();
92  } else if (!type.compare("SourceCapture")) {
93  step = new pipeline::SourceCapture();
94  } else {
95  step = new pipeline::FallbackStep();
96  }
97 
98  this->pipeline_steps.push_back(step);
99  }
100  this->pipeline = new Pipeline(this->pipeline_steps, true);
101 }
102 
103 void GazeTracker::start_trial(const std::string identifier) {
104  if (!this->initialized) {
105  std::cerr << "[GazeTracker] Not initialized (start_trial())." <<
106  std::endl;
107  return;
108  }
109  this->current_trial_id = identifier;
110  std::cout << "[GazeTracker] [Stub] Starting trial " << this->current_trial_id
111  << std::endl;
112 }
113 
115  if (!this->initialized) {
116  std::cerr << "[GazeTracker] Not initialized (stop_trial())." <<
117  std::endl;
118  return;
119  }
120  std::cout << "[GazeTracker] [Stub] Stopping trial " << this->current_trial_id
121  << std::endl;
122  this->current_trial_id = "";
123 }
124 
125 } // namespace gaze
void init(const std::string subject_id, const bool debug=false)
Abstract base class for PipelineSteps. Must be inherited from.
Definition: pipeline_step.h:18
ostream cerr
util::Data get_data()
Definition: pipeline.cpp:69
const std::pair< int, int > get_current_gaze_point() const
cv::Vec2d estimated_gaze_point
Definition: data.h:105
ostream cout
void start_trial(const std::string identifier)
GazeTracker(const std::string subject_id="default_subject", const bool debug=false)
int compare(const basic_string &__str) const
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
YAML::Node get_config()
Definition: config.in.cpp:12