Face and Eye Detection With Raspberry Pi Zero and Opencv

Introduction: Face and Eye Detection With Raspberry Pi Zero and Opencv

About: Electronics lover. Love to design PCB's. IoT enthusiastic.

In this instructable i'm going to show how you can detect face and eye using raspberry pi and opencv. This is my first instructable on opencv. I followed many tutorials to set up open cv in raspberry but every time struck with some errors. Anyhow i solved those errors and thought to write instructable so that everyone else will be able to install it without any difficulty

Things required:

1. Raspberry pi zero

2. SD-card

3. Camera Module

This installation process will take more than 13 hours so plan the installation accordingly

Step 1: Downlaod and Install Raspbian Image

  • Download raspbian stretch with desktop image from raspberry pi website


  • Then insert the memory card into your laptop and burn the raspbian image using etcher tool

Download ethcher from here https://etcher.io

  • After burning the image plug the memory card into your raspberry pi and power on the raspberry

Step 2: Setting Up Opencv

After boot process open terminal and follow the steps to install opencv and setting up virtual environment for opencv


1. Everytime you start any new installation it's better to upgrade existing packages

$ sudo apt-get update
$ sudo apt-get upgrade

Time : 2m 30 sec

2. Then install developer tools

$ sudo apt-get install build-essential cmake pkg-config 

Time: 50 sec

3. Now grab the necessary image I/O packages

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

Time: 37 sec

4. Video I/O packages

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev 

Time: 36 sec

5. Install GTK dvelopment

$ sudo apt-get install libgtk2.0-dev 

Time: 2m 57s

6. Optimization packages

$ sudo apt-get install libatlas-base-dev gfortran

Time: 1 min

7. Now install python 2.7 if it's not there. In my case it was already installed but still check

$ sudo apt-get install python2.7-dev 

Time: 55 sec

8. Now download the opencv source and unzip it

$ cd ~ 

$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip
$ unzip opencv.zip

Time: 1m 58 sec

9. Downloading the opencv_contrib repository

$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.0.0.zip
$ unzip opencv_contrib.zip 

Time: 1m 5sec

10. Now opencv and opencv_contrib have been expanded delete their zip files to save some space

$ rm opencv.zip opencv_contrib.zip 

Time: 2 sec

11. Now install pip

$ wget  https://bootstrap.pypa.io/get-pip.py 
$ sudo python get-pip.py 

Time: 50 sec

12. Install virtualenv and virtualenvwrapper, this will allow us to create separate, isolated python environments for our future projects

$ sudo pip install virtualenv virtualenvwrapper 
$ sudo rm -rf ~/.cache/pip 

Time: 30 sec

13. After that installation, open ~/.profile

$ nano ~/.profile

and add these lines to bottom of the file

# virtualenv and virtualenvwrapper 
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Now source your ~/.profile to reload the changes

$ source ~/.profile 

Time: 20 sec

14. Now create a python virtual env named cv

$ mkvirtualenv cv

Time: 10sec

15. Next step is to install numpy. This will take atleast half an hour so u can have some coffee and sandwiches

$ pip install numpy

Time: 36m

16. Now compile and install opencv and make sure u are in cv virtual environment by using this command

$ workon cv

and then setup up the build using Cmake

$ cd ~/opencv-3.0.0/
$ mkdir build
$ cd build
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \

Time: 5mins

17. Now build is setup, run make to start the compilation process. This is going to take a while so u can let this run overnight

$ make

In my case 'make' thrown me one error which was related to ffpmeg. After lot of search i found the solution. Go to opencv 3.0 folder then modules then inside videoio go to src and replace the cap_ffpmeg_impl.hpp with this file

https://github.com/opencv/opencv/blob/f88e9a748a37e5df00912524e590fb295e7dab70/modules/videoio/src/cap_ffmpeg_impl.hpp and run make again

Time: 13 hours

If it's compiled without any error, install it on raspberry pi using:

$ sudo make install
$ sudo ldconfig 

Time: 2 min 30 sec

18. After completing step 17 your opencv bindings should be in /usr/local/lib/python-2.7/site-packages . Verify this by using this

$ ls -l /usr/local/lib/python2.7/site-packages
total 1549
-rw-r--r-- 1 root staff 1677024 Dec  3 09:44 cv2.so

19. Now only thing left is sym-link the cv2.so file into site-packages directory of cv environment

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

20. Verify your opencv installtion by using:

$ workon cv
$ python
>>> import cv2
>>> cv2.__version__

Step 3: Face and Eye Detection

Now let's try face detection

First thing to do is enable camera by going using:

$ sudo raspi-config

This will bring up a configuration screen. Use your arrow keys to scroll down to Option 5: Enable camera, hit your enter key to enable the camera, and then arrow down to the Finish button and hit enter again. Lastly, you’ll need to reboot your Raspberry Pi for the configuration to take affect.

Now install picamera[array] in cv environment. For this make sure u r in cv environment. If you rebooted your pi, to enter again in cv environment just type:

$ source ~/.profile
$ workon cv

Now install pi camera

$ pip install "picamera[array]"

Run the face-detection-test.py bu using:

python face-detection-test.py

