gaze  0.1.0
Perform gaze tracking with common webcams.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
simple_tracking.cpp
1 #include <cmath>
2 #include <functional>
3 #include <memory>
4 #include <utility>
5 
6 #include "gaze/gaze.h"
7 #include "opencv2/imgproc.hpp"
8 #include "opencv2/highgui.hpp"
9 
10 
11 namespace {
12  cv::Mat Blank(int height, int width) {
13  return cv::Mat(height, width, CV_8UC3, cv::Scalar::all(0));
14  }
15 
16  cv::Mat Spiral(int height, int width) {
17  cv::Mat image = cv::Mat::zeros(height, width, CV_8UC3);
18 
19  double a = 1;
20  double b = 0.3;
21  double theta_step = 0.05;
22  const int num_points = 500;
23 
24  auto x = [a, b](double theta) -> double {
25  return a * std::cos(theta) * std::exp(b * theta);
26  };
27  auto y = [a, b](double theta) -> double {
28  return a * std::sin(theta) * std::exp(b * theta);
29  };
30 
31  cv::Point points[num_points] = {};
32  double theta = 0;
33  double half_width = width / 2.0;
34  double half_height = height / 2.0;
35  for (int i = 0; i < num_points; ++i) {
36  points[i] = cv::Point(half_width + x(theta), half_height + y(theta));
37  theta += theta_step;
38  }
39  const cv::Point* ppt[1] = {points};
40  const int num_points_ptr[1] = {num_points};
41  cv::polylines(image, ppt, num_points_ptr, 1,
42  false, cv::Scalar(255, 255, 0));
43  return image;
44  }
45 
46  cv::Point map_and_convert(std::pair<int, int> point,
47  int to_width, int to_height,
48  int from_width, int from_height) {
49  auto map = [](int value, int from, int to) -> int {
50  return value * to / from;
51  };
52  return cv::Point(map(point.first, from_width, to_width),
53  map(point.second, from_height, to_height));
54  }
55 
56  void main_loop(const std::function<cv::Mat(int, int)> drawing,
57  int width = 400,
58  int height = 400,
59  int target_width = 1920,
60  int target_height = 1080) {
61  cv::namedWindow("Simple Tracker");
62 
64  new gaze::GazeTracker("st_subject", true));
65 
66  cv::Mat image = drawing(height, width);
67  cv::Mat storage = image.clone();
68 
69  tracker->start_trial("simple tracker");
70  while (true) {
71  storage.copyTo(image);
72 
73  std::pair<int, int> gaze_pair = tracker->get_current_gaze_point();
74  cv::Point gaze_point = map_and_convert(gaze_pair,
75  width, height, target_width, target_height);
76  cv::drawMarker(image, gaze_point, cv::Scalar(0, 255, 0));
77 
78  cv::imshow("Simple Tracker", image);
79 
80  if (cv::waitKey(15) != -1) {
81  break;
82  }
83  }
84  tracker->stop_trial();
85  cv::destroyWindow("Simple Tracker");
86  }
87 } // namespace
88 
89 int main(const int, const char** const) {
90  int width = 960;
91  int height = 540;
92  int target_width = 72;
93  int target_height = 45;
94  main_loop(&::Blank, width, height, target_width, target_height);
95  return 0;
96 }
void copyTo(OutputArray m) const
void destroyWindow(const String &winname)
static MatExpr zeros(int rows, int cols, int type)
void imshow(const String &winname, InputArray mat)
complex< _Tp > sin(const complex< _Tp > &)
void polylines(Mat &img, const Point *const *pts, const int *npts, int ncontours, bool isClosed, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
void drawMarker(Mat &img, Point position, const Scalar &color, int markerType=MARKER_CROSS, int markerSize=20, int thickness=1, int line_type=8)
This class wraps the gaze tracking process.
Definition: gaze_tracker.h:24
complex< _Tp > cos(const complex< _Tp > &)
Mat clone() const
static Scalar_< double > all(double v0)
complex< _Tp > exp(const complex< _Tp > &)
Point2i Point
int waitKey(int delay=0)