Bukit Mertajam, Penang

Raspberry Pi Zero W and Pi Camera Application

Last Updated on 4 Febuary 2018
This article is written by internship student Ezyan Roshdi

Previously, we have introduced you to the Raspberry Pi Zero W. You may refer to this tutorial for the introduction of the Raspberry Pi Zero W. In this tutorial, we are going to learn about how to use Raspberry Pi Zero W together with Pi Camera and OpenCV applications.

OpenCV stands for Open Source Computer Vision. It is a library of programming functions mainly for real-time computer vision. This library is open-source under the open-source BSD license.




1. Update and upgrade the raspberry pi before installing anything else.

In the terminal, run these commands:

  • sudo apt-get update
  • sudo apt-get upgrade
  • sudo rpi-update

2. Enable the camera interface.

Run the command sudo raspi-config . Then go to interfacing option, camera and enable the camera. You need to reboot your Pi after doing this.

3. Test the camera by running the command raspistill -o image.jpg

Where the “image” is your picture’s name. If the red LED on the camera turns on and your image is saved, it means your camera is functioning well.


4. Install the 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):

wget https://bootstrap.pypa.io/get-pip.py
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:

import cv2

(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

Open a new python file and name it TestImage. Inside the python idle, write the following code

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

Open a new python file and name it TestVideo. Inside the python idle, write the following code

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. Try out the Face Detection Application

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)

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.

Related Posts

Comments (8)

thanks for the tutorial, i just followed it today with a RASPBIAN STRETCH LITE image. but i get the error
ImportError: libavcodec.so.56: cannot open shared object file: No such file or directory

i checked and it seems that the installed version is libavcodec.so.57 is installed.

how can i solve this error?

today I tryed again using raspbian jessie image, with this one, everything went without error

Is this for python2 only? What about python3?

it’s worked with raspbian jessies (latest version), good tutorial.

[…] Raspberry Pi Zero W and Pi Camera Application […]

Do you have any plans to update your code to use the latest libraries? import CV in python throws multiple errors due to old libraries. Is there a fix for this?

Yes we plan to update the tutorial, but we can’t promise when it can be done. Maybe you can Google your error first and search on forum?

Leave a comment

Get 7 Tips for Developing Electronic Projects
We respect your privacy.