gaze  0.1.0
Perform gaze tracking with common webcams.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
source_capture.cpp
1 #include "gaze/pipeline_steps/source_capture.h"
2 
3 #include <memory>
4 #include <string>
5 
6 #include "dlib/opencv.h"
7 #include "opencv2/opencv.hpp"
8 #include "yaml-cpp/yaml.h"
9 
10 #include "gaze/util/config.h"
11 #include "gaze/util/data.h"
12 
13 
14 namespace gaze {
15 
16 namespace pipeline {
17 
19  YAML::Node config = util::get_config(this->number);
20  this->name = config["name"] ?
21  config["name"].as<std::string>() : "SourceCapture";
22 
23  std::string source = config["source"] ?
24  config["source"].as<std::string>() : "0";
25 
26  bool is_webcam = true;
27  try {
28  int cam_source = std::stoi(source);
29  this->video_capture = cv::VideoCapture(cam_source);
30  } catch (const std::invalid_argument&) {
31  is_webcam = false;
32  this->video_capture = cv::VideoCapture(source);
33  }
34 
35  if (is_webcam) {
36  YAML::Node camera_config = util::get_config()["meta"]["camera"];
37  this->video_capture.set(cv::CAP_PROP_FPS,
38  camera_config["fps"] ? camera_config["fps"].as<double>() : 60.0);
39  this->video_capture.set(cv::CAP_PROP_FRAME_WIDTH,
40  camera_config["resolution"]["width"] ?
41  camera_config["resolution"]["width"].as<double>() : 640.0);
42  this->video_capture.set(cv::CAP_PROP_FRAME_HEIGHT,
43  camera_config["resolution"]["height"] ?
44  camera_config["resolution"]["height"].as<double>() : 360.0);
45  }
46 }
47 
48 SourceCapture::~SourceCapture() {
49  this->video_capture.release();
50 }
51 
53  if (static_cast<int>(this->video_capture.get(cv::CAP_PROP_POS_AVI_RATIO))
54  == 1) {
55  return;
56  }
57  bool received = this->video_capture.read(data.source_image);
58  if (received) {
59  this->last_frame = data.source_image;
60  } else {
61  data.source_image = this->last_frame;
62  }
63  dlib::assign_image(
64  data.image,
65  dlib::cv_image<dlib::bgr_pixel>(data.source_image));
66 }
67 
69  this->widget->set_image(data.image);
70 }
71 
72 } // namespace pipeline
73 
74 } // namespace gaze
std::shared_ptr< widget_type > widget
virtual void release()
virtual bool read(OutputArray image)
void process(util::Data &data) override
void visualize(util::Data &data) override
dlib::array2d< dlib::bgr_pixel > image
Definition: data.h:52
CAP_PROP_FRAME_WIDTH
CAP_PROP_FRAME_HEIGHT
virtual bool set(int propId, double value)
std::string name
Definition: pipeline_step.h:31
Wraps the data acquired per frame into a single instance.
Definition: data.h:27
CAP_PROP_FPS
cv::Mat source_image
Definition: data.h:47
CAP_PROP_POS_AVI_RATIO
virtual double get(int propId) const
YAML::Node get_config()
Definition: config.in.cpp:12