Face Recognition (Pengenalan Wajah) Raspberry Pi (Step 2)

Step 2 – Install softwares (for webcam and computer vision)

1. Install webcam software

Install guvcview webcam viewer to test if your webcam is working.

sudo apt-get update
sudo apt-get install guvcview

Start guvcview to check if your webcam is working fine. If not, change resolution thru GUI (320×240 should be fine). If you still got problem, it could be a permission/rights issue. Just type :

sudo usermod -a -G video pi
sudo modprobe uvcvideo

Don’t be surprised : your RPI is not a very powerful machine. You’ll be able to display pictures at 10 fps max.

2. Install CMake

CMake is mandatory to compile.

sudo apt-get update
sudo apt-get install cmake

3. Install OpenCV Lib for face recognition

OpenCV is a wonderful library to do computer vision. Please take time to read information on the official website or on tutorials pages.

Today (April, 2013), only OpenCV 2.3 is available for RPI. Unfortunatly, face reco API is only available on version 2.4. Thus, we will need to install OpenCV in two steps.

3.1 Install OpenCV 2.3

Install both dev lib and python lib. My soft is C-written. Anyway, Python is still usefull for small scripts. I recommend to install it.

sudo apt-get update
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv

To test if OpenCv library is well installed, write this test software. It just displays a picture using imread and imshow functions. You will need to provide a sample .jpg file.

To compile using OpenCv lib, create a CMakeLists.txt file with

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

Then compile and execute

cmake .
make
./displayimage

If it works, congratulations, OpenCV 2.3 is installed.

3.2 install face recognition API

The face recognition API is called libfacerec-0.04. All information and doc can be found on this excellent website.

Download the zip file here. https://github.com/bytefish/libfacerec/zipball/v0.04

I unzip it on my mac and transfer the whole directory on my rpi.

Go on the directory and just compile it using

cmake .
make

Now, if you want to compile your previous sample with this libfacerec-004 api, you will need to modify your CMakeLists.txt file. It just link with the libface lib. Note :  Replace /home/pi/pierre/ path by the path where you copied the libface directory.

cmake_minimum_required(VERSION 2.8)
project( reco)
find_package( OpenCV REQUIRED )
add_executable( displayimage display_image.cpp )
link_directories( /home/pi/pierre/libfacerec-0.04 )
target_link_libraries( displayimage /home/pi/pierre/libfacerec-0.04/libopencv_facerec.a ${OpenCV_LIBS} )

4. Fix Timeout Problem

After few seconds/minutes using your webcam, you should have a “select timeout error” message. This is linked to some limitation of the Pi. Mr Gomoto gives here a way to avoid this issue. Thanks to him ! Do a simple bash filetimeout.sh with this code.

#/bin/bash
sudo rmmod uvcvideo
sudo modprobe uvcvideo nodrop=1 timeout=5000 quirks=0x80

5. Do a backup

If everything is ok, it’s worth to do a backup. Follow Step 2.5 instructions.

At this stage, do a short prayer for Dennis Ritchie. And read its book (Kernighan/Ritchie C Handbook) you will need it for step 4.

1. Let your Pi talk !
Now, your Pi must be able to speak. A very easy way to do it is to installespeak. (espeak is enough now, you don’t need to install Mbrola any more).
sudo apt-get update
sudo apt-get install espeak

espeak

Test it using a simple command line

espeak “hello world.”
Voice by default is english. If you want to change, select your voice using -vflag.
List of languages is here.
espeak -vfr “coucou lapin.” will talk with french accent.
For same language, you can select alternative voices using +m1 (for male voice, 1..4) or +f1 (for female voice, 1..4).
Speed is also a parameter : -s130 (by default : 100).
I recommend you to play with all these parameters to find a not-too-much-creepy voice 😉
If you want to read content of a file, just use -f option.
espeak -f “to_read.txt” -vfr+f2 -s130

6. understand source code
Once again, this source code is strongly inspired from Philipp Wagner source (here).
Download the source code here. I added a lot of comments to be more pedagogic as possible.
Algorithm is quite simple :

  • read config files with training pictures previously created in step 6 : store them in a picture collection
  • init some parameters, create a Eigen model (could be a Fisher model also)
  • train the Eigen model with collection of pictures
  • init webcam and start an infinite loop
    • capture photo
    • detect faces
    • for each face
      • try to recognize people leanrt
      • if success (confidence > threshold)
        • put name in the output image
        • speak to this person
        • display the output image

All funny parts are inside the speakTo function. Here you can add as many smarts behaviors as you want. It’ll let think your mirror is really a magic one.
Some exemples :

  • say time
  • depending hour of the day, propose important things to do : propose a morning coffee, remind to wash teeth before to go to sleep, remind to call his mother all sunday, ..)
  • connect to internet to get weather, stocks exhange, google calender next meeting, (see step 7 of this blog for code exemples)
  • adapt your speak to personn recognize (basic rules: tell a woman she’s beautiful, and tell a man he’s strong).

For such “smart agent” : the Wii balance board is a very good exemple : when you connect, your Wii always tell you short, funny and very-well adapted things…

7. compile
This step should not be difficult if you use this CMakeLists.txt. (notice the libopencv linked as already seen in step 3)

cmake_minimum_required(VERSION 2.8)
project( reco)
find_package( OpenCV REQUIRED )
add_executable( reco faceReco.cpp )
link_directories( /home/pi/pierre/libfacerec-0.04 )
target_link_libraries( reco /home/pi/pierre/libfacerec-0.04/libopencv_facerec.a ${OpenCV_LIBS} )

8. Test & Enjoy
Now, it’s time to test and to enjoy !
./reco csv-filename histo threshold
./reco faces.csv 1 5500

  • csv-filename : filename of csv file where you put all your training pictures path
  • histo : 0/1 : do you want to do a color equalization histogram before detecting face ? (try both and decide)
  • threshold : level of confidence above wich, you decide to recognize somebody (need to be tune after first run : use 5000 by default for Eigen Model (less for Fischer model)

Note : Light sensibility will depend on your webcam. With my logitech C270, it works well with natural light and artificial light.
Play with paramater (including training picture creation) and find your best set of parameter.

facerecognition

 

This is a face recognition file that I created and succeed
please complete the survey and you will get my files.
download

 

Thanks For  in Magic Mirror

Advertisements

Posted on May 17, 2015, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: