1. Understand collection of portraits need

For best results, OpenCV-train function need a collection of portraits with these properties :

  • picture shows face
  • picture is gray-scaled
  • all pictures have same size
  • eyes are horizontally-aligned
  • at least 30 pictures per people (my own experience)

See Han Solo’s picture as a good exemple :
You could create all this pictures with Gimp… but it’ll be a painful job.
To do it for you, I wrote a C++ software, strongly inspired from Philip Wagner Python script (here). This software, called prepare do, for each jpg file found in current directory :

  • resize the picture (for performance issue : your Pi is not powerfull enough to work with 5M pictures) (target dimension is a parameter, I recommend 800px width)
  • transform to gray scale
  • equalize colors (optional, parameter)
  • detect face
  • detect eyes (with glasses or not)
  • rotate picture to align eyes
  • crop picture around the face (size is an input parameter)
  • create a new picture sizexsize pixels (size is a parameter)
  • save new picture using a prefix to name it (prefix-index.jpg)
  • Warning : only put in input photos with only 1 person. Anyway, only the first person face will be cropped.

./prepare 0.3 100 hs 800 1

  • 0.3 means 30% = I take 30% more than the distance eye-eye.
  • 100 means : my output picture will be 100×100 pixels
  • hs means :my ouput picture name will start by hs (hs1.jpg, hs2.jpg, hs3.jpg…)
  • 800 means : original input pictures will be resized to 800 pixels width (ok for RPI performance)
  • 1 means : do the color histogram equalization (0 = don’t)

With these parameters and with the Han Solo’s picture below, you ‘ll get the gray small face displayed before.


2. Download source code

Download .cpp source code here. http://raufast.org/download/preparePhoto.cpp

3. Compile

CMakeLists.txt should contain :

cmake_minimum_required(VERSION 2.8)
project( prepare )
find_package( OpenCV REQUIRED )
add_executable( prepare preparePhoto.cpp )
target_link_libraries( prepare ${OpenCV_LIBS} )

Then, compile :

make .

And run within a directory full of pictures !
Warning, this soft doesn’t recognize all faces. Especially if original face is tilting too much. My recommandation is to provide a lot of input pictures and see how many you get at the end…
If everything is ok, you should have dozens of nice 100×100 portrait. Perfect for your face recognition training, next step.

At this stage, you should do a break and read The Man Who Planted Trees (Jean Giono).