banner



How To Test Raspberry Pi Camera

raspi_still_example_1

Over the by year the PyImageSearch blog has had a lot of popular blog posts. Using m-means clustering to find the ascendant colors in an image was (and still is) hugely pop. One of my personal favorites, building a kick-ass mobile document scanner has been the well-nigh popular PyImageSearch commodity for months. And the start (large) tutorial I always wrote, Hobbits and Histograms, an commodity on building a simple image search engine, withal gets a lot of hits today.

But by far , the most pop post on the PyImageSearch blog is my tutorial on installing OpenCV and Python on your Raspberry Pi 2 and B+. It'southward really, really crawly to see the dear you and the PyImageSearch readers have for the Raspberry Pi community — and I program to continue writing more articles about OpenCV + the Raspberry Pi in the hereafter.

Anyhow, after I published the Raspberry Pi + OpenCV installation tutorial, many of the comments asked that I go along on and discuss how to access the Raspberry Pi camera using Python and OpenCV.

In this tutorial we'll exist using picamera, 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…

Of import: Be sure to follow ane of my Raspberry Pi OpenCV installation guides prior to following the steps in this tutorial.

Looking for the source code to this post?

Jump Right To The Downloads Section

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

Footstep 1: What practice I need?

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

I got my 5MP Raspberry Pi photographic camera board module from Amazon for under $xxx, with shipping. It's hard to believe that the camera lath module is most equally expensive as the Raspberry Pi itself — but information technology but goes to prove how much hardware has progressed over the past 5 years. I also picked upward a camera housing to keep the camera safe, because why not?

Assuming you already have your camera module, you'll need to install it. Installation is very uncomplicated 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 lath and properly installed and setup, it should expect something similar this:

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

Stride 2: Enable your camera module.

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

$ sudo raspi-config          

This will bring up a screen that looks like this:

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

Use your arrow keys to curl down to Selection 5: Enable camera, hitting your enter cardinal to enable the camera, and and so pointer down to the Finish button and hitting enter over again. Lastly, you'll need to reboot your Raspberry Pi for the configuration to take affect.

Step 3: Test out the photographic camera module.

Before we swoop into the code, permit's run a quick sanity cheque to ensure that our Raspberry Pi camera is working properly.

Annotation: Trust me, you lot'll want to run this sanity check before yous outset working with the lawmaking. It'southward always expert 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'due south but the camera module itself that is causing you issues.

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

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

And from there, I opened upwardly a final and executed the post-obit control:

$ raspistill -o output.jpg          

This command activates your Raspberry Pi camera module, displays a preview of the paradigm, and so after a few seconds, snaps a picture, and saves information technology to your current working directory as output.jpg .

Here'southward an instance of me taking a photograph of my TV monitor (so I could document the process for this tutorial) equally the Raspberry Pi snaps a photograph of me:

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

And here's what output.jpg looks like:

Figure 5: The image captured using the raspi-still command.
Figure 5: The epitome captured using the raspi-all the same command.

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

Footstep 4: Installing picamera.

Then at this signal nosotros know that our Raspberry Pi camera is working properly. Just how do we interface with the Raspberry Pi photographic camera module using Python?

The answer is the picamera module.

Recollect from the previous tutorial how we utilized virtualenv and virtualenvwrapper to cleanly install and segment our Python packages from the the system Python and packages?

Well, nosotros're going to exercise the same thing hither.

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

$ workon cv          

Notation: If you are installing the the picamera module system wide, yous can skip the previous commands. Withal, if you are post-obit along from the previous tutorial, you'll want to make sure y'all are in the cv virtual environs before continuing to the next command.

And from in that location, 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 and then important?

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

Assuming that your install finished without error, you at present accept the picamera module (with NumPy array support) installed.

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

Alright, now nosotros can finally start writing some lawmaking!

Open up a new file, name it test_image.py , and insert the post-obit 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)  # let the camera to warmup time.sleep(0.i)  # grab an image from the camera camera.capture(rawCapture, format="bgr") prototype = rawCapture.array  # brandish the image on screen and wait for a keypress cv2.imshow("Image", image) cv2.waitKey(0)          

Nosotros'll start by importing our necessary packages on Lines 2-v.

From at that place, we initialize our PiCamera object on Line 8 and catch a reference to the raw capture component on Line nine. This rawCapture object is especially useful since it (ane) gives united states of america directly admission to the camera stream and (2) avoids the expensive compression to JPEG format, which nosotros would and then have to take and decode to OpenCV format anyhow. I highly recommend that yous utilise PiRGBArray whenever yous demand to access the Raspberry Pi camera — the operation gains are well worth it.

From there, we sleep for a tenth of a 2d 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 fiddling nuisance is subtle, but very of import to remember as it tin lead to some confusing bugs in your lawmaking down the line.

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

To execute this instance, open upwardly a last, navigate to your test_image.py file, and issue the following command:

$ python test_image.py          

If all goes every bit expected yous should have an paradigm displayed on your screen:

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

Note: I decided to add this section of the blog post afterward I had finished up the rest of the article, so I did not have my camera setup facing the burrow (I was actually playing with some custom home surveillance software I was working on). Sad for whatsoever confusion, merely residual assured, everything will work equally advertised provided y'all have followed the instructions in the article!

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

