These instructions were written by me (mikko metso, 2014) to take notes on how I built the face recognition application on my Raspberry Pi B+ with camera model. Note! This is how I got it all to work, and none is guaranteed to work on any computer. Just my own notes that I made for myself for possible future use. The notes could well be cleaned and shortened but just to be sure not to change anything for the worse I did not want to touch it. All the credit goes to Pierre Raufast who is the father of all this. Most of the following instructions are simply copy pasted here from his valuable web pages. Thank you Pierre. It's a wonderful project. to add facial recognition (http://thinkrpi.wordpress.com/2013/05/22/opencv-and-camera-board-csi/) -get source code (zip file) here: https://github.com/raspberrypi/userland -unzip the file and copy the directory under /opt/vc -go to opt/vc/userland-master/ and type: sed -i 's/if (DEFINED CMAKE_TOOLCHAIN_FILE)/if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)/g' makefiles/cmake/arm-linux.cmake -create a build directory and compile (it takes a while) sudo mkdir build cd build sudo apt-get install cmake sudo cmake -DCMAKE_BUILD_TYPE=Release .. sudo make sudo make install -binary should be under /opt/vc/bin -go to /opt/vc/bin and test one file typing : ./raspistill -t 3000 -create your own project: cd mkdir camcv cd camcv cp -r /opt/vc/userland-master/host_applications/linux/apps/raspicam/* . mv RaspiStill.c camcv.c sudo chmod 777 camcv.c remove then content of CMakeLists.txt and replace with : cmake_minimum_required(VERSION 2.8) project( camcv ) SET(COMPILE_DEFINITIONS -Werror) include_directories(/opt/vc/userland-master/host_applications/linux/libs/bcm_host/include) include_directories(/opt/vc/userland-master/host_applications/linux/apps/raspicam/gl_scenes) include_directories(/opt/vc/userland-master/interface/vcos) include_directories(/opt/vc/userland-master) include_directories(/opt/vc/userland-master/interface/vcos/pthreads) include_directories(/opt/vc/userland-master/interface/vmcs_host/linux) include_directories(/opt/vc/userland-master/interface/khronos/include) include_directories(/opt/vc/userland-master/interface/khronos/common) include_directories(./gl_scenes) include_directories(.) add_executable(camcv RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv.c RaspiTex.c RaspiTexUtil.c gl_scenes/teapot.c gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/yuv.c gl_scenes/sobel.c tga.c) target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so pthread -lm) delete CMakeFiles directory if it exits Compile & test cp gl_scenes/* . cp RaspiTex.* gl_scenes/ cp RaspiTexUtil.* gl_scenes/ cmake . make ./camcv -t 1000 install OpenCV Install both dev lib and python lib sudo apt-get update sudo apt-get install libopencv-dev sudo apt-get install python-opencv cd .. mkdir openCvTest cd openCvTest pico display_image.cpp #include #include #include using namespace cv; using namespace std; int main( int argc, char** argv ) { if( argc != 2) { cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; return -1; } Mat image; image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file if(! image.data ) // Check for invalid input { cout << "Could not open or find the image" << std::endl ; return -1; } namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display. imshow( "Display window", image ); // Show our image inside it. waitKey(0); // Wait for a keystroke in the window return 0; } create compile makefile pico CMakeLists.txt 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 to make sure it works cmake . make ./displayimage If it works, congratulations, OpenCV 2.3 is installed. install face recognition API The face recognition API is called libfacerec-0.04. Download the zip file from here: https://github.com/bytefish/libfacerec/zipball/v0.04 Unzip it and transfer the whole directory on rpi, for example to /pi/camcv/libfacerec/. 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. pico CMakeLists.txt cmake_minimum_required(VERSION 2.8) project( reco) find_package( OpenCV REQUIRED ) add_executable( displayimage display_image.cpp ) link_directories( /home/pi/camcv/libfacerec ) target_link_libraries( displayimage /home/pi/camcv/libfacerec/libopencv_facerec.a ${OpenCV_LIBS} ) Link with OpenCV cd .. rm -r openCvTest go back to the original working dir cd camcv Modify your CMakeFiles.txt to include OpenCV library cmake_minimum_required(VERSION 2.8) project( camcv ) SET(COMPILE_DEFINITIONS -Werror) #OPENCV find_package( OpenCV REQUIRED ) link_directories( /home/pi/camcv/libfacerec ) include_directories(/opt/vc/userland-master/host_applications/linux/libs/bcm_host/include) include_directories(/opt/vc/userland-master/host_applications/linux/apps/raspicam/gl_scenes) include_directories(/opt/vc/userland-master/interface/vcos) include_directories(/opt/vc/userland-master) include_directories(/opt/vc/userland-master/interface/vcos/pthreads) include_directories(/opt/vc/userland-master/interface/vmcs_host/linux) include_directories(/opt/vc/userland-master/interface/khronos/include) include_directories(/opt/vc/userland-master/interface/khronos/common) include_directories(./gl_scenes) include_directories(.) add_executable(camcv RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv.c RaspiTex.c RaspiTexUtil.c gl_scenes/teapot.c gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/yuv.c gl_scenes/sobel.c tga.c) target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so pthread -lm) Recompile. make ./camcv openCV & pi cam: basic use: Download the camcv.c file http://raufast.org/download/camcv.c save older camcv.c only as an original backup: mv camcv.c camcvOriginal.c replace it with the newly downloaded one change ' else if (!raspipreview_create(&state.preview_parameters)) ' line in camcv.c to ' else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) ' add execute rights for it sudo chmod 777 camcv.c replace last line of CMakeLists.txt with this: target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so pthread -lm ${OpenCV_LIBS}) rm -r CMakeFiles/ cmake . make run to test that it works, in x session startx alt-f2 lxterminal /home/pi/camcv/camcv -> a camera module picture should be taken and displayed now by opencv basic use (camera mode) http://raufast.org/download/camcv2.c change ' else if (!raspipreview_create(&state.preview_parameters)) ' line in camcv2.c to ' else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) ' add execute rights for it sudo chmod 777 camcv2.c replace CMakeLists.txt with this: cmake_minimum_required(VERSION 2.8) project( camcv2 ) SET(COMPILE_DEFINITIONS -Werror) #OPENCV find_package( OpenCV REQUIRED ) link_directories( /home/pi/camcv/libfacerec ) include_directories(/opt/vc/userland-master/host_applications/linux/libs/bcm_host/include) include_directories(/opt/vc/userland-master/host_applications/linux/apps/raspicam/gl_scenes) include_directories(/opt/vc/userland-master/interface/vcos) include_directories(/opt/vc/userland-master) include_directories(/opt/vc/userland-master/interface/vcos/pthreads) include_directories(/opt/vc/userland-master/interface/vmcs_host/linux) include_directories(/opt/vc/userland-master/interface/khronos/include) include_directories(/opt/vc/userland-master/interface/khronos/common) include_directories(./gl_scenes) include_directories(.) add_executable(camcv2 RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv2.c RaspiTex.c RaspiTexUtil.c gl_scenes/teapot.c gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/yuv.c gl_scenes/sobel.c tga.c) target_link_libraries(camcv2 /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so pthread -lm ${OpenCV_LIBS}) rm -r CMakeFiles/ cmake . make run to test that it works, in x session startx alt-f2 lxterminal /home/pi/camcv/camcv2 -> (only black pictures, don't seem to work....??) video: download http://raufast.org/download/camcv_vid0.c change ' else if (!raspipreview_create(&state.preview_parameters)) ' line in camcv_vid0.c to ' else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) ' four times in CMakeLists.txt lines that include 'camcv2', replace the word with "camcv_vid0" rm -r CMakeFiles/ cmake . make run to test that it works, in x session startx alt-f2 lxterminal /home/pi/camcv/camcv_vid0 face recognition: replace one .hpp file with a better version of it: sudo mv /usr/include/opencv2/contrib/contrib.hpp /usr/include/opencv2/contrib/contribOriginal.hpp download http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2/download remote: scp ./OpenCV-2.3.1/modules/contrib/include/opencv2/contrib/contrib.hpp pi@192.168.10.122:~/camcv/ sudo mv contrib.hpp /usr/include/opencv2/contrib/ download http://raufast.org/download/camcv_vid1.cpp change ' else if (!raspipreview_create(&state.preview_parameters)) ' line in camcv_vid1.cpp to ' else if ( (status = raspipreview_create(&state.preview_parameters)) != MMAL_SUCCESS) ' also correct libfacerec location on one of the #include lines of camcv_vid1.cpp also in camcv_vid1.cpp, change fn_haar = "/usr/share/opencv/haarcascades/lbpcascade_frontalface.xml"; to fn_haar = "/usr/share/opencv/lbpcascades/lbpcascade_frontalface.xml"; four times in CMakeLists.txt lines that include 'camcv_vid0', replace the word with "camcv_vid1" add '/home/pi/camcv/libfacerec/libopencv_facerec.a' to the list of target_link_libraries of CMakeLists.txt if you want raspberry camera image to be vertically flipped, in RaspiCamControl.c replace: params->hflip = params->vflip = 0; with params->hflip = params->vflip = 1; rm -r CMakeFiles/ cmake . make download sample files from http://raufast.org/download/100x100.zip transfer them to /home/pi/camcv/trains/ run to test that it works, in x session startx alt-f2 lxterminal cd camcv ./camcv_vid1 csv.txt 1 to prepare pictures for face recognition training: (http://thinkrpi.wordpress.com/2013/04/03/step-5-prepare-photos/) Download .cpp source code here. http://raufast.org/download/preparePhoto.cpp create CMakeLists.txt: pico CMakeLists.txt cmake_minimum_required(VERSION 2.8) project( prepare ) find_package( OpenCV REQUIRED ) add_executable( prepare preparePhoto.cpp ) target_link_libraries( prepare ${OpenCV_LIBS} ) compile cmake . make make directory full of pictures, for example with script camCapture.sh: #!/bin/bash numOfPics=3 for i in {1..40} do DATE=$(date +"%y%m%d_%H%M%S") #raspistill -vf -hf -o /home/pi/picam/$DATE.jpg #raspistill -t 500 -vf -o /home/pi/picam/$DATE.jpg raspistill --timeout 100 --vflip --output /home/pi/picam/$DATE.jpg echo $i done echo "done" to run the camCapture.sh: cd ~/picam/ ./scripts/camCapture.sh run it (prepare) within a directory full of pictures format: (note! histogram equalization is better to be off!) prepare %of_picture_cropped size_of_train_picture image_name_prefix original_picture_new_size equalize_color for example: ./prepare 0.3 100 metso 900 0 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)