Raspbian + OpenCV pre-configured and pre-installed.

raspbian_image_header

Ever since I wrote the first PyImageSearch tutorial on installing OpenCV + Python on the Raspberry Pi B+ back in February 2015 it has been my dream to offer a downloadable, pre-configured Raspbian .img file with OpenCV pre-installed.

Today this dream has become a reality.

I am pleased to announce that both the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV now ship with a downloadable Raspbian .img file that comes with OpenCV pre-configured and pre-installed.

This .img file is also tiny, weighing in at only 1.7GB — that’s approximately the same size as the official Raspbian release!

All you have to do is download the .img file, flash it to your SD card, and boot your Pi.

From there, you’ll have a complete Python + OpenCV development environment at your fingertips, all without the hassle of configuring, compiling, and installing OpenCV.

To learn more about the Raspbian .img file, keep reading.

Raspbian + OpenCV out-of-the-box

I went back to my recent tutorials on installing OpenCV on the Raspberry Pi and computed the amount of time it takes to perform each step.

You know what I found?

Even if you know exactly what you are doing it can take over 2.2 hours to compile and install OpenCV on your Raspberry Pi.

I then emailed a sample of novice readers who successfully installed OpenCV on their Pi and asked how long it took them to complete the compile and installation process.

Perhaps not surprisingly I found the amount of time for novice readers to install OpenCV on their Raspberry Pi jumped nearly 4x to over 8.7 hours.

Clearly, the barrier to entry for many PyImageSearch readers trying to learn OpenCV and computer vision is getting OpenCV itself installed on their Raspberry Pi.

In an effort to help these readers get the most out of their Raspberry Pi, I have decided to release my own personal Raspbian .img file that has OpenCV pre-configured and pre-installed.

By bundling the pre-configured Raspbian .img together with Practical Python and OpenCV my goal is to:

  1. Jumpstart your computer vision education by skipping the tedious process of installing OpenCV + Python on your Raspberry Pi.
  2. Provide you with my book, Practical Python and OpenCV, the best introduction to the world of computer vision and image processing that you can possibly get.

Of course, I will continue to createsupport, and provide help to any PyImageSearch reader who is using the many free tutorials I offer on installing OpenCV + Python on the Raspberry Pi.

Again, this pre-configured Raspbian .img is intended for PyImageSearch readers who want to save time and jumpstart their computer vision education.

If this doesn’t sound like you, no worries, I totally understand — I’ll be still be providing free tutorials to help you get OpenCV up and running on your Raspberry Pi.

Raspbian + OpenCV pre-configured and pre-installed

The rest of this document describes how to install and use the Raspbian pre-configured .img file included in your purchase of the Quickstart Bundle or Hardcopy Bundle of Practical Python and OpenCV.

Note: The pre-configured Raspbian + OpenCV .img file is not included in the Basic Bundle.

At the end of this guide you’ll also find answers to frequently asked questions regarding the Raspbian + OpenCV .img file. If you have a question that is not covered in FAQ, please send me a message.

Download and unpack the archive

After downloading your Quickstart Bundle or Hardcopy Bundle unzip the Raspbian.zip  file using your favorite unarchiving utility:

Figure 3: After downloading the Raspbian.zip file, unpack it to obtain the .img file that you'll flash to your SD card.

Figure 3: After downloading the Raspbian.zip file, unpack it to obtain the .img file that you’ll flash to your SD card.

After unzipping you should see the .img file similar to the screenshot above.

Write image to SD card

This Raspbian .img will work on 8GB, 16GB, and 32GB SD cards. I have not tested cards larger than 32GB at this time as I do not currently own a card that large. A 4GB card is too small.

To write the pre-configured Raspbian .img to your card simply follow the official Raspberry Pi documentation. I have provided quick links below:

Booting your Pi for the first time

After writing the the Raspbian .img to your card, insert the card into your Pi and boot it up. On the first boot your Raspbian filesystem will be auto-expanded to utilize the entire disk.

This means that you do not have to run raspi-config => Expand Filesystem  manually.

After the auto-expansion has completed your Pi will reboot and you will be able to use it as normal (the auto-expansion of the filesystem is only performed on the first boot).

Here is a screenshot of the disk utilization on my Pi after it has been auto-expanded:

Figure 4: After booting my Raspberry Pi for the first time your filesystem will be expanded to utilize the entire disk.

Figure 4: After booting my Raspberry Pi for the first time your filesystem will be expanded to utilize the entire disk.

