Accessing a Raspberry Pi Camera Using OpenCV and Python - PyImageSearch (2023)

Click here to download the source code for this publication

There have been many popular posts on the PyImageSearch blog over the past year. Wearingk-means grouping to find dominant colorspictured was (and still is) very popular. One of my favorites,build a great mobile document scanneris the most popular PyImageSearch article for months. And the first (good) tutorial I wrote,Hobby and histogram, an article about building a simple image search engine, still has many hits.

Menwith a lot, the most popular PyImageSearch blog post is my tutorialinstallation of OpenCV and Python on your Raspberry Pi 2 and B+. is veryActuallygreat to see the love you and PyImageSearch readers have for the Raspberry Pi community and I plan to continue writing more articles about OpenCV + Raspberry Pi in the future.

Anyway, after posting the Raspberry Pi + OpenCV installation guide, many comments asked me for discussionhow to access raspberry pi camera using python and opencv.

In this guide we will usepicamerawhich gives the camera module a pure Python interface. And most importantly, I will show you how to use picamera to take photos in OpenCV format.

Read on to find out how…

IMPORTANT:Be sure to follow one of mineRaspberry Pi OpenCV installation instructionsbefore following the steps in this exercise.

Accessing a Raspberry Pi Camera Using OpenCV and Python - PyImageSearch (2)

Looking for the source code for this post?

Go directly to the download section

OpenCV and Python versions:
This example will workPython 2.7/Python 3.4+yOpenCV 2.4.X/OpenCV 3.0+.

To get started, you'll need a Raspberry Pi Camera Board Module.

(Video) Accessing the Raspberry Pi camera using Python and OpenCV

I have mineRaspberry Pi Amazon 5MP kamerakortmodulfor less than $30, including shipping. It's hard to believe that the camera card module is almost as expensive as the Raspberry Pi itself, but it shows how much hardware has evolved over the last 5 years. I also got an Acamera housingto keep the camera safe, because why not?

Assuming you already have a camera module, install it. The installation is very simple, and instead of making your own camera board installation guide, I refer you to the official Raspberry Pi camera installation guide:

Assuming the camera card is properly installed and configured, it should look like this:

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

$ sudo raspi configuration

This will open a screen that looks like this:

Use the arrow keys to scroll down toOption 5: Turn on the camera,press Enterto turn on the camera, then arrow down tofinishbutton and press enter again. Finally,you need to restart your Raspberry Pifor the setting to take effect.

Before we dive into the code, let's run a quick validation check to make sure our Raspberry Pi camera is working properly.

Use:Trust me, you'll want to do that sanity check before you start working on the code. IsAlways goodmaking sure the camera is working before diving into the OpenCV code; otherwise you can waste time trying to figure out when your code isn't working properly when the problem is just the camera module.

Anyway, to do a sanity check, I connected my Raspberry Pi to my TV and positioned it to point at my couch:

From there I opened a terminal and ran the following command:

$ raspistill -o salida.jpg

This command activates the Raspberry Pi camera module, shows a preview of the image, then takes a picture after a few seconds and saves it in the current working directory asoutput.jpg.

Here's an example of me taking a picture of my TV screen (so I can document the process in this tutorial) while the Raspberry Pi takes a picture of me:

And here's whatoutput.jpglooks like:

Apparently my Raspberry Pi camera module is working fine! Now we can move on to more exciting things.

At this point we know our Raspberry Pi camera is working properly. But how do we interact with Raspberry Pi's camera module using Python?

the answer ispicameramodule.

remember aboutfront tutorialwhich we useVirtual environmentyvirtual packagingclean install and segment our python packages from system and python packages?

Well, we do the same here.

before installationpicamera, be sure to activate ourCVvirtual environment:

(Video) Building a Raspberry Pi security camera with OpenCV

$ job i CV

Use:If you installpicamerasystem-wide module, you can omit the above commands. But if you follow alongfront tutorialyou want to make sure you joinCVvirtual environment before continuing with the next command.

From there we can installpicamerauser pip:

$ pip installer "picamera [table]"

IMPORTANT:Note how I specifiedpicamera[matrix]and not onlypicamera.

Why is it so important?

While the normpicameraThe module provides methods to interact with the camera we need (optional)creationsubmodule so we can use OpenCV. Note that when using python bindings, OpenCV renders images as NumPy arrays, acreationThe submodule allows us to get NumPy arrays from the Raspberry Pi's camera module.

Assuming the installation completed without errors, you now have the filepicameramodule installed (with support for NumPy arrays).

Okay, now we can finally start writing code!

