gaze  0.1.0
Perform gaze tracking with common webcams.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
face_landmarks.cpp
1 #include "gaze/pipeline_steps/face_landmarks.h"
2 
3 #include <string>
4 #include <vector>
5 
6 #include "dlib/image_processing.h"
7 #include "dlib/image_processing/frontal_face_detector.h"
8 #include "dlib/image_processing/render_face_detections.h"
9 #include "opencv2/imgproc.hpp"
10 #include "yaml-cpp/yaml.h"
11 
12 #include "gaze/util/config.h"
13 #include "gaze/util/data.h"
14 
15 
16 namespace gaze {
17 
18 namespace pipeline {
19 
20 FaceLandmarks::FaceLandmarks()
21  : face_detector(dlib::get_frontal_face_detector()) {
22  YAML::Node config = util::get_config(this->number);
23  this->name = config["name"] ?
24  config["name"].as<std::string>() : "FaceLandmarks";
25 
26  std::string landmarks_model("shape_predictor_68_face_landmarks.dat");
27  dlib::deserialize(config["model"] ?
28  config["model"].as<std::string>() :
29  landmarks_model) >> this->shape_predictor;
30 }
31 
33  std::vector<dlib::rectangle> faces = this->face_detector(data.image);
34  if (faces.size() > 0) {
35  data.landmarks = this->shape_predictor(data.image, faces[0]);
36  }
37 }
38 
40  this->widget->clear_overlay();
41 
42  // Reprocess the data in case multiple pipeline steps
43  // interfered - this is good for comparisons.
44  this->process(data);
45 
46  if (data.landmarks.get_rect().is_empty()) {
47  this->widget->set_image(data.image);
48  return;
49  }
50 
51  this->widget->set_image(data.image);
52  this->widget->add_overlay(dlib::image_display::overlay_rect(
53  data.landmarks.get_rect(),
54  dlib::rgb_pixel(0, 0, 255)));
55  this->widget->add_overlay(dlib::render_face_detections(data.landmarks));
56 }
57 
58 } // namespace pipeline
59 
60 } // namespace gaze
void visualize(util::Data &data) override
std::shared_ptr< widget_type > widget
dlib::array2d< dlib::bgr_pixel > image
Definition: data.h:52
dlib::full_object_detection landmarks
Definition: data.h:64
void process(util::Data &data) override
std::string name
Definition: pipeline_step.h:31
Wraps the data acquired per frame into a single instance.
Definition: data.h:27
size_type size() const noexcept
YAML::Node get_config()
Definition: config.in.cpp:12