Notice that my entire 16GB card is being utilized.

Using Python and OpenCV on your Raspberry Pi

In order to get OpenCV 3 installed with both Python 2.7 and Python 3 bindings I leveraged Python virtual environments. Each Python virtual environment is totally independent from one another ensuring there are no dependency or versioning issues.

In the remainder of this section I explain (1) what Python virtual environments are and (2) how to access the OpenCV + Python 2.7/Python 3 bindings.

What are Python virtual environments?

At the very core, Python virtual environments allow us to create isolated, independent environments for each of our Python projects. This implies that each project can have its own set of dependenciesregardless of which dependencies another project has.

In the context of OpenCV, this allows us to have one virtual environment for Python 2.7 and then another virtual environment for Python 3. Furthermore, each of these virtual environments can be cloned and have their own set of dependencies for a given project.

For a detailed look at Python virtual environments please refer to this tutorial.

Accessing the Python 2.7 + OpenCV 3 environment

To access the Python 2.7 + OpenCV 3 virtual environment you need to run two commands:

The source ~/.profile  command loads any PATH  configurations from your ~/.profile  file. The workon py2cv3  accesses the Python 2.7 + OpenCV 3 virtual environment.

Conveniently, when you boot up your Pi you’ll notice that I have included a single script that you can use to setup your development environment in your home directory:

This command will execute both the source  and workon  commands for you. After executing it your shell will look like this:

Figure 5: How to access the Python 2.7 + OpenCV 3 environment on your Raspberry Pi.

Figure 5: How to access the Python 2.7 + OpenCV 3 environment on your Raspberry Pi.

Notice how the text (py2cv3)  appears before your shell. This implies that you are in the py2cv3  virtual environment. This command only needs to be executed once for each shell that you open up.

Accessing the Python 3 + OpenCV 3 environment

Similar to the section above, the Python 3 + OpenCV 3 environment can also be accessed using the source  and workon  commands:

You can also just use the following command located in your home directory:

This command only needs to be executed once for each shell you open up. After executing it you’ll notice that the text (py3cv3)  now appears before the prompt indicating that you have access to Python 3 + OpenCV 3:

Figure 6: How to access the Python 3 + OpenCV 3 environment on your Raspberry Pi.

Figure 6: How to access the Python 3 + OpenCV 3 environment on your Raspberry Pi.

Executing code from Practical Python and OpenCV on your Raspberry Pi

There are multiple methods to access the Practical Python and OpenCV source code on your Pi. The first is to use Chromium, Raspbian’s built-in web browser to download the .zip archive(s):

Figure 7: Downloading the source code from Practical Python and OpenCV using the Raspberry Pi web browser.

Figure 7: Downloading the source code from Practical Python and OpenCV using the Raspberry Pi web browser.

You may also use your favorite SFTP/FTP client and transfer the code from your system to your Pi:

Figure 8: Utilize a SFTP/FTP client to transfer the Practical Python and OpenCV code from your system to the Raspberry Pi.

Figure 8: Utilize a SFTP/FTP client to transfer the Practical Python and OpenCV code from your system to the Raspberry Pi.

Or you may want to manually write the code on the Pi using the built-in text editor as you follow along with the book:

Figure 9: Using the built-in text editor that ships with the Raspberry Pi to write code.

Figure 9: Using the built-in text editor that ships with the Raspberry Pi to write code.

I would suggest either downloading the Practical Python and OpenCV source code via web browser or using SFTP/FTP as this also includes the datasets utilized in the book as well. However, manually coding along is a great way to learn and I highly recommend it!

X11 forwarding with your Raspberry Pi

If you know how to use SSH then you can utilize X11 forwarding to access your Raspberry Pi. Simply provide the -X  switch when SSH’ing into your Pi, like this:

You will then be able to execute Python examples that utilize cv2.imshow  and see the results on your laptop/desktop screen.

However, your X11 session will timeout/close so if you see the following error message:

Then simply close your SSH connection and re-connect.

Note: Mac users may first need to install XQuartz to utilize X11 forwarding. Windows users should check their respective SSH client documentation for instructions on how to use X11 forwarding.

Frequently Asked Questions (FAQ)

In this section I detail the answers to frequently asked questions regarding the Raspberry Pi .img file.

Will this Raspbian image work with my Raspberry Pi 2, Raspberry Pi 3, or Raspberry Pi Zero?