If it throws any error just type this command before executing script

sudo modprobe bcm2835-v4l2

Now you are good to go for face detection. Try and share your results


Be the First to Share


    • Mason Jar Speed Challenge

      Mason Jar Speed Challenge
    • Bikes Challenge

      Bikes Challenge
    • Remix Contest

      Remix Contest

    12 Discussions


    1 year ago

    Hi Danish,
    Please help me to solve the issue occurred at 16th step,which as follows.

    CMake Error: The source directory "/home/pi/opencv-3.0.0/build/BUILD_EXAMPLES=ON" does not exist.

    Kindly reply.


    Question 2 years ago

    Hi Danish,

    having error in make... i replaced the file as mentioned.. still the error is there...

    Below is the error am getting


    [ 13%] Built target opencv_core

    [ 17%] Built target opencv_imgproc

    [ 19%] Built target opencv_imgcodecs

    [ 19%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o

    In file included from /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:

    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function ‘bool CvCapture_FFMPEG::open(const char*)’:

    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:655:19: error: ‘struct Image_FFMPEG’ has no member named ‘wid’; did you mean ‘width’?



    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp: In constructor ‘CvCapture_FFMPEG::open(const char*)::icvInitFFMPEG::icvInitFFMPEG()’:

    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:149:9: error: ‘icvCreateFileCapture_FFMPEG_p’ was not declared in this scope

    icvCreateFileCapture_FFMPEG_p = (CvCreateFileCapture_Plugin)cvCreateFileCapture_FFMPEG;


    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp: In member function ‘virtual bool CvCapture_FFMPEG::open(const char*)::CvCapture_FFMPEG_proxy::open(const char*)’:

    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:199:14: error: ‘icvCreateFileCapture_FFMPEG_p’ was not declared in this scope

    if( !icvCreateFileCapture_FFMPEG_p )


    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:201:65: error: ‘icvCreateFileCapture_FFMPEG_p’ was not declared in this scope

    ffmpegCapture = icvCreateFileCapture_FFMPEG_p( filename );


    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp: In member function ‘bool CvCapture_FFMPEG::open(const char*)’:

    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:219:1: error: a function-definition is not allowed here before ‘{’ token



    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:268:1: error: a function-definition is not allowed here before ‘{’ token



    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:275:1: error: expected ‘}’ at end of input



    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:275:1: error: expected ‘}’ at end of input

    In file included from /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:

    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:575:10: warning: unused variable ‘valid’ [-Wunused-variable]

    bool valid = false;


    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:275:1: error: expected ‘}’ at end of input



    In file included from /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:45:0:

    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg_impl.hpp:589:14: error: label ‘exit_func’ used but not defined

    goto exit_func;


    /home/pi/opencv-3.0.0/modules/videoio/src/cap_ffmpeg.cpp:275:1: error: no return statement in function returning non-void [-Werror=return-type]



    cc1plus: some warnings being treated as errors

    modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:182: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o' failed

    make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1

    CMakeFiles/Makefile2:3788: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed

    make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2

    Makefile:149: recipe for target 'all' failed

    make: *** [all] Error 2

    (cv) pi@raspberrypi:~/opencv-3.0.0/build $


    Answer 2 years ago

    fixed.. the file was not copied properly

    Kajal Kaveramma
    Kajal Kaveramma

    Reply 2 years ago

    hello Subhasis,

    Could you please tell me how did you copy the file

    I am a newbie to raspbian and I have got the same error as above. I didnt get the steps to download the file. Could you please help me with the commands you passed to replace the file


    2 years ago

    Great set of instructions.

    However, here is a problem, I'm using picamera2 and I can only detect faces at about 5 or 6 feet. Beyond that, it cannot seem to detect faces. I've read somewhere that we may have to adjust the minSize parameter

    It's likely that the faces are too small at that distance. If you adjust the minSize parameter you'll likely be able to get it to work.

    Here's a link to the documentation:


    I'm not a coder, but can you help me figure this out? I need to be able to detect and recognize faces at upto 10-15 feet



    Reply 2 years ago


    Add these lines in the current code after for loop.

    for (x,y,w,h) in faces:

    x = x + 10 # increasing the x coordinate of detected face to reduce size of bounding box

    y = y + 10 # increasing the y coordinate of detected face to reduce size of bounding box

    w = w - 10 # reducing the w coordinate of detected face to reduce size of bounding box

    h = h - 10 # reducing the h coordinate of detected face to reduce size of bounding box


    I have reduced the boundary box size. I didn't run it on raspberry pi zero. Just check if it works for you. Let me know how it goes


    2 years ago

    Hi Danish,

    Which camera are you using?

    - Pi camera or USB webcam .


    Reply 2 years ago


    I'm using picamera


    2 years ago

    hii bro i am working on a face detection project..
    when i run your code it shows some error i have attached the screenshot..
    can u please help me


    Reply 2 years ago


    Plz check the haarcascade file is there in the location mentioned in code. Error is because of that file location not found. Other thing u can do is just copy the face and eye haarcascade file to that location


    2 years ago

    this is the screenshot i told u about

    Screen Shot 2018-02-13 at 8.08.42 PM.png