Title: Common errors using the Raspberry Pi camera module - PyImageSearch

URL Source: https://pyimagesearch.com/2016/08/29/common-errors-using-the-raspberry-pi-camera-module/

Published Time: 2016-08-29T14:00:44+00:00

Markdown Content:
![Image 1: Figure 2: The strange blank/black frame error.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20375'%3E%3C/svg%3E)

Today’s blog post will take a short diversion from our recent trend of Deep Learning tutorials here on the PyImageSearch blog and instead focus on a topic that I’ve been receiving a ton of emails about lately — **common errors when using the Raspberry Pi camera module.**

I want to start this post by mentioning [Dave Jones](https://github.com/waveform80), the maintainer and chief contributor to the [picamera library](https://github.com/waveform80/picamera). Dave is one of the most active open source contributors that I’ve had the privilege to interact with (and he’s a hell of a nice guy too).

A few months ago, I was using the (at the time) latest `picamera==1.11` library and was running in to a few errors. After checking the `picamera` GitHub, [I noticed an Issue had been posted regarding my problem](https://github.com/waveform80/picamera/issues/297). I confirmed the existence of the bug, which Dave then sought out — and fixed before the day was over, even releasing a new, updated version to the PyPI repository.

It goes without saying that without Dave, computer vision and OpenCV on the Raspberry Pi wouldn’t be nearly as fun — _or half as accessible._

Over the past few years I’ve encountered a few “common” errors when using the Raspberry Pi and the `picamera` library. My goal here today is to document some of these errors so you can easily fix them.

In fact, most of the issues I’m documenting here today are not real “errors” at all — they are simply misunderstandings on how the `picamera` library works in conjunction with your Raspberry Pi setup.

![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)

Before we can look at common errors when using the Raspberry Pi camera module, let’s first discuss how we can simply _access_ the `picamera` video stream.

### How to access your picamera video stream

To start, I am going to assume that you’ve already followed the instructions in _[Accessing the Raspberry Pi Camera with OpenCV and Python](https://pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/)_ and installed the `picamera` library on your Pi.

If you haven’t installed `picamera` , this can be accomplished using `pip` :

$ pip install "picamera\[array\]"

We add the `[array]` portion to the command to ensure we can read frames as NumPy arrays, thus making the module compatible with OpenCV.

After `pip` has finished installing `picamera` , you can check the version number using the following command:

$ pip freeze

**The reported version of picamera should be _at least_ 1.12.**

#### A quick note on Python virtual environments

If you’re a frequent reader of the PyImageSearch blog, you’ll know that I use Python virtual environments _a lot_ — and because of this, you likely do as well.

Before we continue, take a second to see if you are using Python virtual environments by `source` ‘ing your `~/.profile` file and listing all the available virtual environments on your system:

$ source ~/.profile
$ lsvirtualenv

If you get an error related to the `lsvirtualenv` command not being found, then you are _not_ utilizing Python virtual environments (or you have potentially made a mistake editing your `~/.profile` file). If you’re not using Python virtual environments, then you can skip the next paragraph and move to the next sub-section.

Assuming you are using Python virtual environments, you can execute the `workon` command to access each of the individual Python virtual environments on your system. In most install tutorials on the PyImageSearch blog, we name our Python virtual environment `cv` , short for “computer vision”:

$ workon cv

#### A Python template for accessing your Raspberry Pi camera module

In order to access the `picamera` video stream, I’ve created a simple, extendible template which I’ll detail for you below.

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)

**Lines 2-5** handle importing our required Python packages.

We then initialize our `camera` object on **Line 8**, which allows us to access the Raspberry Pi camera module. We’ll define the resolution of the video stream to be _640 x 480_ with a maximum frame rate of 32 FPS (**Lines 9 and 10**).

From there, we initialize our `PiRGBArray` object on **Line 11**, passing in the original `camera` object and then _explicitly_ re-stating the resolution as well. This `PiRGBArray` object allows us to actually read frames from the Raspberry Pi camera module in NumPy format, thereby making the frames compatible with OpenCV.

Finally, we wait 0.1 seconds to allow the Raspberry Pi camera sensor to warmup.

Our next code block handles actually capturing frames from our Raspberry Pi camera sensor:

\# 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 - this array
	# will be 3D, representing the width, height, and # of channels
	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

On **Line 17** we start looping over frames captured from the `camera` using the `capture_continuous` function. We pass three parameters into this method.

The first is `rawCapture` , the format in which we want to read each frame. We then specify the `format` to be `bgr` since OpenCV expects image channels to be in BGR order rather than RGB. Finally, the `use_video_port` boolean indicates that we are treating the stream as video.

Once we have the `frame` , we can access the raw NumPy array via the `.array` attribute (**Line 20**).

We display the frame to our screen on **Lines 23 and 24** using OpenCV GUI functions.

But before we can move on to the next frame, we first need to prepare our stream by calling the `.truncate` method on the `rawCapture` object. If you _do not_ do this, your Python script will throw an error — the exact error we’ll review later in this guide.

Finally, if the `q` key is pressed (**Lines 30 and 31**), we break from the loop.

To execute the `test_video.py` script, just open up a terminal/command line prompt and execute the following command:

$ python test\_video.py

_**Note:** If you’re using Python virtual environments, you’ll want to use the `workon` command to switch to the Python environment that has your OpenCV + picamera library installed._

If all goes well, you should see the Raspberry Pi video stream displayed to your feed:

![Image 3: Figure 1: Displaying the Raspberry Pi video stream to our screen.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20375'%3E%3C/svg%3E)

**Figure 1:** Displaying the Raspberry Pi video stream to our screen.

Otherwise, if you get an error — keep reading. **I’ve detailed the most common error messages that I run in to below.**

### Can’t connect to your picamera module?

Does the following error message look familiar?

$ python test\_video.py 
mmal: mmal\_vc\_component\_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal\_component\_create\_core: could not create component 'vc.ril.camera' (1)
Traceback (most recent call last):
  File "test\_video.py", line 11, in <module>
    camera = PiCamera()
  File "/home/pi/.virtualenvs/cv2/local/lib/python2.7/site-packages/picamera/camera.py", line 488, in \_\_init\_\_
    self.STEREO\_MODES\[stereo\_mode\], stereo\_decimate)
  File "/home/pi/.virtualenvs/cv2/local/lib/python2.7/site-packages/picamera/camera.py", line 526, in \_init\_camera
    "Camera is not enabled. Try running 'sudo raspi-config' "
picamera.exc.PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.

If you are getting this error message, then you likely forgot to (1) run `raspi-config` , 92) enable the camera, and (3) reboot your Pi.

If you are _still_ getting an error message after running `raspi-config` , then your camera is likely installed incorrectly. In this case, I would suggest giving this install video a watch and then trying to install your Raspberry Pi camera module again (be sure to power down your Pi first!)

### Truncation problems

Truncation errors are fairly easy to identify since they always end with the text `Incorrect buffer length for resolution` . An example of such an error message can be found below:

$ python test\_video.py 
Traceback (most recent call last):
  File "test\_video.py", line 20, in <module>
    for frame in camera.capture\_continuous(rawCapture, format="bgr", use\_video\_port=True):
  File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/picamera/camera.py", line 1851, in capture\_continuous
    if not encoder.wait(self.CAPTURE\_TIMEOUT):
  File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/picamera/encoders.py", line 850, in wait
    self.stop()
  File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/picamera/encoders.py", line 881, in stop
    self.\_close\_output()
  File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/picamera/encoders.py", line 795, in \_close\_output
    output.flush()
  File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/picamera/array.py", line 285, in flush
    self.array = bytes\_to\_rgb(self.getvalue(), self.size or self.camera.resolution)
  File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/picamera/array.py", line 174, in bytes\_to\_rgb
    'Incorrect buffer length for resolution %dx%d' % (width, height))
picamera.exc.PiCameraValueError: Incorrect buffer length for resolution 640x480

If you are getting this error message, you likely forgot to call `.truncate` after you were done processing your frame. **Line 27** of our `test_video.py` script above demonstrates how to use the `.truncate` method.

**In short:** Go back to your script and ensure you have called `.truncate` before `.capture_continuous` is called again.

### The picamera==1.11 and Python 3 specific error

The _v1.11_ release of the `picamera` library introduced a Python 3 specific error. It was particularly hard to diagnose, but as Dave Jones points out, the issue was [due to a mis-configuration in his test suite](https://github.com/waveform80/picamera/issues/297#issuecomment-230094920), leading to _no_ Python 3 tests being performed.

This error is often diagnosed by seeing the text `TypeError: startswith first arg must be bytes or a tuple of bytes, not str` as the final line in the error message. A full example of this error message is shown below:

$ python test\_video.py
Traceback (most recent call last):
  File "test\_image.py", line 15, in <module>
    camera.capture(rawCapture, format="bgr")
  File "/home/pi/.virtualenvs/cv/local/lib/python3.4/site-packages/picamera/camera.py", line 1371, in capture
    camera\_port, output\_port, format, resize, \*\*options)
  File "/home/pi/.virtualenvs/cv/local/lib/python3.4/site-packages/picamera/camera.py", line 652, in \_get\_image\_encoder
    self, camera\_port, output\_port, format, resize, \*\*options)
  File "/home/pi/.virtualenvs/cv/local/lib/python3.4/site-packages/picamera/encoders.py", line 1049, in \_\_init\_\_
    parent, camera\_port, input\_port, format, resize, \*\*options)
  File "/home/pi/.virtualenvs/cv/local/lib/python3.4/site-packages/picamera/encoders.py", line 534, in \_\_init\_\_
    if not resize and format != 'yuv' and input\_port.name.startswith('vc.ril.video\_splitter'):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