Open a new file, give it a nametest_obrazu.pyand insert the following code:

# import necessary packages from picamera.array import PiRGBArray from picamera import PiCameraimport timeimport cv2# initialize camera and get raw camera reference capturecamera = PiCamera()rawCapture = PiRBArray(camera)# let camera warm up time.sleep (0.1)# get image from camera .capture(rawCapture, format="bgr")image = rawCapture.array# show the image on the screen and wait for the key presscv2.imshow("Image", image)cv2. waiting key (0)

We start by importing our necessary packages tolines 2-5.

From there we initialize our PiCamera object toline 8and take a reference to the raw capture component inline 9. This israw catchThe object is particularly useful because (1) it gives us direct access to the camera stream and (2) it avoids expensive JPEG compression that we would have to take and decode into OpenCV anyway. Hellohighly recommendedwhat do you usePirGBArraywhen you need access to the Raspberry Pi camera,the performance gain is worth it.

From there we slept a tenth of a secondline 12- this allows the camera sensor to heat up.

Finally, we take a real pictureraw catchobject iline 15where we try our best to make our image in BGR format instead of RGB. OpenCV renders images as NumPy arrays in BGR order, not RGB order: This minor annoyance is subtle, but very important to remember as it can lead to confusing code errors down the road.

Finally, we display our image on the screen iLines 19 and 20.

To run this example, open a terminal, go to yourtest_obrazu.pyfile and issue the following command:

$ python prueba_imagen.py

If everything goes as expected, you should see an image on your screen:

Use:I decided to add this part of the blog post.AfterI finished the rest of the article so I didn't have the camera positioned to face the couch (I was actually playing around with some custom home surveillance software I was working on). Sorry for the confusion, but rest assured that everything will work as advertised as long as you follow the instructions in the article!

Okay, so we've learned how to take a single photo from the Raspberry Pi's camera. But what about the video stream?

It can be assumed that we will use the so-calledcv2.Video recordingthey work here but I really recommend itmodThis here. fewcv2.Video recordinghaving fun with a Raspberry Pi is not a pleasant experience (you will have to install additional drivers) and is something you should generally avoid.

And besides, why should we spendcv2.Video recordingwork when we canletaccess the raw video stream usingpicameramodule?

(Video) Unifying picamera and cv2.VideoCapture into a single class with OpenCV

Let's go ahead and take a look at how we can access video streaming. Open a new file, give it a nameprueba_video.pyand insert the following code:

# import necessary packages from picamera.array import PiRGBArray from picamera import PiCameraimport timeimport cv2# initialize camera and get raw camera reference capturecamera = PiCamera()camera.solution = (640, 480)camera.framerate = 32rawCapture = PirGBArray ( camera , size , 640 , 480))# let the camera warm up.sleep(0.1)# capture images from the camera to the frames in the camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):# grab a NumPy raw array representing the image, and then initialize timestamp # and text occupied/empty image = frame. array # show frame cv2.imshow("Frame", image) key = cv2. next framerawCapture.truncate(0)# if `q` key pressed break out of loop if key == word("q"):break

This example starts the same way as the previous one. We start by importing the packages we needlines 2-5.

And from there we build ourscameraobject iline 8which allows us to interact with the Raspberry Pi camera. However, we also spent some time setting the resolution of our camera (640 x 480 pixels).line 9and the number of images per per second (ie frames per second or simply FPS) iline 10. We are also starting oursPirGBArrayobject iline 11, but we also make sure to provide the same resolution as inline 9.

Access to the actual video stream is supported inline 17callingcapture_continuedour methodcameraobject.

This method returns aMarcovideo stream. The frame then has onecreationthe corresponding propertyMarcoin NumPy array format - all the hard work is done by usLine 17 and 20!

Then we take a frame from the video and display it on screen wLines 23 and 24.

An important line to note isLine 27: TymusiClear the current frame before moving on to the next one!

If you cannot clear the frame, the python script will throw an error:so be sure to pay close attention to this when deploying your own apps!

Finally, if the user presses the buttonQkey we break the loop and exit the program.