Alright, and so we've learned how to grab a single image from the Raspberry Pi photographic camera. Merely what well-nigh a video stream?

You might gauge that we are going to use the cv2.VideoCapture part hither — simply I actually recommend against this. Getting cv2.VideoCapture to play nice with your Raspberry Pi is not a squeamish feel (you'll need to install extra drivers) and something you should by and large avoid.

And besides, why would we use the cv2.VideoCapture function when we tin 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 upwards 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 photographic camera and grab a reference to the raw photographic camera capture camera = PiCamera() photographic camera.resolution = (640, 480) photographic camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(640, 480))  # let the camera to warmup time.sleep(0.1)  # capture frames from the photographic camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=Truthful): 	# take hold of the raw NumPy array representing the epitome, then initialize the timestamp 	# and occupied/unoccupied text 	image = frame.array  	# show the frame 	cv2.imshow("Frame", image) 	fundamental = cv2.waitKey(one) & 0xFF  	# clear the stream in grooming for the next frame 	rawCapture.truncate(0)  	# if the `q` central was pressed, suspension from the loop 	if cardinal == ord("q"): 		pause          

This example starts off similarly to the previous ane. We offset off past importing our necessary packages on Lines 2-five.

And from at that place nosotros construct our camera object on Line viii which allows u.s.a. to interface with the Raspberry Pi camera. Yet, we also take the time to set the resolution of our photographic camera (640 10 480 pixels) on Line 9 and the frame rate (i.e. frames per second, or simply FPS) on Line ten. We too initialize our PiRGBArray object on Line xi, but we as well take care to specify the aforementioned resolution as on Line 9.

Accessing the bodily 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 assortment format — all the hard work is done for united states on Lines 17 and xx!

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: Y'all must clear the current frame before you movement on to the next one!

If yous fail to clear the frame, your Python script will throw an mistake — so exist sure to pay shut attention to this when implementing your own applications!

Finally, if the user presses the q key, we break class the loop and leave the program.

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

$ python test_video.py          

Below follows an instance of me executing the above control:

Every bit you can run across, the Raspberry Pi photographic camera's video stream is being read by OpenCV and and so displayed on screen! Furthermore, the Raspberry Pi camera shows no lag when accessing frames at 32 FPS. Granted, nosotros are not doing any processing on the individual frames, but as I'll evidence in hereafter blog posts, the Pi ii can easily proceed upward 24-32 FPS fifty-fifty when processing each frame.

What's next? I recommend PyImageSearch University.

Form information:
35+ total classes • 39h 44m video • Last updated: Feb 2022
★★★★★ four.84 (128 Ratings) • 3,000+ Students Enrolled

I strongly believe that if you had the right teacher yous could main calculator vision and deep learning.

Exercise you lot think learning estimator 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 non the case.

All y'all need to main computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that'due south exactly what I do. My mission is to alter education and how complex Bogus Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the nigh comprehensive estimator vision, deep learning, and OpenCV grade online today. Here you'll learn how to successfully and confidently apply estimator vision to your work, enquiry, and projects. Join me in reckoner vision mastery.

Inside PyImageSearch University you'll find:

  • 35+ courses on essential estimator vision, deep learning, and OpenCV topics
  • ✓ 35+ Certificates of Completion
  • 39h 44m on-demand video
  • Brand new courses released every month , ensuring you can keep up with state-of-the-art techniques
  • Pre-configured Jupyter Notebooks in Google Colab
  • ✓ Run all code examples in your spider web browser — works on Windows, macOS, and Linux (no dev surroundings configuration required!)
  • ✓ Access to centralized code repos for all 500+ tutorials on PyImageSearch
  • Like shooting fish in a barrel one-click downloads for code, datasets, pre-trained models, etc.
  • ✓ Access on mobile, laptop, desktop, etc.

Click here to join PyImageSearch University

Summary

This article extended our previous tutorial on installing OpenCV and Python on your Raspberry Pi two and B+ and covered how to access the Raspberry Pi camera module using Python and OpenCV.

Nosotros reviewed two methods to admission the camera. The get-go method immune united states to access a unmarried photograph. And the 2d method allowed us to access the raw video stream from the Raspberry Pi camera module.

In reality, there are many ways to admission the Raspberry Pi camera module, every bit the picamera documentation details. Nonetheless, the methods detailed in this blog post are used because (i) they are hands compatible with OpenCV and (2) they are quite speedy. There are certainly more than one mode to skin this true cat, simply if y'all intend on using OpenCV + Python, I would suggest using the code in this commodity as "boilerplate" for your own applications.

In futurity blog posts nosotros'll take these examples and employ it to build computer vision systems todetect 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 alive, you definitely don't want to miss them!

Download the Source Code and FREE 17-folio Resources Guide

Enter your email address below to go a .cypher of the code and a Gratis 17-folio Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll detect my manus-picked tutorials, books, courses, and libraries to help yous main CV and DL!

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

Posted by: christensenwele1946.blogspot.com

0 Response to "How To Test Raspberry Pi Camera"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel