FaceTracker2
VisionExampleDoc.h
1 // Example7Doc.h : interface of the CVisionExampleDoc class
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 #pragma once
5 
6 #if !defined(AFX_EXAMPLE7DOC_H__53884DF7_67A9_4F97_9BB2_031E50E618B5__INCLUDED_)
7 #define AFX_EXAMPLE7DOC_H__53884DF7_67A9_4F97_9BB2_031E50E618B5__INCLUDED_
8 
9 #if _MSC_VER > 1000
10 #pragma once
11 #endif // _MSC_VER > 1000
12 
13 
14 #include "visageVision.h"
15 #include "VisageLivenessAction.h"
16 #include "VisageLivenessBrowRaise.h"
17 #include "VisageLivenessSmile.h"
18 #include "VisageLivenessBlink.h"
19 #include "OpenGLWndFaceTracker.h"
20 #include "FolderManager.h"
21 #include <string>
22 #include <videoInput.h>
23 
24 using namespace VisageSDK;
25 
26 #define WM_MYMESSAGE (WM_USER + 1)
27 
28 class CMainFrame;
29 
30 /*! \mainpage visage|SDK FaceTracker2 example project
31 *
32 * \if WIN64_DOXY
33 * \htmlonly
34 * <table border="0">
35 * <tr>
36 * <td width="32"><a href="../../../../../../bin64/DEMO_FaceTracker2_x64.exe"><img src="../../../../../../doc/OpenGL/images/run_sample.png" border=0 title="Run Sample (this may not work in all browsers, in such case please run DEMO_FaceTracker2_x64.exe from the visageSDK\bin64 folder.)"></a></td>
37 * <td width="32"><a href="../../../../../../bin64"><img src="../../../../../../doc/OpenGL/images/open_bin_folder.png" border=0 title="Open Binary Folder (visageSDK\bin64)"></a></td>
38 * <td width="32"><a href="../../../../data/FaceTracker2"><img src="../../../../../../doc/OpenGL/images/open_data_folder.png" border=0 title="Open Data Folder"></a></td>
39 * <td width="32"><a href="../../../../data/Videos"><img src="../../../../../../doc/OpenGL/images/open_folder.png" border=0 title="Open Video Folder"></a></td>
40 * <td width="32"><a href="../../../../build/msvc140/FaceTracker2"><img src="../../../../../../doc/OpenGL/images/open_project_folder.png" border=0 title="Open Project Folder"></a></td>
41 * <td width="32"><a href="../../../../source/FaceTracker2"><img src="../../../../../../doc/OpenGL/images/open_source_folder.png" border=0 title="Open Source Code Folder"></a></td>
42 * </tr>
43 * </table>
44 * \endhtmlonly
45 * \else
46 * \htmlonly
47 * <table border="0">
48 * <tr>
49 * <td width="32"><a href="../../../../../../bin/DEMO_FaceTracker2.exe"><img src="../../../../../../doc/OpenGL/images/run_sample.png" border=0 title="Run Sample (this may not work in all browsers, in such case please run DEMO_FaceTracker2.exe from the visageSDK\bin folder.)"></a></td>
50 * <td width="32"><a href="../../../../../../bin"><img src="../../../../../../doc/OpenGL/images/open_bin_folder.png" border=0 title="Open Binary Folder (visageSDK\bin)"></a></td>
51 * <td width="32"><a href="../../../../data/FaceTracker2"><img src="../../../../../../doc/OpenGL/images/open_data_folder.png" border=0 title="Open Data Folder"></a></td>
52 * <td width="32"><a href="../../../../data/Videos"><img src="../../../../../../doc/OpenGL/images/open_folder.png" border=0 title="Open Video Folder"></a></td>
53 * <td width="32"><a href="../../../../build/msvc140/FaceTracker2"><img src="../../../../../../doc/OpenGL/images/open_project_folder.png" border=0 title="Open Project Folder"></a></td>
54 * <td width="32"><a href="../../../../source/FaceTracker2"><img src="../../../../../../doc/OpenGL/images/open_source_folder.png" border=0 title="Open Source Code Folder"></a></td>
55 * </tr>
56 * </table>
57 * \endhtmlonly
58 * \endif
59 *
60 * The FaceTracker2 example project demonstrates the following capabilities of visage|SDK:
61 *
62 * - real-time head/facial features tracking on multiple faces from video file or camera, based on the VisageSDK::VisageTracker tracker.
63 * - liveness verification from a camera
64 *
65 * <br/><br/>
66 * In order to try the sample application, you need to have a valid license key. More information on how to obtain license key and include it in the application can be found in
67 * <a href="../../../../../../doc/OpenGL/licensing.html"><em>Licensing</em></a> section of the documentation.
68 * <br/><br/>
69 *
70 * For a quick start just run the demo, and select "Track From Camera"; or select "Track From Video" from the "Tracking" menu and select one of the video files available in the <a href="../../../../data/Videos">Samples/OpenGL/data/Videos folder</a>
71 * which may require, depending on the system, installation of <a href="http://www.xvidmovies.com/codec/" target="_blank">Xvid video codec</a>.
72 * \n\n For liveness verification demonstration, please select Liveness -> Perform liveness.
73 *
74 * The specific classes and methods that demonstrate visage|SDK are:
75 * - CVisionExampleDoc::OnTrackingTrackfromvideo(): Performs real time tracking from a video file.
76 * - CVisionExampleDoc::OnTrackingTrackfromcam(): Performs real time tracking from a camera.
77 * - CVisionExampleDoc::OnTrackingTrackinimage(): Performs real time tracking from an image.
78 * - CVisionExampleDoc::OnTrackingStop(): Stops the real time face tracking.
79 * - CVisionExampleDoc::OnLivenessStart() Performs liveness verification from a camera.
80 *
81 * Further classes and methods that are important for detailed understanding how this
82 * sample project is implemented and how it used visage|SDK are:
83 * - VisageSDK::FaceData class that is used for storing all the tracking data
84 *
85 * \section S1_export_to_file Tracking data into a file
86 *
87 * This example project includes code that implements writing of tracking data into a text file. The functionality is disabled by default and can be enabled by choosing Tracking from menu and selecting Write tracking data.
88 * In VisionExampleDoc.cpp close to the top are the lines defining VS_WRITE_AU and VS_WRITE_HEADPOSE. Each of these constants controls the writing of one type of data,
89 * specifically Action Units and head pose. For example, to disable writing of head pose and leave only Action Units, simply comment the line defining VS_WRITE_HEADPOSE.
90 *
91 * When activated, the data is written into a text file with values delimited by colon (;). The first line is the header.
92 *
93 * When tracking from an image, the output data file is created in the same folder with the image and has the same name as the image with extension .txt.
94 *
95 * When tracking from a video file, the output data file is created in the same folder with the video file and has the same name as the video file with extension .txt.
96 *
97 * When tracking from camera, the output file is created in the tracker working folder (by default this is "Visage Technologies\visageSDK\Samples\data").
98 * The name of the file is cam-data-<time>.txt, where "time" is the date and time when data was recorded.
99 */
100 
101 /**
102 * The document.
103 */
104 class CVisionExampleDoc : public CDocument
105 {
106 protected: // create from serialization only
108  DECLARE_DYNCREATE(CVisionExampleDoc)
109 
110 // Attributes
111 public:
112 
113  /**
114  * Initialisation.
115  *
116  * Initializes the tracker. Called from CVisionExampleApp::InitInstance().
117  */
118  void init();
119 
120  /**
121  * Initializes liveness detection.
122  * Sets array of actions that will need to be verified with accompanying messages and images that will be displayed to user during liveness verification.
123  */
124  void SetLivenessActions();
125 
126  /**
127  * Shuffles array of actions.
128  */
129  void RandomizeActions();
130 
131  /**
132  * Releases objects alocated for liveness detection.
133  */
134  void ReleaseActions();
135 
136  /**
137  * The Face Tracker object (VisageTracker).
138  * It is used to track the user's face from video and encode the tracked movements into MPEG-4 FAPs.
139  */
141 
142  /**
143  * Maximal number of states that the actions will have.
144  * It should be set to the number of states that the action with maximal number of states has.
145  */
146  static const int MAX_ACTION_STATES = 5;
147  /**
148  * Number of actions that will need to be verified in order to confirm liveness.
149  */
150  static const int NUM_ACTIONS = 3;
151 
152  /**
153  * Structure that holds the action and its accompanying messages and images that will be displayed to user during action verification
154  */
155  struct Action
156  {
157  VisageLivenessAction *action;
158  std::string images[MAX_ACTION_STATES];
159  std::string messages[MAX_ACTION_STATES];
160  };
161 
162  /**
163  * Array of actions that will need to be verified.
164  */
165  Action actions[NUM_ACTIONS];
166 
167  /**
168  * VisageLivenessBrowRaise object.
169  * Used to verify that the user has raised its eyebrows.
170  */
172 
173  /**
174  * VisageLivenessSmile object.
175  * Used to verify that the user has smiled.
176  */
178 
179  /**
180  * VisageLivenessBlink object.
181  * Used to verify that the user has blinked.
182  */
184 
185  /**
186  * Test image used for demonstrating raw image tracking interface.
187  */
188  IplImage *testImage;
189 
190  /**
191  * The OpenGL window.
192  * An OpenGL window with camera manipulation facilities. It is used by VisageTracker as an TrackerOpenGLInterface and TrackerGUIInterface, and also to render the animated 3D face model.
193  */
195 
196  CMainFrame* m_MainWnd;
197 
198  /**
199  * fbaFileName.
200  * This string contains the name of the .fba file where the facial movements will be encoded.
201  */
202  std::string fbaFileName;
203 
204  std::string videoFileName;
205 
206  /**
207  * Data file.
208  * This file will contain written data for each frame of the video.
209  * Data is written as CSV file where each line contains data for one frame separated with ';'.
210  * The first line contains data header describing meaning of that column.
211  *
212  */
213  FILE *datafile;
214  bool headerSet;
215  int frameNum;
216  bool writeDataEnabled;
217 
218  void writeResults(int track_stat, FaceData &trackingData);
219  void finishWriteResults();
220 
221 // Operations
222 public:
223 // Overrides
224  // ClassWizard generated virtual function overrides
225  //{{AFX_VIRTUAL(CVisionExampleDoc)
226  public:
227  virtual void Serialize(CArchive& ar);
228  //}}AFX_VIRTUAL
229 
230 // Implementation
231 public:
232 
233  virtual ~CVisionExampleDoc();
234 #ifdef _DEBUG
235  virtual void AssertValid() const;
236  virtual void Dump(CDumpContext& dc) const;
237 #endif
238 
239 // Generated message map functions
240 protected:
241  DECLARE_MESSAGE_MAP()
242 
243 public:
244 
245  void OnCloseDocument();
246 
247  afx_msg void OnTrackingStop();
248 
249 private:
250  /**
251  * A flag that is false initially, and set to true when the tracker is used the first time.
252  * Used to avoid loading a 3D model after tracking has been used, because this causes problems.
253  */
254  bool trackerUsed;
255  /**
256  * A flag used to indicate that actions have been set (setLivenessActions())
257  */
258  bool livenessInited = false;
259  volatile bool threadActive;
260  HANDLE trackingThreadHandle; // Handle to the thread used for tracking
261  int cam_device;
262  videoInput *VI;
263 
264 public:
265  afx_msg void OnTrackingTrackfromvideo();
266  afx_msg void OnTrackingTrackfromcam();
267  afx_msg void OnTrackingCamerasettings();
268  static void testingThread(void *thisdoc);
269  static void trackFromImage(void *thisdoc);
270  static void trackFromCAM(void *thisdoc);
271  static void trackFromVideo(void *thisdoc);
272  static void detectLivenessOnCamera(void *thisdoc);
273  afx_msg void OnCfgChange();
274  afx_msg void OnTrackingBatchprocessing();
275  afx_msg void OnTrackingTrackinimage();
276  afx_msg void OnTrackingWritedata();
277  afx_msg void OnLivenessStart();
278 };
279 
280 /////////////////////////////////////////////////////////////////////////////
281 
282 //{{AFX_INSERT_LOCATION}}
283 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
284 
285 #endif // !defined(AFX_EXAMPLE7DOC_H__53884DF7_67A9_4F97_9BB2_031E50E618B5__INCLUDED_)
The document.
VisageTracker * m_Tracker
The Face Tracker object (VisageTracker).
The OpenGL-enabled window with rendering and basic interaction.
VisageLivenessAction * BrowRaiseAction
VisageLivenessBrowRaise object.
std::string fbaFileName
fbaFileName.
FILE * datafile
Data file.
VisageLivenessAction * BlinkAction
VisageLivenessBlink object.
COpenGLWnd * m_OpenGLWnd
The OpenGL window.
Structure that holds the action and its accompanying messages and images that will be displayed to us...
IplImage * testImage
Test image used for demonstrating raw image tracking interface.
VisageLivenessAction * SmileAction
VisageLivenessSmile object.