Yes, this pre-configured Raspbian image is compatible with the Raspberry Pi 2 , Raspberry Pi 3 and Raspberry Pi Zero out-of-the-box. This allows you to save potentially 8+ hours of compile and install time and get started learning OpenCV on your Raspberry Pi immediately.

What if I want to install OpenCV + Python on my Raspberry Pi by myself?

By all means, I encourage you to do so. It’s a great exercise and you’ll learn a lot about the Linux environment. I would suggest you follow one of my many free tutorials on installing OpenCV + Python your Raspberry Pi.

Again, this pre-configured Raspbian image is intended for readers who want to skip the install process and jumpstart their education.

How long will it take to install Python + OpenCV by hand?

I’ve ran the numbers and even if you know exactly what you are doing it will take a bare minimum of 2.2 hours to compile and install OpenCV.

If you have never installed OpenCV before or you are not familiar with Linux-based environments that number can easily jump to over 8.7 hours based on my survey of novice readers who successfully installed OpenCV on their Raspberry Pi.

It really comes down to how much you value your time and how quickly you want to get started learning computer vision. I always encourage you to use my free tutorials on installing OpenCV on the Raspberry Pi, but if you want to save yourself time (and headaches), then definitely consider going with the pre-configured Raspbian .img.

Which Practical Python and OpenCV bundles is the Raspbian image included in?

The pre-configured Raspbian image is included in both the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV. The pre-configured Raspbian image is not included in the Basic Bundle.

After installing your distribution of Raspbian, how do I access Python + OpenCV?

See the “Using Python and OpenCV on your Raspberry Pi” section above.

Is Wolfram’s Mathematica included in your Raspbian distribution?

No, I am not legally allowed to distribute a modified version of Raspbian (that is part of a product) with Mathematica installed.

How did you reduce the size of the Raspbian image file?

To start, I removed unneeded software such as Wolfram’s Mathematica and LibreOffice. Removing these two pieces of software alone saved nearly 1GB of space.

From there, the size of the main partition was reduced and was set to auto-expand on first boot.

What Python packages are installed on the Raspberry Pi?

After accessing either the respective py2cv3  or py3cv3  virtual environments run pip freeze  to see a full list of Python packages installed.

In short, I have included all necessary Python packages you will need to be successful executing the examples in Practical Python and OpenCV, including OpenCV, NumPy, SciPy, scikit-learn, scikit-image, mahotas, and many others.

Where can I learn more about Python virtual environments?

My favorite resource and introduction to Python virtual environments can be found here. I also discuss them in the first half of this blog post.

Where can I purchase a copy of Practical Python and OpenCV?

To purchase your copy of Practical Python and OpenCV, simply click here, select your bundle (I recommend either the Quickstart Bundle or the Hardcopy Bundle), and checkout.

I have another question.

If you have a question not listed in this FAQ, please send me a message.

Sound good?

To purchase your copy of Practical Python and OpenCV, just use this link.

You will find the pre-configured Raspbian .img file inside both the Quickstart Bundle and the Hardcopy Bundle (the Basic Bundle does not include the Rasbpian .img).

, , , , ,

