Introduction: 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
Step 2: Connect Raspberry Pi
- Connect Ethernet cord to port
- Connect Keyboard and Mouse to USB ports
- Insert SD card
- Connect HDMI cord to monitor
- Connect HDMI cord to Raspberry Pi
- 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.
- Type the Following into the terminal
- sudo apt-get update
- sudo apt-get upgrade -y
- sudo reboot
- sudo apt-get update
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
- Open a terminal and type the following lines. We are installing some python libraries this step should take approx 3 minutes.
- sudo apt-get install python-pygame python-scipy python-numpy
- sudo apt-get install python-pip
- sudo pip install --upgrade pip
- Click "Menu" in top right corner
- Click "Shutdown" at the bottom of list
- Select log out
- Log back in by typing "stratx" followed by the return key
- sudo pip install virtualenvwrapper
- vi ~/.bashrc
- type "i" to enter insert mode
- paste the following on any line without quotes "source /usr/local/bin/virtualenvwrapper.sh"
- . .bashrc
- mkvirtualenv --system-site-packages env
- wget https://raw.github.com/jayrambhia/Install-OpenCV/master/Ubuntu/2.4/opencv2_4_5.sh
- chmod +x opencv2_4_5.sh
- ./opencv2_4_5.sh
- wget -O OpenCV-2.4.11.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.11/opencv-2.4.11.zip/download
- unzip OpenCv-2.4.11.zip
- cd opencv-2.4.11
- mkdir build
- cd build
- 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 ..
- make -j4
- sudo make install
- sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
- sudo ldconfig
- apt-get remove libopencv-core2.3
- export PYTHONPATH=/usr/local/lib/python2.7/site-packages/
- export LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so
Step 6: (Optional) - Install SimpleCv
- Installation approx 5 minutes
- sudo apt-get install git
- git clone git://github.com/sightmachine/SimpleCV.git
- cd SimpleCV
- sudo python setup.py install
- simplecv
- camera = Camera()
Step 7: 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...
15 Comments
8 years ago on Introduction
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!
Reply 8 years ago on Introduction
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.
Reply 8 years ago on Introduction
I featured your comment, great suggestion!
Reply 8 years ago on Introduction
Thanks!
Reply 8 years ago on Introduction
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).
8 years ago on Introduction
This is awesome! I've never done anything like this before. I think that I will look into this more when I have time!
Reply 8 years ago on Introduction
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!
Question 5 years ago on Step 5
make -j4 says No targets specified and no makefile found. Stop.
6 years ago
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/
7 years ago
are you serious about the 5-10 hours thing??
7 years ago
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?
7 years ago
İ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?
7 years ago
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?
7 years ago
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.
Reply 7 years ago
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/