VisageTrackerDemo
SimpleTracker.cpp
Go to the documentation of this file.
1 //doxygen
2 /*! \file */
3 
4 #include <opencv2/opencv.hpp>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include "visageVision.h"
8 #include "Common.h"
9 #include "term.h"
10 #include <math.h>
11 #include <sys/timeb.h>
12 #include "VisageDrawing.h"
13 #include "LicenseString.h"
14 
15 using namespace std;
16 using namespace VisageSDK;
17 using namespace cv;
18 
19 VisageTracker *m_Tracker = 0;
20 
21 Mat framePtr;
22 
23 const int MAX_FACES = 4;
24 static FaceData trackingData[MAX_FACES];
25 
26 int *track_stat = 0;
27 
28 long trackingTime;
29 
30 //if user closes window used for displaying results, this will be set to 1 and tracking will stop
31 bool exitApp;
32 
33 Mat imageBGRA;
34 
35 /**
36  *
37  */
38 void flushInput(void)
39 {
40  int c;
41 
42  do
43  {
44  c = getchar();
45  } while (c != '\n' && c != EOF);
46 }
47 
48 /**
49 * Function for clearing console window.
50 */
51 void clearScreen(void)
52 {
53  if (!cur_term)
54  {
55  int result;
56  setupterm( NULL, STDOUT_FILENO, &result );
57 
58  if (result <= 0) return;
59  }
60 
61  putp( tigetstr( "clear" ) );
62 }
63 
64 void displayResults()
65 {
66  clearScreen();
67  printf("Tracking in progress!\n\n");
68 
69  char *tstatus;
70 
71  for (int i = 0; i < MAX_FACES; i++)
72  {
73  if (track_stat[i] == TRACK_STAT_OFF)
74  tstatus = "OFF";
75  }
76 
77  for (int i = 0; i < MAX_FACES; i++)
78  {
79  if (track_stat[i] == TRACK_STAT_INIT)
80  tstatus = "INITIALIZING";
81  }
82 
83  for (int i = 0; i < MAX_FACES; i++)
84  {
85  if (track_stat[i] == TRACK_STAT_RECOVERING)
86  tstatus = "RECOVERING";
87  }
88 
89  for (int i = 0; i < MAX_FACES; i++)
90  {
91  if (track_stat[i] == TRACK_STAT_OK)
92  tstatus = "OK";
93  }
94 
95  printf(" Tracking status: %s\n\n", tstatus);
96 
97  static float eyeDistance = -1.0f;
98 
99  // Example code for accessing specific feature points, in this case nose tip and eyes
100  if(track_stat[0] == TRACK_STAT_OK)
101  {
102  FDP *fp = trackingData[0].featurePoints3DRelative;
103  float nosex = fp->getFPPos(9,3)[0];
104  float nosey = fp->getFPPos(9,3)[1];
105  float nosez = fp->getFPPos(9,3)[2];
106  float lex = fp->getFPPos(3,5)[0];
107  float ley = fp->getFPPos(3,5)[1];
108  float lez = fp->getFPPos(3,5)[2];
109  float rex = fp->getFPPos(3,6)[0];
110  float rey = fp->getFPPos(3,6)[1];
111  float rez = fp->getFPPos(3,6)[2];
112  float eyedist = lex -rex;
113  int dummy = 0;
114  }
115 
116  // display the frame rate, position and other info
117  float trackingFrameRate = trackingData[0].frameRate;
118 
119  if (!imageBGRA.empty())
120  printf(" %4.1f FPS (track %ld ms) \n Resolution: input %dx%d\n\n",trackingFrameRate, trackingTime, framePtr.size().width, framePtr.size().height);
121 
122  imshow("SimpleTracker",imageBGRA);
123  waitKey(10);
124 }
125 
126 
127 /**
128 * Function used for measuring tracker speed.
129 * Used for video synchronization (if flag video_file_sync is set to 1)
130 *
131 */
132 static long getCurrentTimeMs()
133 {
134  struct timeb timebuffer;
135  ftime( &timebuffer );
136 
137  long clockTime = 1000 * (long)timebuffer.time + timebuffer.millitm;
138 
139  return clockTime;
140 }
141 
142 
143 /**
144 * The main function for tracking from a video file.
145 * It is called when the 'v' is chosen.
146 *
147 */
148 void trackFromVideo(const char *strAviFile)
149 {
150  double frameTime;
151  int frameCount = 0;
152 
153  //If the flag is set to 1 playback is synchronised by skipping video frames
154  //or introducing delay as needed, so the video file is played at its normal (or desired) speed (set variable targetFps to desired fps)
155  bool video_file_sync = true;
156 
157  long currentTime;
158 
159  //set to desired fps you wish to achieve from tracker
160  float targetFps = 30.0;
161  //float targetFps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
162 
163  namedWindow("SimpleTracker", WINDOW_AUTOSIZE);
164  moveWindow("SimpleTracker",100,100);
165 
166  VideoCapture capture(strAviFile);
167 
168  if(!capture.isOpened())
169  {
170  printf("Can't read video file.");
171  return;
172  }
173 
174  // moment when video started
175  long startTime = getCurrentTimeMs();
176 
177  //frame duration if video fps was equal to targetFps
178  frameTime = 1000.0/targetFps;
179 
180  Mat logo = imread("../Samples/Linux/data/images/logo.png", IMREAD_UNCHANGED);
181 
182  //tracking is performed while user doesn't close window for displaying results
183  while(!exitApp)
184  {
185  if(!capture.isOpened())
186  break;
187 
188  if (!capture.grab())
189  break;
190  frameCount++;
191 
192  if (video_file_sync)
193  {
194  currentTime = getCurrentTimeMs();
195 
196  //checking if tracker is too slow
197  while((currentTime-startTime) > frameTime*(1+frameCount) )
198  {
199  if(!capture.grab()) //end of stream
200  return;
201  frameCount++;
202  currentTime = getCurrentTimeMs();
203  }
204 
205  currentTime = getCurrentTimeMs();
206 
207  //checking if tracker is too fast (we don't want to risk unnecessary delay so we allow some extra frames, that's why framecount-5)
208  while((currentTime-startTime) < frameTime*(frameCount-5))
209  {
210  sleep(0.001);
211  currentTime = getCurrentTimeMs();
212  }
213  }
214 
215  if(!capture.retrieve(framePtr))
216  break;
217  long startTime = getCurrentTimeMs();
218  track_stat = m_Tracker->track(framePtr.size().width, framePtr.size().height, (const char*)framePtr.data, trackingData, VISAGE_FRAMEGRABBER_FMT_BGR, VISAGE_FRAMEGRABBER_ORIGIN_TL, framePtr.step, frameTime*frameCount, MAX_FACES);
219 
220  long endTime = getCurrentTimeMs();
221  trackingTime = endTime - startTime;
222 
223  for (int i= 0; i<MAX_FACES; i++)
224  {
225  if (track_stat[i] == TRACK_STAT_OK)
226  VisageDrawing::drawResults(framePtr, &trackingData[i]);
227  }
228  cvtColor(framePtr, imageBGRA, COLOR_BGR2BGRA);
229 
230  if (!logo.empty())
231  VisageDrawing::drawLogo(imageBGRA, logo);
232  displayResults();
233  }
234 
235  capture.release();
236 }
237 
238 
239 /**
240 * The main function for tracking from a video camera connected to the computer.
241 * It is called when the 'c' is chosen.
242 *
243 */
245 {
246  double cam_width = 640;
247  double cam_height = 480;
248  double cam_device = 0;
249 
250  namedWindow("SimpleTracker", WINDOW_AUTOSIZE);
251  moveWindow("SimpleTracker",100,100);
252 
253  VideoCapture capture( cam_device );
254  capture.set(CAP_PROP_FRAME_WIDTH, cam_width);
255  capture.set(CAP_PROP_FRAME_HEIGHT, cam_height);
256  capture.set(CAP_PROP_FPS, 30);
257 
258  if(!capture.isOpened())
259  {
260  printf("Can't initialize camera (OpenCV). Please check if your camera is installed properly.\n\n");
261  return;
262  }
263 
264  Mat logo = imread("../Samples/Linux/data/images/logo.png", IMREAD_UNCHANGED);
265  while(!exitApp)
266  {
267  if(!capture.isOpened())
268  break;
269 
270  if(!capture.grab())
271  break;
272 
273  if(!capture.retrieve(framePtr))
274  break;
275 
276  flip(framePtr, framePtr, 1);
277  long startTime = getCurrentTimeMs();
278  track_stat = m_Tracker->track(framePtr.size().width, framePtr.size().height, (const char*)framePtr.data, trackingData, VISAGE_FRAMEGRABBER_FMT_BGR, VISAGE_FRAMEGRABBER_ORIGIN_TL, 0, -1, MAX_FACES);
279  long endTime = getCurrentTimeMs();
280  trackingTime = endTime - startTime;
281 
282  for (int i= 0; i<MAX_FACES; i++)
283  {
284  if (track_stat[i] == TRACK_STAT_OK)
285  VisageDrawing::drawResults(framePtr, &trackingData[i]);
286  }
287  cvtColor(framePtr, imageBGRA, COLOR_BGR2BGRA);
288 
289  if (!logo.empty())
290  VisageDrawing::drawLogo(imageBGRA, logo);
291  displayResults();
292  }
293  capture.release();
294 }
295 
296 /*! \mainpage visage|SDK VisageTrackerDemo example project
297 *
298 * \htmlonly
299 * <table border="0">
300 * <tr>
301 * <td width="32"><a href="../../../../../bin/VisageTrackerDemo"><img src="../../../../../doc/images/run_sample.png" border=0 title="Run Sample (this may not work in all browsers, in such case please run VisageTrackerDemo from the visageSDK\bin folder.)"></a></td>
302 * <td width="32"><a href="../../../../../bin"><img src="../../../../../doc/images/open_bin_folder.png" border=0 title="Open Executable Folder (visageSDK\bin)"></a></td>
303 * <td width="32"><a href="../../../data"><img src="../../../../../doc/images/open_data_folder.png" border=0 title="Open Data Folder"></a></td>
304 * <td width="32"><a href="../../../data/video"><img src="../../../../../doc/images/open_folder.png" border=0 title="Open Video Folder"></a></td>
305 * <td width="32"><a href="../../../build"><img src="../../../../../doc/images/open_project_folder.png" border=0 title="Open Project Folder"></a></td>
306 * <td width="32"><a href="../../../source/VisageTrackerDemo"><img src="../../../../../doc/images/open_source_folder.png" border=0 title="Open Source Code Folder"></a></td>
307 * </tr>
308 * </table>
309 * \endhtmlonly
310 *
311 * \if REDHAT_DOXY
312 * \htmlonly
313 * <h2>Dependencies</h2>
314 *
315 * Prerequisites to building the sample project are:
316 * <ul>
317 * <li>You should have the Software Development Workstation version of RHEL6 (you can choose the version during the installation of RHEL6)</li>
318 * <li>OpenGL with dynamic libraries: GL, GLU (yum install mesa-libGL-devel mesa-libGLU-devel)</li>
319 * <li>GTK+ 2.0 with dynamic library: gtk-x11-2.0 (yum install gtk2-devel)
320 * - provided OpenCV library is built with GTK support
321 * - libVision library also uses GTK to display error messages
322 * <li>Install OpenBLAS library from EPEL repository: </li>
323 * <ul>
324 * <li>1. Enable EPEL Repository by running: \n
325 * $> wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \n
326 * $> sudo rpm --import https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 \n
327 * </li>
328 * $> sudo rpm -Uvh epel-release-latest-7.noarch.rpm
329 * <li>2. Install OpenBLAS library by running: \n
330 * $> sudo yum install openblas-devel
331 * </li>
332 * </ul>
333 * <li>curses terminal control library (yum install ncurses-devel)</li>
334 * <li>FFMPEG libraries (previously installed using ATRPMS repository) are now available on RPM Fusion repository. In order to install them, please do the following steps:
335 * <ul>
336 * <li>1. Enable EPEL Software repository (this step can be skipped if you already have EPEL repository enabled): \n
337 * $> sudo yum install epel-release </li>
338 * <li>2. Install the RPM Fusion rpm package: \n
339 * $> sudo yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm</li>
340 * <li>3. Install FFMPEG: \n
341 * $> sudo yum install ffmpeg </li>
342 * </ul>
343 * </ul>
344 * \endhtmlonly
345 * \else
346 * \htmlonly
347 * <h2>Dependencies</h2>
348 *
349 * Prerequisites to building the sample project are:
350 * <ul>
351 * <li>g++ compiler (versions 9.3.x) (sudo apt-get install build-essential)</li>
352 * <li>libz (sudo apt-get install zlib1g-dev)</li>
353 * <li>OpenCV and OpenCV dependencies (sudo apt-get install libopencv-dev):
354 * <ul>
355 * <li>jpeg dynamic libraries (sudo apt-get install libjpeg8-dev)</li>
356 * <li>FFMPEG with dynamic libraries: swscale, avformat (sudo apt-get install libswscale-dev libavformat-dev)</li>
357 * <li>libpng16 (sudo apt-get install libpng-dev)</li>
358 * <li>libv4l (sudo apt-get install libv4l-dev)</li>
359 * </ul>
360 * </li>
361 * <li>OpenBLAS library (sudo apt-get install libopenblas-dev)
362 * <li>curses terminal control library (sudo apt-get install libncurses5-dev)</li>
363 * <li>high level programming interface for IEEE1394 digital camera (sudo apt-get install libdc1394-22-dev)</li>
364 * </ul>
365 * </p>
366 * \endhtmlonly
367 * \endif
368 *
369 * \htmlonly
370 * <h2>Running prebuilt sample</h2>
371 * \endhtmlonly
372 *
373 * The VisageTrackerDemo example project demonstrates the following capabilities of visage|SDK:
374 * - real-time head/facial features tracking on multiple faces from video file or camera, with on-the-fly animation, based on the VisageSDK::VisageTracker tracker. Currently, tracking is limited to 4 faces, although this can be modified.
375 *
376 * To use the application:
377 *
378 * Run the './runTracker.sh' command from the terminal\n\n
379 * This is an intentionally simplified version of a tracking application, intended to be used by developers as
380 * an introduction. VisageTrackerDemo is a console application, with keyboard based choices made by the
381 * user. It then starts tracking and displays tracking status and numeric results in the console. Results are also visualized in an additional window as an overlay
382 * over the original image.
383 *
384 * Classes and methods that are important for detailed understanding how this
385 * sample project is implemented and how it used visage|SDK are:
386 * - main(): implements the main user interaction.
387 *
388 * \htmlonly
389 * <h2>Building the project</h2>
390 *
391 * <p>
392 * The VisageTrackerDemo sample project source file is located in the <a href="../../../source/VisageTrackerDemo">Samples/Linux/source/VisageTrackerDemo</a> subfolder of the visage|SDK for Linux folder.
393 * It is built using the provided Makefile located in <a href="../../../build/">Samples/Linux/build </a> subfolder.
394 * </p>
395 * The application needs to be built with license key in order to work properly. More information on how to obtain license key and include it in the application can be found
396 * in <a href="../../../../../doc/licensing.html"><em>Licensing</em></a> section of the documentation.
397 
398 * \endhtmlonly
399 */
400 
401 /**
402 * The simple tracker.
403 *
404 */
405 int main( int argc, const char* argv[] )
406 {
407  std::string videoFilename;
408  std::string cfgFile;
409  std::string dataPath = "../Samples/data/";
410  char source;
411 
412  exitApp = false;
413 
414  printf("\nSimple Tracker application\n\n");
415 
416  if (argc > 1)
417  {
418  cfgFile = argv[1];
419  }
420  else
421  {
422  printf("Error: no configuration argument provided.\nUsage: %s <configuration_filename> [video_file_path]\nwhere video_file_path argument is optional.\n", argv[0]);
423  return 1;
424  }
425 
426  if ((cfgFile == "NeuralNet.cfg") || (cfgFile == "Face Detector.cfg"))
427  {
428  printf("Error: not a valid configuration file.\n");
429  return 1;
430  }
431 
432  printf("Track from (v)ideo or (c)am? ");
433  scanf("%c", &source); flushInput();
434 
435  if (argc > 2)
436  {
437  videoFilename = argv[2];
438  }
439 
440  if ((source == 'v') && (argc == 2))
441  {
442  printf("Error: for track from video functionality second argument should be a path to video file.\n");
443  return 1;
444  }
445 
446  initializeLicenseManager(licenseKey.c_str());
447 
448  cfgFile = dataPath + cfgFile;
449 
450  m_Tracker = new VisageTracker(cfgFile.c_str());
451 
452  switch (source)
453  {
454  case 'v':
455  trackFromVideo(videoFilename.c_str());
456  break;
457  case 'c':
458  trackFromCam();
459  break;
460  default:
461  printf("Wrong choice!\n");
462  break;
463  }
464 
465  delete m_Tracker;
466  m_Tracker = 0;
467 
468  return 0;
469 }
VisageSDK::FaceData::featurePoints3DRelative
FDP * featurePoints3DRelative
VisageSDK
trackFromCam
void trackFromCam()
The main function for tracking from a video camera connected to the computer.
Definition: SimpleTracker.cpp:244
VisageSDK::VisageTracker
VisageSDK::FDP::getFPPos
const float * getFPPos(int group, int n) const
trackFromVideo
void trackFromVideo(const char *strAviFile)
The main function for tracking from a video file.
Definition: SimpleTracker.cpp:148
clearScreen
void clearScreen(void)
Function for clearing console window.
Definition: SimpleTracker.cpp:51
VisageSDK::FDP
VisageSDK::FaceData::frameRate
float frameRate
VisageSDK::VisageTracker::track
virtual int * track(int frameWidth, int frameHeight, const char *p_imageData, FaceData *facedata, int format=VISAGE_FRAMEGRABBER_FMT_RGB, int origin=VISAGE_FRAMEGRABBER_ORIGIN_TL, int widthStep=0, long timeStamp=-1, int maxFaces=1)
main
int main(int argc, const char *argv[])
The simple tracker.
Definition: SimpleTracker.cpp:405
VisageSDK::FaceData