Last Updated on 7 August 2017
This article is written by internship student Ezyan Roshdi
Previously, we have introduced to you the Raspberry Pi Zero W. You may refer to this tutorial for the introductory part of Raspberry Pi Zero W. In this tutorial, we are going to learn on how to use Raspberry Pi Zero W together with Pi Camera and OpenCV application.
OpenCV is an Open Source Computer Vision. It is a library of programming functions mainly aimed at real-time computer vision. This library is an open sources library under the open-source BSD license.
1. Update and upgrade the raspberry before installing anything else. Inside the command prompt, run the command:
- sudo apt-get update
- sudo apt-get upgrade
- sudo rpi-update
run the command sudo raspi-config , go to interfacing option, camera, enable the camera (you will need to reboot your Pi after doing this)
3. Test the camera by running the command raspistill -o image.jpg (where “image” is your picture’s name). If the led red on the camera turn on and your image is saved, meaning that your camera can function well, then you may proceed.
4. Next we need to install necessary packages to use OpenCv inside Raspberry Pi. OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. To do so, run all these necessary commands :
Install python 2.7.9 version using command:
sudo apt-get install python2.7-dev
install the python wrapper for OpenCV (imutils is a set of convenience functions to make basic image processing tasks easier):
sudo apt-get install python-opencv sudo pip install imutils
OpenCV process images:
sudo apt-get install libtiff5-dev libjasper-dev libpng12-dev
OpenCv process video:
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
GTK library for some GUI (graphical user interface) stuffs like viewing images:
sudo apt-get install libgtk2.0-dev
for various operations in OpenCV:
sudo apt-get install libatlas-base-dev gfortran
install pip (tool for installing and managing Python packages):
sudo python get-pip.py
install numpy (fundamental package for scientific computing with Python):
sudo pip install numpy
install this file to get the latest opencv:
wget "https://github.com/jabelone/OpenCV-for-Pi/raw/master/latest-OpenCV.deb" sudo dpkg -i latest-OpenCV.deb
Test if it has been installed correctly by running the command:
python import cv2 cv2.__version__
(this command should return the version of OpenCV you are using)
5. Now that we have OpenCV installed, lets try some simple code to try the Pi Camera with OpenCV.
But first, you need to run this command :
sudo pip install "picamera[array]"
we need the “array” sub-module so that we can utilize OpenCV since OpenCV represents images as NumPy arrays and the “array” sub-module allows us to obtain NumPy arrays from the Raspberry Pi camera module.
6. Accessing a single image on your Raspberry Pi Zero W using Python and OpenCV
i. pen a new python file and name it TestImage. Inside the python idle, write the following code
ii. Click “Run” and check the output. It should able to capture an image and display it back (make sure you are running from the python 2.7.9 version).
7. Accessing the video stream on your Raspberry Pi Zero W using Python and OpenCV
i. Open a new python file and name it TestVideo. Inside the python idle, write the following code
ii. click “Run” and check the output. It should able to display a live stream video (make sure you are running from the python 2.7.9 version)
8. Now that you have done all that, let’s go a little bit deeper and do a Face Detection Application on Raspberry Pi Zero W using Python and OpenCV. This face detection application is a simple application to make Raspberry Pi (with OpenCV) detect how many faces are included inside the frame. Since all the necessary packages have been downloaded before, now all you have to do is open a new python file and name it face_detect.py . Then write the code below inside the python idle (make sure you are using python 2.7.9 version when you want to Run it)
9. Click run to see the output. Considering you have done everything smoothly without any error, your desktop should display a rectangle frame of live stream video and circle around the faces. Simple and easy right? You can watch the video below to see how it functions.
Note: For further inquiry, please visit our technical forum as we seldom check the comment here. ?