24 Responses to Raspbian + OpenCV pre-configured and pre-installed.

  1. Hugh November 21, 2016 at 12:29 pm #

    Excellent work Adrian, many thanks! I am working with a group of students who are using a USB monochrome camera on a Pi-3; the camera supports 1394 protocol over USB-2 (it’s a Point Grey Chameleon 2), have you any examples using that interface with Python?

    • Adrian Rosebrock November 21, 2016 at 12:43 pm #

      Thanks Hugh. I don’t have any examples that directly cover the firewire protocol, but I would instead look to see if the camera is compatible with the cv2.VideoCapture function. If it is, this is by far the easiest way to access frames from a video stream for peripheral cameras.

  2. Atas Feti November 21, 2016 at 1:30 pm #

    Dear Adrian thank you for your great explanation. I am about to start rasppery pi and İ find your module very useful. So that raspian.zip file should we buy some kind of course to be able download it cause ı couldnt see a link to do so

    • Adrian Rosebrock November 21, 2016 at 2:11 pm #

      Hey Atas — the Raspbian .img file is included in both the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV.

      Once you purchase your copy you will be able to download a file named “Raspbian.zip”. Inside this .zip file is your Raspbian .img file.

  3. Bogdan November 22, 2016 at 3:01 am #

    I was waiting for this for some time. This is really helpful Adrian! I have just finished installing OpenCV on a CHIP computer (9 bucks). Compiling takes even longer (~9 h), so I preferred using apt-get, but I get only Opencv 2.x, which is fine (at least for now).
    I wish Chip would have a way to backup and restore, so that I can just flash an *.img file like I can on RPi.
    Btw, do you think this img would run on RPi zero also?

    • Adrian Rosebrock November 22, 2016 at 12:33 pm #

      Let me boot up my Raspberry Pi Zero and test it out. I will edit this comment as soon as I get the result.

      EDIT: Confirmed. I just booted up my Raspberry Pi Zero and confirmed that my Raspbian .img file will work with the Pi Zero as well. I have included a photo as proof here (the terminal is simply demonstrating that Python can import the OpenCV bindings).

  4. Melrick Nicolas November 22, 2016 at 4:33 am #

    where is the download link?

    • Adrian Rosebrock November 22, 2016 at 12:31 pm #

      The Raspbian .img file is part of the of the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV. You would need to purchase one of these bundles to download the pre-configured Raspbian + OpenCV .img file.

  5. Rick November 23, 2016 at 7:42 pm #

    Have a wonderful time with all your friends and family. You’re an very class act and I look forward to another year!

    • Adrian Rosebrock November 24, 2016 at 9:36 am #

      Thank you Rick, I appreciate that :-)

  6. Aly November 28, 2016 at 2:55 am #

    Adrian,

    How do I rebuild opencv in your new img. I want to rebuild it because of a opencv bug in face.hpp I commented something out, and now I want to rebuild opencv how would I do that?

    Aly

    • Adrian Rosebrock November 28, 2016 at 10:18 am #

      If you would like to install compile + install OpenCV again you would need to download the source, make your modifications, and then re-run CMake and make. I would refer to my previous tutorial on installing OpenCV on the Pi for more information.

  7. Tony Eckel November 29, 2016 at 4:08 pm #

    Note for Windows Users: To use remote desktop, at SSH prompt do:

    sudo apt-get install xrdp vnc4server

    That will allow Remote Desktop access to the Raspberry Pi desktop

    Notice that Mathimatica and Wolfram icons are dead in the menu bar. Were these removed from the build?

    • Adrian Rosebrock December 1, 2016 at 7:43 am #

      Yes, they were removed from the build. Please see the “Is Wolfram’s Mathematica included in your Raspbian distribution?” section of this post for more details.

  8. Sam December 11, 2016 at 12:14 pm #

    What default password have you set for the user ‘pi’ in this pre-configured image?

    • Adrian Rosebrock December 12, 2016 at 10:37 am #

      The password is the same default password for all Raspberry Pi machines: raspberry

  9. steve gale January 3, 2017 at 3:39 pm #

    Hi Adrian,

    I have been following your blog for quite a while and have created an application which I want to run automatically when my raspberry PI boots.

    Do you know if it is possible to get a virtual environment to run on power up?
    I have created a script similar to your start_py2cv3.sh ,
    source ~/.profile
    workon cv
    python /home/pi/homesurv.py –conf conf.json

    when the python script runs I get an error message along the lines of can’t find cv2 implying that the virtual environment is not running.

    If not, I will start with a clean image and install opencv manually, following one of your blogs of course!

    cheers
    Steve

  10. Roberto January 9, 2017 at 2:05 pm #

    Hi and thanks for the valuable posts you do.
    I’d like to know (probably I just missed it) the raspian version you’ve installed opencv on, is it pixel?
    Thanks
    Rob

    • Adrian Rosebrock January 10, 2017 at 1:08 pm #

      Hey Roberto — you are correct, the Raspbian version that comes with OpenCV pre-installed has PIXEL.

  11. Daniella Solomon January 14, 2017 at 12:53 pm #

    Hi!
    What application do you use in mac for SFTP/FTP client?

    • Adrian Rosebrock January 15, 2017 at 12:04 pm #

      I normally use Transmit or CyberDuck, but you could just as easily use the command line as well.

  12. ALOK MISHRA January 20, 2017 at 2:16 am #

    sir,i want to ask u
    i just copy my sd card which have already rasbian and open cv and paste it into another new memory card and then i want to use that new memory card
    is it possible

    • Adrian Rosebrock January 20, 2017 at 10:55 am #

      Hey Alok — I’m not sure I understand what you mean. You have a new memory card and you want to install my pre-configured Raspbian .img file on it?

Leave a Reply