Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi

It’s been over two years since the release of Raspbian Jessie. As of August 17th, 2017, the Raspberry Pi foundation has officially released the successor to Raspbian Jessie — Raspbian Stretch.

Just as I have done in previous blog posts, I’ll be demonstrating how to install OpenCV 3 with Python bindings on Raspbian Stretch.

If you are looking for previous installation instructions for different platforms, please consult this list:

Otherwise, let’s proceed with getting OpenCV 3 with Python bindings installed on Raspian Stretch!

The quick start video tutorial

If this is your first time installing OpenCV or you are just getting started with Linux I highly suggest that you watch the video below and follow along with me as you guide you step-by-step on how to install OpenCV 3 on your Raspberry Pi running Raspbian Stretch:

Otherwise, if you feel comfortable using the command line or if you have previous experience with Linux environments, feel free to use the text-based version of this guide below.


In this tutorial, I am going to assume that you already own a Raspberry Pi 3 with Raspbian Stretch installed.

If you don’t already have the Raspbian Stretch OS, you’ll need to upgrade your OS to take advantage of Raspbian Stretch’s new features.

To upgrade your Raspberry Pi 3 to Raspbian Stretch, you may download it here and follow these upgrade instructions (or these for the NOOBS route which is recommended for beginners). The former instructions take approximately 10 minutes to download via a torrent client and about 10 minutes to flash the SD card at which point you can power up and proceed to the next section.

Note: If you are upgrading your Raspberry Pi 3 from Raspbian Jessie to Raspbian Stretch, there is the potential for problems. Proceed at your own risk, and consult the Raspberry Pi forums for help.

Important: It is my recommendation that you proceed with a fresh install of Raspbian Stretch! Upgrading from Raspbian Jessie is not recommended.

Assuming that your OS is up to date, you’ll need one of the following for the remainder of this post:

  • Physical access to your Raspberry Pi 3 so that you can open up a terminal and execute commands
  • Remote access via SSH or VNC.

I’ll be doing the majority of this tutorial via SSH, but as long as you have access to a terminal, you can easily follow along.

Can’t SSH? If you see your Pi on your network, but can’t ssh to it, you may need to enable SSH. This can easily be done via the Raspberry Pi desktop preferences menu (you’ll need an HDMI cable and a keyboard/mouse) or running sudo service ssh start  from the command line of your Pi.

After you’ve changed the setting and rebooted, you can test SSH directly on the Pi with the localhost address. Open a terminal and type ssh pi@  to see if it is working.

Keyboard layout giving you problems? Change your keyboard layout by going to the Raspberry Pi desktop preferences menu. I use the standard US Keyboard layout, but you’ll want to select the one appropriate for your keyboard or desire (any Dvorkac users out there?).

Installing OpenCV 3 on a Raspberry Pi 3 running Raspbian Stretch

If you’ve ever installed OpenCV on a Raspberry Pi (or any other platform before), you know that the process can be quite time consuming with many dependencies and pre-requisites that have to be installed. The goal of this tutorial is to thus guide you step-by-step through the compile and installation process.

In order to make the installation process go more smoothly, I’ve included timings for each step so you know when to take a break, grab a cup of coffee, and checkup on email while the Pi compiles OpenCV.

Let’s go ahead and get started installing OpenCV 3 on your Raspberry Pi 3 running Raspbian Stretch.

Step #1: Expand filesystem

Are you using a brand new install of Raspbian Stretch?

If so, the first thing you should do is expand your filesystem to include all available space on your micro-SD card:

And then select the “Advanced Options” menu item:

Figure 1: Select the “Advanced Options” item from the “raspi-config” menu.

Followed by selecting “Expand filesystem”:

Figure 2: Expanding the filesystem on your Raspberry Pi 3.

Once prompted, you should select the first option, “A1. Expand File System”, hit Enter on your keyboard, arrow down to the “<Finish>” button, and then reboot your Pi — you may be prompted to reboot, but if you aren’t you can execute:

After rebooting, your file system should have been expanded to include all available space on your micro-SD card. You can verify that the disk has been expanded by executing df -h and examining the output:

As you can see, my Raspbian filesystem has been expanded to include all 32GB of the micro-SD card.

However, even with my filesystem expanded, I have already used 15% of my 32GB card.

If you are using an 8GB card you may be using close to 50% of the available space, so one simple thing to do is to delete both LibreOffice and Wolfram engine to free up some space on your Pi:

After removing the Wolfram Engine and LibreOffice, you can reclaim almost 1GB!

Step #2: Install dependencies

This isn’t the first time I’ve discussed how to install OpenCV on the Raspberry Pi, so I’ll keep these instructions on the brief side, allowing you to work through the installation process: I’ve also included the amount of time it takes to execute each command (some depend on your Internet speed) so you can plan your OpenCV + Raspberry Pi 3 install accordingly (OpenCV itself takes approximately 4 hours to compile — more on this later).

The first step is to update and upgrade any existing packages:

Timing: 2m 14s

We then need to install some developer tools, including CMake, which helps us configure the OpenCV build process:

Timing: 19s

Next, we need to install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:

Timing: 21s

Just as we need image I/O packages, we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:

Timing: 32s

The OpenCV library comes with a sub-module named highgui which is used to display images to our screen and build basic GUIs. In order to compile the highgui module, we need to install the GTK development library:

Timing: 1m 36s

Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:

Timing: 23s

These optimization libraries are especially important for resource constrained devices such as the Raspberry Pi.

Lastly, let’s install both the Python 2.7 and Python 3 header files so we can compile OpenCV with Python bindings:

Timing: 45s

If you’re working with a fresh install of the OS, it is possible that these versions of Python are already at the newest version (you’ll see a terminal message stating this).

If you skip this step, you may notice an error related to the Python.h header file not being found when running make to compile OpenCV.

Step #3: Download the OpenCV source code

Now that we have our dependencies installed, let’s grab the 3.3.0 archive of OpenCV from the official OpenCV repository. This version includes the dnn  module which we discussed in a previous post where we did Deep Learning with OpenCV (Note: As future versions of openCV are released, you can replace 3.3.0 with the latest version number):

Timing: 41s

We’ll want the full install of OpenCV 3 (to have access to features such as SIFT and SURF, for instance), so we also need to grab the opencv_contrib repository as well:

Timing: 37s

You might need to expand the command above using the “<=>” button during your copy and paste. The .zip in the 3.3.0.zip may appear to be cutoff in some browsers. The full URL of the OpenCV 3.3.0 archive is:


Note: Make sure your opencv and opencv_contrib versions are the same (in this case, 3.3.0). If the versions numbers do not match up, then you’ll likely run into either compile-time or runtime errors.

Step #4: Python 2.7 or Python 3?

Before we can start compiling OpenCV on our Raspberry Pi 3, we first need to install pip , a Python package manager:

Timing: 33s

You may get a message that pip is already up to date when issuing these commands, but it is best not to skip this step.

If you’re a longtime PyImageSearch reader, then you’ll know that I’m a huge fan of both virtualenv and virtualenvwrapper. Installing these packages is not a requirement and you can absolutely get OpenCV installed without them, but that said, I highly recommend you install them as other existing PyImageSearch tutorials (as well as future tutorials) also leverage Python virtual environments. I’ll also be assuming that you have both virtualenv and virtualenvwrapper installed throughout the remainder of this guide.

So, given that, what’s the point of using virtualenv and virtualenvwrapper ?

First, it’s important to understand that a virtual environment is a special tool used to keep the dependencies required by different projects in separate places by creating isolated, independent Python environments for each of them.

In short, it solves the “Project X depends on version 1.x, but Project Y needs 4.x” dilemma. It also keeps your global site-packages neat, tidy, and free from clutter.

If you would like a full explanation on why Python virtual environments are good practice, absolutely give this excellent blog post on RealPython a read.

It’s standard practice in the Python community to be using virtual environments of some sort, so I highly recommend that you do the same:

Timing: 35s

Now that both virtualenv and virtualenvwrapper have been installed, we need to update our ~/.profile file to include the following lines at the bottom of the file:

In previous tutorials, I’ve recommended using your favorite terminal-based text editor such as vim , emacs , or nano to update the ~/.profile file. If you’re comfortable with these editors, go ahead and update the file to reflect the changes mentioned above.

Otherwise, you should simply use cat and output redirection to handle updating ~/.profile :

Now that we have our ~/.profile updated, we need to reload it to make sure the changes take affect. You can force a reload of your ~/.profile file by:

  1. Logging out and then logging back in.
  2. Closing a terminal instance and opening up a new one
  3. Or my personal favorite, just use the source command:

Note: I recommend running the source ~/.profile file each time you open up a new terminal to ensure your system variables have been setup correctly.

Creating your Python virtual environment

Next, let’s create the Python virtual environment that we’ll use for computer vision development:

This command will create a new Python virtual environment named cv using Python 2.7.

If you instead want to use Python 3, you’ll want to use this command instead:

Timing: 24s

Again, I can’t stress this point enough: the cv Python virtual environment is entirely independent and sequestered from the default Python version included in the download of Raspbian Stretch. Any Python packages in the global site-packages directory will not be available to the cv virtual environment. Similarly, any Python packages installed in site-packages of cv will not be available to the global install of Python. Keep this in mind when you’re working in your Python virtual environment and it will help avoid a lot of confusion and headaches.

How to check if you’re in the “cv” virtual environment

If you ever reboot your Raspberry Pi; log out and log back in; or open up a new terminal, you’ll need to use the workon command to re-access the cv virtual environment. In previous blog posts, I’ve seen readers use the mkvirtualenv command — this is entirely unneeded! The mkvirtualenv command is meant to be executed only once: to actually create the virtual environment.

After that, you can use workon and you’ll be dropped down into your virtual environment:

To validate and ensure you are in the cv virtual environment, examine your command line — if you see the text (cv) preceding your prompt, then you are in the cv virtual environment:

Figure 3: Make sure you see the “(cv)” text on your prompt, indicating that you are in the cv virtual environment.

Otherwise, if you do not see the (cv) text, then you are not in the cv virtual environment:

Figure 4: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and need to run “source” and “workon” to resolve this issue.

To fix this, simply execute the source and workon commands mentioned above.

Installing NumPy on your Raspberry Pi

Assuming you’ve made it this far, you should now be in the cv virtual environment (which you should stay in for the rest of this tutorial). Our only Python dependency is NumPy, a Python package used for numerical processing:

Timing: 11m 12s

Be sure to grab a cup of coffee or go for a nice walk, the NumPy installation can take a bit of time.

Note: A question I’ve often seen is “Help, my NumPy installation has hung and it’s not installing!” Actually, it is installing, it just takes time to pull down the sources and compile. You can verify that NumPy is compiling and installing by running top . Here you’ll see that your CPU cycles are being used compiling NumPy. Be patient. The Raspberry Pi isn’t as fast as your laptop/desktop.

Step #5: Compile and Install OpenCV

We are now ready to compile and install OpenCV! Double-check that you are in the cv virtual environment by examining your prompt (you should see the (cv) text preceding it), and if not, simply execute workon :

Once you have ensured you are in the cv virtual environment, we can setup our build using CMake:

Timing: 2m 56s

Now, before we move on to the actual compilation step, make sure you examine the output of CMake!

Start by scrolling down the section titled Python 2 and Python 3 .

If you are compiling OpenCV 3 for Python 2.7, then make sure your Python 2 section includes valid paths to the Interpreter , Libraries , numpy and packages path , similar to my screenshot below:

Figure 6: Checking that Python 3 will be used when compiling OpenCV 3 for Raspbian Stretch on the Raspberry Pi 3.

Notice how the Interpreter points to our python2.7 binary located in the cv virtual environment. The numpy variable also points to the NumPy installation in the cv environment.

Similarly, if you’re compiling OpenCV for Python 3, make sure the Python 3 section looks like the figure below:

Figure 6: Checking that Python 3 will be used when compiling OpenCV 3 for Raspbian Stretch on the Raspberry Pi 3.

Again, the Interpreter points to our python3.5 binary located in the cv virtual environment while numpy points to our NumPy install.

In either case, if you do not see the cv virtual environment in these variables paths, it’s almost certainly because you are NOT in the cv virtual environment prior to running CMake!

If this is the case, access the cv virtual environment using workon cv and re-run the cmake command outlined above.

Configure your swap space size before compiling

Before you start the compile process, you should increase your swap space size. This enables OpenCV to compile with all four cores of the Raspberry PI without the compile hanging due to memory problems.

Open your /etc/dphys-swapfile  and then edit the CONF_SWAPSIZE  variable:

Notice that I’ve commented out the 100MB line and added a 1024MB line. This is the secret to getting compiling with multiple cores on the Raspbian Stretch.

If you skip this step, OpenCV might not compile.

To activate the new swap space, restart the swap service:

Note: It is possible to burn out the Raspberry Pi microSD card because flash memory has a limited number of writes until the card won’t work. It is highly recommended that you change this setting back to the default when you are done compiling and testing the install (see below). To read more about swap sizes corrupting memory, see this page.

Finally, we are now ready to compile OpenCV:

Timing: 1h 30m

Once OpenCV 3 has finished compiling, your output should look similar to mine below:

Figure 7: Our OpenCV 3 compile on Raspbian Stretch has completed successfully.

From there, all you need to do is install OpenCV 3 on your Raspberry Pi 3:

Timing: 52s

Step #6: Finish installing OpenCV on your Pi

We’re almost done — just a few more steps to go and you’ll be ready to use your Raspberry Pi 3 with OpenCV 3 on Raspbian Stretch.

For Python 2.7:

Provided your Step #5 finished without error, OpenCV should now be installed in /usr/local/lib/python2.7/site-pacakges . You can verify this using the ls command:

Note: In some cases, OpenCV can be installed in /usr/local/lib/python2.7/dist-packages (note the dist-packages rather than site-packages ). If you do not find the cv2.so bindings in site-packages , we be sure to check dist-packages .

Our final step is to sym-link the OpenCV bindings into our cv virtual environment for Python 2.7:

For Python 3:

After running make install , your OpenCV + Python bindings should be installed in /usr/local/lib/python3.5/site-packages . Again, you can verify this with the ls command:

I honestly don’t know why, perhaps it’s a bug in the CMake script, but when compiling OpenCV 3 bindings for Python 3+, the output .so file is named cv2.cpython-35m-arm-linux-gnueabihf.so (or some variant of) rather than simply cv2.so (like in the Python 2.7 bindings).

Again, I’m not sure exactly why this happens, but it’s an easy fix. All we need to do is rename the file:

After renaming to cv2.so , we can sym-link our OpenCV bindings into the cv virtual environment for Python 3.5:

Step #7: Testing your OpenCV 3 install

Congratulations, you now have OpenCV 3 installed on your Raspberry Pi 3 running Raspbian Stretch!

But before we pop the champagne and get drunk on our victory, let’s first verify that your OpenCV installation is working properly.

Open up a new terminal, execute the source and workon commands, and then finally attempt to import the Python + OpenCV bindings:

