Title: Accessing the Raspberry Pi Camera with OpenCV and Python - PyImageSearch

URL Source: https://pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/

Published Time: 2015-03-30T14:00:06+00:00

Markdown Content:
[![Image 1: raspi_still_example_1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/raspi_still_example_1.jpg)

Over the past year the PyImageSearch blog has had a lot of popular blog posts. Using [k-means clustering to find the dominant colors](https://pyimagesearch.com/2014/05/26/opencv-python-k-means-color-clustering/ "OpenCV and Python K-Means Color Clustering") in an image was (and still is) hugely popular. One of my personal favorites, [building a kick-ass mobile document scanner](https://pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/ "How to Build a Kick-Ass Mobile Document Scanner in Just 5 Minutes") has been the most popular PyImageSearch article for months. And the first (big) tutorial I ever wrote, [Hobbits and Histograms](https://pyimagesearch.com/2014/01/27/hobbits-and-histograms-a-how-to-guide-to-building-your-first-image-search-engine-in-python/ "Hobbits and Histograms – A How-To Guide to Building Your First Image Search Engine in Python"), an article on building a simple image search engine, still gets a lot of hits today.

But _**by far**_, the most popular post on the PyImageSearch blog is my tutorial on [installing OpenCV and Python on your Raspberry Pi 2 and B+](https://pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/ "Install OpenCV and Python on your Raspberry Pi 2 and B+"). It’s really, _really_ awesome to see the love you and the PyImageSearch readers have for the Raspberry Pi community — and I plan to continue writing more articles about OpenCV + the Raspberry Pi in the future.

Anyway, after I published the Raspberry Pi + OpenCV installation tutorial, many of the comments asked that I continue on and discuss **_how to access the Raspberry Pi camera using Python and OpenCV._**

In this tutorial we’ll be using [picamera](https://picamera.readthedocs.org/en/release-1.9/), which provides a pure Python interface to the camera module. And best of all, I’ll be showing you how to use picamera to capture images in OpenCV format.

Read on to find out how…

_**IMPORTANT:** Be sure to follow one of my [Raspberry Pi OpenCV installation guides](https://pyimagesearch.com/opencv-tutorials-resources-guides/) prior to following the steps in this tutorial._

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

**OpenCV and Python versions:**  
This example will run on **Python 2.7/Python 3.4+** and **OpenCV 2.4.X/OpenCV 3.0+**.

Step 1: What do I need?
-----------------------

To get started, you’ll need a Raspberry Pi camera board module.

I got my [5MP Raspberry Pi camera board module from Amazon](http://www.amazon.com/gp/product/B00E1GGE40/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B00E1GGE40&linkCode=as2&tag=trndingcom-20&linkId=XF5KMO3TGBUENU5T) for under $30, with shipping. It’s hard to believe that the camera board module is almost as expensive as the Raspberry Pi itself — but it just goes to show how much hardware has progressed over the past 5 years. I also picked up a [camera housing](http://www.amazon.com/gp/product/B00IJZJKK4/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B00IJZJKK4&linkCode=as2&tag=trndingcom-20&linkId=PMQZXV7K7MWCPAZ3) to keep the camera safe, because why not?

Assuming you already have your camera module, you’ll need to install it. Installation is very simple and instead of creating my own tutorial on installing the camera board, I’ll just refer you to the official Raspberry Pi camera installation guide:

Assuming your camera board and properly installed and setup, it should look something like this:

[![Image 3: Figure 1: Installing the Raspberry Pi camera board.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20800'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/installing_camera_board.jpg)

**Figure 1:** Installing the Raspberry Pi camera board.

Step 2: Enable your camera module.
----------------------------------

Now that you have your Raspberry Pi camera module installed, you need to enable it. Open up a terminal and execute the following command:

$ sudo raspi-config

This will bring up a screen that looks like this:

[![Image 4: Figure 2: Enabling the Raspberry Pi camera module using the raspi-config command.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/raspi_config.jpg)

**Figure 2:** Enabling the Raspberry Pi camera module using the raspi-config command.

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.

Step 3: Test out the camera module.
-----------------------------------

Before we dive into the code, let’s run a quick sanity check to ensure that our Raspberry Pi camera is working properly.

_**Note:** Trust me, you’ll want to run this sanity check before you start working with the code. It’s **always good** to ensure that your camera is working prior to diving into OpenCV code, otherwise you could easily waste time wondering when your code isn’t working correctly when it’s simply the camera module itself that is causing you problems._

Anyway, to run my sanity check I connected my Raspberry Pi to my TV and positioned it such that it was pointing at my couch:

[![Image 5: Figure 3: Example setup of my Raspberry Pi 2 and camera.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20800'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/example_setup.jpg)

**Figure 3:** Example setup of my Raspberry Pi 2 and camera.

And from there, I opened up a terminal and executed the following command:

$ raspistill -o output.jpg

This command activates your Raspberry Pi camera module, displays a preview of the image, and then after a few seconds, snaps a picture, and saves it to your current working directory as `output.jpg` .

Here’s an example of me taking a photo of my TV monitor (so I could document the process for this tutorial) as the Raspberry Pi snaps a photo of me:

[![Image 6: Figure 4: Sweet, the Raspberry Pi camera module is working!](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/raspi_still_example_simple.jpg)

**Figure 4:** Sweet, the Raspberry Pi camera module is working!

And here’s what `output.jpg` looks like:

[![Image 7: Figure 5: The image captured using the raspi-still command.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/raspberry_pi_output.jpg)

**Figure 5:** The image captured using the raspi-still command.

Clearly my Raspberry Pi camera module is working correctly! Now we can move on to the some more exciting stuff.

Step 4: Installing picamera.
----------------------------

So at this point we know that our Raspberry Pi camera is working properly. But how do we interface with the Raspberry Pi camera module using Python?

The answer is the [picamera](http://picamera.readthedocs.org/en/release-1.9/index.html) module.

Remember from the [previous tutorial](https://pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/ "Install OpenCV and Python on your Raspberry Pi 2 and B+") how we utilized `virtualenv` and `virtualenvwrapper` to cleanly install and segment our Python packages from the the system Python and packages?

Well, we’re going to do the same thing here.

Before installing `picamera` , be sure to activate our `cv` virtual environment:

$ workon cv

_**Note:** If you are installing the the `picamera` module system wide, you can skip the previous commands. However, if you are following along from the [previous tutorial](https://pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/ "Install OpenCV and Python on your Raspberry Pi 2 and B+"), you’ll want to make sure you are in the `cv` virtual environment before continuing to the next command._

And from there, we can install `picamera` by utilizing pip:

$ pip install "picamera\[array\]"

_**IMPORTANT:** Notice how I specified `picamera[array]` and not just `picamera` ._

Why is this so important?

While the standard `picamera` module provides methods to interface with the camera, we need the (optional) `array` sub-module so that we can utilize OpenCV. Remember, when using Python bindings, OpenCV represents images as NumPy arrays — and the `array` sub-module allows us to obtain NumPy arrays from the Raspberry Pi camera module.

Assuming that your install finished without error, you now have the `picamera` module (with NumPy array support) installed.

Step 5: Accessing a single image of your Raspberry Pi using Python and OpenCV.
------------------------------------------------------------------------------

Alright, now we can finally start writing some code!

Open up a new file, name it `test_image.py` , and insert the following code:

\# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)

# allow the camera to warmup
time.sleep(0.1)

# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array

# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)

We’ll start by importing our necessary packages on **Lines 2-5**.

From there, we initialize our PiCamera object on **Line 8** and grab a reference to the raw capture component on **Line 9**. This `rawCapture` object is especially useful since it (1) gives us direct access to the camera stream and (2) avoids the expensive compression to JPEG format, which we would then have to take and decode to OpenCV format anyway. I _highly recommend_ that you use `PiRGBArray` whenever you need to access the Raspberry Pi camera — _**the performance gains are well worth it.**_

From there, we sleep for a tenth of a second on **Line 12** — this allows the camera sensor to warm up.

Finally, we grab the actual photo from the `rawCapture` object on **Line 15** where we take special care to ensure our image is in BGR format rather than RGB. OpenCV represents images as NumPy arrays in BGR order rather than RGB — this little nuisance is subtle, but very important to remember as it can lead to some confusing bugs in your code down the line.

Finally, we display our image to screen on **Lines 19 and 20**.

To execute this example, open up a terminal, navigate to your `test_image.py` file, and issue the following command:

$ python test\_image.py

If all goes as expected you should have an image displayed on your screen:

[![Image 8: Figure 6: Grabbing a single image from the Raspberry Pi camera and displaying it on screen.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20451'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/03/single_image_example.png)

**Figure 6:** Grabbing a single image from the Raspberry Pi camera and displaying it on screen.

_**Note:** I decided to add this section of the blog post **after** I had finished up the rest of the article, so I did not have my camera setup facing the couch (I was actually playing with some custom home surveillance software I was working on). Sorry for any confusion, but rest assured, everything will work as advertised provided you have followed the instructions in the article!_

Step 6: Accessing the video stream of your Raspberry Pi using Python and OpenCV.
--------------------------------------------------------------------------------

Alright, so we’ve learned how to grab a single image from the Raspberry Pi camera. But what about a video stream?

You might guess that we are going to use the `cv2.VideoCapture` function here — but I actually recommend _against_ this. Getting `cv2.VideoCapture` to play nice with your Raspberry Pi is not a nice experience (you’ll need to install extra drivers) and something you should generally avoid.

And besides, why would we use the `cv2.VideoCapture` function when we can _easily_ access the raw video stream using the `picamera` module?

Let’s go ahead and take a look on how we can access the video stream. Open up a new file, name it `test_video.py` , and insert the following code:

\# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

# allow the camera to warmup
time.sleep(0.1)

# capture frames from the camera
for frame in camera.capture\_continuous(rawCapture, format="bgr", use\_video\_port=True):
	# grab the raw NumPy array representing the image, then initialize the timestamp
	# and occupied/unoccupied text
	image = frame.array

	# show the frame
	cv2.imshow("Frame", image)
	key = cv2.waitKey(1) & 0xFF

	# clear the stream in preparation for the next frame
	rawCapture.truncate(0)

	# if the \`q\` key was pressed, break from the loop
	if key == ord("q"):
		break

This example starts off similarly to the previous one. We start off by importing our necessary packages on **Lines 2-5**.

And from there we construct our `camera` object on **Line 8** which allows us to interface with the Raspberry Pi camera. However, we also take the time to set the resolution of our camera (640 x 480 pixels) on **Line 9** and the frame rate (i.e. frames per second, or simply FPS) on **Line 10**. We also initialize our `PiRGBArray` object on **Line 11**, but we also take care to specify the same resolution as on **Line 9**.

Accessing the actual video stream is handled on **Line 17** by making a call to the `capture_continuous` method of our `camera` object.

This method returns a `frame` from the video stream. The frame then has an `array` property, which corresponds to the `frame` in NumPy array format — all the hard work is done for us on **Lines 17 and 20!**

We then take the frame of the video and display on screen on **Lines 23 and 24**.

An important line to pay attention to is **Line 27:** **You _must_ clear the current frame before you move on to the next one!**

If you fail to clear the frame, your Python script will throw an error — **_so be sure to pay close attention to this when implementing your own applications!_**

Finally, if the user presses the `q` key, we break form the loop and exit the program.

To execute our script, just open a terminal (making sure you are in the `cv` virtual environment, of course) and issue the following command:

$ python test\_video.py

Below follows an example of me executing the above command:

As you can see, the Raspberry Pi camera’s video stream is being read by OpenCV and then displayed on screen! Furthermore, the Raspberry Pi camera shows no lag when accessing frames at 32 FPS. Granted, we are not doing any processing on the individual frames, but as I’ll show in future blog posts, the Pi 2 can easily keep up 24-32 FPS even when processing each frame.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

This article extended our previous tutorial on [installing OpenCV and Python on your Raspberry Pi 2 and B+](https://pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/ "Install OpenCV and Python on your Raspberry Pi 2 and B+") and covered how to access the Raspberry Pi camera module using Python and OpenCV.

We reviewed two methods to access the camera. The first method allowed us to access a single photo. And the second method allowed us to access the raw video stream from the Raspberry Pi camera module.

In reality, there are many ways to access the Raspberry Pi camera module, as the [picamera documentation details](http://picamera.readthedocs.org/en/release-1.9/index.html). However, the methods detailed in this blog post are used because (1) they are easily compatible with OpenCV and (2) they are quite speedy. There are certainly more than one way to skin this cat, but if you intend on using OpenCV + Python, I would suggest using the code in this article as “boilerplate” for your own applications.

In future blog posts we’ll take these examples and use it to build computer vision systems to **detect motion in videos** and **recognize faces in images**.

Be sure to sign up for the PyImageSearch Newsletter to receive updates when new Raspberry Pi and computer vision posts go live, you definitely don’t want to miss them!

![Image 9](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!