human-sensing
faceLandmarks.h
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Vadim Tikhanoff
4  * email: vadim.tikhanoff@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16  */
17 
18 #ifndef __ICUB_FACEMODULE_MOD_H__
19 #define __ICUB_FACEMODULE_MOD_H__
20 
21 #include <yarp/os/BufferedPort.h>
22 #include <yarp/os/RFModule.h>
23 #include <yarp/os/Network.h>
24 #include <yarp/os/Thread.h>
25 #include <yarp/os/RateThread.h>
26 #include <yarp/os/Time.h>
27 #include <yarp/os/Semaphore.h>
28 #include <yarp/os/Stamp.h>
29 #include <yarp/os/Os.h>
30 #include <yarp/os/Log.h>
31 #include <yarp/os/LogStream.h>
32 #include <yarp/os/RpcClient.h>
33 
34 #include <yarp/sig/Vector.h>
35 #include <yarp/sig/Image.h>
36 
37 #include <dlib/image_processing/frontal_face_detector.h>
38 #include <dlib/matrix/lapack/gesvd.h>
39 #include <dlib/image_processing.h>
40 #include <dlib/image_io.h>
41 #include <dlib/opencv.h>
42 
43 #include <cv.h>
44 #include <highgui.h>
45 #include <opencv2/imgproc/imgproc.hpp>
46 
47 #include <time.h>
48 #include <map>
49 #include <dirent.h>
50 #include <iostream>
51 #include <string>
52 #include <iomanip>
53 
54 #include "faceLandmarks_IDLServer.h"
55 
56 #define DISPLAY_LANDMARKS VOCAB4('d','i','s','p')
57 
58 typedef struct __circle_t {
59  float x;
60  float y;
61  float r;
62 }circle_t;
63 
64 class FACEManager : public yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> >
65 {
66 private:
67 
68  std::string moduleName; //string containing module name
69  std::string predictorFile; //stringc containing the path of the predictor file
70  std::string cntxHomePath; //contect home path
71  std::string inImgPortName; //string containing image input port name
72  std::string outImgPortName; //string containing image output port name
73  std::string outTargetPortName; //string containing the target port name
74  std::string outLandmarksPortName; //string containing the target port name
75 
76  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > imageInPort; //input image ports
77  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > imageOutPort; //output port Image
78  yarp::os::BufferedPort<yarp::os::Bottle> targetOutPort; //target port
79  yarp::os::BufferedPort<yarp::os::Bottle> landmarksOutPort;
80 
81  cv::Mat imgMat;
82 
83  dlib::frontal_face_detector faceDetector;
84  dlib::shape_predictor sp;
85  cv::Scalar color;
86 
87  cv::Point leftEye, rightEye;
88 
89  void drawLandmarks(cv::Mat &mat, const dlib::full_object_detection &d);
90 
91 public:
97  FACEManager( const std::string &moduleName, const std::string &predictorFile, const std::string &cntxHomePath );
98  ~FACEManager();
99 
100  yarp::os::Semaphore mutex;
101  bool displayLandmarks;
102  bool displayPoints;
103  bool displayLabels;
104  bool displayDarkMode;
105 
106  bool open();
107  void close();
108  void onRead( yarp::sig::ImageOf<yarp::sig::PixelRgb> &img );
109  void interrupt();
110  bool execReq(const yarp::os::Bottle &command, yarp::os::Bottle &reply);
111 };
112 
113 class FACEModule:public yarp::os::RFModule, public faceLandmarks_IDLServer
114 {
115  /* module parameters */
116  std::string moduleName;
117  std::string predictorFile;
118  std::string handlerPortName;
119  yarp::os::RpcServer rpcPort; //rpc port
120 
121  /* pointer to a new thread */
122  FACEManager *faceManager;
123  bool closing;
124  std::string cntxHomePath;
125 
126 public:
127 
128  bool configure(yarp::os::ResourceFinder &rf); // configure all the module parameters and return true if successful
129  bool interruptModule(); // interrupt, e.g., the ports
130  bool close(); // close and shut down the module
131 
132  double getPeriod();
133  bool updateModule();
134 
135  //IDL interfaces
139  bool attach(yarp::os::RpcServer &source);
143  bool display(const std::string& element, const std::string& value);
147  bool quit();
148 };
149 
150 #endif
151 //empty line to make gcc happy
virtual bool display(const std::string &element, const std::string &value)
faceLandmarks_IDLServer Interface.
virtual bool quit()
Quit the module.