As you can see from the screenshot of my own terminal, OpenCV 3 has been successfully installed on my Raspberry Pi 3 + Python 3.5 environment:

Figure 8: Confirming OpenCV 3 has been successfully installed on my Raspberry Pi 3 running Raspbian Stretch.

Once OpenCV has been installed, you can remove both the opencv-3.3.0 and opencv_contrib-3.3.0 directories to free up a bunch of space on your disk:

However, be cautious with this command! Make sure OpenCV has been properly installed on your system before blowing away these directories. A mistake here could cost you hours in compile time.

Don’t forget to change your swap size back!

Open your  /etc/dphys-swapfile  and then edit the  CONF_SWAPSIZE  variable:

Notice that I’ve commented out the 1024MB line and uncommented the 100MB line.

If you skip this step, your memory card won’t last as long. As stated above, larger swap spaces may lead to memory corruption, so I recommend setting it back to 100MB.

To revert to the smaller swap space, restart the swap service:

Troubleshooting and FAQ

Q. When I try to execute mkvirtualenv and workon , I get a “command not found error”.

A. There are three reasons why this could be happening, all of them related to Step #4:

  1. Make certain that you have installed virtualenv and virtualenvwrapper via pip . You can check this by running pip freeze and then examining the output, ensuring you see occurrences of both virtualenv and virtualenvwrapper .
  2. You might not have updated your ~/.profile correctly. Use a text editor such as nano to view your ~/.profile file and ensure that the proper export and source commands are present (again, check Step #4 for the contents that should be appended to ~/.profile .
  3. You did not source your ~/.profile after editing it, rebooting, opening a new terminal, etc. Any time you open a new terminal and want to use a virtual environment, make sure you execute source ~/.profile to load the contents — this will give you access to the mkvirtualenv and workon commands.

Q. After I open a new terminal, logout, or reboot my Pi, I cannot execute mkvirtualenv or workon .

A. See reason #3 from the previous question.

Q. When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error: ImportError: No module named cv2 .

A. Unfortunately, this error is extremely hard to diagnose, mainly because there are multiple issues that could be causing the problem. To start, make sure you are in the cv virtual environment by using workon cv . If the workon command fails, then see the first question in this FAQ. If you’re still getting an error, investigate the contents of the site-packages directory for your cv virtual environment. You can find the site-packages directory in ~/.virtualenvs/cv/lib/python2.7/site-packages/ or ~/.virtualenvs/cv/lib/python3.5/site-packages/ (depending on which Python version you used for the install). Make sure that your sym-link to the cv2.so file is valid and points to an existing file.

Q. I’m running into other errors.

A. Feel free to leave a comment and I’ll try to provide guidance; however, please understand that without physical access to your Pi it can often be hard to diagnose compile/install errors. If you’re in a rush to get OpenCV up and running on your Raspberry Pi be sure to take a look at the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV. Both of these bundles include a Raspbian .img file with OpenCV pre-configured and pre-installed. Simply download the .img file, flash it to your Raspberry Pi, and boot! This method is by far the easiest, hassle free method to getting started with OpenCV on your Raspberry Pi.

So, what’s next?

Congrats! You have a brand new, fresh install of OpenCV on your Raspberry Pi — and I’m sure you’re just itching to leverage your Raspberry Pi to build some awesome computer vision apps.

But I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and you’re probably feeling a bit confused and overwhelmed on where exactly to start.

Personally, I’m a big fan of learning by example, so a good first step would be to read this blog post on accessing your Raspberry Pi Camera with the picamera module. This tutorial details the exact steps you need to take to (1) capture photos from the camera module and (2) access the raw video stream.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real world computer vision problems including face detection in images and video streams, object tracking in video, and handwriting recognition.


All code examples covered in the book are guaranteed to run on the Raspberry Pi 2 and Pi 3 as well! Most programs will also run on the B+ and Zero models, but might be a bit slow due to the limited computing power of the B+ and Zero.

So let’s put your fresh install of OpenCV on your Raspberry Pi to good use — just click here to learn more about the real-world projects you can solve using your Raspberry Pi + Practical Python and OpenCV .


In this blog post, we learned how to upgrade your Raspberry Pi 3‘s OS to Raspbian Stretch and to install OpenCV 3 with either Python 2.7 or Python 3 bindings.

If you are running a different version of Raspbian (such as Raspbian Wheezy) or want to install a different version of OpenCV (such as OpenCV 2.4), please consult the following tutorials:

Are you looking for a project to work on with your new install of OpenCV on Raspbian Stretch? Readers have been big fans of this post on Home surveillance and motion detection with the Raspberry Pi, Python, OpenCV, and Dropbox.

But before you go…

I tend to utilize the Raspberry Pi quite a bit on this blog, so if you’re interested in learning more about the Raspberry Pi + computer vision, enter your email address in the form below to be notified when these posts go live!

, , , , , , ,

832 Responses to Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi

  1. Hilman September 4, 2017 at 7:22 pm #

    Adrian. I want to ask something.
    I have the Raspberry Pi 2 (and its camera module), but I just don’t know what kind of project I can do with it.
    Since I am busy, if possible, I want to make a project that can contribute the most to what I am learning right now (mainly machine learning). Do you have any idea?
    I were thinking of using it for scrapping data, but do not know where to begin. I would be very happy if you could recommend some suggestions.


    • Adrian Rosebrock September 5, 2017 at 9:18 am #

      Hi Hilman — the Raspberry Pi 2 is a bit underpowered so I wouldn’t recommend training a machine learning classifier on your Pi, but I could see deploying one. Have you considered training an image classifier to recognize a particular object on your laptop/desktop and then actually running it on your Raspberry Pi?

      Also, keep in mind that all chapters inside Practical Python and OpenCV will run on the Raspberry Pi. Go through any of those chapters and you can execute the projects on the Pi (such as face detection + tracking). Those chapters make for excellent starting points for projects.

      I hope that helps!

      • Hilman September 5, 2017 at 8:59 pm #

        Ah… I forgot about that book. Will take a look at it later. Thanks!

      • Sijin John February 2, 2018 at 5:32 am #

        Hi Adrian

        for me i don’t know why the comment bar is not showing up, so i decided to write in the reply section.
        ok for me i find there is an error after make -j4 step.every thing up till then worked fine,but don’t know what went wrong. I just followed your post, i am not familiar with terminal window commands .please help.
        thank you

      • Math due March 18, 2018 at 10:32 pm #

        Can i install OpenCV- 2.4.9 in Raspbian stratch…..?

        • Adrian Rosebrock March 19, 2018 at 5:04 pm #

          It’s a bit of a pain, to be honest. You’ll need to combine the steps from this tutorial along with original one on installing OpenCV 2.4 on Raspbian Wheezy. Be prepared to run into problems and do a bit of debugging.

    • Math due March 22, 2018 at 9:20 am #

      Thq…..very much Adrian . I successfully OpenCV-3.3.0 with Python-3.5 in pi 3.Only problem, it takes 6h duration . Anyway i install Opencv .

      • Adrian Rosebrock March 22, 2018 at 9:31 am #

        Congrats on getting OpenCV installed!

      • richard April 30, 2018 at 11:10 pm #

        manda como lo isiste amigo

    • Oshada August 30, 2018 at 3:09 am #

      Hi Admin,

      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

      error: (-215) scn == 3 || scn == 4 in function cvtColor

      this’s my error. can I know how to fix this error.
      thankz in advance

      • Adrian Rosebrock August 30, 2018 at 8:53 am #

        Double-check your path to the input image. It sounds like the path is incorrect and cv2.imread is returning None.

        • Oscar Mejia September 3, 2018 at 12:21 pm #

          I was the same issue, it was solved with the following command:

          sudo modprobe bcm2835-v4l2

          BTW: Thanks a lot Adrian, you’re a Rock!!!!

          • Adrian Rosebrock September 5, 2018 at 8:54 am #

            Thanks Oscar, and congrats on getting it working 🙂

  2. Jorge September 5, 2017 at 4:40 am #

    Great post as always.

    I wanted to share a neat little trick: You can actually enable SSH on the RPi just by placing an empty “ssh” file (case sensitive!) in the root of the sd card (once Raspbian is flashed).
    This makes running a 100% “headless” RPi possible: From there you can keep working with SSH or enable VNC and see the desktop from there.

    • Adrian Rosebrock September 5, 2017 at 9:11 am #

      Wow, that’s a neat trick! Thanks for sharing Jorge.

  3. Hidenori Kaga September 5, 2017 at 8:09 am #

    Your page has been explained in an easy-to-understand and polite manner, so it’s always helpful very much.
    I immediately installed the CV with reference to this page.
    However, Python IDLE in the program menu causes an error in importing. How can I use CV from IDLE?

    • Adrian Rosebrock September 5, 2017 at 9:10 am #

      Hi Hidenori — as far as I understand, the GUI version of Python IDLE does not support virtual environments, thus you cannot use it. I would suggest you use IDLE via the command line (so you can access the Python virtual environment) or use Jupyter Notebooks. I hope that helps!

      • raju March 24, 2018 at 7:56 am #

        how to install open cv 3 without installing virtual environments?

        • Adrian Rosebrock March 27, 2018 at 6:32 am #

          Don’t install virtualenv/virtualenvwrapper and don’t use the “mkvirtualenv” command to create a Python virtual environment. You’ll need “sudo” permission to install any pip-based packages.

          • Shyam Mohan May 16, 2018 at 1:03 pm #

            I already installed virtual environments. Now I’m not able to import cv2 in Idle. Any solution? I Don’t want to access Idle from command line.

          • Adrian Rosebrock May 17, 2018 at 6:47 am #

            Python IDLE does not respect Python virtual environments. You would need to the command line. Another approach would be to install Jupyter Notebooks which will give you an incredibly powerful IDLE-like environment.

  4. Steve Nicholson September 5, 2017 at 3:26 pm #

    If you aren’t going to run the tests, you can save a fair amount of compile time by not including them. To do that, add “-D BUILD_TESTS=OFF” and “-D BUILD_PERF_TESTS=OFF” to the CMake command line.

  5. Abkul Orto September 6, 2017 at 6:41 am #

    Great tutorial.

    All your ” Quickstart Bundle and Hardcopy Bundle book, Practical Python and OpenCV” I bought are a jewel i am happy to have invested in.

    in your blog on “Drowsiness detection with OpenCV of May 8, 2017 in dlib, Facial Landmarks, Tutorials” you suggested :

    “If you intend on using a Raspberry Pi for this, I would:

    1. Use Haar cascades rather than the HOG face detector. While Haar cascades are less accurate, they are also faster.
    2. Use skip frames and only detect faces in every N frames. This will also speedup the pipeline.”

    I have Pi 3, Kindly do a tutorial with an example to implement the above options.

    • Adrian Rosebrock September 7, 2017 at 7:05 am #

      Hi Abkul — thank you for picking up a copy of Practical Python and OpenCV, I appreciate your support! And yes, I will be covering an updated drowsiness detector for the Raspberry Pi in the future. I can’t say exactly when this will be as I’m very busy finishing up the new deep learning book, but it will happen before the end of the year.

  6. Islam September 6, 2017 at 10:52 am #

    Where new blog about pixel by pixel for loops

    • Adrian Rosebrock September 7, 2017 at 7:01 am #

      I already covered the blog post you are referring to here. I’ll also be doing an updated one on OpenMP in the future.

  7. Rick Free September 6, 2017 at 1:37 pm #

    Followed step-by-step and it worked like a charm. Compile took about 4 hours, as expected. Thanks!

    • Adrian Rosebrock September 7, 2017 at 6:59 am #

      Congrats on getting OpenCV installed, Rick! Nice job.

  8. Galang Hakim September 7, 2017 at 9:18 am #

    Hi andrian

    thanks for sharing
    I have success installed opencv to raspberry pi 3
    thanks to your guidance

    but I wonder
    for compiling OpenCV 3 for Python 2.7 and python3.5
    the libraries, numpy and site packages only for phyton 3

    I tried for 3x
    the result are still same
    any idea about that ?

    • Adrian Rosebrock September 11, 2017 at 9:32 am #

      You would need to create two separate Python virtual environments. One for Python 2.7 and one for Python 3. Form there you can run CMake + make from inside each virtual environment to build OpenCV.

  9. Charles Horan September 7, 2017 at 2:13 pm #

    Worked like a charm..mind you I installed opencv3.3 rather than 3.1 … still worked great 😀

    • Adrian Rosebrock September 11, 2017 at 9:30 am #

      Congrats on getting OpenCV installed, Charles! Nice job.

  10. Larry Pechacek September 8, 2017 at 8:26 am #

    Upon following this latest tutorial The compile did hang up around 91% using 4 cores on RPi2. Following the tutorial RPi2 on Jessie however compiled on Stretch using all 4 cores without issue.

    • Adrian Rosebrock September 11, 2017 at 9:25 am #

      Thank you for sharing your experience, Larry!

  11. Andrew September 10, 2017 at 5:41 pm #

    I am following this install on a Pi 2b. If I backup the sd card – will it work on a Pi 3 ?

  12. Stephane September 12, 2017 at 3:12 am #

    Hi Adrian,

    I installed OpenCv on my raspberry pi3 (2017-08-16-raspbian-stretch) by following step by step your latest tutorial.
    The compilation went well and the result is similar to yours.

    When I launch a simple program, see what it returns me:

    (cv) pi@raspberrypi:~ $ sudo modprobe bcm2835-v4l2
    (cv) pi@raspberrypi:~ $ python script.py
    Unable to init server: Could not connect: Connection refused

    (video test:1029): Gtk-WARNING **: cannot open display:
    (cv) pi@raspberrypi:~ $

    This program works fine on my computer with Linux Mint.
    Thanks for your reply and sorry for my approximate English.

    Bonjour de France 🙂


    • Adrian Rosebrock September 12, 2017 at 7:13 am #

      How are you accessing your Raspberry Pi? Over SSH? Enable X11 forwarding when you SSH into your Pi:

      $ ssh -X pi@your_ip_address

  13. Stephane September 12, 2017 at 2:20 pm #

    Via ssh well on 🙂

    Ok I’ll try the x11 server activation


  14. Chris September 14, 2017 at 6:24 am #

    The issue with the multi threaded build is the lack of size of the swap file. You need to increase it to something like 1GB for doing intensive builds.

  15. Michael September 14, 2017 at 4:23 pm #

    I tried serveral times and did follow your great tutorial in detail. Anyhow, I am not able to get the virtualenvwrapper working (and, due to this, I have issues later on).

    After the installation of virtualenv and virtualenvwrapper (both were successful, including the dependancies) and updating the ~/.profile file, I always get the error:

    pi@raspberrypi:~ $ source ~/.profile
    /usr/bin/python: No module named virtualenvwrapper
    virtualenvwrapper.sh: There was a problem running the initialization hooks.

    If Python could not import the module virtualenvwrapper.hook_loader,
    check that virtualenvwrapper has been installed for
    VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
    set properly.

    I did not find any clue to overcome this problem.

    Any help is appreciated.

    • Adrian Rosebrock September 18, 2017 at 2:17 pm #

      Which Python version were you trying to install OpenCV + Python for? Python 2.7? Or Python 3?

      • Gerard October 5, 2017 at 1:16 am #

        I’m trying to do 2.7 and get the same error

        • Adrian Rosebrock October 6, 2017 at 5:11 pm #

          I would suggest explicitly setting your Python version for virtualenvwrapper inside your .profile file, like this:

          • Math due March 21, 2018 at 10:25 am #

            Thanks …. It works.

          • Adrian Rosebrock March 22, 2018 at 9:58 am #

            Awesome, I’m glad to hear it 🙂

          • Manu March 21, 2018 at 2:51 pm #

            Thanks. I had the same error. This solved it.

          • Adrian Rosebrock March 21, 2018 at 3:45 pm #

            Awesome, I’m glad it worked for you, Manu! 🙂

        • prateek December 18, 2017 at 7:58 am #

          add below code in ~/.profile along with other part, it worked for me

          export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

          • Salam Alabassi April 3, 2018 at 4:51 am #

            thank you guys
            it is work when adding
            export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

    • Stephen Hayes September 20, 2017 at 1:50 pm #

      In setting up the ~/.profile, I had to add the line: “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3” before “source /usr/local/bin/virtualenvwrapper.sh”. Otherwise, I got the “No module named virtualenvwrapper.hook_loader” error.

      • Eyal October 18, 2017 at 2:21 am #

        I also had to add export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3” otherwise I“No module named virtualenvwrapper.hook_loader” error.

        But what is the implication for the next step: mkvirtualenv cv -p python2


        • Adrian Rosebrock October 19, 2017 at 4:55 pm #

          It should have have an impact.

    • Danny March 8, 2018 at 1:17 pm #

      I am using Python 3 and was able to fix the problem by running this command:

      sudo pip3 install virtualenv virtualenvwrapper

      using pip3 instead of pip to make the virtual env.

    • JUN WANG July 4, 2018 at 10:10 pm #

      Well, it is very strange for my case. I installed on python 2.7, was seeing”no module named virtualenvwrapper”.
      I added ”
      virtualenv and virtualenvwrapper

      export WORKON_HOME=$HOME/.virtualenvs

      export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2

      source /usr/local/bin/virtualenvwrapper.sh” Still didn’t fix the problem.

      Then I modified to “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3”, the error is magically gone.

  16. Tom September 17, 2017 at 6:06 am #

    Just a word of warning: Stretch takes more space than Jessie (~4GB vs ~3GB) so to prevent OpenCV from building on a 8GB card.

    • Adrian Rosebrock September 18, 2017 at 2:09 pm #

      Thanks for sharing, Tom!

    • Gustavo February 8, 2018 at 5:49 am #

      Did not work on the 8GB, not enoght space. Fail in make -j4 at 30%.
      Going for the no desktop version for more space

  17. Stephen Hayes September 18, 2017 at 4:16 pm #

    In setting up the ~/.profile, I had to add the line: “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3” before “source /usr/local/bin/virtualenvwrapper.sh”. Otherwise, I got the “No module named virtualenvwrapper.hook_loader” error.

  18. Stephen Hayes September 20, 2017 at 10:18 pm #

    Another note: It is possible to run the make with 4 cores. Stretch suffers a bit from software bloat, so the 2GB of memory isn’t sufficient to compile with 4 cores. Edit /etc/dphys-swapfile
    and change CONF_SWAPSIZE to 2048 and reboot. You can then do make -j4. you also need to do the same thing to install dlib as that will also hang under stretch when it runs out of memory.

    • Adrian Rosebrock September 21, 2017 at 7:15 am #

      Great point Stephen, thank you for sharing.

    • Philip February 11, 2018 at 9:51 pm #

      How exactly is this done? I can’t save any changes to the swapfile after I make them.

      • Alex April 25, 2018 at 1:18 pm #

        Use sudo when you open the file

        i.e. sudo vim /etc/dphys-swapfile

        substitute vim for your preferred editor (nano, etc)

        • tom May 29, 2018 at 9:55 am #

          premision denied.. (with nano)

          • Adrian Rosebrock May 31, 2018 at 5:19 am #

            Make sure you use “sudo” to give yourself the proper permissions to edit the file.

  19. toukir September 22, 2017 at 2:23 am #

    why can’t I import cv2 directly from python shell? but when I go to terminal and write work on and import cv2 then it works.
    But I like to import cv2 from the script. so what can I do for that? I am using python 2.7 and pi3.
    please help me.

    • Adrian Rosebrock September 22, 2017 at 8:54 am #

      To clarify, are you trying to execute a Python script from your terminal? If so you still need to use “workon” before executing the script (workon only needs to be executed once):

    • George October 1, 2017 at 1:05 pm #

      Same problem here, except i’m using Python 3.5.3. Did you find a solution ? Does anyone have a solution ?

  20. Syed Tauseef September 22, 2017 at 11:26 am #

    Hey Adrian Rosebrock ! Thanks for such a wonderful and simple tutorial, currently i am installing opencv (currently at installing numpy) i would like to know whether some of the open cv modules are dependent or matplotlib ? If so is it similar to installing like numpy ? with cv virtual environment “pip install matplotlib”

    • Adrian Rosebrock September 22, 2017 at 11:29 am #

      No, there are no OpenCV modules that are dependent on matplotlib. You can install it via:

      $ pip install matplotlib

      • Syed Tauseef September 23, 2017 at 5:02 am #

        Thanks ! I was successfully able to install open cv on my pi3 . At end you forgot to mention about deleting those zips (open cv and contrib ) file which might add few more mb of free space.

      • Syed Tauseef September 23, 2017 at 6:06 am #

        How do we access the environment variable in VNC or desktop terminal as” source ~/.profile “and” workon cv ” give invalid option . SSH i am able to get the cv environment.

        • Adrian Rosebrock September 23, 2017 at 9:59 am #


          Will work over SSH, VNC, and terminal on the desktop.

          I’m not sure what error message you are getting, but again, the above commands will work just fine on all setups (provided there is not a misconfiguration, of course).

          • Syed Tauseef September 23, 2017 at 10:55 am #

            Yes ! Now i am able run these line without any errors fron VNC ,dont kown what was the reason for such error . Anyways thanks !

      • Niyazi Toker September 26, 2017 at 5:03 am #

        It does not worked for me. I took Memory Error, although I have memory.
        I tried “pip –no-cache-dir install matplotlib” command. Although I took again another error, at the end of I have installed matplotlib.

        • Adrian Rosebrock September 26, 2017 at 8:06 am #

          Correct, using:

          $ pip install matplotlib --no-cache-dir

          Will help if you are running into a MemoryError.

  21. Himanshu Singh September 26, 2017 at 6:49 am #

    Hi Adrian,

    I have followed your tutorial to install opencv3 using python3 in raspberri Pi3 stretch.

    I ran following commands:
    source ~/.profile
    workon cv
    python file_name.py


    Traceback (most recent call last):
    File “face_detection.py”, line 2, in
    importError : No module named ‘picamera’

    I tried to install ‘picamera’ using following commands:
    sudo apt-get update
    sudo apt-get install python3-picamera


    python3-picamera is already installed the newest version (1.13)
    0 upgraded, 0 newly installed, 0 to remove and 20 not upgraded

    Again I tried to run:
    python file_name.py

    same importError :

    Traceback (most recent call last):
    File “face_detection.py”, line 2, in
    importError : No module named ‘picamera’

    I am not getting why this is happening. It’ll be very helpful if you can help me out of this.

    • Adrian Rosebrock September 26, 2017 at 8:06 am #

      You installed picamera via apt-get which will install it into your system install of Python, not the virtual environment. You should use:

      And you’ll be all set.

      • Himanshu Singh September 26, 2017 at 11:00 am #

        Thanks man! It’s working perfectly fine 🙂 🙂 _/\_

  22. James September 27, 2017 at 5:19 am #

    Really great instructions Adrian, thank you, saved me loads of time. However, i installed everything and found it used up 9.5GB of the sd card, so it might be worth mentioning that 16GB is recommend. I noticed on your setup it only took ~4.2GB. Is that after removing LibreOffice and Wolfram engine?

    • Adrian Rosebrock September 27, 2017 at 6:37 am #

      Correct, that is after removing LibreOffice and Wolfram Engine.

      • James September 27, 2017 at 2:28 pm #

        So worth mentioning if you use a 8GB sd card, you will need to remove LibreOffice and Wolfram Engine, otherwise when you try and build openCV you will most likely run out of space and get an error, then have to build it again, and this step can take hours so not something you want to happen.

  23. Bryan September 28, 2017 at 8:12 pm #

    when I try to download https://gethub.com/Itseez/opencv/archive/3.3.0.zip the connection just times out over and over

    • Adrian Rosebrock October 2, 2017 at 10:26 am #

      Hi Bryan — please double-check your internet connection and try again. There might have also been a problem with GitHub when you tried to download the code.

    • Dayle November 30, 2017 at 11:01 am #

      Hi Bryan,

      I’ve run in to that problem a couple of times using apt-get as well – bet you were running it in the afternoon. Try running it later at night or early in the morning. I suspect the servers get overloaded at peak times.


    • kamalov March 15, 2018 at 6:54 am #

      missing link

      you can try this https://github.com/Itseez/opencv/archive/3.3.0.zip

  24. arjun September 30, 2017 at 11:40 am #

    can openCV3.3 be installed in jessie

    • Adrian Rosebrock October 2, 2017 at 9:54 am #

      Yes, absolutely. I provide a number of OpenCV install tutorials for various Raspbian distributions here.

  25. Ace October 2, 2017 at 4:08 am #

    after expand the file system and reboot …
    i cant access to the raspberry pi through Remote Desktop.
    but able to use ssh access to the pi

    Anyone can help ?

    • Adrian Rosebrock October 2, 2017 at 9:25 am #

      Hmm, I don’t think this is an issue related to expanding the file system and rebooting. Can you ensure the remote desktop service is properly running?

  26. Banjo October 3, 2017 at 8:24 pm #

    Love the content, I wish I had snapped up the newest course at the early bird discount.

    If anyone waited 4 hours for compile, and without thinking pasted the “make clean” command. I feel your pain…from now on, read twice, paste once.

    Silver lining: I’ll never forget what make clean does.

    • Adrian Rosebrock October 4, 2017 at 12:35 pm #

      Ugh, I’m sorry to hear that Banjo. I’ll be posting an updated Raspberry Pi install tutorial this coming Monday, October 9th which will enable you to compile OpenCV on the Raspberry Pi in about 45 minutes.

  27. Sinan October 5, 2017 at 1:42 pm #

    Hi Adrian,
    When I reached the creation of virtual environment part, I accidently created another virtual part for Python 2.7 as well. But I want to work with Python 3.
    In Cmake output, both interpreter and numpy of Python 2.7 and Python 3 are located in the virtual environment cv.
    Will it cause any problem in the future?

    Thanks in advance

    • Adrian Rosebrock October 6, 2017 at 5:00 pm #

      Just to be safe I would suggest deleting both your Python virtual environment via the rmvirtualenv command and correctly creating your Python 3 one. From there, delete your “build” directory, re-create it, and re-run CMake.

  28. zet October 6, 2017 at 3:49 am #

    thank you

  29. Franco October 7, 2017 at 5:25 pm #

    Hello, thank you Adrian for this tutorial.
    I am having trouble at step 5:
    when I try the cmake -D……
    I get a bash: cmake: command not found.
    Any ideas why?

    • Adrian Rosebrock October 9, 2017 at 12:33 pm #

      Please make sure you “Step #2” where the “cmake” command is installed.

  30. Huy Nguyen October 7, 2017 at 11:26 pm #

    Hi Adrian,

    I followed the instructions and were able to install OpenCV 3.3.0 on my Raspberry Pi 3. Everything seemed to work pretty well until I ran into a Face Recognition Script that contained the following line:

    model = cv2.face.createEigenFaceRecognizer()

    The following Error Message was shown when I ran Python on it:

    “AttributeError: module ‘cv2’ has no attribute ‘face’ ”

    Everywhere I looked, the answer seemed to point to the “OpenCV’s extra modules” which I thought was already installed with opencv_contrib from the github.

    Any idea on how I can fix this?


    Huy –

    • Adrian Rosebrock October 9, 2017 at 12:32 pm #

      Indeed, it sounds like your install of OpenCV did not include the “opencv_contrib” modules. You will need to re-compile and re-install OpenCV.

  31. Leon October 8, 2017 at 12:26 am #

    the virtualenvironment is killing me.
    Unless I’m in it, I can’t see cv2 library.

    If I am in it, python can’t see picamera.array and other modules.

    This makes the home surveillance blog that was suggested to try opencv out impossible.

    time spent thus far: 10h compiling (even without make -j, it crashed at 83%, although power cycle -ie too hung to ssh into and stop cleanly) and 3h on this. Definitely not for the faint hearted!

    Is there an easy way to get needed modules into virtual environment? I have to abandon this soon – it is consuming too much time. A pity really, because I was hoping to bring it to my classroom.


    • Adrian Rosebrock October 9, 2017 at 12:31 pm #

      Hi Leon — that is the intended behavior of Python virtual environments. Python environments keep your system Python packages separate from your development ones. As far as your Pi crashing during the compile, take a look a this blog post which provides a solution. The gist is that you need to increase your swap space.

  32. Aaron October 8, 2017 at 2:12 pm #

    Will this tutorial work on a raspberry pi 2? Currently its os is raspian wheezy and from the offial website you can only download raspian stretch

    • Adrian Rosebrock October 9, 2017 at 12:21 pm #

      Hi Aaron — can you please clarify your comment? Are you running Raspbian Wheezy on your Pi and want to install OpenCV?

      • Aaron October 13, 2017 at 3:44 am #

        I have changed to Raspian Stretch but i have a Raspberry Pi 2 just wondering will this tutorial work for it as you are using a Raspberry Pi 3

        • Adrian Rosebrock October 13, 2017 at 8:35 am #

          I have not tested on the Raspberry Pi 2, but yes, this tutorial should work.

  33. Haseeb Ahmed October 10, 2017 at 6:36 am #

    Hi Adrian, i am using sift for features extratction. but it is showing that
    “module” has no attribute “xfeatures2d”. I have downloaded opencv_contrib and unzip it properly but still getting error when i run my code on raspberry pi 3.

    • Adrian Rosebrock October 13, 2017 at 9:10 am #

      Hi Haseeb — it sounds like your path to the opencv_contrib module during the CMake step was incorrect. Double-check the path, re-run CMake, and re-compile + re-install OpenCV.

  34. Bryan October 12, 2017 at 12:24 pm #

    Made it to the open CV compile
    stopped at 86%

    Spec ( Ras pi 0 wireless , stretch , python 2.7)

    should I attempt to change to python 3.0, reformat the sd card and start over, or attempt optimizing open CV and make -j4? useing your oct 9th article.


    • Adrian Rosebrock October 13, 2017 at 8:39 am #

      I would actually update your swap size, as I discuss in this post. From there, delete your “build” directory, re-create it, and re-compile.

      • Kaustubh Kadu September 2, 2018 at 12:40 am #

        sir i have done the solution you mention above that modify the the swap size , but still my installation get stuck at 98 % along with this whole RPI get hang and can’t do anything . So i have to forcefully switch it off directly. Please tell me the solution !!

        • Adrian Rosebrock September 5, 2018 at 9:06 am #

          Hey Kaustubh, if you are still having trouble compiling and installing OpenCV I would recommend taking a look at the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV. Inside I have included a pre-configured Raspbian .img file with OpenCV pre-installed. Check it out as it will solve your current issue installing OpenCV.

  35. bob October 13, 2017 at 5:03 am #

    is there a way you can you run the Idle shell for python in the virtual environment

    • Adrian Rosebrock October 13, 2017 at 8:33 am #

      No, IDLE does not respect Python virtual environments. Please use the command line. If you like IDLE, try using Jupyter Notebooks that do work with Python virtual environments.

  36. Raju October 13, 2017 at 7:40 am #


    I am not able to import PIL from virtualenv. Can you please suggest how to fix the issue?

    Thanks in advance

    • Adrian Rosebrock October 13, 2017 at 8:32 am #

      How did you install PIL? Did you install it into the Python virtual environment?

  37. Keith Glasnapp October 14, 2017 at 7:12 am #

    Why can’t your tutorial be run from a shell script? It would eliminate a lot of errors and retries?

    • Adrian Rosebrock October 14, 2017 at 10:30 am #

      It can be executed via a shell script in some situations; however, it’s also important to understand how the compile works, especially if you intend on optimizing your install. I also offer a pre-configured Raspbian .img file inside Practical Python and OpenCV.

  38. Terry October 14, 2017 at 11:28 am #

    Can this tutorial work on a 8GB card?

    • Adrian Rosebrock October 16, 2017 at 12:34 pm #

      If you purge Wolfram Alpha and LibreOffice you should be okay, but I would really suggest using a 16GB card.

  39. rich October 15, 2017 at 8:51 pm #

    Oh man. I found this link for stretch. It seems to work because everything went smoothly untill I try to compile opencv. At this step cd ~/opencv-3.3.0/ it tells me that the directory does’t exist.

    So I mkdir one then mkdir build, cd into build. when try to compile, it tells me it doesn’t contain CMakeLists.txt , where is this CMakeLists.txt ?

    • Adrian Rosebrock October 16, 2017 at 12:22 pm #

      Hi Rich — it’s hard to say what the exact issue is without seeing the directory structure of your project. Can you ensure that OpenCV was properly downloaded and unzipped in your home directory?

  40. Daniel October 16, 2017 at 2:13 am #

    I’m very new to this and I just followed your video and now I have it downloaded so thank you. However, I don’t know what to do know, like how to write and run scripts in this virtual environment because every time I type python into it, the shell pops up. Could someone tell me how to open blank scripts so I can start writing code. Thank you

    • Adrian Rosebrock October 16, 2017 at 12:20 pm #

      Hey Daniel — you would need to supply the path to the Python script you would like to execute:

      $ python your_script.py

      Open up the a file in your favorite plaintext editor, save it as a .py file, and insert your code. From there execute it via the command line.

  41. Marc October 16, 2017 at 10:54 am #

    Could add “nohup” to long-running commands? It’s not unusual to lose the ssh connection and it gets frustrating as the installation is pretty long already.

    • Adrian Rosebrock October 16, 2017 at 12:14 pm #

      You could absolutely use “nohop”; however, I prefer using “screen”.

  42. Scott October 17, 2017 at 12:24 pm #

    In step 5, after cmake completes I type ‘make’ but there are no Makefiles that it can run against.

    • Adrian Rosebrock October 19, 2017 at 5:02 pm #

      Please check your output of “CMake” as it likely returned an error (and thus no Makefiles were generated).

      • Tony February 19, 2018 at 11:47 am #

        I had gotten this error too, and the cmake log files told me that I was missing a header file. I went back through the instructions and found that I had missed installing the opencv_contrib package.

  43. olivia October 19, 2017 at 12:09 am #

    Hello adrian,
    thank you. this is the best tutorial i ever seen.
    but i got one problem
    when i’m run this script

    $ ls -l /usr/local/lib/python2.7/site-packages/

    i just get 300+ (3 digit) even though you get 1852

    and then i’m
    $ ls -l /usr/local/lib/python3.5/site-packages/
    i got 3500+ (4 digit) and you got 1852

    please help me adrian to fix this, thank you

    • Adrian Rosebrock October 19, 2017 at 12:20 pm #

      Hi Olivia — thanks for the comment. That number will depend upon the packages you have installed in your environment.

      • olivia October 29, 2017 at 9:07 am #

        i just follow your step adrian, why i get that value? is that ok for my next step when i want to using python?

        • Adrian Rosebrock October 30, 2017 at 2:58 pm #

          I think it is safe to carry on with the instructions. Don’t worry about these values.

          • olivia November 2, 2017 at 2:44 am #

            ok.. thank you adrian

  44. Akash October 19, 2017 at 3:44 am #

    Hello Adrian,
    I have installed the opencv inside the virtual environment ,
    but i’m unable to access outside (i.e)., Inside the IDLE.
    How to access it.

    • Adrian Rosebrock October 19, 2017 at 8:36 am #

      Hi Akash — thanks for the comment. Unfortunately, IDLE cannot access Python virtual environments. I would suggest using Jupyter Notebooks which do work with Python virtual environments.

  45. Cahit October 21, 2017 at 11:11 pm #

    Hello Adrian. I just wanted to say thanks. It took a long time but it was a problem-free installation. Nice guide.

    • Adrian Rosebrock October 22, 2017 at 8:24 am #

      Congrats on getting OpenCV installed, nice job!

  46. Enzo October 23, 2017 at 1:37 pm #

    Hi Adrian, great walkthrough, tutorial or wathever you want to call it, made it without any trouble in the times that are say… although this only work as long as someone work on the virtual environment, is there any way to make this work outside the virtual env. to use opencv on python directly??

    Thanks for everything mate, cheers.

    • Adrian Rosebrock October 23, 2017 at 5:15 pm #

      Hi Enzo — it’s a best practice to use Python virtual environments. Each Python virtual environment copies the binaries and libraries of your system Python but doesn’t keep any existing installed libraries. Therefore, you can use OpenCV + Python directly. If you do not want to use Python virtual environments you can either (1) follow the steps and ignore virtualen/virtualenvwrapper steps or (2) sym-link any packages into your system install of Python.

  47. mory October 23, 2017 at 4:55 pm #

    i do everything in this tutorial but in step #5 when compiling opencv it freeze up at 86%….
    i use $ make clean $ make
    but freeze up again at 86%
    .what can i do??? please help me….please help me…please (:

    • Adrian Rosebrock October 23, 2017 at 5:19 pm #

      It sounds like you might be running out of swap space. Please see this tutorial on how to increase your swap.

  48. mory October 23, 2017 at 5:00 pm #

    is there a respbian stretch image that opencv and python have been installed on it??? and i just write this on my sd card and use…. is this possible????

    • Adrian Rosebrock October 23, 2017 at 5:17 pm #

      Hi Mory — I would suggest you take a look at the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV. Both these bundles include a pre-configured Raspbian .img file with OpenCV pre-installed.

      • Mory October 24, 2017 at 2:04 am #

        thank you very much Adrian..

  49. Devaraju October 24, 2017 at 1:48 am #

    I’m getting following error after CMake it says “Configuration incomplete, errors occurred!”

    • Adrian Rosebrock October 24, 2017 at 7:12 am #

      Please check the output of CMake — it will report what the specific error is and why the command failed.

      • Alex July 2, 2018 at 7:47 am #

        How i can do this ? I got same erro

        • Adrian Rosebrock July 3, 2018 at 7:29 am #

          As I mentioned in my previous reply: scroll through the output of “cmake” in your terminal.

  50. Shane October 25, 2017 at 5:37 pm #

    Great tutorial… Is there a way to install SimpleCV on top of this? I’ve tried, but keep getting memory errors. Thanks Adrian..

  51. GIBIN October 27, 2017 at 9:01 am #

    I had completed installing the opencv to 100%. But when I check it using the code below

    source ~/.profile
    $ workon cv
    $ python
    >>> import cv2
    it is showing errors that the file is not created or something like that

    • Adrian Rosebrock October 27, 2017 at 11:50 am #

      Make sure you’ve properly linked cv2.so to your virtual environment. See Step #6.

  52. Eyal October 27, 2017 at 11:26 pm #

    Hello Adrian,
    do you have a similar guide to install scikit-learn on a pi as well?

    • Adrian Rosebrock October 30, 2017 at 2:17 pm #

      The easiest way is via PIP in your virtual environment: pip install -U scikit-learn

  53. Suzanna October 28, 2017 at 12:23 pm #

    Hi Adrian,
    I have tried to follow these steps multiple times now, but at step I keep getting a wrong output from the cmake. The library, numpy and packages paths are missing. Do you know what I could be doing wrong?
    Kind regards,

    • Adrian Rosebrock October 30, 2017 at 2:41 pm #

      Hi Suzannna — perhaps you aren’t inside your virtual environment when you issued the CMake command.

  54. Don October 28, 2017 at 8:25 pm #

    Adrian, I followed, I believe all the instructions up to the actual compile where the last thing is type in ‘make’ and it should start compiling. What I get is an error message >> make: *** No targets specified and no makefile found. stop.
    What did I miss doing?

    • Adrian Rosebrock October 31, 2017 at 8:02 am #

      Hey Don — try double-checking your output of CMake. It sounds like the “cmake” command exited with an error. Check the terminal output and you should be able to spot what threw the error.

      • Hrishi January 17, 2018 at 2:07 am #

        Adrian, I got the same error for make -j4…. i also checked cmake command execution and it returned a few errors….what should i do next??

        • Adrian Rosebrock January 17, 2018 at 10:11 am #

          Be sure to check your output of the “cmake” command. CMake will report an error and what caused the error.

  55. Raghuram October 29, 2017 at 2:43 pm #

    when i enter make this comes up:
    (cv) pi@raspberrypi:~/opencv-3.3.0/build $ make
    make: *** No targets specified and no makefile found. Stop.

    • Adrian Rosebrock October 30, 2017 at 2:46 pm #

      Hi Raghuram, did you first use CMake which will generate the Makefile?

      • Raghuram October 31, 2017 at 1:56 pm #

        I started the process again and now it shows an error at 86% ,that says boostdec_bgm.i missing.
        thanks for the reply please help me ou

        • Raghuram November 1, 2017 at 11:56 pm #

          I was able to install it but could and import cv2 worked on terminal , but it could not work in python please help!!

          • Adrian Rosebrock November 2, 2017 at 2:16 pm #

            Hey Raghuram — I’m not sure what you mean by you could import the cv2 library in your terminal but not in Python. Could you please elaborate? Are you trying to use Python IDLE? Keep in mind that Python IDLE does not respect Python virtual environments.

      • Maanasa November 5, 2019 at 7:17 am #

        Hi Adrian, how to generate Makefile using CMake file? Please explain

  56. olivia November 1, 2017 at 6:18 am #

    Adrian, i wanna restart my raspberry from zero. i have install following your step and now i want deleting all and start from zero.. how to do that?

    • Adrian Rosebrock November 2, 2017 at 2:30 pm #

      If you want to start completely from scratch I would recommend re-flashing Raspbian onto your SD card. If you want to restart your OpenCV compile just delete your “build” directory and recreate it.

  57. santiago g November 1, 2017 at 4:58 pm #

    help!. in the step of executing cmake I have a problem only appear the directories for python 3 and not for python 2.7 I tried many times updating my profile and even repeat from the beginning the procedure and do not appear please could help me

    • Adrian Rosebrock November 2, 2017 at 2:19 pm #

      You can only compile OpenCV bindings for ONE Python version at a time. If you have the correct directories for Python 3 then you can proceed with the compile to obtain your Python 3 + OpenCV bindings.

  58. Raghuram November 1, 2017 at 10:58 pm #

    ln: failed to create symbolic link what do i do for this kind of error

  59. Ivan Chuang November 2, 2017 at 12:41 pm #

    Dear Adrian,
    In step #5 , I am sure I am in the cv virtual env. — “(cv) pi@raspberrypi:~/opencv-3.3.1/build $ “.

    But when I type “cmake -D CMAKE_BUILD_TYPE=RELEASE \…..”. I got a error message –“CMake Error: The source directory “/home/pi/opencv-3.3.1/build/CMAKE_BUILD_TYPE=RELEASE” does not exist.”.

    Yours faithfully
    Ivan Chuang

    • Ivan Chuang November 2, 2017 at 1:22 pm #

      HI Adrian,
      I got the answer, just like this “cmake -DCMAKE_BUILD_TYPE=RELEASE \”.

      But I have another question. When I want to make sure my Python3 section, I don’t know I need to check which file.

      Yours faithfully
      Ivan Chuang

      • Adrian Rosebrock November 2, 2017 at 2:06 pm #

        Congrats on resolving the CMake issue. As for checking your “Python 3” section just scroll up in your terminal and examine the output of the “cmake” command.

  60. CESAR November 2, 2017 at 5:29 pm #

    There is no sudo make install
    I get it, “not exitis file install”

    HELP !!!!

    • Adrian Rosebrock November 6, 2017 at 10:53 am #

      I would suggest checking the output of CMake. It sounds like the “cmake” command exited with an error and did not create the Makefile.

  61. Shiv Trivedi November 5, 2017 at 4:11 pm #

    Hi Adrian,
    I’ve been following a lot of your blog posts and tutorials, and I find them amazing! Thank you so much for this amazing and super easy to follow guide to setting up OpenCV 3 on the new Raspbian Stretch!

    Big Fan,

    • Adrian Rosebrock November 6, 2017 at 10:34 am #

      Thank you for the comment, Shiv! And congrats on getting OpenCV installed on your Raspberry Pi!

  62. jad November 5, 2017 at 4:23 pm #

    that was really helpful .
    thank you very much for your help .
    i did the make command when i was in the env .
    and it worked just fine and finished successfully
    when i did sudo make install
    it is taking me the same time that “make” did. (that sentence makes no sense 😛 )
    is it normal ?

    • Adrian Rosebrock November 6, 2017 at 10:34 am #

      Hi Jad — as long as the command executes without error you should be okay.

  63. Colin November 6, 2017 at 4:08 pm #

    Hi Adrian
    I have compiled OpenCV3 on a fresh version of Rasbian Stretch following your recipe, (although I did not use a virtual environment).
    I am using a Python script that was developed in Jessie and OpenCV 2.4.9 and have updated the syntax where necessary. Everything seems to be successful apart from one thing, cv2.resize() and cv2.resizeWindow()are not working. They do not throw an error but have no effect on the size of the image or the window (I am using cv2.WINDOW_NORMAL). Is there anything obvious that I may have missed?

    • Adrian Rosebrock November 6, 2017 at 4:26 pm #

      I’m not sure about cv2.resizeWindow as I’ve never used that function before but to check cv2.resize print the resulting .shape to your terminal. If that’s not your expected dimensions then there is likely a logic error somewhere else in the code.

  64. Ronald November 7, 2017 at 11:13 pm #

    Hello Adrian

    I enjoyed this tutorial. The most advanced project I’ve ever attempted but its not complete. I reached the total “1852”

    I got to step 7 and entered “workon cv” and got command not found

    To my horror I suspect I forgot, yes the sym-link step. Can’t get in the cv environment.

    Help!! Thanks


    • Adrian Rosebrock November 9, 2017 at 6:45 am #

      Hey Ronald, I would suggest going back to Step 4 and ensure you have updated your .profile file. I would also suggest ensuring virtualenv/virtualenvwrapper have been properly installed.

  65. Jonathan Thompson November 8, 2017 at 1:02 pm #

    Hello Adrian I’m also having an issue with the “make” command,

    I’ve followed the previous step with the “cmake” command successful and when i “ls” in the “build” directory the make files are there but when i use the “make” command it says:

    make: *** No targets specified and no makefile found. Stop.

    Its defiantly in the (cv) environment also.

    any suggestions would be much appreciated,

    Kind regards,


    • Adrian Rosebrock November 9, 2017 at 6:21 am #

      Please see my reply to “Don” and “Raghuram”.

      • dorontal December 12, 2017 at 4:42 pm #

        I saw your reply to “Don” and “Raghuram” and still don’t know what to do and I am wondering whether you have any input that could help, please?

        I followed your instructions to the tee – brand new board, brand new stretch OS flashed.

        As you suspected, CMake indeed spits out errors. What I’m curious about is why have those errors shown up for a few of us on here, but not for some others?

        ./opencv-3.3.0/build/CMakeFiles/CMakeError.log is 343 lines long —

        i.e. CMake reports LOTS of errors under this recipe – I followed it perfectly.

        There are 4 files on which CMake failed, as shown by this post cmake step command:

        (cv) pi@pishow:~/workspace/opencv-3.3.0/build $ grep -A1 failed CMakeFiles/CMakeError.log|grep ‘source\ file’| sort -u
        source file: ‘/home/pi/workspace/opencv-3.3.0/build/CMakeFiles/CMakeTmp/src.c’
        source file: ‘/home/pi/workspace/opencv-3.3.0/build/CMakeFiles/CMakeTmp/src.cxx’
        source file: ‘/home/pi/workspace/opencv-3.3.0/cmake/checks/cpu_fp16.cpp’
        source file: ‘/home/pi/workspace/opencv-3.3.0/cmake/checks/cpu_neon.cpp’

        • dorontal December 12, 2017 at 5:16 pm #

          Woops. I know what I did wrong and why CMake failed. It was MY mistake. The recipe works perfectly. The mistake was to cut and paste the CMake command as is. But I had downloaded the original packages and had unzipped them at a location OTHER than the user’s home directory, so the paths in the CMake command had to be changed. This solved the issues and a Makefile got created. Sorry about the false alarm and I hope this helps others.

          • Adrian Rosebrock December 15, 2017 at 8:38 am #

            Congrats on resolving the issue!

    • pragati August 14, 2018 at 5:38 am #

      i am facing same problem how you solved it please tell me it will be great help

  66. Jehl November 12, 2017 at 4:47 am #

    On my Pi no problem to compile with make -j4. But, I have added blas and lapack with :
    sudo apt-get install libblas-dev liblapack-dev

  67. Julien November 13, 2017 at 3:25 am #

    Please you used the full or lite version for Raspbian ?

    • Adrian Rosebrock November 13, 2017 at 1:57 pm #

      I used the full version of Raspbian but you can use the lite version as well.

  68. Sarthak November 13, 2017 at 12:29 pm #

    Hello Adrian I’m also having an issue with the “make” command,
    the compilation stops at 30% i even tried to increase the swap size to 1024
    and compiling using make -j4 but still it does not compile more than 30%

    • Adrian Rosebrock November 13, 2017 at 1:53 pm #

      Hi Sarthak — try using only a single core via make. The compile will take longer but provided you increased your swap it should work.

      • Sarthak November 14, 2017 at 1:08 am #

        I did the make but did not increase the swap size just used make not make -j4 but the complilation stopped at 89%
        Will increase the swap size help in this?

        • Adrian Rosebrock November 15, 2017 at 1:06 pm #

          Yes. Increasing the swap size should resolve this issue completely.

  69. Sakshi Patel November 19, 2017 at 9:12 am #

    I have installed opencv 3 as explained above and followed all the steps for python 3
    When i am running a program of face recognition data set in python IDE 3 it is giving error in the line import cv2
    Importerror: No module named “cv2”
    Plz help me to solve this

    • Adrian Rosebrock November 20, 2017 at 3:59 pm #

      Please see my reply to “Hidenori Kaga”, “bob”, “Akash”, and others. I have addressed this question multiple times.

  70. 冰河 November 20, 2017 at 1:38 am #

    Hello Adrian.
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install libopencv-dev
    sudo apt-get install python-opencv
    Is this ok? install opencv

    • Adrian Rosebrock November 20, 2017 at 3:52 pm #

      I DO NOT recommend this method. The apt-get package definitions are often out of date and you won’t have the additional extra modules with OpenCV. I highly encourage you to compile OpenCV from source, using apt-get is not recommended.

  71. Cirkp November 20, 2017 at 6:34 pm #

    Hello Adrian,

    I followed this tutorial and everything went well to step 5.
    After the command cmake I get an instant error: “segmention fault” and i can’t go on.

    Could you help me?

    • Adrian Rosebrock November 21, 2017 at 1:26 pm #

      The “cmake” command caused a segmentation fault? That’s not good. It sounds like something might be wrong with your Raspberry Pi/Raspbian. Could you re-install Raspbian then try again? Unfortunately I’m not sure what the exact problem is here.

      • Nisal August 1, 2019 at 10:18 am #

        I got the same error and was pulling my hair out for days. The fix is easy. you just have to update the firmware of your pi. right after ” $ sudo apt-get upgrade”, give “$ sudo rpi-update”. This worked for me.

        • Adrian Rosebrock August 7, 2019 at 12:39 pm #

          Thanks for sharing, Nisal!

  72. Ali Raza November 22, 2017 at 10:41 am #

    Thank you so much sir you are such a great teacher for me. i going to order your book as soon as possible. thank you again God bless you.

    • Adrian Rosebrock November 25, 2017 at 12:46 pm #

      Thank you Ali, I really appreciate the comment 🙂

  73. Walter B Kulecz November 23, 2017 at 10:31 am #

    I thing the virtual environment step is fragile.

    Initially I created the cv env with python3 as shown, then I created another cvpy2.7 and used workon cv to switch back and then finished the instructions but the build failed. I then tried workon cvpy2.7 and got the failure message I posted above.

    Before calling it a night I deleted both my virtual environments using rmvirtualenv and did rm -rf of my build directory. I then recreated the python2 virtual environment and finished the steps, but I did the make without the -j4 and went to bed.

    Complie appears to have completed OK, doing the make install and ldconfig steps now,

    So you can disregard my comment from last night, but perhaps you could explain the steps to make a second virtual environment using python3. Or is it not possible to have both on the same system?

    • Adrian Rosebrock November 25, 2017 at 12:30 pm #

      You can create as many Python virtual environments as you want on a single machine. That is why we use Python virtual environments.


      You DO need to re-compile OpenCV for each Python version you want to use it for. You cannot use the same OpenCV bindings for multiple Python versions. I personally like to create a build directory for each unique Python + OpenCV version and then sym-link the resulting .so file into my respective Python virtual environment.

  74. Ravi November 23, 2017 at 12:49 pm #

    While using the cmake command , I get the following results:

    interpreter: /home/pi/.virtualenvs/cv/bin/python2.7(2.7.13)

    and that’s it.

    I don’t get the rest …the libraries,the numpy…..
    how to solve this?

    • Adrian Rosebrock November 25, 2017 at 12:28 pm #

      Hi Ravi — thanks for the comment. It sounds like you are indeed in the “cv” environment based on your “interpreter” output. Can you run “pip freeze” to ensure that NumPy is also installed?

  75. Eka November 24, 2017 at 3:48 am #

    While running the cmake step, it failed again and again and didn’t make the makefile until I added swap space. This was on a freshly installed stretch distribution running on a Pi Zero W. I’m now waiting for the compile to finish. Should finish sometime over night. It’s only 20% done after 4 hours.

    As for wearing out the flash card, you can try running fstrim on the card every week to make sure the maximum amount of blocks are available for wear leveling.

    • Chakradhar Reddy Veeramreddy November 27, 2017 at 2:07 am #

      Did you finally get it to work? Is this process mentioned above completely right for Pi Zero also?

  76. Walter B Kulecz November 24, 2017 at 6:40 pm #

    I have virtual environments running on my Pi3 for both python2.7 and python3.

    I think my problem was in step 4, where you forgot to mention that for a python3 virtual environment numpy needs to be installed with: pip3 install numpy instead of the pip install numpy listed. I failed to catch it.

    Perhaps some guidance on choosing python3 vs. python2.7?

    My take is python3 is the future and unless a module you need is not available for it, python3 is what should be used. Your samples seem to be written to be compatible with both.

    I’ve successfully run the deep-learning-opencv example code in both my python2.7 and python3 virtual envirmonments.

    Thanks for this great tutorial.

    One other tidbit, current Raspbian throws an ugly gtk warning after each run, it can be stopped by adding: export NO_AT_BRIDGE=1 to .profile

    This is my first experience with virtual environments, I’m not sold on their utility, but time will tell.

    • Adrian Rosebrock November 25, 2017 at 12:17 pm #

      Hi Walter — my opinion is similar to yours. If you need legacy support and are perhaps running OpenCV 2.4 as well, go with Python 2.7. If you are developing a new project and are concerned with the future, absolutely use Python 3.

      Also, thank you for the tip on the GTK warning message, that’s a great one!

  77. Habib Ali November 30, 2017 at 4:38 pm #

    Hello Adrian,

    im really thankful for this tutorial, i got a problem i will appreciate it if you could help me,
    after the workon and python commands, import cv2 couldnt work and it says “Traceback (most recent call last): File “”, line 1, in importError : no module named ‘cv2’

    • Adrian Rosebrock December 2, 2017 at 7:34 am #

      Hi Habib — there are a number of reasons why the “cv2” import would fail. Without physical access to your machine it’s impossible to diagnose just from the import error. I have compiled the most common reasons why the import would fail in the “Troubleshooting and FAQ” section. Please take a look at it.

  78. Dayle Hogg November 30, 2017 at 5:27 pm #


    Can’t say thank you enough. I completely forgot you put this together and tried to take your previous building OpenCV on Jessie instructions and update it for Stretch. It kept crashing 90% of the way through make leaving the cpu cranking full out doing nothing with a frozen screen. After a number of attempts at fixing it I stumbled upon the one source I should have started with.

    You suggestion to expand the swap memory was spot on. Works perfectly!

    • Adrian Rosebrock December 2, 2017 at 7:33 am #

      Awesome, congrats on getting OpenCV installed Dayle 🙂

  79. Suns December 1, 2017 at 12:09 am #

    Hi – I’m doing a fresh install, and when setting up the build with CMake, Python 2 output looks like this:
    — numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.12.1)

    Will this create an issue? Am new to this, so not sure what to do, or if it matters.

    • Adrian Rosebrock December 2, 2017 at 7:30 am #

      Hi Suns — it sounds like you are not in the “cv” Python virtual environment as this blog post suggests. Make sure you run “workon cv” before you actually run CMake. I would also suggest deleting your “build” directory and restarting the compile.

  80. Subrahmanya K N December 1, 2017 at 5:33 am #

    I religiously followed all the steps until Step#5. However, I’m experiencing a fatal error while compiling opencv (using- make -j4). It says out of memory space..

    I’m using 8GB SD card and installed Raspbian Stretch (Desktop version) using NOOBS. And I’ve deleted all the unnecessary software (SonicPi, Wolfram-engine, libreoffice etc.). Before executing make -j4, I had 1.7GB of free space.

    I’ve tried to install both opencv3.3.0 and opencv3.0.0..

    Please help me!!!

    • Adrian Rosebrock December 2, 2017 at 7:25 am #

      Hi Subrahmanya — it sounds like you may need to upgrade to a 16GB card to resolve this issue or continue to purge packages from your Pi that you do not need.

      • SUBRAHMANYA K N December 6, 2017 at 4:32 am #

        Thank you Adrian for your timely reply.. I upgraded to 16GB and it worked like a charm!

        However, when I try to run my script using Python 3 (IDLE), it returns be traceback error for import cv2. I executed my script in the terminal after ¨source ~/.profile¨ and ¨workon¨, it executed.

        Do you have any solution for that? I tried to include above two lines in my script before import cv2, but no help.

        All I need is to run my script using python shell using import cv2!

        Please help!

        • Adrian Rosebrock December 8, 2017 at 5:12 pm #

          IDLE does not respect Python virtual environments. You would need to use either the command line, Python scripts (from within the virtual environment), or Jupyter Notebooks.

  81. Jack Wilkinson December 2, 2017 at 3:09 pm #

    Hi Adrian,

    Pretty sure I’ve installed openCV successfully as it imports and tells me its version. However I’m getting a strange error:

    ** (Displayed Image:1734): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files

    It seems to be when I do cv2.imshow(“Displayed Image”,img)

    Could you shed some light on how I can resolve this?



    • Adrian Rosebrock December 5, 2017 at 7:52 am #

      Hey Jack — take a look at the comment from “Walter B Kulecz”. Walter discusses how to remove the warning (it’s due to GTK). Again, it’s a warning not an error can be safely ignored.

  82. kumar December 3, 2017 at 10:55 am #

    i successfully installed opencv 3 in python 2.7
    but while i’m working on face detection project it is showing

    can any one help…

    • Adrian Rosebrock December 5, 2017 at 7:43 am #

      Hi Kumar — there are a number of reasons why you may not be able to import the “cv2” library. I have compiled the most common reasons in the “Troubleshooting and FAQ” section of this post. Please take a look and use them to diagnose the issue. If you are still having trouble with the install you can reply back but without knowing which methods in the troubleshooting guide you tried it’s hard to provide any suggestions.

  83. Shreyas December 3, 2017 at 12:25 pm #

    Thanks for your step by step installation instructions
    You can skip 4 hours of compilation by using UBUNTU-mate ,if your only purpose is to use
    opencv you can just run:
    sudo apt-get install python-opencv on UBUNTU-mate

    • Adrian Rosebrock December 5, 2017 at 7:41 am #

      I do not suggest doing this. Installing OpenCV via apt-get will install an old version of OpenCV rather than the latest release. You’ll also be missing out on the contrib module along with a bunch of optimizations.

  84. Matt December 4, 2017 at 4:30 pm #

    Hi Adrian,
    I’m following your tutorial but at step 5 I hit my personal nightmare.
    I get a CMake error “the source directory “/home/pi” does not appear to contain CMakeLists.txt

    and then I’m lost.

    I folloed the video again to see if I did something wrong, but same result…

    does anyone have a hint for me?

    any help would be appreciated.

    • Adrian Rosebrock December 5, 2017 at 7:30 am #

      Hey Matt, I think I think you may have copied and pasted the “cmake” command incorrectly. Make sure you use the “<=>” button at the top of the code block to expand the entire code block and grab the command. It seems like there might be a space after “/home/pi” in your command.

  85. Jack December 7, 2017 at 3:43 am #

    Thanks, I successfully installed the opencv for python 3 in my pi2 by your steps, I can access it from my terminal but i can’t access it with my python 3 idle. its show the following error.
    Traceback (most recent call last):
    File “”, line 1, in import cv2
    ImportError: No module named ‘cv2’
    Why is it?
    please help me..

    • Adrian Rosebrock December 8, 2017 at 4:53 pm #

      Hi Jack — IDLE does not respect Python virtual environments and thus you cannot use IDLE with Python virtual environments. I would suggest using either the command line, a Python script, or Jupyter Notebooks.

      • Jack December 14, 2017 at 2:35 am #

        thanks, now i got the following error while i run imshow

        OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp, line 605
        Traceback (most recent call last):

        but i installed libgtk2.0-dev and pkg-config then why is it coming?

        • Adrian Rosebrock December 15, 2017 at 8:27 am #

          It sounds like you have have installed GTK after running CMake. Try deleting your “build” directory, re-creating it, and re-running CMake + make. You should also verify that GTK has been successfully installed.

  86. Jason December 7, 2017 at 9:11 am #

    Hi Adrian,

    Thanks for the excellent blog.
    After successfully installed opencv, I tried to get frame with webcam (Logitech 310), it throw error like “Corrupt JPEG data: 2 extraneous bytes before marker 0xd1 Corrupt JPEG data: 1 extraneous bytes before marker 0xd6”.
    I have tried to build opencv again with WITH_JPEG=OFF, it does remove the error but I am not able to write an image to disk anymore.
    Do you know how to solve this problem?
    Thank you.

    • Adrian Rosebrock December 8, 2017 at 4:47 pm #

      Hey Jason, unfortunately I have not ran into this error before so I’m not sure what the exact problem or solution is. Sorry I couldn’t be of more help!

      • Jason December 8, 2017 at 9:40 pm #

        Thanks Adrian. The error disappear when I use raspberry pi camera camera module

        • Adrian Rosebrock December 9, 2017 at 7:26 am #

          No worries, it sounds like you were trying to use the cv2.VideoCapture function to access the Raspberry Pi camera module (you can’t do that without installing more drivers). I put together a class to help switch between USB cameras and the Raspberry Pi camera module — you can find it here.

  87. Ludovic December 7, 2017 at 9:57 am #

    Thanks Adrian for another clear and detailed guide! I’ve successfully used your guides for several opencv installations; I’ve now one interrogation. I have compiled Opencv in its “cv” virtual environment, but I now also want opencv in another separate virtual environment, with a slightly different python 3 version (3.4 for the new virtualenv vs 3.6 for “cv”). Do I need to recompile opencv in the new virtual environment? If not how should I proceed (maybe simply simlimk cv2.so into the appropriate ~/.virtualenvs/ sub directory?)?

    • Adrian Rosebrock December 8, 2017 at 4:46 pm #

      Please see my reply to “Geoff Riley” as I discus show to do this.

  88. Geoff Riley December 7, 2017 at 10:49 am #

    Hi Adrian,

    It appears that the current make process builds and installs for both versions of python at the same time, so if you attempt to compile once for python2.7 and then for python3.5 the latter install overwrites the previous sandboxed version.

    I’m sure there must be a way around this, but the brief glance at the makefile didn’t reveal an obvious client.

    Kind regards,

    • Adrian Rosebrock December 8, 2017 at 4:46 pm #

      Hey Geoff — the current process should only compile one Python version at a time. Run sudo make install once. Delete your build directory. Re-create it. Run cmake and make again. Copy the resulting cv2.so file to your site-packages directory of your Python install.

  89. Youssra December 9, 2017 at 4:09 pm #

    Hello Adrian,

    I’m new to using OpenCV and I’m planning to use it for face detection on a raspberry pi. I’ve followed your tutorial exactly, but I can’t run the command: “make -j4″. This is the error that I get:

    ” $ $ make -j4
    make: *** No targets specified and no makefile found. Stop. ”

    I don’t know what I have to do now. And I did increase the swap size to 1024. I’m hoping to hear from you soon.

    Kind regards,

    • Adrian Rosebrock December 12, 2017 at 9:17 am #

      Please see my reply to “Don”, “Raghuram”, and “Jonathan”.

  90. Xare December 13, 2017 at 4:03 am #

    Hi all,

    how can i add unofficial python modules/libraries (picar) to the virtual cv environment (cv)?


    • Adrian Rosebrock December 15, 2017 at 8:36 am #

      All you have to do is use the “workon” command and “pip”:

      And this will install the library into the virtual environment.

      You could also use setup.py as well. Assuming you have already used the workon command:

      $ cd your_library
      $ python setup.py --install

      If you’re new to Python virtual environments I would suggest taking a look at this excellent guide.

  91. JaneLI December 15, 2017 at 2:18 am #

    Hi, thanks for sharing. I follow the steps and after successfully install the virtualenv and virtualenvwrapper, and update the ~/.profile file, I got the error : bash: source/usr/local/bin/virtualenvwrapper.sh: No such file or directory.
    I have no idea about this error…

    • Adrian Rosebrock December 15, 2017 at 8:19 am #

      It sounds like virtualenv/virtualenvwrapper were not properly installed. Try running pip freeze and ensuring both are installed.

  92. GregW December 16, 2017 at 1:58 pm #

    Will this work with the Stretch OS when downloaded with NOOBS?

    When i went to resize my partition it says it was not possible, that i was running noobs and it was most likely already done.

    • Adrian Rosebrock December 19, 2017 at 4:31 pm #

      If your partition is already expanded you can certainly use this method to install OpenCV on Raspbian Stretch.

  93. Alberto December 16, 2017 at 7:35 pm #

    Hey Adrian!
    First of all thank you for this guide. I was following your guide and made it all the way to step 6. At step 6 where you say to verify that OpenCV + Python bindings are installed by using ‘ls’ command. I get an error saying “No such file or directory”. Does this mean that it installed with an error so I have to redo the “make -j4” command? How can I check what pythons are installed and if I have too many what should I?

    • Adrian Rosebrock December 19, 2017 at 4:30 pm #

      Hey Alberto — did both “make” and “cmake” execute correctly?

      • Alberto December 21, 2017 at 2:38 am #

        Thanx Bro I just refollowed your guide and good to go. I realized where I made the mistake

        • Fan April 16, 2018 at 10:48 pm #

          same error for me. how did u fix it?

  94. Muhammad Hashir Waqar December 19, 2017 at 12:31 pm #

    Hi adrian! i m too much furstrated because when i start building opencv3.3 on raspabian stretch it gives the error stack smashing detected and dont even start building i have reinstalled the whole os and again tried but the output is same .. kindly reply me because i am doing my final year project and time is limited.. thanks in advance..

    • Adrian Rosebrock December 19, 2017 at 4:11 pm #

      Hey Muhammad, I’m sorry to hear about the issues installing OpenCV. However, without knowing what the exact error message is I can’t point you in the right direction.

  95. Bill Edds December 20, 2017 at 10:20 am #

    Hi Adrian, if I install OpenCV in a virtual environment per the instructions, can I use OpenCV outside of the virtual environment? Thanks in advance!

    • Adrian Rosebrock December 22, 2017 at 8:22 am #

      This would not work. What is your reason for needing OpenCV outside of a virtual environment? There are a few reasons, so I’m interested to hear yours.

  96. Pal S December 21, 2017 at 4:55 am #


    I was trying to follow the guide with slight differences:
    the environment I’m deploying to will not be used for development, so I felt no need of a virtualenv. Also the links to the project zips are not working anymore, I found /archives/master to get the latest version from the opencv and opencv-contrib repos.

    Now the problem is the following:
    I used wget -O opencv.zip https://github.com/Itseez/opencv/archive/master.zip then I unzip opencv-master.zip -d ~/repos/ which will unpack the zip into ~/repos/opencv-master/. I do the same with opencv_contrib, which will go into ~/repos/opencv_contrib-master.

    The cmake command I use:
    Note: I tried to play around with the switches, turning then on and off, the results were the same.

    and the error I get is:
    CMake Error at cmake/OpenCVModule.cmake:300 (message):
    No extra modules found in folder: /home/pi/opencv_contrib-master/modules

    Please provide path to 'opencv_contrib/modules' folder.

    I checked ~/repos/opencv_contrib-master/modules/ and there are a bunch of modules defined there, I’m not missing them.
    The cmake error log keeps crying about Regex: 'command line option .* is valid for .* but not for C\+\+' so it’s really confusing me what am I missing.
    Any hint would help 🙂


    • Pal S December 21, 2017 at 5:01 am #

      as soon as I submitted this comment the site loaded it in a way so the cmake command was in the middle of the screen so I noticed I specified -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-master/modules instead of `-D OPENCV_EXTRA_MODULES_PATH=~/repos/opencv_contrib-master/modules.

      It indeed fixed the problem, sorry for taking your time for moderating! :DD


      • Adrian Rosebrock December 22, 2017 at 6:52 am #

        Congrats on resolving the issue Pali, nice job!

  97. GIBIN VARGHESE December 21, 2017 at 8:32 pm #

    hello sir,
    I am a beginner to the pi. i had done all your steps written on this page and the open cv has been installed and i checked it using “import cv2 ” commad in the terminal and it was also a sucess.
    but unfortunately i can’t access open cv from python 3.5 using import cv2. it is telling me that no module named cv2. how will i rectify this.
    please help me
    thank u

    • Adrian Rosebrock December 22, 2017 at 6:44 am #

      I assume you mean that when “import cv2” is working that it works for Python 2.7? If you want to also use OpenCV for Python 3.5 you’ll need to compile OpenCV again, this time using a Python 3.5 virtual environment. The “make” command does not build OpenCV bindings for every Python version on your system (just the one specified in the “cmake” command).

      • GIBIN VARGHESE December 26, 2017 at 8:23 pm #

        It is not working on python 2.7 too.I actually compilled for python3 but outside the terminal i can’t access it.
        can u please explain the steps to do so.
        Thank u

        • Adrian Rosebrock December 27, 2017 at 9:52 am #

          Can you clarify what you mean by “outside the terminal”?

  98. Alberto December 23, 2017 at 9:49 pm #

    Hey Adrian, I followed your guide because it was mentioned in this guide, https://makezine.com/projects/raspberry-pi-potter-wand/ I was able to successfully install OpenCV but I have never done any code stuff before and your guide is super easy to follow. While the link i mention is confusing, specially to someone with no code experience. I read through some of your other OpenCV guides and found some references that you suggested to other people. I am looking into learning python and linux commands. I was wondering if you could help me out with that project and understanding what some of the stuff means. Thank you Happy Holidays!

    • Adrian Rosebrock December 26, 2017 at 4:26 pm #

      Congrats on getting OpenCV installed, nice job! As for the project you are referring to, I personally have not gone through it and unfortunately I do not have enough time to cover each individual command and Python function. All that said, if you’re new to the world of computer vision and Python please take a look at my book, Practical Python and OpenCV. Many readers have successfully used this book to learn both Python and OpenCV together.

      • Alberto December 26, 2017 at 9:13 pm #

        ok yea thank you very much i will definitely be looking at your book. Thanks again!

  99. Jalaj December 26, 2017 at 6:34 am #

    Hi Adrian,
    I am a beginner with no knowledge of Linux. My Raspberry Pi 3B hangs at 94% during make and I have tried all combinations of make like ‘make -j4’ with CONF_SWAPSIZE 1024 MB, and with 100 MB, with only ‘make’ with 1024MB and with 100MB, nothing seems to work.
    I am using a 64 GB MicroSD card(earlier my 8 GB card got full during make), should I try by increasing the size from 1024 to a higher number ? Thought of checking with you first so that I don’t corrupt anything. Thanks.

    • Adrian Rosebrock December 26, 2017 at 3:48 pm #

      I would suggest changing your swap to 2048MB and then use a single core via just “make”. The compile will take longer but it will be less likely to hang.

      • Jalaj December 27, 2017 at 11:51 am #

        It worked !! Thanks and wish you a happy new year 🙂

  100. javier zarate December 27, 2017 at 11:14 am #

    hello Adrian, thanks for your time, i have a trouble: my rpi has a 8gb sd card, i tried install twice opencv but i got the same error: can’t write pch file: no space left on device.

    i removed wolfram and office but the space that i can see on windows show me near to 1 mb free after the compilaton didn´t finish (37 % complete and the message about space on device).
    i hope you’ll can identify the problem, thank you again.

    • Adrian Rosebrock December 28, 2017 at 2:13 pm #

      Hey Javier, unfortunately it’s hard to say without access to your Pi. Can you reformat your Pi with a fresh Raspbian .img file and try again? I would suggest removing the Wolfram engine as soon as you boot the Pi.

  101. Joe December 28, 2017 at 4:06 am #

    I think I looked at all the questions, but did not see clearly whether this could be complied on a Pi Zero. So two questions. Can this be compiled on a Pi 3 and SD card used in a Pi Zero? Or is it possible to compile this on a Pi Zero? It will probably take forever but will it do it? Or is the build not platform specific?

    Thank you for this tutorial.

    • Adrian Rosebrock December 28, 2017 at 2:09 pm #

      Can you compile OpenCV on the Pi Zero? Yes, you can. It will take somewhere between 36-48 hours. Should you? No, I don’t recommend it. The Pi Zero is too underpowered for computer vision and image processing. I recommend a Pi 2 or Pi 3.

    • Frank Rizzo December 29, 2017 at 12:01 pm #

      Comments are always rejected!?

      • Adrian Rosebrock December 31, 2017 at 9:48 am #

        I’m not sure what you mean by “Comments are always rejected”. All comments go into a moderation queue but comments are not rejected.

  102. lagoa December 28, 2017 at 7:38 pm #

    hi. I’v tried to install opencv3.3.0. Every thing goes fine except when trying to compile hdf5 module : don’t find hdf5.h.
    Any idea?

    • Adrian Rosebrock December 31, 2017 at 9:57 am #

      Based on your error you can try installing HDF5:

      $ sudo apt-get install libhdf5-serial-dev

      Otherwise, you may want to turn off HDF5 by updating your “cmake” command to include the following switch:

      -D WITH_HDF5=OFF

  103. Hannes Römer December 29, 2017 at 6:39 am #

    Thanks for your great tutorials.
    Based upon them I created a simple command line tool that eases the whole build process by incorporating all steps. It automatically downloads the current OpenCV sources (v3.4.0), configures them, compiles them on the maximum available CPU cores and installs them inside the currently active virtual environment.
    You can install it via pip from PyPI (https://pypi.python.org/pypi/cvbuilder) and just have to run „cvbuilder build“ to start the whole process. It takes around 10 min on a Core i5-3320M.
    On a Raspberry Pi3 you need to run it on one core by overriding the CPU count „cvbuilder build –cpus 1“ due to memory constraints and heavy swapping. It will take approx. 2h, but you can install it into other virtual environments without compiling as long as the folder ~/temp/opencv remains.

  104. Frank Rizzo December 29, 2017 at 12:00 pm #

    Failed after 32h, won’t compile at all.

    • Adrian Rosebrock December 31, 2017 at 9:47 am #

      Hey Frank — I’m sorry to hear about the issue getting OpenCV installed. It can be quite a pain sometimes, especially if there is your first time installing OpenCV on the Raspberry Pi. Did you receive an error during the compile?

  105. Bread December 30, 2017 at 4:03 am #

    Hi, Adrian i have followed the steps and i have done it successfully.
    The problem is i don’t have “module named skimage.measure” so i have to repeat step #4 and create a new environment named “cv2”,
    and followed the rest of the steps.

    $ mkvirtualenv cv2 -p python2
    $ source ~/.profile
    $ workon cv2
    $ pip install numpy
    $ cd ~/opencv-3.3.0/
    $ mkdir build

    When i try to create the “build” directory is says “cannot create directory build: File exists”
    What shall i do? and i only have 3GB available will it be enough for the new environment?

    • Adrian Rosebrock December 31, 2017 at 9:43 am #

      If your error is that you do not have the scikit-image library installed is there a particular reason you can’t install it into your existing Python virtual environment?

      $ workon your_original_env
      $ pip install scikit-image --no-cache-dir

      This library and SciPy will take a long time to install so you should leave your Pi running overnight.

      To answer your second question provided you have already ran “sudo make install” you can delete your “build” directory and re-create it.

  106. Roberto December 31, 2017 at 2:35 pm #

    Adrian, thank you for this step by step comprehensive guide.
    I was able to install and compile the opencv on my Pi 3, using the 1024 big swap value and 4 cores in 50 minutes only :)…. a record, probably due to a very fast Sandisk SD.

    I had the aluminum coooler installed on the CPU, but even with this, with the CPU usage that was constantly around 100%, I got an overtemp icon on the screen after 30 minutes…
    I had to quicky add a fan over it to cool down the cooler, and so the overtemp icon disappear, and I was successfully able to finish the compile.

    I was so impressed and interested that I decided to immediately purchase the Bundle 🙂
    I wish you an Happy New Year.

    • Adrian Rosebrock January 3, 2018 at 1:18 pm #

      Congrats on getting OpenCV installed on your Raspberry Pi — and in under 1 hour, great job! 🙂

      And thank you for picking up a copy of my book, I hope you enjoy. Please reach out if you have any questions.

  107. Mike van der Hulst January 2, 2018 at 1:34 pm #

    regarding virtualenv you mentioned: Any Python packages in the global site-packages directory will not be available to the cv virtual environment

    I need the smbus package for I2C though. How can I use smbus in the virtualenv ?


  108. Mike van der Hulst January 2, 2018 at 2:14 pm #

    ok….using the virtualwrapper command: toggleglobalsitepackages 🙂

    This virtual blabla is confusing and probably unnessesairy when working on one project isn’t it. ?

    • Adrian Rosebrock January 3, 2018 at 1:00 pm #

      You can also install any Python package you need inside your Python virtual environment:

  109. Abe January 2, 2018 at 3:55 pm #

    Hi Adrian, love your tutorials. I was wondering if you know the proper name of the application that displays the openCV windows? I ask this because I’m trying to control the parameters of the opencv window in OpenBox (for example, no decor on opencv windows). OpenBox allows one to control how the window is displayed, but the correct name of each application needs to be called. I’ve tried python3, opencv, gtk, highgui, and many others. I’m stabbing in the dark at this point. Hopefully you have some insight in this.

    • Adrian Rosebrock January 3, 2018 at 12:59 pm #

      If you installed OpenCV on the Raspberry Pi using this tutorial then GTK and the X window manager should be used. The name of the GUI library in OpenCV is called “highgui”. How that interfaces with OpenBox I’m not sure. I hope that helps!

  110. bodet January 3, 2018 at 6:57 am #


    great job !

    I have following your tuto on my Pi on Rasbian Stretch and Rasbian strech lite.
    For the first installation all work, but on the lite, my virtualenv only work when i’m superuser!

    how to modify my actual virtual environment so that it is accessible as a simple user and that cv2 is recognized under python3?


    • Adrian Rosebrock January 3, 2018 at 12:51 pm #

      That is quite strange that it only works when you are the superuser. Did you install the Python virtual environment for the normal “pi” user or for the superuser account?

  111. Abe January 3, 2018 at 6:37 pm #

    Thanks Adrian, it was actually the name I assigned to my imshow function…cv2.imshow(“MyWindow’sName”, frame). I found this out by running “xprop WM_CLASS” then clicked on my window.

  112. phil January 6, 2018 at 3:12 pm #

    Thanks for a really helpful tutorial Adrian.

    The snag that I have is that previously installed packages (such as wx) are not available from within the virtual environment. Could you suggest a set of steps to either move cv2 out of the virtual env, or put other packages into it – whichever is most appropriate.

    I’m not familiar with virtualenvs to sort this by myself

    Many thanks Phil

    • Adrian Rosebrock January 8, 2018 at 2:51 pm #

      Hey Phil, are your packages pip-installable? If so you can install of your packages via:

      $ pip install your_package_name

  113. Ariane January 7, 2018 at 9:09 am #

    Hi Adrian
    Thanks for the step by step tutorial
    Everything is OK with the four first steps, but when it comes to compiling opencv (step #5) I get wrong path to the interpreter (/usr/bin/python3 instead of /home/pi/.virtualsenvs/cv/bin/python3)
    I though I had forgotten the workon thing somewhere, so I did it again with a new virtual environment…same failure on step 5
    How can I find what’s wrong ?
    (I tried to “make” despite th error and it stops somewhere between 4% and 26% with ” error: stdlib.h: No such file or directory
    #include_next “)

    • Adrian Rosebrock January 8, 2018 at 2:44 pm #

      This isn’t a failure, it’s just picking up the Python 3 binary in your Python virtual environment. You’re okay there.

      As for your error, try turning off pre-compiled headers by updating your CMake command to include the following switch:


      Make sure you delete your “build” directory first and then re-create it before re-running CMake.

      • Ariane January 9, 2018 at 4:08 am #

        Hi Adrian
        thank you for your help
        it’s not a failure, but it is not picking the binary in the virtual environment…(wrong path)
        I got a new SD card, installed Raspbian Stretch and everything is OK now

        • Adrian Rosebrock January 10, 2018 at 1:02 pm #

          Congrats on resolving the issue, Ariane! Nice job 🙂

  114. Gary January 7, 2018 at 6:11 pm #

    Hello Adrian,

    Thanks for the tutorial. After installing everything going down the pyhton2.7 path, I test the installation with the import cv2 command and get nothing as a response, just the symbols >>> on a blank line.
    My cv2 file ended up in the dist-packages folder instaed of the site-pacjkages folder, does that matter? I can’t figure out how to move it. I get “permission denied” when using the gui and “no such file or diresctory exists” with the mv command, can you help?

    • Adrian Rosebrock January 8, 2018 at 2:39 pm #

      As long as you can import it without an error you should be okay. I’m not sure why it would have installed in the “dist-packages” directory though. You can use the “sudo mv” command to move the file if you so wish.

  115. Aditya Garg January 8, 2018 at 3:16 am #

    hi Adrian, i am following your tutorial on my Pi on Rasbian Stretch on raspberry pi3. while editing swapsize i was not able to save /etc/dphys-swapfileit says permission denied . i am not able to figure out what to do .

    i hope you will help me out ,

    • Adrian Rosebrock January 8, 2018 at 2:37 pm #

      Make sure you use the “sudo” command to give yourself root permissions to edit the file.

  116. Damon January 8, 2018 at 11:08 am #

    Dear Adrian,

    The instruction is amazing!

    I am using the Raspberry Pi zero W. I followed the instruction and it works well. However, I meet a problem.

    I don’t know why the pi always stucked at “87% Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o”. And I use the VNC to connect the pi. Once the process reach the 87%, the VNC will be stucked. And if I disconnect it and re-connect it. It will be failed. So the only thing I can do is disconnect the power line and reboot it. And when I process “make”. It will be stucked at 87% again. Could you please show me some hints to fix this problem?

    It has already take more than 6 hours. And from 0% to 87%, it took me more than 15 hours.

    Waiting for your reply. It is quite urgent. Thank you so much.

    Best regards,

    • Adrian Rosebrock January 8, 2018 at 2:33 pm #

      So a few things here:

      1. I don’t recommend using a Pi Zero for computer vision. With only a single core it’s far too underpowered. I would recommend a Pi 2 or Pi 3.

      2. It sounds like your might need to increase your swap size, as I do in this tutorial.

      I hope that helps!

  117. Allan January 10, 2018 at 7:19 am #

    Hi Adrian can you please guide me through this? Can you tell me which commands to use in the terminal to do so? For a start how do I delete the build directory?

    This isn’t a failure, it’s just picking up the Python 3 binary in your Python virtual environment. You’re okay there.

    As for your error, try turning off pre-compiled headers by updating your CMake command to include the following switch:


    Make sure you delete your “build” directory first and then re-create it before re-running CMake.

  118. Allan January 10, 2018 at 8:49 am #

    Okay I managed to remove the directory build and create it again. And I tried to squeeze in the switch in cmake, but still got an fatal error: stdlib.h: No such file or directory.

    cd –
    rm -rf build
    mkdir build
    cd build
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \

    It wasn’t really clear how and where to add the switch -D ENABLE_PRECOMPILED_HEADERS=OFF

    I am using opencv 3.1.0 and not 3.3.0 as the video tutorial is based upon.

    Best, Allan

    • Adrian Rosebrock January 10, 2018 at 12:42 pm #

      Looking at your command above, your cmake command looks correct Nice job adding in the switch. Unfortunately, without physical access to your machine I’m not sure what the exact issue is. My only other suggestion would be to make sure you installed the development tools:

      $ sudo apt-get install build-essential cmake pkg-config

  119. Scott January 10, 2018 at 2:52 pm #

    During step 5, after running cmake, my output in the python 2 section does not include the libraries, numpy, and packages paths. It only lists Interpreter. It does look correct for the python 3 section but I would much rather use python 2. How do I fix this?

    Thanks, Scott

    • Adrian Rosebrock January 11, 2018 at 7:21 am #

      Hey Scott — did you use a Python virtual environment? Double-check that you didn’t accidentally create a Python 3 virtual environment instead of a Python 2 one.

  120. Shreyas Manjunath January 10, 2018 at 11:45 pm #

    $ make -j4
    It is compiling till 90% and freezes out later. Tried 4 times reinstalling raspian stretch.
    Any suggestions?

  121. Allan January 11, 2018 at 4:06 am #

    Thanks for the quick answer Adrian.

    I have been trying everything all over again with version 3.3.0 instead of 3.1.0. That seemed to have fixed the issue. Atleast python doesn’t throw any import errors for cv2.

    • Adrian Rosebrock January 11, 2018 at 7:12 am #

      Awesome, I’m glad to hear it’s working! Congrats on resolving the issue, Allan 🙂

      • Allan January 11, 2018 at 9:10 am #

        A more questions, Adrian.

        I have a USB cam connected to my Raspberry Pi 3 and would like to test if everything is working and so forth.
        Which IDE do I use to run the python code in? I was thinking of doing this example for a start: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

        This example also requires that ffmpeg and gstreamer is installed. So before potientially disrupting the system, by installing more dependencies. I would like to hear you out, what your take is on this.

        • Adrian Rosebrock January 11, 2018 at 1:08 pm #

          1. If you have a working system now make sure you backup your .img file to your laptop/desktop, just in case you ever want to flash your .img back to your SD card.

          2. An IDE/code editor is a personal preference. I would suggest either PyCharm or Sublime Text. While you can code in your editor I would highly recommend that you execute the code via the terminal. If you’re interested in working with video a great beginner tutorial can be found inside Practical Python and OpenCV

  122. sadi January 11, 2018 at 10:40 am #

    make -j4 doesn’t work
    Founds: make: *** No targets specified and no makefile found. Stop.

    • Adrian Rosebrock January 11, 2018 at 1:04 pm #

      Please see my reply to “Don” on October 28, 2017 and and “Raghuram” on October 29, 2017. If you ctrl + f your error message on this page you’ll likely find a similar content.

  123. paul January 12, 2018 at 12:48 am #

    Sir, in the pi terminal i can import the cv2 but when i try it in the python shell i got an error

    • Adrian Rosebrock January 12, 2018 at 5:26 am #

      Hey Paul, I’m sorry to hear about the import issue. So you can import the “cv2” library when executing a script from the command line? But not from the Python shell? Are you launching the Python shell from the terminal? Or using IDLE? Keep in mind that Python IDLe does not respect Python virtual environments.

  124. Rob Buttrose January 16, 2018 at 12:42 am #

    Hi Adrian,

    any tips on how to expand a NOOBS created image on a larger card? After a NOOBs install, “file expand” does not work (apparently because the max space is already available to the OS, although I don’t believe this).

    This applies even if you copy the image and transfer it to a larger card. In my case, I started with an 8G card. df – h shows 5.0G is available to /dev/root. I copied the image to a 32G card, and /dev/root still only has 5.0G. If I try to expand it through raspi-config, Advanced Options , I still get the NOOBs error “Your partition layout is not supported by this tool. .. You are probably using NOOBs, in which case your file system is already expanded anyway”

    It is extremely frustrating (and boring) trying to get something so simple done. Complexities expanding to a larger card should also be mentioned as a downside to installing with NOOBs.

    Do I need to resort to editing the partitions directly with gparted? (as described here https://learn.adafruit.com/resizing-raspberry-pi-boot-partition/edit-partitions).



    PS I’d rather not start over with a fresh install of Raspbian on the 32G card. I’ve downloaded quite a lot of packages.

    • Adrian Rosebrock January 16, 2018 at 12:52 pm #

      Hey Rob, I’m sorry to hear about the issues expanding the SD card. As far as I understand, using raspi-config to expand should handle it. If the partition is still not being resized you’ll have to either use (1) gparted to resize the partition or (2) start over fresh if you cannot resize the partition. Other PyImageSearch readers might have better suggestions as well.

  125. Rob Buttrose January 16, 2018 at 9:17 pm #

    Hi Adrian,

    thanks for your reply. raspi-config definitely won’t expand for an image created under NOOBs. I think this is reasonably well known.

    I appear to have expanded the root partition of my 32G card with gparted. The tricky bit is to recognise the extended partition which contains other (sub?) partitions. You need to resize that first before you can increase the size of anything inside it. Not knowing much about partitions, I may have mucked this up but for the moment it appears to be working. Raspbian boots and the /dev/root has all the space I need.

    I hope this helps others with a similar issue.


    • Adrian Rosebrock January 17, 2018 at 10:14 am #

      Thanks for sharing Rob! I’m glad you’re up and running with OpenCV on your Raspberry Pi 🙂

  126. Roy Shearer January 17, 2018 at 7:50 am #

    Hi Adrian,

    Great tutorial thanks. I would like the Pi to automatically run a python file that uses OpenCV on boot. Am I right in thinking I will need to make it execute the
    source ~/.profile
    workon cv
    lines before running the python file?

    I have been trying to make up a shell script to do that but I get a ‘not found’ error when it encounters the source and workon lines.

    This is the shell script:

    #! /bin/sh
    echo “switching to cv virtual environment”
    source ~/.profile
    workon cv

    • Adrian Rosebrock January 17, 2018 at 10:06 am #

      Hey Roy, I cover how to run a Python script on reboot in this blog post. You’re on the right track but be sure to read the comments section as well where I discuss a few alternatives as well.

  127. Roy January 17, 2018 at 11:19 am #

    Great, ideal! Thanks Adrian.

  128. Ferishta Ansari January 19, 2018 at 2:51 am #

    So I followed your guide, step by step on how to install open cv 3 with python 3 on raspbian stretch…. on my terminal it says that cv2 was imported, and shows me the correct version. Basically I get the exact same message that I see on your screenshot of step 7 which I assume is telling me that I have successfully installed openCV. However when I run a script that imports cv2, I get the message “No module cv2″ using the ” ls ” command to user/local/lib/python3.5/site-packages, I see cv2 on my terminal…but when I actually visit this path from my desktop, there is no cv2 on site-packages, or in dist-packages…Please help 🙁

    • Adrian Rosebrock January 19, 2018 at 6:43 am #

      Hey Ferishta — so if I understand your question, you can import the “cv2” library into your Python shell but you cannot import the “cv2” library from a Python script? When executing your Python script are you sure you are in the “cv” virtual environment? Make sure you are, otherwise the script will not be able to find your libraries.

      • Ferishta January 19, 2018 at 2:16 pm #

        Omg thank you for your quick reply!! When I try to run the python script it gives me that “no module named cv2 error” so you’re saying prior to running my script I should use the workon command to get on the virtual environment? Also when I follow the path to site packages, it says “no sub folders” and from my understanding that’s where cv2 should be installed. Correct ?

        • Adrian Rosebrock January 22, 2018 at 6:41 pm #

          Correct, use the “workon” command to enter the Python virtual environment. You only need to run this command once per terminal session.

      • ferishta January 19, 2018 at 7:00 pm #

        to answer your question, yes I am in the “cv” virtual environment when executing the python script

  129. Hashir January 19, 2018 at 4:29 am #

    When am running this command 1
    $ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev in raspbian stretch (debain 9) normal pc version, there are some errors like no such file to install or download.
    So how can I fix it ?

    • Alex March 15, 2018 at 11:14 pm #

      I also had this problem.

    • Alex April 25, 2018 at 1:45 pm #

      ^^^ I got this error also

      I ran sudo apt-get update and then everything worked

      • Georged January 4, 2019 at 6:41 am #

        almost a year later, I just encountered this same error. apt-get purge jasper did not correct the problem. I did solve it. The issue was that the cached jasper-dev install image was corrupt. I had to manually remove the cached files sudo rm -f /var/cache/apt/archives/libjasper*

  130. Ferishta January 20, 2018 at 5:41 am #

    For the third time, I completely wiped out my sd Card, downloaded raspbian stretch, and installed “openCV”. I followed your guide step by step, and I got the same message that I see in your screenshot of step 7 which “verifies” that the installation was successful. However, upon running a python script that imports cv2, I get the same error message, “no module named cv2” and yes I’m in the virtual cv environment when running the script. I see that this is an issue that a lot of people have. I really think you should try to do some research in finding some possible solutions for this possible. Otherwise your blog can be very misleading, and shouldn’t be up for public…

    • Adrian Rosebrock January 20, 2018 at 8:06 am #

      Hi Ferishta — I’m sorry you are having trouble getting up and running with OpenCV. It can be a trying process, especially if this is your first time installing OpenCV. The first time I installed OpenCV back in undergrad it took me 3 days. Since then the install process has gotten easier. All install posts here on PyImageSearch are thoroughly documented and tested by me. There are undoubtedly some “gotchas” that creep up — that is part of configuring your development environment.

      If you would like to share the command you are trying to run, or better yet, take a screenshot of your terminal as you run the command, it would be helpful in diagnosing the issue. Keep in mind that myself or other PyImageSearch readers cannot replicate your environment exactly and most of the time these issues creep up due to an error in a previous step that was missed or an issue when actually executing the command (for example, trying to execute the script as “sudo”). Help us help you by sharing more details. I get that you’re frustrated but without knowing more we cannot diagnose the issue.

  131. Ferishta January 21, 2018 at 12:36 am #

    Adrian, I want to apologize to you from the bottom
    Of my heart. This is my first time working with openCV, so I was struggling a lot. The thing is, I was saving my script on my desktop. Instead of “home/pi” this helped resolve all of my issues. I can now access my raspberry pi camera with openCV. I was also able to run your Pokémon color detector game! Im very happy that I’ve found you, and I’m looking forward to learning a lot from you…

    I did have one concern though. While all of the python scripts now run, and I see the excpected out, I do get a warning message on my screen.

    WARNING**: error retrieving accessibility bus address: org.freedesktop.Dbus. Error. ServiceUnknown: the name org. ally. Bus was not provided by any. Service files

    Could you please tell me what this warning is and how can I make it go away?

    Thank you so much once again

    • Adrian Rosebrock January 21, 2018 at 6:47 am #

      Congrats on getting up and running with OpenCV, Ferishta. Nice job. This warning is related to GTK/X11. It can be ignored, it has no impact on your code running. As for making it go away, try this:

      $ sudo apt-get install libcanberra-gtk*

      • Ferishta January 21, 2018 at 2:51 pm #

        Thank you so much, Adrian!!!

  132. Sanjaya Kumar Das January 25, 2018 at 4:28 am #

    I installed open cv 3.3 successfully yesterday but today when I am trying to import cv2 its giving error no module named cv2.Why is it like this?

    • Adrian Rosebrock January 25, 2018 at 3:47 pm #

      Make sure you are in the “cv” virtual environment before trying to import the OpenCV library:

      $ workon cv

      And from there open your Python shell or execute the script.

  133. Sam January 25, 2018 at 10:11 pm #

    Dear Sir,

    Huge thanks for the step-by-step tutorial. You are a savoir, however i still need to get over one obstacle to proceed.

    i am certainly in the (cv).

    And i have checked that the cv2.so exists in these paths (‘find’ command):

    BUT i am getting the import error no module named cv after executing a script (one you provided here: https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/)

    Please help me i have a competition two days from now.

    • Adrian Rosebrock January 26, 2018 at 10:12 am #

      From what I can see it looks like you have OpenCV correctly installed on your system but I would double-check that your sym-link points to a valid file. The “find” command would still report a “cv2.so” file on disk (since a sym-link is still a file) but the sym-link may be pointing to a file that does not exist. Make sure you double-check those paths.

      If you’re in a rush and need to get up and running for your competition I offer a Raspbian .img file with OpenCV pre-configured and pre-installed inside the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV. This would enable you to download my Raspbian .img file, flash it to your SD card, and boot.

  134. Varadharajan January 26, 2018 at 8:18 am #

    Actually my problem is that while executing the compiling process of opemcv 3.3.0…it strucks at 48%…….give me some solution for it

    • Adrian Rosebrock January 26, 2018 at 10:04 am #

      When you say “stuck” do you mean the compile ends with an error? Or your compile + Raspberry Pi freezes? The more detail you can give the better myself and other PyImageSearch readers can help you.

  135. Varadharajan January 26, 2018 at 7:34 pm #

    The compile and raspberry pi freezes

  136. Varadharajan January 27, 2018 at 7:34 pm #

    In the 6th step im getting an error…

    When i type

    Im not getting the respective line as shown in the tutorial video….

  137. JP January 28, 2018 at 4:28 am #

    Thank you so much Adrian i have installed it all with your help, hope my anti drowsiness detection system will be done soon. Thank you!

    • Adrian Rosebrock January 30, 2018 at 10:31 am #

      I’m happy to help JP — best of luck with the project!

  138. varadharajan.s January 28, 2018 at 7:55 pm #

    actually after completing the “make” code,the installing creates in problem
    when attempting to type
    <<import cv2
    there occurs error

  139. Ron January 29, 2018 at 6:54 pm #

    1st, thanks for all you do, I can’t get over how many tutorials you provide.

    Step 5. When I do cmake CMAKE_BUILD_TYPE=RELEASE \ should the process hang at ”>” (if I include the ”-D” I get a ”bash: -D: command not found”)?
    Nothing happens until I press [Enter] a second time. And then I get ”CMake Error: The source directory “/home/pi/opencv-3.3.0/build/CMAKE_BUILD_TYPE=RELEASE” does not exist.
    Specify –help for usage, or press the help button on the CMake GUI.@ Except we aren’t using a GUI . . . and I thought the build was supposed to create the directory?



    • Adrian Rosebrock January 30, 2018 at 10:08 am #

      Hey Ron — I’m pretty sure there was a problem copying and pasting the cmake command. If you’re having trouble try copying and pasting it line-by-line (without hitting enter). Once the entire command is copied into your shell, execute it.

  140. varadharajan s January 29, 2018 at 7:50 pm #

    sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so

    when I type the above code……….im getting error……..and to be frank,i have installed this in discontinuous manner…………….after some shutdowns……..do reply fast

  141. Carl Maraño January 31, 2018 at 12:29 am #

    Hello sir, Its been 1 and a half hour of compiling and its 90% already but as I’ve observed itt taking too long to make a progress. Is it a sign that I should repeat the installation or should I wait for it because as you have said it takes 4 hrs to finish. Hoping for your fast response. Thanks

  142. Carl Maraño January 31, 2018 at 1:12 am #

    It’s stuck in 90% i followed your swap size and it still doesn’t work. Please help me.

    • Adrian Rosebrock January 31, 2018 at 6:35 am #

      Hey Carl — can you try compiling with a single core via “make”?

  143. Carl Maraño February 2, 2018 at 1:04 am #

    Hello sir, thanks a lot! I tried compiling it with a single core via “make” and it was a success. I already installed it and it worked. However, i’m just curious, what’s the difference of the 4 cores and single core when compiling?

    • Adrian Rosebrock February 3, 2018 at 10:44 am #

      In short: speed. Using multiple cores can reduce the time the compile takes, making the compile run faster. It has no impact on how fast the OpenCV library will run after it’s installed.

  144. koropeta February 2, 2018 at 4:57 pm #

    hello adrian!

    I could not install.Could you tell me solving?
    Install error message is below:

    [100%] Built target example_tapi_squares
    [100%] Built target example_tapi_ufacedetect
    Install the project…
    — Install configuration: “RELEASE”
    CMake Error at cmake_install.cmake:36 (file):
    file cannot create directory: /usr/local/include/opencv2. Maybe need
    administrative privileges.


  145. koropeta February 2, 2018 at 5:00 pm #

    My os was installed by NOOBS ver.2.4.5

  146. koropeta February 2, 2018 at 6:04 pm #

    I solved by myself

  147. koropeta February 2, 2018 at 11:07 pm #

    Hi, Adrian!

    Thanks to you I have installed and I was able to run the sample program.
    Can I run OpenCV on Thonny Python IDE (Python 3) without using Virtualenv on LXTerminal?
    “Import cv2” always causes an error.
    My environment is Raspberry pi 3, NOOBS 2.4.5.


    • Adrian Rosebrock February 3, 2018 at 10:31 am #

      Yes you can, but you would need to skip all Python virtual environment instructions in this guide. I’m not familiar with the Thonny Python IDE but you might want to see if it has a project interpreter similar to PyCharm. You can set the project interpreter to point to the Python virtual environment that has OpenCV installed.

  148. franky February 3, 2018 at 3:01 am #

    I had an issue where the builder stalled around 45%, but I reran make without the -j4 command and it worked – but had to be run overnight as it probably took at least 4 hours.

    Many thanks for the excellent walk through – there is no way I could have installed openCV without your notes!

    • Adrian Rosebrock February 3, 2018 at 10:30 am #

      Congrats on getting OpenCV installed on your Raspberry Pi! 🙂

  149. Amber February 4, 2018 at 4:07 am #

    After using source ~/.profile command
    I get error no directory found

    • Kurt April 29, 2018 at 8:46 am #

      Me too??? Did you find a fix?

      • Adrian Rosebrock April 30, 2018 at 12:58 pm #

        Hi Kurt and Amber — did you follow Step #4 to create the ~/.profile file with the important virtualenvwrapper information?

  150. Philip February 7, 2018 at 9:26 am #

    Hello, while attempting to create my build files for my Pi 3 using a python 3 virtual environment using the cmake commands, I keep getting this error, “The source directory Blah/Blah/blah does not exist”. How do I fix this?

    • Adrian Rosebrock February 8, 2018 at 7:57 am #

      Hey Philip — make sure you are in the “build” directory before executing the “cmake” command. Don’t forget the “..” at the end of the cmake command as well.

  151. Kalyan February 11, 2018 at 8:46 am #

    Hello Adrian, I’m facing a problem when I try to compile opencv by typing make it freezes at 98% please help me

    • Kalyan February 11, 2018 at 8:51 am #

      Also I have tried make -j1 and make j-4 neither are working please help me

      • Adrian Rosebrock February 12, 2018 at 6:25 pm #

        Try updating your swap size as we do in this tutorial. Updating your swap size will resolve the issue in the vast majority of situations.

  152. Miguel Xochicale February 11, 2018 at 1:17 pm #

    Thanks Adrian for your wonderful work and your time and help.

    I have finally suceded on installing opencv-3.4.0 on the PI3.
    After ten trials, what works for me was not to increase your swap space
    size and build opencv with just a simple make

    Just for the record, I created this logBook to register the trials and errors:


    • Adrian Rosebrock February 12, 2018 at 6:21 pm #