To run our script, simply open a terminal (make sure you're withCVvirtual environment of course) and issue the following command:

$ python prueba_video.py

Below is an example of how I run the above command:

As you can see, OpenCV reads the video stream from the Raspberry Pi camera and then displays it on the screen. The Raspberry Pi camera also shows no lag when accessing images at 32 fps. Granted, we're not rendering individual frames, but as I'll show in future blog posts, the Pi 2 can easily maintain 24-32 FPS even when rendering every single frame.

What's next? I recommendUniversity PyImageSearch.

Accessing a Raspberry Pi Camera Using OpenCV and Python - PyImageSearch (9)

Course information:
76 lessons in total • 90 hours of on-demand video tutorials • Last updated: May 2023
★★★★★4.84 (128 degrees) • Over 16,000 registered students

I firmly believe that if you had the right teacher, you couldmaestrocomputer vision and deep learning.

(Video) Multiple cameras with the Raspberry Pi and OpenCV

Do you think learning computer vision and deep learning has to be time consuming, overwhelming and complicated? Or does it have to involve math and complex equations? Or does it require an education in computer science?

This isNOsuitcase.

All you need to master computer vision and deep learning is someone to explain everything to yousimple, intuitiveconditions.And that's exactly what I do. My mission is to change education and the way of teaching complex issues related to artificial intelligence.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive online course on computer vision, deep learning, and OpenCV. Find out how heresuccessfullyywith confidenceapply computer vision to your work, research and projects. Join us in mastering computer vision.

Within PyImageSearch University you will find:

  • &control76 courseson basic topics related to computer vision, deep learning and OpenCV
  • &control76 certificatescompletion
  • &control90 timervideo on demand
  • &controlLaunch of new courseson a regular basismake sure you can keep up with the latest techniques
  • &controlForudkonfigurerede Jupyter-notebooks i Google Colab
  • &control Run all code samples in your web browser - works on Windows, macOS and Linux (no development environment setup required!)
  • &Control access tocentralized code repositories foratOver 500 tutorialspl PyImageSearch
  • &controlEasy download with one clickfor code, datasets, pre-trained models, etc.
  • &controlaccesson a mobile phone, laptop, desktop computer, etc.

Click here to become a member of PyImageSearch University

This article expanded on our previous tutorial oninstallation of OpenCV and Python on your Raspberry Pi 2 and B+and discussed how to access the Raspberry Pi camera module using Python and OpenCV.

We checked two methods to access the camera. The first method gave us access to a single image. And the second method gave us access to the raw video stream from the Raspberry Pi's camera module.

There are actually several ways to access the Raspberry Pi camera module such aspicamera documentation details. However, the methods described in this blog post are used because (1) they are easily compatible with OpenCV and (2) they are quite fast. There's certainly more than one way to skin this cat, but if you're going to use OpenCV + Python, I suggest you use the code in this article as a "boilerplate" for your own applications.

In future blog posts, we will use these examples to build computer vision systemsmotion detection in videosyrecognize faces in pictures.

Be sure to sign up for the PyImageSearch newsletter to get updates when new Raspberry Pi and computer vision posts are published, you definitely don't want to miss them!

Accessing a Raspberry Pi Camera Using OpenCV and Python - PyImageSearch (10)

Download the FREE 17-page source code and resource guide

Enter your email address below to receive your .zip code inA FREE 17-page guide to computer vision, OpenCV and deep learning resources.You'll find handpicked tutorials, books, courses and libraries to help you master your CV and DL!

FAQs

How do I access cameras using OpenCV in Python? ›

Compile and install:

The following sample OpenCV python code explain how to open the device video node, set the resolution, grab the frame and then display the frame in preview window. # Check whether user selected camera is opened successfully. Release the camera, then close all of the imshow() windows.

How do I open my Raspberry Pi camera in Python? ›

Enable Raspberry Pi camera

The camera module is not enabled by default. After attaching the camera module, boot up the Raspberry Pi. Go to the main menu and open the Raspberry Pi configuration tool. You can also open the configuration tool from the terminal by typing sudo raspi-config.

How do I access the camera module on my Raspberry Pi? ›

Open the Camera Port on the Raspberry Pi:

On the Raspberry Pi B+, 2 and 3, the camera port is between the audio port and the HDMI port. On the original Raspberry Pi B, it is between the Ethernet port and the HDMI port.

Videos

1. Building a custom home surveillance system using Python, OpenCV, and your Raspberry Pi (Part 1)
(PyImageSearch)
2. How To Access the RASPBERRY PI Camera Inside Docker and OpenCV
(CODE MENTAL)
3. Raspberry Pi Camera: Python Setup
(Unboxing Tomorrow)
4. 👉 Measuring TEMPERATURE 🔥 from a THERMAL IMAGE, VIDEO, or CAMERA using Python and OpenCV!
(RGM Vision | Computer Vision)
5. HOW TO INSTALL OPENCV4 ON RASPBERRY PI 4 - Guide based on PYIMAGESEARCH website.
(Circuit Desolator)
6. Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi
(PyImageSearch)

References

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated: 09/20/2023

Views: 6068

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.