Introduction: RasPi + OpenCV = Face Tracking

Picture of RasPi + OpenCV = Face Tracking

This instructable will teach you everything you need to know about installing your new RasPi Camera to your Raspberry Pi and implement OpenCV's Face tracking libraries. The tutorial will not assume that you know how to program or understand the inner workings of a Raspberry Pi. Intermediate users can easily use the tutorial. Beginners should be able to use this tutorial but may run into problems along the way. The time to complete is approximately 10hrs due to the slow processing power of the Raspberry Pi and the size of OpenCV. It will be easier if you use a usb webcam to start out then go back and install the necessary libraries for the Raspi Camera. By the end of the tutorial you will have a fully functioning OpenCV installation on your Raspberry Pi capable of face tracking and much much more.

Materials Needed

  • Raspberry Pi
  • SD Card
  • RasPi Camera Module
  • Keyboard
  • Mouse
  • Micro USB Charger
  • Ethernet cord
  • HDMI cord
  • Monitor

Step 1: Install Operating System to SD Card

Picture of Install Operating System to SD Card

Download OS Image - raspberrypi.org/downloads

Installation Guides

Step 2: Connect Raspberry Pi

Picture of Connect Raspberry Pi
  1. Connect Ethernet cord to port
  2. Connect Keyboard and Mouse to USB ports
  3. Insert SD card
  4. Connect HDMI cord to monitor
  5. Connect HDMI cord to Raspberry Pi
  6. Power on device by connecting micro USB

Step 3: First Time Setup

Connect the Camera module to the CSI port on the Raspberry Pi computer. Configure the Raspberry Pi computer On first boot, you will be presented with the ‘raspi-config’ menu.

  1. Type the Following into the terminal
    1. sudo apt-get update
    2. sudo apt-get upgrade -y
    3. sudo reboot

NOTE:

default credentials are pi with the password raspberry

Step 4: (Optional) Connect to Raspberry Pi Over Local Internet

This will allow you to use your Raspberry Pi from another computer.

SSH -link

Step 5: Install OpenCv and Required Libraries

Picture of Install OpenCv and Required Libraries
  1. Open a terminal and type the following lines. We are installing some python libraries this step should take approx 3 minutes.
    1. sudo apt-get install python-pygame python-scipy python-numpy
    2. sudo apt-get install python-pip
  2. Upgrade pip. approx 2 minutes.
    1. sudo pip install --upgrade pip
  3. You will need to log out and log back in.
    1. Click "Menu" in top right corner
    2. Click "Shutdown" at the bottom of list
    3. Select log out
    4. Log back in by typing "stratx" followed by the return key
  4. Install a virtual environment. approx 2 minutes.
    1. sudo pip install virtualenvwrapper
  5. Edit bashrc file using your favorite text editor, I will be using vi
    1. vi ~/.bashrc
    2. type "i" to enter insert mode
    3. paste the following on any line without quotes "source /usr/local/bin/virtualenvwrapper.sh"
  6. Reload bash file by typing the following in the terminal. approx 1 minute
    1. . .bashrc
  7. Make a virtual enviroment, I named mine env you may name it what ever you please
    1. mkvirtualenv --system-site-packages env
  8. Download the following script from Jay Rambhia, The script is outdated but we can work around this. aprox 20 minutes.
    1. wget https://raw.github.com/jayrambhia/Install-OpenCV/master/Ubuntu/2.4/opencv2_4_5.sh
    2. chmod +x opencv2_4_5.sh
    3. ./opencv2_4_5.sh
  9. The terminal will yell at you saying there is a problem just ignore this and enter the following commands. This step will easily take 5-10 hours so grab yourself a nice book to read.
    1. wget -O OpenCV-2.4.11.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.11/opencv-2.4.11.zip/download
    2. unzip OpenCv-2.4.11.zip
    3. cd opencv-2.4.11
    4. mkdir build
    5. cd build
    6. cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
    7. make -j4
    8. sudo make install
    9. sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
    10. sudo ldconfig
  10. Follow the instructions linked to install UV4L
    1. http://www.linux-projects.org/modules/sections/ind...
  11. Fix some outdated dependencies
    1. apt-get remove libopencv-core2.3
    2. export PYTHONPATH=/usr/local/lib/python2.7/site-packages/
    3. export LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so

Step 6: (Optional) - Install SimpleCv

  1. Installation approx 5 minutes
    1. sudo apt-get install git
    2. git clone git://github.com/sightmachine/SimpleCV.git
    3. cd SimpleCV
    4. sudo python setup.py install
  2. To Test
    1. simplecv
    2. camera = Camera()

Step 7: Face Tracking

Picture of Face Tracking

Once everything is installed you may run the OpenCV sample code provided in the directory you installed to.