The easiest solution is to either upgrade or downgrade your `picamera` module by one point version.

To upgrade to _v1.12_ (or whatever the current version of `picamera` is), use the following command:

$ pip install --upgrade picamera

To downgrade to _v1.10_, just use this commands:

$ pip uninstall picamera
$ pip install "picamera\[array\]"==1.10

Make sure you are being mindful of whether you’re using Python virtual environments or not. If you are, execute the `workon` command before running `pip` to ensure `picamera` is installed in to your virtual environment.

### Blank and/or black frame

![Image 4: Figure 2: The strange blank/black frame error.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20375'%3E%3C/svg%3E)

**Figure 2:** The strange blank/black frame error.

The blank/black frame is a particularly strange problem — frames are being read from the video stream, they are just not being decoded and displayed properly.

First, run the `rpi-update` to grab the latest firmware updates for your Raspberry Pi:

$ sudo rpi-update

After your Pi reboots, try re-executing your Python script.

If the frame retrieved by the Raspberry Pi camera is _still_ blank/black, then downgrade your `picamera` installation to _v1.10_:

$ pip uninstall picamera
$ pip install "picamera\[array\]"==1.10

I’ve encountered situations where I’ve only had to run `rpi-update`  to resolve the issue. And I’ve also needed to _both_ upgrade my firmware and downgrade my `picamera`  version. I’m not sure what the common underlying thread is (I _think_ it’s related to using the newer version of the Raspberry Pi camera hardware + `picamera`  versioning, but I haven’t been able to nail that down yet).

### 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
-------

In this blog post, I reviewed common error messages that you may encounter when using your [Raspberry Pi camera module and the picamera library](https://pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/).

These errors can be quite frustrating to debug, especially if you’re just getting started. My hope is that this guide helps point you in the right direction if you run in to any of these problems — I’ve certainly had my share of long nights trying to track these errors down!

Finally, I would like to mention again the wonderful work and contribution of [Dave Jones](https://github.com/waveform80), the maintainer and chief contributor to the [picamera library](https://github.com/waveform80/picamera). Without him, we wouldn’t be able to have _near_ as much computer vision fun with our Pi’s!

![Image 5](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!