1. Let your Pi talk !
Now, your Pi must be able to speak. A very easy way to do it is to install espeak. (espeak is enough now, you don’t need to install Mbrola any more).
sudo apt-get update
sudo apt-get install 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 -v flag.
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
2. 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…
3. 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} )
4. 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.
5. Install your Magic Mirror !
Select a wooden home-made mirror, put the webcam on its top.
Put your Pi and speakers behind the mirror to hide all technical stuffs.
At this stage, you should have a magic mirror, which is a perfect geek useless stuff. Start to impress your girlfriend, your step-mother or the pizza deliveryman.
Testé rapidement sur une fedora sur un PC (mes pi gèrent ma domotique, pas envie de tout casser), ça marche pas mal du tout. Faut juste bien penser à adapter les chemins vers les haarcascades qui peuvent varier d’une distro à l’autre.
J’arrive à une reconnaissance d’environ 8 fois sur 10 pour un premier essai avec des photos pas optimisé pour cet usage à la base.
Merci pour tout, ça m’a permit de mettre le pied à l’étrier avec opencv alors que ça fait des mois que je range ça dans mes bookmarks sans avoir le courage de m’y plonger 😉
I am not able to get audio output using espeak. it says Jack server not running or something. I tried all the trouble shooting methods mentioned in http://elinux.org/R-Pi_Troubleshooting but no sound in the speaker. Have you faced this error?
are you sure to set the jack output (value=1): “amixer cset numid=3 1 “
yes I tried that as well. But still no output. It is little strange but. I am getting the hello_audio.bin sine wave output in the speaker. So II can confirm there is no issue with the hardware. Aplay for wav file also not giving any output.
Also I would like to appreciate your article. They are quite brief and well explained. Thank you for that!!
update: it is working in my CAR Aux.. So problem with My Speaker 🙂
I have one more question. for webcam when I used guvcview it is very slow. I am getting maximum of 1FPS. Is it the problem with the camera? If i want to do some video processing with opencv will the usual webcam help in that? Or Raspberry Pi is a bad choice to do video processing?
hello. USB webcam are not working very well with RPI.(that’s why they propose the camera module). After, picture size is very important for speed. Basically a 320X240 picture will be treated much faster than a 640×480 frame. Your question is tricky because it depends on your use : RPI is definitely not the good solution for full HD real-time video processing. But, if you don’t have a real-time need and accept smaller frame, it could be a good (and affordable) choice. More over, video processing could mean many things : face recognition requires more cpu than simple pattern recognition (square…). For exemple : with opencv and my camera module board, I’ve got btw 8 and 17 FPS using 320×240 face recognition : which is far enough for my need. (article to be written ! ;-))
Pingback: OpenCV&Pi Cam – Step 7 : Face recognition | Think RPI
A little error,
At the end :
Note:
— Play with paramater (including training picture creation)
Play with parameters
Thanks for the tutorial !, it has been a long time since I wanted to play with opencv but didn’t know where to start.
What is the csv file or how do you create the csv file?
hello. read previous post “magic mirror”. Csv file structure is described.
Thanks anyways!
Never mind. I got figured it out!
I get a segmentation fault after running ./reco faces.txt 1 5500 code. I’m not sure how to fix this problem
I got it to work
what did you do to fix the problem?
how did you fix that ?
Salut,
Vous devriez ajouter l’API de google pour la voix c’est beaucoup plus naturel comme voix. 🙂
Oui, c est ce que j ai fait sur mon nouveau projet…. Voir mon prochain post au retour des congés 🙂
I get also a segmentation fault after running ./reco faces.txt 1 5500 code. Maby i have my .csv file not correct? anyone who solve this? 🙂
Probably an empty Line in your csv file….
Thank you for the reply!
I got now loaded the images, everything is ok, but after i initialized the people it says at the end :
mmal: main: Failed to create preview component
mmal: Failed to run camera app. Please check for firmware updates
i have already installed all the updates and my camera is working with opencv or the raspistill command.
Thank you for this nice project!
regards,
Erwin
Solve it with the reaction of Gabriel in Step 5 Opencv and PI Camera Board:
change: ” else if (!raspipreview_create(&state.preview_parameters)) ” line in main to
” else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) ”
Works great now! thank you pierre!
Your post is very nice and interesting!! But I have a problem… when I order ./camcv faces.csv 1 5500 rasberry said usage what should I do? Please email me hjkwon0123@naver.com I’m really dissapointed because of this error…help me…
Usage help!!
Pierre,
Great tutorial, I have spent today going through every step, when I build the reco project I get an error saying it can’t find this file,in line 27 of the cpp file
#include “home/pi/pierre/libfacerec-0.04/include/facerec.hpp”
Is this something I should have down loaded , if so, where from, or something I should have created
Steve Gale
As soon as I posted the above I realised what the problem was and sorted it. Have managed to build everything and am now busy trying to get a set of images together, it is a great tutorial
Hello !
What was your fix ?
Thank’s ^^ !
I think it was the path to the file, it was not in a. Folder with Pierre in the name
Hi Pierre,
I loved your tutorial and we got our Raspberry Pi to recognize faces! I was reading through your source code and I was a little confused on how you can convert the Eigen model to the Fisher model. We would like to use OpenCV for gender detection and it is my understanding that the Fisher model can do just that. How could you do that in your source code? If it is too complicated could you recommend a link which explains Fisher vs. Eigen? Been doing so much reading my brain hurts…
Thank you.
Hello Pierre,
when i try to compile faceReco.cpp, this the output error:
In file included from /home/pi/faceReco/faceReco.cpp:27:0:
/home/pi/bytefish-libfacerec/include/facerec.hpp:33:7: error: redefinition of ‘class cv::FaceRecognizer’
/usr/local/include/opencv2/contrib/contrib.hpp:921:24: error: previous definition of ‘class cv::FaceRecognizer’
make[2]: *** [CMakeFiles/reco.dir/faceReco.cpp.o] Error 1
make[1]: *** [CMakeFiles/reco.dir/all] Error 2
make: *** [all] Error 2
Whats the solution?? tnhx
I’ve got the same problem/error…any Ideas how to solve?
Hi, did you find a solution?, i have the same problem. =(
Hey, I have the same error. Did you find the solution?
thanks!
I have the same issue,did you fix it?
And me too…
Great tutorial, Pierre! Thanks! I made it successfully all the way up to this point, but unfortunately I am stuck getting this same error as well. What are your suggestions to fix this issue? Thanks again.
Remove or comment the line,
#include “/usr/local/include/opencv2/contrib/contrib.hpp”
It seems the libraries become redundant if you use both contrib.hpp and facerec.hpp. I got the same error and this fixed it. So hope its helpful.
Thanks! I solved the problem!
Hey,
How ist the prediction with you guys?
I have like 6 People in my dataset(me, freinds, geroge clooney) but the recognition is really bad. sometimes its me, sometimes im goerge clooney, sometime one of my friends. it seems really more random then recognition.
any ideas?
yeah, to be honest, results are not very good. My code is designed to go as fast as possible…
try to modify the Size parameter here (80,80) (try smaller)
face_cascade.detectMultiScale(gray, faces, 1.1, 3, CV_HAAR_SCALE_IMAGE, Size(80,80));
try also with the haarcascade_frontalface_alt and not lbp.
//fn_haar = “/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml”;
// change fn_harr to be quicker LBP (see article)
fn_haar = “/usr/share/opencv/haarcascades/lbpcascade_frontalface.xml”;
thanks mate
Hi Pierre,
This code work in last version of OpenCV (2.4.9)?
Thanks for share your knowledge.
Regards,
Marcelo
I’m using the last opencv in Ubuntu 14.04 without libefacerec-0.04, because in this version, the facerec is native.
The principal issue are untrained faces are recognized faces as if they were trained.
I have 2 persons with 100 faces (100pxx100px) and your 40 “anonymous” pictures as “others”.
Regards,
Marcelo
Hey Marcelo!
i’m using the opencv-2.4.9. How did you change the CMakeLists.txt file? Can you help me?Thank!
Can anyone explain exactly how to create .csv files? the instruction given is confusing.
Can someone here explain how to make .csv / txt ? I’m still confused , please explain step by step , thank you very much . 🙂
Sourece: http://docs.opencv.org/trunk/modules/contrib/doc/facerec/tutorial/facerec_video_recognition.html
python code:
#!/usr/bin/env python
import sys
import os.path
if __name__ == “__main__”:
BASE_PATH=”reclace with the path to the directory containing the images”
SEPARATOR=”;”
f = open(“facecsv.txt”,’w’)
label = 0
for dirname, dirnames, filenames in os.walk(BASE_PATH):
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
abs_path = “%s/%s” % (subject_path, filename)
#write the output into the file “facecsv.txt” opened earlier
print >>f, “%s%s%d” % (abs_path, SEPARATOR, label)
label = label + 1
I got eror message like this :
root@raspberrypi:/home/camcv# ./camcv faces.csv 1 5800
start
(init) People initialized
(init) 21 pictures read to train
(init) 21 pictures of Qodri (0) read to train
(init) taille images ok
(init) start train images
Segmentation fault
Can you help me guys ?
Maybe you got fixed problem like this ?
please someone help me I would appreciate any help
i get this error after running the following:
./reco /home/nouman/photos.csv 1 5500
start
(init) People initialized
(init) 0 pictures read to train
(init) 0 pictures of Pierre (0) read to train
(init) 0 pictures of Natacha (1) read to train
(init) 0 pictures of Nellie (2) read to train
(init) 0 pictures of Lisa (3) read to train
Segmentation fault (core dumped)
Thanks for a great howto Pierre .. very handy..
I am a complete noob to opencv and managed to find my way with a little linux skills.
For those still wondering what a csv looks like. Its on the site however this is what mine looks like. It literally just a comma separated file with the person number next to it.
csv file looks as follows cat kevin.csv
./images/hs11.jpg;1
./images/hs12.jpg;1
./images/hs15.jpg;1
Mine works fine with latest opencv however I did also manually compile the libfacerec lib and included it from my home folder.
It seems to work I do however get the following. Not exactly sure what this means. “prediction too low” Maybe I need more pictures? They are only 640 though instead of 800. Not sure if that’s the problem.
./reco2 kevin.csv 1 5500
start
(init) People initialized
(init) 18 pictures read to train
(init) 0 pictures of Pierre (0) read to train
(init) 18 pictures of Kevin (1) read to train
(init) 0 pictures of Nellie (2) read to train
(init) 0 pictures of Lisa (3) read to train
(init) taille images ok
(init) start train images
(init) train images : ok
(init) Load modele : ok
(init) webcam initialized : ok
– prediction too low = Kevin (1) confiance = (3270)
– prediction too low = Kevin (1) confiance = (3423)
Hi,
I have successfully completed steps 1 through 5, and installed espeak. However, when I try the command:
espeak “hello world.”
I get several lines of errors, ending with:
FATAL: cannot locate cpu MHz in /proc/cpuinfo
Does anyone know what’s going wrong, or a way around this?
Thanks
This issue seems related to an update on the Raspberry Pi as I had no problem using espeak this way before.
As a workaround,I made it works with aplay
Try this command instead (you will need aplay)
espeak “hello world” –stdout|aplay
Thanks maditnerd.
I got it working 🙂
I now have my robot saying all kinds of great geeky things.
hi everyone! i have this error message :
root/camcv//libfacerec/include/facerec.hpp:33:7: error: redefinition of ‘class cv::FaceRecognizer’
/usr/include/opencv2/contrib/contrib.hpp:921:22: error: previous definition of ‘class cv::FaceRecognizer’
anyone has solution for this ?!
Remove or comment the line,
#include “/usr/local/include/opencv2/contrib/contrib.hpp”
It worked for me. 🙂
Hello everyone. I finally got my face recognition working on my RPI thanks to this amazing tutorial. After getting it to work, I found that the prediction results are a little off. With slight movements of the face the recognition either fails or gives incorrect results. I was hoping if someone could suggest ways to improve upon this.
And also what are the parameters that we can play around with beside the threshold?
Thanks.
Did you come across any seg faults while running ./reco …..?
Hi, I have been going over the code and many different files. I keep getting the error “(E) Capture Device cannot be opened.” which I traced back to “if (!capture)…” can anyone tell me how to fix this?
P.S. sorry if my question doesn’t make much sense, I have been working on this all night (and morning) and am quite tired.
Nevermind, I installed uv4l and the error is gone
Hi, How did you do the installation of these drivers.
I installed, UV4l using , ‘m not getting this error any more. Although on giving /reco faces.csv 1 5500… I get the following:
start
(init) People initialized
(init) 9 pictures read to train
(init) 0 pictures of Pierre (0) read to train
(init) 5 pictures of Natacha (1) read to train
(init) 4 pictures of Nellie (2) read to train
(init) 0 pictures of Lisa (3) read to train
(init) taille images ok
(init) start train images
(init) train images : ok
(init) Load modele : ok
/dev/video0 does not support memory mapping
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
(init) webcam initialized : ok
HIGHGUI ERROR: V4L: Initial Capture Error: Unable to load initial
memory buffers.
Segmentation fault
I dont get it, should I be worried about uv4l buffering APIs?…Please help with a workaround.
Hi, How do you install these Drivers. I did it from “http://www.linux-projects.org/uv4l/installation/”
I dont get this error anymore. Although when I run ./reco faces.csv 1 5500
I get this:
**************************************************************************************************
start
(init) People initialized
(init) 9 pictures read to train
(init) 0 pictures of Pierre (0) read to train
(init) 5 pictures of Natacha (1) read to train
(init) 4 pictures of Nellie (2) read to train
(init) 0 pictures of Lisa (3) read to train
(init) taille images ok
(init) start train images
(init) train images : ok
(init) Load modele : ok
/dev/video0 does not support memory mapping
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
(init) webcam initialized : ok
HIGHGUI ERROR: V4L: Initial Capture Error: Unable to load initial
memory buffers.
Segmentation fault
**************************************************************************************************
Do I need to worry about uv4l Buffering APIs or what? Please help with any workaround to this..
Hi Pierre,
I have followed your tutorial from start to this point, but when compiling your source code I keep getting errors. Any idea how can I fix this?
raspberrypi:~/camface# make
[100%] Building CXX object CMakeFiles/reco.dir/camcv_vid1.cpp.o
In file included from /opt/vc/include/interface/vcos/vcos_assert.h:149:0,
from /opt/vc/include/interface/vcos/vcos.h:114,
from /opt/vc/include/interface/vmcs_host/vc_dispmanx.h:33,
from /opt/vc/include/bcm_host.h:50,
from /root/camface/camcv_vid1.cpp:29:
/opt/vc/include/interface/vcos/vcos_types.h:40:38: fatal error: interface/vcos/vcos_attr.h: No such file or directory
compilation terminated.
CMakeFiles/reco.dir/build.make:54: recipe for target ‘CMakeFiles/reco.dir/camcv_vid1.cpp.o’ failed
make[2]: *** [CMakeFiles/reco.dir/camcv_vid1.cpp.o] Error 1
CMakeFiles/Makefile2:60: recipe for target ‘CMakeFiles/reco.dir/all’ failed
make[1]: *** [CMakeFiles/reco.dir/all] Error 2
Makefile:72: recipe for target ‘all’ failed
make: *** [all] Error 2
Thanks,
Alexis
could you please send me your csv file, because i get the error:
(init) People initialized
(init) 0 pictures read to train
(init) 0 pictures of Pierre (0) read to train
(init) 0 pictures of Natacha (1) read to train
(init) 0 pictures of Nellie (2) read to train
(init) 0 pictures of Lisa (3) read to train
Segmentation fault (core dumped)
Could you please help me.
I get the error:
/dev/video0/ does not support memory mapping
…
Unable to stop the stream.: Bad file descriptor
HIGHGUI ERROR: V4L: Initial Capture Error: Unable to load initial memory buffers.
Segmentation fault.
I’d like to thank you for the efforts you have put
in writing this site. I really hope to view the same high-grade content from you later on as well.
In truth, your creative writing abilities has inspired me to
get my own site now 😉