To run python scripts you must type python then the name of the script.

After all your hard work and patience you should backup your operating system so you won't need to spend 10hrs reinstalling if something goes wrong.

Further projects and information can be found at http://docs.opencv.org/doc/tutorials/tutorials.htm...

Comments

Mascal (author)2015-03-26

You can do it without recompiling OCV (so saving several hours):

http://rpihome.blogspot.com/2015/03/face-detection-with-raspberry-pi.html

Anyway this is a really nice tutorial!

bytefoolish (author)Mascal2015-03-26

This approach would definitely save plenty of time. Have you noticed any problems using the PiCamera? I was unable to grab the stream directly since OpenCV was expecting a usb device not a CSI one.

SimpleCV is also a great alternative.

Both approaches are great if you want to use python.

bytefoolish (author)bytefoolish2015-03-26

I featured your comment, great suggestion!

Mascal (author)bytefoolish2015-03-26

Thanks!

Mascal (author)bytefoolish2015-03-26

I used this method since few days and found no problems right now. Anyway I'm just using it for single images as I need to take just an image every 1/2 second or more, so I do not used a video stream.

It could be also used with a video stream, but it would be more complex and probably recompiling would be better (less cpu load when acquiring the frames).

tomatoskins (author)2015-03-25

This is awesome! I've never done anything like this before. I think that I will look into this more when I have time!

bytefoolish (author)tomatoskins2015-03-25

I am glad you like it!

A few tips:

Run make -j4 without the gui it will run faster and will not go into power save mode every couple of minutes.

You can use a standard usb camera just by compiling OpenCV, many of my steps are geared towards RasPi Camera users.

Compilation took me 10hrs on the original Raspberry Pi, its a grueling process no way around it. It should take under 5hrs to complete on a newer model.

A slight overclock will cut down the time it takes to compile but be wary this makes kernal panics and errors more likely to happen.

Make sure you have at least 2gb in your installation directory, there is nothing more frustrating than having to start all over with a new SD card.

You may email me at bytefoolish@gmail.com if you have any questions.

I look forward to seeing what you come up with!

markserrano (author)2017-06-28

Good share. I am adding a comment too because I can't find a way to bookmark this in Instructables. Anyway as mentioned by someone a prebuilt image is good because it lessens the setup time.

However there are questions I want to be clarified:
1. Does the prebuilt image have the latest OpenCV feature set and bug fixes?
2. Are we losing anything by using outdated images?
3. What are the pros of building openCV from scratch?

I probably have a good set of answers to these, but I just want readers to understans further the pros and cons. Also I might be missing something that isn't obvious.

Also it would be great if we can Dockerize all these steps. So you have one file and one command to run the whole infrastructure. I understand this might require Docker knolwedge. I will try this weekend if I can build one if and only if there is no prebuilt updated image that are bing built by someone already

https://hub.docker.com/r/hypriot/rpi-python/

randycool279 (author)2016-08-17

are you serious about the 5-10 hours thing??

snoop911 (author)2016-08-16

From what I've read on OpenCV, it seems for face recognition, it's necessary to capture positive and negative images...

But, do you know if generating synthetic positive images from actual (positive) images using opencv_createsamples is necessary when not using the Haar algorithm.. like LBP, eigenface, fisherface?

For example, this tutorial creates new positive images by placing inside negative images:

http://www.memememememememe.me/training-haar-casca...

By contrast, this example, only uses FaceRecognition class, and does not use opencv_createsamples

http://nates.space/2016/01/13/face-recognition-wit...

Any thoughts on this?

Baycemrak (author)2016-08-12

İt's a very nice tutorial thank you a lot. But I got a problem, why does it say "command not found" for 6 and 7 which is ..bashrc ve mkvirtualenv?

dmarinho1 (author)2016-05-26

Hi , nice tuturial!
But do u have problems whit low rate FPS ?
Im doing someting whit openCV libs in C++ for a University Project and i cant get a good fps Rate.. can u help me?

chriscambridge (author)2016-05-10

Hi Bytefoolish, I am about to work though this tutorial and I wanted to ask which OS Image would be best to install, or does it not really matter? As a newbie I am thinking that NOOBS might be best. Also I have bought a Raspberry Pi but it is a R-Pi 2; will this make any difference when going through this tutorial? Your comment's would be most welcome. Thanks.

May 2016. If anyone else is looking to install NOOBS (Raspian Jessie) and OpenCV on a Raspberry Pi 2 Model B (UK), then this is the guide that worked for me; it also has extensive guides and tutorials on OpenCV: http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/

About This Instructable

191,070views

144favorites

License:

More by bytefoolish:RasPi + OpenCV = Face Tracking
Add instructable to: