VisageDetectorDemo
SimpleDetector.cpp
Go to the documentation of this file.
1 //doxygen
2 /*! \file */
3 
4 #include "cv.h"
5 #include "highgui.h"
6 #include "VisageFeaturesDetector.h"
7 #include "visageVision.h"
8 #include "Common.h"
9 #include "VisageDrawing.h"
10 #include "LicenseString.h"
11 
12 
13 #define MAX_IMAGE_SIZE 1000.0f
14 
15 using namespace VisageSDK;
16 
17 /**
18  * VisageFeaturesDetector instance.
19  */
21 IplImage* frame;
22 IplImage *imageBGRA;
23 
24 /**
25 * Performs facial features detection on a file.
26 
27  Perform facial feature detection on the image given by fileName. If successful, return the image with the feature points drawn on top of it. Also, an FDP object is created and saved in the current working directory.
28  Otherwise, return NULL.
29 
30  @param fileName The name of the image file to process.
31  @return annotated image file if successful, NULL if not.
32 *
33 */
34 IplImage* DetectFeatures(char *fileName)
35 {
36  int value;
37 
38  // Load the image
39  frame = 0;
40  IplImage* frame_resized = 0;
41  frame = cvLoadImage(fileName);
42 
43  if(!frame)
44  {
45  printf("Cannot open %s\n",fileName);
46  return NULL;
47  }
48  //cvCvtColor(frame, frame, VS_BGR2RGB);
49 
50  IplImage *logo = cvLoadImage("../Linux/data/images/logo.png", CV_LOAD_IMAGE_UNCHANGED);
51 
52  //resize the image if image is too big
53  if( frame->height > MAX_IMAGE_SIZE || frame->width > MAX_IMAGE_SIZE )
54  {
55  float step = 1.0f;
56  if( frame->height >= frame->width )
57  step = MAX_IMAGE_SIZE/static_cast<float>(frame->height);
58  else
59  step = MAX_IMAGE_SIZE/static_cast<float>(frame->width);
60  int w = static_cast<int>(static_cast<float>(frame->width)*step);
61  int h = static_cast<int>(static_cast<float>(frame->height)*step);
62  frame_resized = cvCreateImage(cvSize(w, h), frame->depth, frame->nChannels);
63  cvResize(frame, frame_resized);
64  cvReleaseImage(&frame);
65  }
66  else frame_resized = frame;
67 
68 
69  //MORE THAN 1 FACE
70  int maxFaces = 100;
71  FaceData* data = new FaceData[maxFaces];
72 
73  value = m_Detector->detectFacialFeatures((VsImage*)frame_resized, data, maxFaces);
74 
75  if(value > 0)
76  {
77  // save feature points into file
78  for(int i = 0; i < value; i++)
79  {
80  char tmpFileName[200];
81  strcpy(tmpFileName,fileName);
82  if(strlen(tmpFileName)>4)
83  {
84  tmpFileName[strlen(tmpFileName)-4] = 0;
85 
86  char idx[10];
87  sprintf(idx, "%d", i);
88  strcat(tmpFileName, "_");
89  strcat(tmpFileName, idx);
90  strcat(tmpFileName,".fdp");
91  //data[i].featurePoints2D->saveToFile(); //UN-COMMENT THIS LINE IN ORDER TO WRITE THE FDP FILE
92  }
93  VisageDrawing::drawResults((VsImage*)frame_resized, &data[i]);
94  imageBGRA = cvCreateImage(cvSize(frame_resized->width, frame_resized->height), IPL_DEPTH_8U, 4);
95  cvCvtColor(frame_resized, imageBGRA, CV_BGR2BGRA);
96  if (logo)
97  VisageDrawing::drawLogo((VsImage*)imageBGRA, (VsImage*)logo);
98  }
99  }
100 
101  delete[] data;
102 
103  return imageBGRA;
104 }
105 
106 void help(){
107  const char help[500] = \
108  "Usage:\n" \
109  "./runDetector imageName.jpg\n" \
110  "*Working directory is being set to Samples/data folder*";
111  printf("%s\n",help);
112 }
113 
114 /*! \mainpage visage|SDK VisageDetectorDemo example project
115 *
116 * \htmlonly
117 * <table border="0">
118 * <tr>
119 * <td width="32"><a href="../../../../../bin/VisageDetectorDemo"><img src="../../../../../doc/images/run_sample.png" border=0 title="Run Sample (this may not work in all browsers, in such case please run VisageDetectorDemo from the visageSDK\bin folder.)"></a></td>
120 * <td width="32"><a href="../../../../../bin"><img src="../../../../../doc/images/open_bin_folder.png" border=0 title="Open Executable Folder (visageSDK\bin)"></a></td>
121 * <td width="32"><a href="../../../data/images"><img src="../../../../../doc/images/open_data_folder.png" border=0 title="Open Data Folder"></a></td>
122 * <td width="32"><a href="../../../build"><img src="../../../../../doc/images/open_project_folder.png" border=0 title="Open Project Folder"></a></td>
123 * <td width="32"><a href="../../../source/VisageDetectorDemo"><img src="../../../../../doc/images/open_source_folder.png" border=0 title="Open Source Code Folder"></a></td>
124 * </tr>
125 * </table>
126 * \endhtmlonly
127 *
128 * \if REDHAT_DOXY
129 * \htmlonly
130 * <h2>Dependencies</h2>
131 *
132 * Prerequisites to building the sample project are:
133 * <ul>
134 * <li>You should have the Software Development Workstation version of RHEL6 (you can choose the version during the installation of RHEL6)</li>
135 * <li>OpenGL with dynamic libraries: GL, GLU (yum install mesa-libGL-devel mesa-libGLU-devel)</li>
136 * <li>GCC: gcc 7.0+ (sudo yum install gcc-c++)</li>
137 * <li>GTK+ 2.0 with dynamic library: gtk-x11-2.0 (yum install gtk2-devel)</li>
138 * <ul>
139 * <li> provided OpenCV library is built with GTK support</li>
140 * <li>libVision library also uses GTK to display error messages</li>
141 * </ul>
142 * <li>curses terminal control library (yum install ncurses-devel)</li>
143 * <li>FFMPEG with dynamic libraries: swscale, avformat.</li> <br>
144 * FFMPEG libraries which were previously installed using ATRPMS repository are now available on ATRPMS repository mirror server. In order to install them, please do the following steps:
145 * <ul>
146 * <li>1. Install RPM repository package (this step can be skipped if you already have ATRPMS repository installed):</li>
147 * <ul style="list-style-type:none">
148 * <li>$> sudo rpm -ivh <a href="http://ftp-stud.fht-esslingen.de/Mirrors/atrpms/dl.atrpms.net/el7-x86_64/atrpms/stable/atrpms-repo-7-7.el7.x86_64.rpm">http://ftp-stud.fht-esslingen.de/Mirrors/atrpms/dl.atrpms.net/el7-x86_64/atrpms/stable/atrpms-repo-7-7.el7.x86_64.rpm</a> </li>
149 * </ul>
150 * <li>2. Modify atrpms.repo as follows:</li>
151 * <ul style="list-style-type:none">
152 * <li>$> sudo sed -ri 's/baseurl=(.*)/baseurl=http:\/\/ftp-stud.fht-esslingen.de\/Mirrors\/atrpms\/dl.atrpms.net\/el7-x86_64\/atrpms\/stable/g' /etc/yum.repos.d/atrpms.repo </li>
153 * </ul>
154 * <li>3. Run the following command in terminal: </li>
155 * <ul style="list-style-type:none">
156 * <li>$> sudo yum-config-manager --enable atrpms </li>
157 * </ul>
158 * <li>4. Install FFMPEG libraries and create required symlinks:
159 * <ul style="list-style-type:none">
160 * <li>$> sudo yum install libavformat55.x86_64 </li>
161 * <li>$> sudo ln -s /usr/lib64/libavformat.so.55 /usr/lib64/libavformat.so </li>
162 * <li>$> sudo yum install libswscale2.x86_64 </li>
163 * <li>$> sudo ln -s /usr/lib64/libswscale.so.2 /usr/lib64/libswscale.so </li>
164 * <li>$> sudo ln -s /usr/lib64/libjpeg.so.62 /usr/lib64/libjpeg.so </li>
165 * <li>$> sudo ln -s /usr/lib64/libavutil.so.52 /usr/lib64/libavutil.so </li>
166 * <li>$> sudo ln -s /usr/lib64/libavcodec.so.55 /usr/lib64/libavcodec.so </li>
167 * </ul>
168 * </ul>
169 * </ul>
170 * \endhtmlonly
171 * \else
172 * \htmlonly
173 * <h2>Dependencies</h2>
174 *
175 * Prerequisites to building and running the sample project are:
176 * <ul>
177 * <li>g++ compiler (versions 4.7.x) (sudo apt-get install g++)</li>
178 * <li>OpenGL with dynamic libraries: GL, GLU (sudo apt-get install freeglut3-dev)</li>
179 * <li>OpenCV library (sudo apt-get install libopencv-dev)</li>
180 * <li>Video4Linux library (sudo apt-get install libv4l-dev)</li>
181 * <li>GTK+ 2.0 with dynamic library: gtk-x11-2.0 (sudo apt-get install libgtk2.0-dev)</li>
182 * <li>curses terminal control library (sudo apt-get install libncurses5-dev)</li>
183 * <li>high level programming interface for IEEE1394 digital camera (sudo apt-get install libdc1394-22-dev)</li>
184 * </ul>
185 * </p>
186 * \endhtmlonly
187 * \endif
188 * \htmlonly
189 * <h2>Running prebuilt sample</h2>
190 * \endhtmlonly
191 *
192 * The VisageDetectorDemo example project demonstrates the following capabilities of visage|SDK:
193 *
194 * - facial features detection in still images
195 *
196 *
197 * To use the application:
198 * Run the './runDetector.sh <filename>' command from the terminal\n\n
199 * This will perform facial features detection on a provided image (<filename>). If no image is provided detection will be performed on a default image. The sample is
200 * modified to run from the visageSDK/bin folder. It changes the current directory to ../Samples/data folder where Face Detector.cfg configuration file is located.
201 * Images are provided relative to that folder. The image should represent a face, in frontal pose and with a neutral expression. The face should not take a too big portion of the image,
202 * i.e., less than 50%. Several test images are available in the folder <a href="../../../data/images">Samples/Linux/data/images</a>.
203 *
204 * The specific classes and methods that demonstrate visage|SDK are:
205 * - main(): Initialises the Facial Feature Detector @ref m_Detector.
206 * - IplImage* DetectFeatures(char *fileName): Performs facial features detection on a still image and displays the result
207 *
208 * \htmlonly
209 * <h2>Building the project</h2>
210 *
211 * <p>
212 * The VisageDetectorDemo sample project source file is located in the <a href="../../../source/VisageDetectorDemo">Samples/Linux/source/VisageDetectorDemo</a> subfolder of the visage|SDK for Linux folder.
213 * It is built using the provided Makefile located in Samples/Linux/build subfolder.
214 * </p>
215 * 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
216 * in <a href="../../../../../doc/licensing.html"><em>Licensing</em></a> section of the documentation.
217 * \endhtmlonly
218 */
219 int main( int argc, const char* argv[] ){
220 
221  initializeLicenseManager(licenseKey.c_str());
222 
223  //Set current directory to data directory because Face Detector.cfg is located there
224  if (chdir("../Samples/data")!=0){
225  printf("Unable to change current directory.");
226  help();
227  return -1;
228  }
229 
230  m_Detector = new VisageFeaturesDetector("Face Detector.cfg");
231 
232  char imageName[200] = "../Linux/data/images/faces1.jpg";
233 
234  if (argc == 1) {
235  help();
236  printf("Default image will be used - faces1.jpg\n");
237  }
238  else if (argc >=2) {
239  printf("Detecting faces on %s ...\n",argv[1]);
240  strcpy(imageName,argv[1]);
241  }
242 
243  if (!m_Detector->Initialize()) {
244  printf("Could not load required data files.\n");
245  }
246  else{
247  IplImage* detected = DetectFeatures(imageName);
248  cvNamedWindow("FaceDetector");
249  cvShowImage("FaceDetector", detected);
250  cvWaitKey(0);
251  cvDestroyWindow("FaceDetector");
252  cvReleaseImage(&frame);
253  }
254 
255  return 0;
256 }
int height
VisageFeaturesDetector * m_Detector
VisageFeaturesDetector instance.
IplImage * DetectFeatures(char *fileName)
Performs facial features detection on a file.
int detectFacialFeatures(VsImage *frame, FaceData *output, int maxFaces=1, float minFaceScale=0.1f, float maxFaceScale=1.0f, bool outputOnly2DFeatures=false)