VisageDetectorDemo
SimpleDetector.cpp
Go to the documentation of this file.
1 //doxygen
2 /*! \file */
3 
4 #include <opencv2/opencv.hpp>
5 #include "VisageFeaturesDetector.h"
6 #include "visageVision.h"
7 #include "Common.h"
8 #include "VisageDrawing.h"
9 #include "LicenseString.h"
10 
11 
12 #define MAX_IMAGE_SIZE 1000.0f
13 
14 using namespace VisageSDK;
15 using namespace cv;
16 
17 /**
18  * VisageFeaturesDetector instance.
19  */
21 Mat frame;
22 Mat 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 Mat DetectFeatures(char *fileName)
35 {
36  int value;
37 
38  // Load the image
39  Mat frame_resized;
40  frame = imread(fileName);
41 
42  if(frame.empty())
43  {
44  printf("Cannot open %s\n",fileName);
45  return Mat{};
46  }
47  cvtColor(frame, frame, COLOR_BGR2RGB);
48 
49  Mat logo = imread("../Linux/data/images/logo.png", IMREAD_UNCHANGED);
50 
51  //resize the image if image is too big
52  if( frame.size().height > MAX_IMAGE_SIZE || frame.size().width > MAX_IMAGE_SIZE )
53  {
54  float step = 1.0f;
55  if( frame.size().height >= frame.size().width )
56  step = MAX_IMAGE_SIZE/static_cast<float>(frame.size().height);
57  else
58  step = MAX_IMAGE_SIZE/static_cast<float>(frame.size().width);
59  int w = static_cast<int>(static_cast<float>(frame.size().width)*step);
60  int h = static_cast<int>(static_cast<float>(frame.size().height)*step);
61  resize(frame, frame_resized, Size(w,h));
62  }
63  else frame_resized = frame;
64 
65 
66  //MORE THAN 1 FACE
67  int maxFaces = 100;
68  FaceData* data = new FaceData[maxFaces];
69 
70  VsImage* frame_resized_vs = vsCreateImageHeader(vsSize(frame_resized.size().width, frame_resized.size().height), VS_DEPTH_8U, frame_resized.channels());
71  vsSetImageData(frame_resized_vs, frame_resized.data, frame_resized.size().width * frame_resized.channels());
72 
73  value = m_Detector->detectFacialFeatures((VsImage*)frame_resized_vs, 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(frame_resized, &data[i]);
94  cvtColor(frame_resized, imageBGRA, COLOR_BGR2BGRA);
95  if (!logo.empty())
96  VisageDrawing::drawLogo(imageBGRA, logo);
97  }
98  }
99 
100  delete[] data;
101  vsReleaseImageHeader(&frame_resized_vs);
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>GTK+ 2.0 with dynamic library: gtk-x11-2.0 (yum install gtk2-devel)
137 * - provided OpenCV library is built with GTK support
138 * - libVision library also uses GTK to display error messages
139 * <li>Install OpenBLAS library from EPEL repository: </li>
140 * <ul>
141 * <li>1. Enable EPEL Repository by running: \n
142 * $> wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \n
143 * $> sudo rpm --import https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 \n
144 * $> sudo rpm -Uvh epel-release-latest-7.noarch.rpm
145 * </li>
146 * </li>
147 * <li>2. Install OpenBLAS library by running: \n
148 * $> sudo yum install openblas-devel
149 * </ul>
150 * <li>curses terminal control library (yum install ncurses-devel)</li>
151 * <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:
152 * <ul>
153 * <li>1. Enable EPEL Software repository (this step can be skipped if you already have EPEL repository enabled): \n
154 * $> sudo yum install epel-release </li>
155 * <li>2. Install the RPM Fusion rpm package: \n
156 * $> sudo yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm</li>
157 * <li>3. Install FFMPEG: \n
158 * $> sudo yum install ffmpeg </li>
159 * </ul>
160 * </ul>
161 * \endhtmlonly
162 * \else
163 * \htmlonly
164 * <h2>Dependencies</h2>
165 *
166 * Prerequisites to building and running the sample project are:
167 * <ul>
168 * <li>g++ compiler (versions 9.3.x) (sudo apt-get install g++)</li>
169 * <li>OpenGL with dynamic libraries: GL, GLU (sudo apt-get install freeglut3-dev)</li>
170 * <li>OpenCV library (sudo apt-get install libopencv-dev)</li>
171 * <li>Video4Linux library (sudo apt-get install libv4l-dev)</li>
172 * <li>OpenBLAS library (sudo apt-get install libopenblas-dev)
173 * <li>GTK+ 2.0 with dynamic library: gtk-x11-2.0 (sudo apt-get install libgtk2.0-dev)</li>
174 * <li>curses terminal control library (sudo apt-get install libncurses5-dev)</li>
175 * <li>high level programming interface for IEEE1394 digital camera (sudo apt-get install libdc1394-22-dev)</li>
176 * </ul>
177 * </p>
178 * \endhtmlonly
179 * \endif
180 * \htmlonly
181 * <h2>Running prebuilt sample</h2>
182 * \endhtmlonly
183 *
184 * The VisageDetectorDemo example project demonstrates the following capabilities of visage|SDK:
185 *
186 * - facial features detection in still images
187 *
188 *
189 * To use the application:
190 * Run the './runDetector.sh <filename>' command from the terminal\n\n
191 * 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
192 * 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.
193 * 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,
194 * i.e., less than 50%. Several test images are available in the folder <a href="../../../data/images">Samples/Linux/data/images</a>.
195 *
196 * The specific classes and methods that demonstrate visage|SDK are:
197 * - main(): Initialises the Facial Feature Detector @ref m_Detector.
198 * - IplImage* DetectFeatures(char *fileName): Performs facial features detection on a still image and displays the result
199 *
200 * \htmlonly
201 * <h2>Building the project</h2>
202 *
203 * <p>
204 * 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.
205 * It is built using the provided Makefile located in Samples/Linux/build subfolder.
206 * </p>
207 * 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
208 * in <a href="../../../../../doc/licensing.html"><em>Licensing</em></a> section of the documentation.
209 * \endhtmlonly
210 */
211 int main( int argc, const char* argv[] ){
212 
213  initializeLicenseManager(licenseKey.c_str());
214 
215  //Set current directory to data directory because Face Detector.cfg is located there
216  if (chdir("../Samples/data")!=0){
217  printf("Unable to change current directory.");
218  help();
219  return -1;
220  }
221 
222  m_Detector = new VisageFeaturesDetector("Face Detector.cfg");
223 
224  char imageName[200] = "../Linux/data/images/faces1.jpg";
225 
226  if (argc == 1) {
227  help();
228  printf("Default image will be used - faces1.jpg\n");
229  }
230  else if (argc >=2) {
231  printf("Detecting faces on %s ...\n",argv[1]);
232  strcpy(imageName,argv[1]);
233  }
234 
235  if (!m_Detector->Initialize()) {
236  printf("Could not load required data files.\n");
237  }
238  else{
239  Mat detected = DetectFeatures(imageName);
240  if (detected.empty())
241  return 0;
242  namedWindow("FaceDetector");
243  imshow("FaceDetector", detected);
244  waitKey(0);
245  destroyWindow("FaceDetector");
246  }
247 
248  return 0;
249 }
VisageSDK
m_Detector
VisageFeaturesDetector * m_Detector
VisageFeaturesDetector instance.
Definition: SimpleDetector.cpp:20
VisageSDK::VisageFeaturesDetector::Initialize
bool Initialize()
VisageSDK::VisageFeaturesDetector
VisageSDK::VisageFeaturesDetector::detectFacialFeatures
int detectFacialFeatures(VsImage *frame, FaceData *output, int maxFaces=1, float minFaceScale=0.1f, float maxFaceScale=1.0f, bool outputOnly2DFeatures=false)
DetectFeatures
Mat DetectFeatures(char *fileName)
Performs facial features detection on a file.
Definition: SimpleDetector.cpp:34
VisageSDK::FaceData