Face and Eye Detection With Raspberry Pi Zero and Opencv

by DanishMalhotra in Circuits > Computers

21150 Views, 44 Favorites, 0 Comments

Face and Eye Detection With Raspberry Pi Zero and Opencv

Test.jpeg

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

Downlaod and Install Raspbian Image

  • Download raspbian stretch with desktop image from raspberry pi website

https://www.raspberrypi.org/downloads/raspbian

  • 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

Setting Up Opencv

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

Steps:

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
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \
    -D BUILD_EXAMPLES=ON 
	-D ENABLE_PRECOMPILED_HEADERS=OFF .. 

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__
'3.0.0'
>>>

Face and Eye Detection

raspi_config.jpg
Test.jpeg

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

Cheers!