Install OpenCV and Python on your Raspberry Pi 2 and B+


My Raspberry Pi 2 just arrived in the mail yesterday, and man is this berry sweet.

This tiny little PC packs a real punch with a 900mhz quadcore processor and 1gb of RAM. To give some perspective, the Raspberry Pi 2 is faster than the majority of the desktops in my high school computer lab.

Anyway, since the announcement of the Raspberry Pi 2 I’ve been getting a lot of requests to provide detailed installation instructions for OpenCV and Python.

So if you’re looking to get OpenCV and Python up-and-running on your Raspberry Pi, look no further!

In the rest of this blog post I provide detailed installation instructions for both the Raspberry Pi 2 and the Raspberry Pi B+.

I’ve also provided install timings for each step. Some of these steps require a lot of processing time. For example, compiling the OpenCV library on a Raspberry Pi 2 takes approximately 2.8 hours versus the 9.5 hours on the Raspberry Pi B+, so please plan your install accordingly.

Finally, it’s worth mentioning that we’ll be utilizing the Raspberry Pi inside the PyImageSearch Gurus computer vision course. Our projects will include home surveillance applications such as detecting motion and tracking people in a room.

Here’s a quick example of detecting motion and tracking myself as I walk around my apartment on the phone:


Install OpenCV and Python on your Raspberry Pi 2 and B+

UPDATE: The tutorial you are reading now covers how to install OpenCV 3 with Python 2.7 and Python 3 bindings on Raspbian Wheezy. Raspbian Jessie has now replaced Raspbian Wheezy and if this is the first time you are reading this tutorial then in all likelihood you are using Raspbian Jessie. Please use the following updated guides to help you install OpenCV + Python on your Raspberry Pi.

I’m going to assume that you have either your Raspberry Pi 2 or Raspberry Pi B+ unboxed and setup. If you don’t have a Raspberry Pi yet, I definitely suggest picking one up. They are super cheap and a lot of fun to play with.

Personally, I prefer to spend a little extra money and purchase from Canakit — their shipping is fast and reliable, plus their complete ready-to-go bundles are really nice.

Anyway, let’s get into the OpenCV and Python install instructions.

Step 0:

Again, I’m going to assume that you have just unboxed your Raspberry Pi 2/B+. Open up a terminal and we’ll start by updating and upgrading installed packages, followed by updating the Raspberry Pi firmware:

Step 1:

Install the required developer tools and packages:

Both build-essential  and pkg-config  are likely already installed, but just in case they are not, be sure to include them in your apt-get  command.


Raspberry Pi B+: < 2 minutes

Raspberry Pi 2: < 40 seconds

Step 2:

Install the necessary image I/O packages. These packages allow you to load various image file formats such as JPEG, PNG, TIFF, etc.


Raspberry Pi B+: < 5 minutes

Raspberry Pi 2: < 30 seconds

Step 3:

Install the GTK development library. This library is used to build Graphical User Interfaces (GUIs) and is required for the highgui  library of OpenCV which allows you to view images on your screen:


Raspberry Pi B+: < 10 minutes

Raspberry Pi 2: < 3 minutes

Step 4:

Install the necessary video I/O packages. These packages are used to load video files using OpenCV:


Raspberry Pi B+: < 5 minutes

Raspberry Pi 2: < 30 seconds

Step 5:

Install libraries that are used to optimize various operations within OpenCV:


Raspberry Pi B+: < 2 minutes

Raspberry Pi 2: < 30 seconds

Step 6:

Install pip :


Raspberry Pi B+: < 2 minutes

Raspberry Pi 2: < 30 seconds

Step 7:

Install  virtualenv  and virtualenvwrapper :

Then, update your ~/.profile  file to include the following lines:

Reload your .profile  file:

Create your computer vision virtual environment:


Raspberry Pi B+: < 2 minutes

Raspberry Pi 2: < 2 minutes

Step 8:

Now we can install the Python 2.7 development tools:

Note: Yes, we are going to use Python 2.7. OpenCV 2.4.X does not yet support Python 3 and OpenCV 3.0 is still in beta. It’s also unclear when the Python bindings for OpenCV 3.0 will be complete so I advise to stick with OpenCV 2.4.X for the time being.

We also need to install NumPy since the OpenCV Python bindings represent images as multi-dimensional NumPy arrays:


Raspberry Pi B+: < 45 minutes

Raspberry Pi 2: < 15 minutes

Step 9:

Download OpenCV and unpack it:

Setup the build:


Raspberry Pi B+: < 3 minutes

Raspberry Pi 2: < 1.5 minutes

Compile OpenCV:

Important: Make sure you’re in the  cv  virtual environment so OpenCV is compiled against the virtual environment Python and NumPy. Otherwise, OpenCV will be compiled against the system Python and NumPy which can lead to problems down the line.


Raspberry Pi B+: < 9.5 hours

Raspberry Pi 2: < 2.8 hours

Finally, we can install OpenCV:


Raspberry Pi B+: < 3 minutes

Raspberry Pi 2: < 1 minute

Step 10:

If you’ve gotten this far in the guide, OpenCV should now be installed in  /usr/local/lib/python2.7/site-packages

But in order to utilize OpenCV within our cv  virtual environment, we first need to sym-link OpenCV into our site-packages  directory:

Step 11:

Finally, we can give our OpenCV and Python installation a test drive:

OpenCV and Python is now successfully installed on your Raspberry Pi!

Here is an example of me ssh’ing (with X11 forwarding) into my Raspberry Pi, followed by loading and displaying an image:


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 I detailed how to install OpenCV and Python on your Raspberry Pi 2 or Raspberry Pi B+. Timings for each installation step were also provided so you could plan out the install accordingly.

As the Raspberry Pi (along with Raspbian/NOOBS) evolves the installation instructions will likely change. If you run across any edge cases or variations in the install instructions, please feel free to let me know. While I can’t promise that I can reply to every email, but I think it would be good to curate a list of methods to setup OpenCV and Python on Raspberry Pi systems.

And in future blog posts we’ll explore how to utilize the camera add-on for the Raspberry Pi.

Until then, take a look at the PyImageSearch Gurus computer vision course. We’ll be utilizing the Raspberry Pi inside the course for a few projects, including building a home surveillance application that can detect motion and people in rooms.

, , , , ,

650 Responses to Install OpenCV and Python on your Raspberry Pi 2 and B+

  1. centryfox February 23, 2015 at 11:44 am #

    What are the FPS looking like with openCV+python on the RPi2?

    • Adrian Rosebrock February 23, 2015 at 12:34 pm #

      I’m actually in the process of figuring that out. More posts to come 🙂

      • Niyas NAJEEM February 16, 2018 at 3:57 pm #

        How can we get your kit

        • Adrian Rosebrock February 18, 2018 at 9:49 am #

          What do you mean by “my kit”? Can you elaborate? Thanks!

    • Adrian Rosebrock February 25, 2015 at 11:04 am #

      Hey, as I followup I wanted to let you know the results of my testing. I am able to run 32 FPS at 640×480 for motion detection without a problem at all. Very, very fast.

      • RJ May 7, 2015 at 6:55 am #

        May I know if this installation runs on multi-cores processing or it’s a single core processing? My installation with synaptic packages give around 10fps on 640 X 480 on color tracking. And the CPU usage monitors only shows 30- 40% utilizations.


        • Adrian Rosebrock May 7, 2015 at 7:13 am #

          The OpenCV installation can utilize multiple cores, but your bottleneck with the camera is likely the I/O between the module and the OS.

          • RJ May 7, 2015 at 8:49 am #

            I’m using the CSI camera like the one you have. How come the performance difference? Any tweak that you made that’s not covered in this tutorial?

          • Adrian Rosebrock May 7, 2015 at 10:46 am #

            Nope, no difference in tweaks. Are you using the Pi2 as well?

          • Rajeev September 17, 2015 at 3:43 am #

            Hey Adrian, awesome work. My OpenCV installation seems to be using only a single core (25% CPU usage) on my Pi 2. I’m making use of the inRange function to isolate colored lines for a line following bot. Any way of making my code run on all 4 cores? I’d already compiled OpenCV with TBB and OPEN_MP, but there seems to be no noticeable difference.

          • Adrian Rosebrock September 17, 2015 at 7:35 am #

            Hey Rajeev, not all OpenCV functions are setup in a way to utilize multiple cores.

      • Shaun August 22, 2015 at 1:15 pm #

        Hi Adrian. I’m a total noob when it comes to the pi and image processing in general, so forgive me if this is a silly question “^^

        How do you check the frame rate when doing something like motion detection? If I wanted to show some evidence of the frame rate, what would I need to do?

        Thanks in advance! Your blog posts have helped me quite a bit.

        • Adrian Rosebrock August 23, 2015 at 6:53 am #

          Hey Shaun — the easiest way to check the frame rate is use the time or datetime Python module. You’ll basically want loop over a preset number of frames (i.e. 2,000) and accumulate how long it takes to loop over each of those frames. Then you divide the total number of times by 2,000 (or whatever your number is) to get your average FPS. This isn’t a perfect method, but it will give you a rough estimate.

          • Shaun August 23, 2015 at 10:30 am #

            Alright, I will try that. Thank you!

            By the way, if it’s not much trouble to ask, when tracking a ball specifically, using the pi 2, what sort of frame rate would you expect?

            My approach for tracking is as follows:
            1. Grab a frame from pi camera
            2. Convert to HSV color space (for better accuracy)
            3. Filter out the ball’s color range
            4. Convert that filtered image to gray scale
            5. Hough Circles to find the ball (accounts for partial occlusions) – initially I used moments, but that doesnt work for multiple tracking, and I will be doing that, hence another reason to use hough circles

            Just judging by eye, it looks like I’m getting anywhere between 15-30 fps. Should I be able to get higher than that? Or at least a constant 30fps?

          • Adrian Rosebrock August 24, 2015 at 6:43 am #

            Out of curiosity, if you can filter the ball’s color range using color, why even bother with Hough Circles? You could compute the moments directly on the mask generated by the color range. That said, 15-30 FPS sounds about right depending on the complexity of the image. If you get rid of Hough Circles you should be able to go even faster. You could also try downscaling your image which should improve the speed.

        • Mike November 4, 2015 at 5:34 pm #

          I am trying to do the exact same thing that you are trying to do: track a ball using HoughCircles.

          I cannot get it to work! When I do only color detection, I get very low FPS, like 1-3 FPS.

          Can you send me your source code for what you did to get 15-30 FPS?

          • Adrian Rosebrock November 5, 2015 at 6:42 am #

            Hey Mike — if you have a question related to ball tracking or motion detection, please comment on the appropriate blog post. This post is dedicated to installing OpenCV on your Raspberry Pi. You’ve also submitted multiple comments on most OpenCV installation threads. Please do not spam the blog. If you have a question, comment on the appropriate post or email me directly.

      • aaron September 23, 2015 at 4:12 am #

        Hi Adrian,

        I’ve got everything banging away on my pi2, the only thing is I’m not seeing those kinds of frame rates, no where near when using a webcam.

        I pretty much stripped everything out of the webcam case study code until all it’s doing is reading from the camera and updating the window and I’m only seeing just over 17 FPS. If I take out even displaying the frame, I’m clocking in at 24 FPS (not super useful like that, but I’m troubleshooting). I tried 2 different web cams with (near) the same results (maybe they’re both old)

        When I run the example and add some timings, I see a wretched 1.65 FPS. I stuck some timers in for the loop and for the facial detection, and it’s spending .5 seconds running through the cascade! This is obviously the long straw even if my cameras suck.

        What am I doing wrong? Is there a tweak or something I need to do to get this to run faster?

        • Adrian Rosebrock September 23, 2015 at 6:40 am #

          Ouch, 1.65 FPS is quite slow. How large are the frames that you are trying to process? The first step is always to resize your frame, making it smaller, thus there is less data to process. By doing a simple resizing you should be able to get the cascade to run much faster.

  2. Gabriel February 25, 2015 at 10:32 am #

    One of the best compiled manuals/tutorials.
    Installation went so smooth and according to the estimates provided.
    Thanks a lot.

    • Adrian Rosebrock February 25, 2015 at 11:33 am #

      Thanks Gabriel! 🙂

      • Romeo Eduardo Luna Arias April 7, 2015 at 1:08 am #

        hello I have a problem with step 9 and 10, opencv not installed in the folder

        and i do not know why
        I am newbie

        • Adrian Rosebrock April 7, 2015 at 7:24 am #

          Did you run sudo make install after OpenCV was compiled?

          • shawn April 29, 2015 at 11:31 pm #

            Hi Andrian! I have same problem. I’ve tried sudo make install and sudo ldconfig several times but the folder /usr/local/python2.7/site-packages is still empty.

            How should I proceed this install? Thank you

          • Adrian Rosebrock May 1, 2015 at 6:56 pm #

            Hi Shawn, please read the other comments on this post. Check your dist-packages directory and make sure that the output of cmake has correctly picked up your Python version.

          • mrar April 30, 2015 at 9:27 pm #

            I compile on UDOO board using your guide and successful. i face the same problem where I couldn’t find ang cv2 in that folder “/usr/local/lib/python2.7/site-packages” even after running this command “sudo make install”

          • Adrian Rosebrock May 1, 2015 at 6:54 pm #

            Try checking the dist-packages directory and seeing if the output of cmake has correctly picked up Python.

          • Kevin Karagitz May 1, 2015 at 8:14 am #

            Same issue for me. I ran the make install and it’s not installed under /usr/local/lib/python2.7/sites-packages

            A quick sudo find / -name “”

            comes up empty… Thoughts?

          • Adrian Rosebrock May 1, 2015 at 6:50 pm #

            That’s definitely really strange. I would re-run cmake and make sure that the compilation is picking up the appropriate Python version correctly.

          • Kevin Karagitz May 1, 2015 at 8:25 am #

            One thing of note, I noticed the make install log doesn’t show it installing any files under a python directory.

          • Marcellus May 2, 2015 at 1:24 pm #

            Hi Adrian,

            I’m stuck at this import cv issue as well. I have read all of the comments in regards to this issue. You mentioned removing OpenCV, and re-installing it; how do I un-install something off of Raspberry? Also, I just realized that my Raspberry had Python 3.0 and 2.6 installed on it, so do I need to un-install 3.0 and 2.6 as well?

          • Adrian Rosebrock May 2, 2015 at 1:53 pm #

            Hi Marcellus, in your particular case it might be easier to just wipe your Raspbian install and try again. For questions related to uninstalling software from the Raspberry Pi, I’ll refer you to the Raspberry Pi forums. You shouldn’t need to uninstall Python 3, provided that your cmake command picked up the 2.7 version of Python. You can verify this by viewing the output of cmake.

          • Marcellus May 5, 2015 at 7:52 am #

            Hello Adrian,

            Wiping Raspian, and re-installing it did the trick. The mistake I made was doing 2 steps a day; I would do 2 steps, then poweroff Raspian. I realized that I needed to do Steps 7 – 11 all in one day. Thanks! Now, I just need to install the camera; I’m not sure if you have a tutorial for installing cameras on Raspian, but I’ll find it if you do. Thanks again!

          • Adrian Rosebrock May 5, 2015 at 11:13 am #

            Hi Marcellus, here you go: 🙂

          • Marcellus May 5, 2015 at 8:12 pm #


  3. Neil February 25, 2015 at 10:44 am #

    Alternatively, I’ve had success with:

    sudo apt-get install python-numpy
    sudo apt-get install python-scipy
    sudo apt-get install python-imaging
    sudo apt-get install libopencv-dev
    sudo apt-get install python-opencv

    • Adrian Rosebrock February 25, 2015 at 11:32 am #

      Hi Neil, that certainly will work, but I strongly recommend against using apt-get to install these packages. You will not have direct control over the versions of each library (including OpenCV, which can be a problem) and you’ll effectively lose the ability to leverage virtual environments.

  4. Chris Viehoff February 26, 2015 at 12:08 pm #

    Hi Neil, thanks for the excellent tutorial. Will you be covering

    $ sudo apt-get install python-picamera

    to access the PI camera module as well? Or are you using a webcam via USB for your vision projects?

    • Adrian Rosebrock February 26, 2015 at 12:31 pm #

      It’s Adrian, actually 🙂 And yes, I will be covering the picamera module in the future, which is what I use for my Raspberry Pi + computer vision projects.

      • Helmut March 5, 2015 at 8:52 am #

        I guess that “workon cv” is needed right before Step8 ?

        This way Python2.7 is installed in the cv environment. Same for numpy and OpenCV?

        In other words: Is it correct to work withinin the cv environment starting from Step8 onwards?

        Thanks for this great tutorial !

        • Adrian Rosebrock March 5, 2015 at 11:29 am #

          Yes, that is correct. If you followed the tutorial exactly, then running mkvirtualenv cv will drop you into the cv virtual environment. But after you logout and login to your Raspberry Pi again, you’ll need to use the workon cvcommand to access your cv virtual environment.

          • shawn April 29, 2015 at 11:40 pm #

            Hi Adrian! in case I happen to run “mkvirtualenv cv” again after re-login, will the old virtual environment “cv” is overridden?

          • Adrian Rosebrock May 1, 2015 at 6:56 pm #

            No, you’ll get an error message saying that the virtual environment already exists.

  5. Chris Viehoff February 26, 2015 at 12:25 pm #

    Just noticed picamera is already installed. On another note, I have to run

    source ~/.profile

    to get the command “workon” to work. I this ok or does the change need to be
    done in .bashrc.

    • Adrian Rosebrock February 26, 2015 at 12:32 pm #

      You can put it in .bashrc if you would like. I personally like to keep my .bashrc file as tidy as possible and keep separate source files. It’s a personal preference.

  6. Ban February 26, 2015 at 3:26 pm #


    Great tutorial, thank you Adrian!

    I have some issues, any help would be highly appreciated

    How can i check this?
    Make sure you’re in the cv virtual environment

    I wish to SSH from a win7 computer with putty, i set the X11 forwarding in putty, but io get the following error:
    PuTTY X11 proxy: wrong authorisation protocol attempted
    (Face:2193): Gtk-WARNING **: cannot open display: localhost:10.0

    And i get command not found for the workon command.

    Thank you

    • Adrian Rosebrock February 27, 2015 at 7:35 am #

      Hi Ban, make sure you source ~/.profile when you login. Then you will be able to use the workon command. Once you are in the cv virtual environment your shell will have (cv) at the front of it.

      • Kevin February 28, 2015 at 1:20 pm #

        Hello Adrian,

        We finished the installation and wanted to run the so we …

        -closed our putty session
        -enabled x11 forwarding
        -opened an ssh session
        – logged in and typed …

        pi@raspberrypi ~ $ source ~/.profile
        pi@raspberrypi ~ $ workon cv
        (cv)pi@raspberrypi ~ $ python
        python: can’t open file ‘’: [Errno 2] No such file or directory
        (cv)pi@raspberrypi ~ $

        Any suggestions?


        • Adrian Rosebrock March 1, 2015 at 10:58 am #

          Hi Kevin, there is no file pre-loaded on the Raspberry Pi. You need to create the yourself. Sorry for any confusion. Anyway, here is the contents of

          import cv2
          image = cv2.imread("image.jpg")
          cv2.imshow("Example", image)

          This of course assumes that you have an image named image.jpg in your directory.

          • Manish Gupta April 23, 2015 at 3:02 am #

            Adrian. Thanks for this very detailed set of steps. I was able to go through the steps as you mentioned. Ran the above program. The image popped up but it does not show the “face” as it is shown in the final step of your example. Any suggestions where I could have gone wrong?

          • Adrian Rosebrock April 23, 2015 at 6:17 am #

            Hey Manish, there is nothing that you have done wrong. The “face” example was a quick Python script that I wrote to demonstrate that an image could be loaded from disk and displayed to screen. I would go read this post on on loading and displaying an image or the first few chapters of Practical Python and OpenCV to learn more about loading and displaying images to your screen.

          • CadoroV July 5, 2015 at 4:47 pm #

            Adrian ))).
            Your guide is very good , btw after this answer i think you’re a good enginering

          • Abhi March 6, 2017 at 1:11 am #

            Hi Adrian, Do you have to be in ‘workon cv’ everytime you want to use opencv?

            Can I just not do ‘workon cv’ and run my python script? would it work?

          • Adrian Rosebrock March 6, 2017 at 3:38 pm #

            You only have to execute the “workon” command once per terminal session.

    • Milan Kryl March 5, 2015 at 6:55 pm #

      You need to install Xming to Windows to enable X11 forwarding with PuTTY.

      • Adrian Rosebrock March 5, 2015 at 6:57 pm #

        Great tip, thanks for passing it along.

  7. Michael Little February 26, 2015 at 10:15 pm #

    Awesome and thorough tutorial. Much appreciated

  8. Michael Little February 26, 2015 at 11:17 pm #

    This virtual environment is confusing to me. The install when fine but when I try to use the cv2 in python it says No module named cv2. How does this virtual environment come into play? thanks

    • Adrian Rosebrock February 27, 2015 at 7:32 am #

      Hi Michael, make sure that you source ~/.profile followed by workon cv. Also, here’s a little more information on Python virtual environments.

      • Michael Little February 27, 2015 at 9:12 am #

        Works perfectly. Thanks for the tutorial.

        • Adrian Rosebrock February 28, 2015 at 9:07 am #

          Fantastic, I’m glad it worked for you! 🙂

  9. GooseNinja February 27, 2015 at 7:57 am #

    This is an awesome Tutorial, and so is this website 🙂

    But I would like to point out one thing about the Raspberry Pi 2 installation.

    When you compile with make you are only using 1 of the CPU’s cores.
    I followed your instructions, but I changed



    make -j 5

    by doing that, make runs in parallel mode and utilize each core.
    Making the installation faster.

    unfortunately, I didn’t time my progress. So I’m not sure how much faster it went.

    • Adrian Rosebrock February 28, 2015 at 9:08 am #

      Very true! The reason I only used 1 core was so I could directly compare to the B+. But you’re absolutely right, utilizing multiple cores will dramatically increase the compilation stage. As Matt reports above, by using -j4 he was able to reduce the compile time to 45 minutes.

      • Justin May 31, 2015 at 11:55 pm #

        What is the point of directly comparing a single core of the Pi 2 to the B+? Few of us want to see the comparison at the expense of missing those 2 hours of useful time. Besides, the merits of the Pi 2 are in the quad core chip, so you might as well highlight those instances where it’s particularly helpful.

  10. apri triansah February 27, 2015 at 11:08 am #

    hi adrian, it is nice tutorial, im newbie in raspberry. is raspberry can run c++ code in open cv tutorial,bcause im not familiar with pyton

    • Adrian Rosebrock February 28, 2015 at 9:07 am #

      Hi Apri, yes, the the Raspberry Pi can run C++ code as well.

  11. Matt Kraemer February 27, 2015 at 7:16 pm #

    Hi Adrian,

    I’m loving my rPi2 as well. I’m curious why you installed the virtual environment to run your programs in?

    Also if you use ‘make -j4’ it only takes 45 minutes to build opencv on the rPi2 🙂

    You may notice that the rPi and rPi2 both have hardware h264 support through the OMX driver. Gstreamer offers a plugin that wraps the OMX driver. Since OpenCV can use Gstreamer… this would enable writing h264 video to file without slowing down the 4 cores.

    I’ve been trying to get this working but there are still a few bugs in the build process. hmmphf. Any chance you could lend a hand… or a friends hand?

    • Adrian Rosebrock February 28, 2015 at 9:05 am #

      Hi Matt, thanks for the make -j4 tip! I left it out of the original post so I could do a single core comparison to the B+. But you’re absolutely right, using -j4 will dramatically speed up the compile.

      Also, the reason virtual environment is used is so we can have different “environments” for each project that won’t conflict with each other. For example, you may be working on a legacy project that requires a very early version of NumPy. But your newer projects require the latest version of NumPy. Using virtual environments allows you to create containers for each of your projects where libraries do not overlap and cause versioning issues.

  12. dim February 27, 2015 at 7:58 pm #

    Hi Adrian,

    Thanks for the great tutorial! Do you think it would be possible to use a GoPro instead of whichever camera you are using? I’m assuming the processing will be a lot slower, but I’m probably fine with 1-2 fps. I know it’s possible to get composite out from the USB since it uses a 10 pin USB as opposed to the usual 5 pin USB. I can then use a composite to USB to connect to the RPi. I’m not sure where I would go from there, however. Do you have any pointers?


    • Adrian Rosebrock February 28, 2015 at 9:03 am #

      Hi Dim, that’s a really great question. I personally have never used a GoPro before, so perhaps I’m not the best person to answer that question. Perhaps start with this forum post?. Best of luck and be sure to let me know how it goes!

      • dim February 28, 2015 at 3:04 pm #

        Hi Adrian,

        Unfortunately, I will not be using wifi to connect to the GoPro. I have decided to look into using the HDMI port instead. Will something like this:


        • Adrian Rosebrock March 1, 2015 at 10:59 am #

          Hi Dim, sorry I can’t be of much help, but the only camera I have used in the standard Raspberry Pi camera so I can’t honestly say if that approach will work or not.

  13. Skuli Oskarsson February 28, 2015 at 7:06 am #

    Great Tutorial, thanks

  14. alex March 3, 2015 at 11:01 am #

    could you guys kindly provide some help on this step? I m a very beginner in linux.
    “update your ~/.profile file to include the following lines:” in step 7

    I have tried “vi ~/.profile” to edit the file.
    However, it seems that it is empty file and when i finished editing, i failed to save.

    • Adrian Rosebrock March 3, 2015 at 12:04 pm #

      Hi Alex, you save a file in vi after editing you need to press escape, followed by : and then wq to save and exit the editor. I would suggest reading up on some vi basics.

      • krzivn March 19, 2015 at 5:43 am #

        I just used nano, not as l33t as vi, but way less cryptic.

  15. Technoshaman March 3, 2015 at 3:19 pm #

    I installed everythig smoothly on my new Pi 2 (new to RPi, OpenCV, and Python) thanks to your tutorial. Thank you 🙂 However, I was careless and naive to think that I could think fit all these on a 4G SD card, with 3.7G Raspbian image installed (opencv’s make failed thanks to a full SD). So I had to copy my image to an 8G SD, expand and make again. Without the quad core and the -j4 switch, I’d probably suffer for a whole day. I have 3 BeagleBone’s, but this my first, RPi. I’m glad I waited, this is a very mature and powerful platform right now. Time to get into OpenCV and Python…

    • Adrian Rosebrock March 3, 2015 at 5:37 pm #

      That’s awesome, congrats on getting all setup! And if you’re just getting started with Python and OpenCV, consider signing up for one of the free OpenCV crashes courses on PyImageSearch. But if you’re really interested in jumpstarting your education, take a look at Practical Python and OpenCV. You’ll be able to run programs like face detection, handwriting recognition, and book cover identification right on your Pi 2.

      • Technoshaman March 4, 2015 at 2:06 am #

        Thank you again. I’m really considering your courses here now.

  16. phython March 4, 2015 at 10:29 am #

    ~/.virtualenvs/cv/lib/python2.7/site-packages $ workon cv
    bash: workon: command not found

    work on cv is not working

    • Adrian Rosebrock March 4, 2015 at 11:58 am #

      Please see the other comments. You need to source ~/.profile before using the workon command. If you do not want to use the source command, then I suggest putting the virtual environment scripts inside your .bashrc file.

      • Vishaal March 5, 2015 at 6:16 am #

        Hi Adrian,

        I was facing a similar problem. Just finished installation and I shut the LXterminal and opened it again to test it out (So, I’m at the home directory). I typed in the following:
        1. source ~/.profile
        2. workon cv
        I’m still getting the error saying command not found. Do i have to type in script after being in a certain directory or is something else wrong?

        • Adrian Rosebrock March 5, 2015 at 6:48 am #

          It sounds like your ~/.profile file was not updated correctly with the virtual environment export path.

          • Miguel Amezcua March 23, 2015 at 1:41 am #

            and what can we do if we have this error??

          • Adrian Rosebrock March 23, 2015 at 7:35 am #

            You can open up your .profile using vi ~/.profile (or whatever you prefer) and copy + paste in the following contents:

            Then save and exit the editor. Please see Step 7 for more details.

          • aaron April 1, 2015 at 12:45 am #

            Had something similiar happen to me. The workon cv command worked from the bare terminal, but would not work once I startedx. running the source .profile command fixed it. Not 100% sure why it didn’t just work though.

          • mobin July 23, 2015 at 6:14 am #

            Hi Adrian
            I have it problem,How ~/.profile file updated correctly?

          • Adrian Rosebrock July 23, 2015 at 6:40 am #

            Hey Mobin, all you need to do is load the ~/.profile file in your favorite text editor and copy in paste the lines from Step 7. You can open the file using editors such as vi, nano, etc. For example, to open the ~/.profile in nano, just execute this command:

            $ nano ~/.profile

            And you’ll be able to edit the file.

  17. mohamad March 5, 2015 at 2:11 am #

    Hello Adrian
    To Do this, need to install vision software such as guvcview or luvcview? If not how to open the image in Open CV ?

    • Adrian Rosebrock March 5, 2015 at 6:45 am #

      Hi Mohamad, no, you do not need to install other vision software if you do not want to. It sounds like you are just getting started using OpenCV, so I would suggest taking a look at this blog post on OpenCV basics.

  18. Vishaal March 5, 2015 at 2:54 am #

    Hi Adrian,
    The tutorial was awesome because it was very simple and easy to follow. I had a query regarding the ‘make’ step. I had started the building process when I came across the comment regarding the option of using multiple cores. If I were to terminate the building and start with ‘make -j5’ would it work? Or will have to delete everything built till now and start over again from the beginning?

    Thanks in advance!

    • Adrian Rosebrock March 5, 2015 at 6:47 am #

      Hi Vishaal, that’s a good question. Theoretically you should be able to terminate the build and restart with make -j5. However, due to compilation dependencies I would play it safe and do make clean followed by make -j5. The compilation should only take roughly 45 minutes.

      Also, only do this if you’re using a Raspberry Pi 2. The B+ does not have multiple cores and thus you can’t take advantage of making the compile process parallel.

      • Vishaal March 5, 2015 at 2:31 pm #

        Ok, definitely sticking to the safer side and yes I am using an RPi.

      • Helmut March 5, 2015 at 6:11 pm #

        The prior messages refer to “make -j5”.

        But Matt Kraemer wrote in an earlier message:

        I believe that the Raspberry Pi2 is a quad core.

        So shouldn’t it be j4 instead of j5?

        @Adrian: I’m loving your tutorials.

        • Adrian Rosebrock March 5, 2015 at 6:46 pm #

          Yep, you’re exactly right. I prefer to use -j3 instead, leaving one core open to the OS for other tasks. Otherwise you run into the problem of too much context switching.

  19. Vishaal March 5, 2015 at 2:30 pm #

    Hello Adrian,

    I was retrying your procedure and am facing a peculiar problem. While installing pip if I type in exactly what you’ve said I get an error saying ‘unable to resolve the bootstrap host ‘whereas i have confirmed that the link is active. The link refuses to open on the RPi 2’s browser as well but opens perfectly on a Windows browser. Any ideas as to what may be the issue?


    • Adrian Rosebrock March 5, 2015 at 2:34 pm #

      Hi Vishaal, I honestly have never ran into that error before, that’s very strange.

  20. Rob Stevens March 10, 2015 at 1:28 am #

    If I want to use this locally compiled version 2.4.10 outside of the CV virtual environment… how would I install it.

    • Adrian Rosebrock March 10, 2015 at 7:16 am #

      OpenCV itself is actually not installed in the virtual environment (it’s only symbolically linked there). If you open up a Python shell (again, assuming you are not in a virtual environment) and type import cv2, OpenCV will import. However, if you go this route make sure you install the latest version of NumPy using pip install numpy to install the latest version of NumPy system-wide.

      • Nishanth April 5, 2015 at 11:39 am #

        Hi Adrian,
        The installation went fine thanks to your tutorials, however if i open Python shell and use the command “import cv2” I am getting this error
        “Traceback (most recent call last):
        File “”, line 1, in
        import cv2
        ImportError: No module named cv2″
        What might be wrong?OR do i have to write the program in the terminal itself,I am new to RPI so kindly help.
        OR do i have to write the program in python shell and just call it using the the terminal?

        • Adrian Rosebrock April 5, 2015 at 12:56 pm #

          Hi Nishanth, please make sure you are in the cv virtual environment prior to importing cv2. As long as you are in the cv environment, you will not have to write a script. You can simply execute it from Python IDLE. Also, please take the time to read the comments on this post. A lot of other readers have had the exact same error as you. There are many steps in this process where the problem could arise, so please be sure to read through the comments and see which ones apply to you.

  21. Jon Brown March 10, 2015 at 2:35 pm #

    Hi Adrian.

    Thanks for great tutorial! I followed it and it works 🙂 You also mentioned that you are going to write tutorial on how to use PI camera module in OpenCV. Is it coming soon? Can you please briefly explain the procedure so that I can do it myself? I have some deadlines soon…

    Kind regards,

    • Adrian Rosebrock March 10, 2015 at 2:39 pm #

      Hi Jon, I’ve got a bunch of blog posts in the queue right now so I can’t give a firm timeframe regarding the camera module post just yet. The gist is that you need (1) a Raspberry Pi camera (of course) and (2) the picamera module. That will get you started.

    • Jeehyung Lee March 11, 2015 at 2:54 am #

      The way I got it working:
      1. install pi camera physically (make sure the ‘silver’ part faces away from network port)
      2. run sudo raspi-config and enable camera (may ask you to reboot)
      3. go into your virtualwrapper environment (if you went through Adrian’s tutorial above – type > workon cv)
      4. run pip install picamera (note: tells you to install with sudo apt-get install python-picamera , you could, I actually haven’t checked if it installs any dependencies but my virtualenv cv could not find the module until after I did pip install picamera. It could be my virtualenv setting…)
      5. Test / Use (instruction:

      • Adrian Rosebrock March 11, 2015 at 6:34 am #

        It does not install any dependencies, actually. 🙂

        I would also make sure you include the “array” package of picamera to ensure you have NumPy support:

        pip install "picamera[array]"

        I’ll definitely accelerate the blog posts on setting up picamera and the camera module in the schedule.

  22. William Tandy March 10, 2015 at 10:48 pm #

    Hi Adrian!

    I have already followed your tutorial to install opencv in my raspberry B+, but at the step 11 I’m stuck.
    when I executed “import cv2” it always show error “importError: No module named cv2”
    Can you help? I’m beginner on Linux..Thanks before

    • Adrian Rosebrock March 11, 2015 at 6:36 am #

      Hi William, please read through the previous comments as these comments address your problem. Your error is likely because you (1) did not update your .profile file, (2) did not source .profile after updating it, or (3) did not sym-link the cv2 module into your virtual environment.

      • William Tandy March 13, 2015 at 4:45 am #

        Hi Adrian! It looks like the problem is pip-numpy installation (numpy not installed correctly). when I re-do the step, it’s going well. Thanks Adrian, for the tutorial and the followed up..really helpful!

        • Adrian Rosebrock March 13, 2015 at 6:19 am #

          Awesome, glad to hear it!

  23. Kevin Carlson March 11, 2015 at 4:38 pm #

    Thanks for the great tutorial on installing cv!

    Other “RPi newbies” such as myself might notice that keys such as the “tilde” (~) won’t work correctly out of the box. In the US, it is necessary to modify the keyboard settings from the default Great Britain settings.

    At the command line, you can enter sudo raspi-config to get the configuration menu.

    Alternatively, at the command line you can enter

    sudo nano /etc/default/keyboard

    and change “gb” to “us”. Use Ctrl-O to save the changes and Ctrl-X to exit.

    Thanks again,

    • Adrian Rosebrock March 11, 2015 at 5:20 pm #

      Good point, thanks for sharing!

      • Kevin Carlson March 12, 2015 at 12:01 am #

        Would you consider posting SD card images on Github, with CV preinstalled?
        That could save users a lot of time, who would then have a dedicated “known good” instance of Raspbian/CV to play with…

        • Adrian Rosebrock March 12, 2015 at 6:40 am #

          Certainly, I would love to do that. I have never generated a SD card image for the Raspberry Pi though, so I would have to do some research on how exactly that is done. If you have any resources, please pass them along!

          • MIhaly Sogorka October 10, 2015 at 4:54 pm #


            thanks for the tutorial.

            Any progress with the Raspbian SD card image that includes OpenCV? Raspberry PI 2 devices are often using OpenCV so this should be part of the OS.

          • Adrian Rosebrock October 11, 2015 at 8:09 am #

            I’ve tried a few times, but the resulting .img file is near 7gb, which is too large to distribute. I am looking at alternative ways to create the .img file, or even forking the Raspbian OS itself.

        • meysam July 21, 2015 at 9:53 am #

          Hi Kevin
          do you have this now ?

  24. Jose Antonio March 13, 2015 at 1:11 pm #

    Hi Adrian,
    Thanks to take the time to make this kind of tutorial, maybe can you help me with the links of the hardware that you use with it? I will travel to USA in the next months and I want to buy the hardware that I need to follow your course.
    Thanks again.

    • Adrian Rosebrock March 13, 2015 at 1:47 pm #

      Hi Jose, the only real hardware that I use outside of the Raspberry Pi 2 itself, is the camera, which you can get off Amazon.

  25. Dim March 19, 2015 at 12:10 pm #

    Also worth mentioning that some OpenCV requires SciPy, which can be installed by:

    sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose

    • Adrian Rosebrock March 19, 2015 at 3:54 pm #

      OpenCV does not actually require SciPy, just NumPy. And it’s also a good idea to stay away from apt-get installs for the stack. Install from source to ensure the latest versions are used and your life will be much, much easier.

  26. Toon March 19, 2015 at 2:37 pm #


    I have some problems by step 7.
    Is there anybody who can help me?


    • Adrian Rosebrock March 19, 2015 at 3:55 pm #

      What are your problems with Step 7?

  27. Christian March 20, 2015 at 6:34 am #

    Thank you for this tutorial I am very interested for personal application. I followed exactly the procedure (used gedit to edit ~ / .profile file and make -j4) but after sudo make install and sudo ldconfig : /usr/local/lib/python2.7/site-packages is empty. ..
    Where is my mistake, what can I do ? Thanks in advance.

    • Adrian Rosebrock March 20, 2015 at 6:51 am #

      Hi Christian, that is indeed very strange. I would the cmake script again and see if you can figure out where OpenCV is getting installed to. Also, make sure you were in the cv virtual environment before compiling OpenCV.

      • Christian March 20, 2015 at 7:19 am #

        Hi Adrian, thank you for your fast reply.
        I just recompile, with the same result, but I just found out that everything seems in : /usr/local/lib/python2.7/dist-packages
        Did I finish the installation with that subdirectory name?

        • Adrian Rosebrock March 20, 2015 at 7:53 am #

          I’m not sure why it would be in the dist-packages directory, but if it compiled correctly and you can import the OpenCV library, I guess I wouldn’t worry about it.

          • Stephane Richardier April 18, 2015 at 7:12 am #

            Hi Adrian,
            Same thing for me, site-packages directory is empty but everything is in the dist-packages.
            I’ve modified the “ln” commands to adapt the path and it seems to work.
            Great tuto, very nice job!

          • Adrian Rosebrock April 18, 2015 at 1:34 pm #

            Awesome, I’m glad that it worked for you 🙂

  28. Govind agrawal March 21, 2015 at 1:42 am #

    Hi Adrian, there is a problem in unpacking of the files. when I was opening the file its showing “Can’t open file “/home/i/”.So I am confused , What should I do?


    • Adrian Rosebrock March 21, 2015 at 6:51 am #

      Hi Govind, that’s definitely a strange error. But is /home/i/ the actual path to your file? Because that /i/ should be /pi/ and I’m willing to be that you’re trying to unzip a file that doesn’t exist.

  29. Christian March 21, 2015 at 8:46 am #

    Hi Adrian,
    I actually compiled outside of the virtual environment cv.
    Now it works fine !
    And I look forward to more on motion detection and tracking !
    Best Regards

  30. Cameron March 21, 2015 at 5:27 pm #

    Fantastic walk-through!

    • Adrian Rosebrock March 21, 2015 at 5:37 pm #

      I’m glad you enjoyed it Cameron! 🙂

  31. Shopnodip March 23, 2015 at 12:44 am #

    after i put the “make” command as :

    (cv)pi@raspberrypi ~/opencv-2.4.10/build $ make

    The following error message comes up :

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

    Please help me out how to solve this issue…

    • Adrian Rosebrock March 23, 2015 at 7:34 am #

      Based on your error message, t sounds like either (1) you did not run cmake so the Makefile could be generated or (2) cmake exited with an error and thus the Makefile was not generated. Please investigate the output of cmake and ensure that it was properly generated.

      • Shopnodip March 23, 2015 at 11:23 am #

        Yeah it worked! 🙂 I didn’t run cmake…. Thanks a lot!

        I have one more question :

        Do I ALWAYS need to work in “cv” virtual environment while writing programs in Python + Opencv ?

        • Adrian Rosebrock March 23, 2015 at 12:53 pm #

          Yes, you should be in the cv virtual environment when writing programs for Python and OpenCV. But that’s the beauty of virtual environments — you could create a new virtual environment called shopnodip and just sym-link and into there as well! The possibilities are endless.

          • Shopnodip March 23, 2015 at 1:38 pm #

            What if I need to install packages like “matplotlib” ?

            1. Which directory should I install it in?

            2. Is it necessary to be in “cv” virtual env as well when downloading and installing the packages for Python 2.7

            Thanks in advance!

          • Adrian Rosebrock March 23, 2015 at 1:44 pm #

            If you need to install matplotlib:

            where your_virtualenv_name could be cv or your own custom virtual environment. If you are in that virtual environment, pip will only install into that virtual environment (and no others). If you’re new to virtual environments I would definitely take some time and read up on them! They are an amazing tool.

      • decepticon December 5, 2018 at 8:12 am #

        I got the same error but I couldn”t understand how I can fit it

  32. Randy March 25, 2015 at 10:33 pm #

    Hi Adrian. I have installed OpenCV successfully. Now, is it safe to remove the extraction directory and the build directory? Secondly, is it possible to use the build directory to install OpenCV on other raspbian installation so I don’t have to do another compilation? Thank you very much.

    • Adrian Rosebrock March 26, 2015 at 6:26 am #

      Hi Randy, yes, you can safely remove the build directory and .zip now. And if you are installing on the exact same Raspberry Pi model and followed the exact same install steps detailed in this post, then yes, you can save the build directory and not have to do another compilation.

  33. Scott Routley March 26, 2015 at 10:22 am #

    Fantastic Tutorial! Easily the best OpenCV installation instructions I have found, mostly because it worked the first time!

    Unfortunately I don’t understand the virtual environment very well, which is where my question stems from.

    1) I have a test script, “” that will take a picture with the raspberry pi camera. It works as expected.
    2) I have run your tutorial and after running “workon cv” the import cv2 statement works great in scripts. Your test script will give me the version number
    3) When I am in the “cv” environment, i can’t run “” as the statement “import picamera” fails.
    4) When not in the “cv” environment, I cannot the version number to print (I think this is expected behavior)

    My interpretation is that I need to install the”picamera” within the “cv” environment, but I tried that with no success. apt-get seems to recognize that it’s already installed.

    Do you have any suggestions on how to resolve this issue?

    • Adrian Rosebrock March 26, 2015 at 10:58 am #

      Hi Scott, thanks for all the comment. All you need to do is workon cv followed by pip install picamera — that will install the picamera module inside the cv environment. Next week I’ll have a blog post on exactly how to do this and access the raw Raspberry Pi camera stream using OpenCV.

      • Scott Routley March 26, 2015 at 11:30 am #

        Thanks Adrian,

        Oh I did an apt-get instead of a pip…maybe that is the issue. I’m not sure as it was late last night.

        I’ll try again when I get home and report back on the results.

        I appreciate the reply and I immensely look forward your upcoming blog post! 🙂

      • Scott Routley March 26, 2015 at 11:10 pm #

        Hi Adrian, Just to confirm the pip did work! I would have bet 1 doughnut that I had tried that already. 😉

        Thanks for your help and thank you for creating such great content.

        • Adrian Rosebrock March 27, 2015 at 6:31 am #

          Of course, happy to help! And I’m glad to hear that the pip-install worked! 🙂

  34. Max Kostka March 26, 2015 at 3:35 pm #

    Hi Adrian,
    I can totally agree with everbody else. Thanks for this great tutorial.
    I have been working with the old raspberry and opencv before, but this is the best tutorial i have come across. Great work!

    • Adrian Rosebrock March 26, 2015 at 4:17 pm #

      Thanks Max, I really appreciate that 😀 Check back on Monday for another post on the Raspberry Pi!

      • Max Kostka March 29, 2015 at 7:30 am #

        I will! And after taking a look at the sample chapter of your book and considering this great tutorial i ordered a copy, hopefully this will give a boost to my project, the mice detecting cat flap 🙂

        • Adrian Rosebrock March 29, 2015 at 11:24 am #

          Thanks for picking up a copy Max! 😀

  35. Harry March 29, 2015 at 6:03 pm #

    Hi, Adiran
    I have an issue. I was fine after installing Opencv following by your tutorial. However, after i reboot raspberry pi, i can not get into virtual environment any more.
    What i did:
    log in pi
    type : source ~/.profile
    type : workon
    -bash: workon: command not found
    ( it is showing now.)

    Do you have any solution for this then i will be vary happy.

    • Adrian Rosebrock March 30, 2015 at 6:44 am #

      Hi Harry, it sounds like you didn’t update your .profile file with the paths for virtualenvwrapper. Go back to Step 7 and ensure your .profile file is updated correctly.

  36. Nagaraj April 3, 2015 at 4:46 am #

    Hi Adrian,
    In the final step,we tried to import cv2 after the python instruction, but we are getting an error saying “importError: No module named cv2”
    what should be done to resolve this.kindly help ASAP.

    Thank you

    • Adrian Rosebrock April 3, 2015 at 5:19 am #

      Hi Nagaraj, please see the other comments on this article. Your error could be any number of issues. Did OpenCV install correctly? If so, did you edit your .profile file correctly? Other readers have had similar problems so please take the time to read through their solutions as well.

  37. Shivam Gargshya April 3, 2015 at 8:06 am #

    I followed all the steps mentioned above still not able to start virtual environment with workon cv.when I type workon cv following error message is displayed.
    ~bash:workon: command not found.

    • Adrian Rosebrock April 3, 2015 at 12:33 pm #

      Hi Shivam, take a look at the rest of the comments on this article. Make sure you have properly updated your .profile file and that virtualenv has been properly installed. There are many steps that things could have gone awry.

  38. Nagaraj April 3, 2015 at 11:43 am #

    Hi Adrian, OpenCV was installed successfully,However when I tried to edit the .profile in the vi window I am getting an error saying “~/.profile E212: Cant open file for writing” so i just edited it in the .profile text window. Is that the problem? If so how to should i get it done in the vi window?

    The workon command works fine,but after the python command in step 11 if i import cv2 I am getting the same error as above.
    kindly help
    Thank you

    • Adrian Rosebrock April 3, 2015 at 12:35 pm #

      I have honestly not encountered that error before with vi. From what I understand that is a permission issue? You should look into the problem of why you cannot write to the .profile file before proceeding. Also, make sure you have sym-linked the and files as detailed in Step 10.

  39. jerryk April 3, 2015 at 4:18 pm #

    Has anyone else had an issue running get-pip/py on Step 6?

    When I run the command I get error:

    bash: $: command not found

    • Adrian Rosebrock April 4, 2015 at 7:12 am #

      Leave off the “$” from the command. The “$” is simply meant to signify the command line prompt.

  40. Nagaraj April 5, 2015 at 6:27 am #

    Hi Adrian,
    After installing the opencv-python in raspberry pi, i tried to code in python IDLE. When type “import cv2” its giving an error named as “no module named cv2”.Please help me to solve this error.
    Thank you.

    • Adrian Rosebrock April 5, 2015 at 6:37 am #

      Hi Nagaraj, other readers have had similar issues. Please read through the comments to this post and ensure you have followed all steps correctly.

  41. Nagaraj April 6, 2015 at 3:33 am #

    Hi Adrain, i tried installing matplotlib using “pip install matplotlib” however after 99% its showing an error called “Memory Error” and it fails install later.
    I still have 8gb worth of free space in my SD card.
    What might be going wrong?
    KIndly help
    Thank you.

    • Adrian Rosebrock April 6, 2015 at 7:42 am #

      If you are getting a memory error it’s because the Raspberry Pi is out of RAM, not out of space on your SD card. Installing matplotlib takes a lot of memory to compile from source — I am looking into alternatives to install matplotlib on the Raspberry Pi that do not involve apt-get.

  42. Nishanth April 6, 2015 at 4:45 am #

    Hi Adrian,
    I want to install matplotlib in raspberry pi ,so i followed your instruction that you mentioned for others in above comments but i am getting an memory error. How to overcome this problem..?
    Thank you.

    • Adrian Rosebrock April 6, 2015 at 7:43 am #

      Please see my reply to Nagaraj:

      If you are getting a memory error it’s because the Raspberry Pi is out of RAM, not out of space on your SD card. Installing matplotlib takes a lot of memory to compile from source — I am looking into alternatives to install matplotlib on the Raspberry Pi that do not involve apt-get.

  43. Neeraj April 8, 2015 at 6:24 am #

    Hi Adrian,
    Wow! Thanks a lot, struggling with open cv installation for so many days, came to your link and installed open cv successfully, This is one of the best tutorials I came across on open cv installation and I am sure the knowledge which you have on this topic will surely reflect in your book, I am now going to order your book now and want to learn more on opencv library.
    Thanks & Best Wishes

    • Adrian Rosebrock April 8, 2015 at 10:33 am #

      Thank you for the kind comment Neeraj, I appreciate it! I’m happy that I could help. And enjoy the book! 🙂

  44. ReneLunna April 9, 2015 at 6:10 am #

    Adrian, first… thanks a lot. Great tutorial.

    i dont have any experience in Linux based OS but i can follow all steps and have no errors during setup, but when i tried import cv2- in (cv)- i have the same error:
    “No module named cv2”. I read all comments 4 times and all the things sound perfect… Any idea?
    thanks again!

    ps* i have a picamera whit pip install in virtual enviroment…so…no idea.

  45. ReneLunna April 9, 2015 at 6:30 am #

    I found! i Dont do right step 10.Nagaraj, try Again step 10 and i think… your problem is solved. One Thousand thanks Adrian;)

  46. Martin April 15, 2015 at 3:57 am #

    I’m stucked at Step 7. after entering vi ~/.profile, i don’t know where to write down the stated coding.

    • Adrian Rosebrock April 15, 2015 at 9:39 am #

      Hey Martin, I’m not sure what you mean by “write down the stated coding”? You can edit the ~/.profile file with whatever your favorite text editor is.

  47. Morganna April 15, 2015 at 5:12 am #

    I’m stucked at step 7. done with the vi ~/.profile but i don’t seems to understand on how to enter the required coding you had stated

    • Adrian Rosebrock April 15, 2015 at 9:39 am #

      Hi Morganna, you don’t have to use vi, you can use whatever text editor you are comfortable with.

      • Morganna April 15, 2015 at 11:00 am #

        can i know how? please explain step 7 more because i’m new on this thing and i don’t understand how to proceed with the step

        • Adrian Rosebrock April 15, 2015 at 12:44 pm #

          Take a look at the list of available text editors on the Raspberry Pi, I’m sure you’ll be able to find one that suites your needs.

          • Morganna April 16, 2015 at 8:58 am #

            finally done with step 7, but i’m stuck at step 11 where the error stated was “no module named cv2”

          • Adrian Rosebrock April 16, 2015 at 9:09 am #

            Hey Morganna, there are a lot of reasons why the import statement could fail. You could not be in the cv virtual environment. You might not have edited the ~/.profile correctly (or reloaded it after editing). There are a lot of comments on this post about why import statement could fail, so definitely take some time and read through them to see which one is most relevant to you.

          • Morganna April 16, 2015 at 9:48 am #

            how exactly you reload the ./profile? been editing the /.profile and still the error is present

          • Adrian Rosebrock April 16, 2015 at 11:55 am #

            Please see Step 7: source ~/.profile

          • Morganna April 16, 2015 at 11:58 pm #

            i guess i have problem not at step 7, but at step 10 or 11 where i don’t know how to do the sym to do the sym link by the way?

          • Adrian Rosebrock April 17, 2015 at 7:08 am #

            Step 10 shows you how to sym-link using the ln command.

          • Morganna April 19, 2015 at 8:50 am #

            At which line shall step 7 be written down in the editor? at the first line?

          • Adrian Rosebrock April 19, 2015 at 11:53 am #

            Yes, the first line.

          • Morganna April 20, 2015 at 11:50 pm #

            Do you provide a youtube video for this post? i think i did everything (plus reinstall the raspbian just in case i made a mistake for the first 10 steps) but the error of no module CV2 still occur. i did the editing ~/.profile, resource it and also sym-link, everything. i think if you do have the youtube tutorial video will help me identify the mistake(s) i might do.

          • Adrian Rosebrock April 21, 2015 at 6:29 am #

            Currently I do not have a YouTube video, but I will definitely consider it in the future.

  48. Ishank gulati April 15, 2015 at 6:54 am #

    Hi Adrian
    Thanks a lot for this post. I have successfully installed openCV on my pi as per your instructions and I am using it in a robot. So i have to execute my python script automatically on boot but the problem is OpenCV is installed in a virtual environment and I dont know how to automatically enter in Virtual environment before executing my script. Can you help me on this, I have to demonstrate it in a few days.

    • Adrian Rosebrock April 15, 2015 at 9:38 am #

      Hi Ishank, you should be able to create a crontab entry on reboot that calls a script that (1) enters in the cv virtual environment and (2) executes your script. Do a little research on crontab on reboot and this will help solve your problem.

      • Ishank Gulati April 15, 2015 at 11:53 am #

        Thanks for quick reply. I will do the same

  49. Darren April 16, 2015 at 9:00 pm #

    I have followed each step precisely and when I get to Step 9 and type:


    I get this error:

    CMake Error: The source directory “/home/pi/opencv-2.4.10/build/BUILD_EXAMPLES=ON” does not exist.
    Specify –help for usage, or press the help button on the CMake GUI.

    Should the last part be: -D INSTALL_BUILD_EXAMPLES=ON

    What I am I doing wrong?

    Thanks everyone

    • Adrian Rosebrock April 17, 2015 at 7:07 am #

      Hey Darren, the reason you are getting the error is because you’re forgetting the “..” at the end of the command, indicating that cmake should look up one directory:


      Notice how I have the .. right after the -D BUILD_EXAMPLES=ON

      • Marcellus May 1, 2015 at 12:11 am #

        Bro, this saved my life right here!!! I thought . . was a couple of spots on my tv screen! LOL! Thanks a lot Darren and Adrian!!! Great tutorial & much, much, much appreciated!!!! You rock!

        • Adrian Rosebrock May 1, 2015 at 6:53 pm #

          Nice! 🙂

      • Péter Lehoczki April 19, 2016 at 4:59 pm #

        The exact same happened to me too, thanks a lot!

  50. Maula Ramadhan April 17, 2015 at 9:28 pm #

    Hi, Adrian.
    I’ve actually followed these steps, except that I’ve already installed the opencv before the virtualenv installed and run. Now I am having a problem that I cannot find any ‘’ or ‘’ anywhere. When I run the command

    $ workon cv
    $ python
    >>> import cv2

    it says that no module of cv2 there. where is the problem lies here? Is it because the way I’ve installde the opencv not on the (cv) state?
    Waiting for your answer soon. Thanks.

    • Adrian Rosebrock April 18, 2015 at 6:26 am #

      Hi Maula, you mentioned that you installed OpenCV prior to running these steps? Was it via an apt-get installation? If so (as far as I understand), that’s where your problem is. If at all possible I would try removing the previous version of OpenCV from your system and then re-following the steps in the tutorial. Hopefully the whole setup isn’t hosed, but you might be in a state where you need to re-install Raspbian to get everything to place nicely together again.

      • Maula Ramadhan April 19, 2015 at 1:33 pm #

        Thank you for the reply.
        Actually I installed it by downloading the package via PC (Windows) then I copied them into pi and do the rest of the steps after “wget” thing.
        But I can’t seem to get the “sudo pip install virtualenv virtualenvwrapper” the raspi says that it is downloading the package but cannot fetch the index base, then keep says that “could not find any downloads that satisfy the requirement virtualenv” –> “No distribution at all found for virtualenv”.

        I’ve tried a step where I separate the sudo command :
        sudo pip install virtualenv
        sudo pip install virtualenvwrapper
        It’s actually work, but when I’ve done with it I can use any function of python used inside the (cv) always having an error.

        so if the problem was as you said it was, i would just install my backup image (before the OpenCV installed) and did the steps all over again. There are several questions that I want to ask you.

        will it be a problem if I do the steps in ‘root’ mode?
        which one is the right state that is necessary to install OpenCV?
        “pi@raspberry ~:” or “(cv)pi@raspberry”

        I am sorry for the very long question, but I am being really curious about all this. 🙂

        • Adrian Rosebrock April 20, 2015 at 6:57 am #

          Yeah, given your situation I definitely suggest that you revert to back your backup image and follow the steps I provided exactly. It’s pretty hard to diagnose where the error would be coming from. As for being root, OpenCV needs to be installed via root anyway, so that shouldn’t be an issue. And you’ll want to make sure you are in the cv virtual environment prior to compiling OpenCV. As long as you follow the steps I detailed, your install should go painlessly.

  51. Hung April 19, 2015 at 4:14 am #

    Thanks Adrian, It is successfully installed and run ok, but after reboot, I can’t control raspberry by remote destop connection on win 7

  52. ibrahim Sholeh April 20, 2015 at 6:40 pm #

    thanks for your tutorial adrian… but, after installation, what i should to do to compile opencv with c++ ??? im just familiar with c/c++…

    • Adrian Rosebrock April 20, 2015 at 6:51 pm #

      Hi Ibrahim, thanks for the comment, but this blog focuses only on Python + OpenCV. I don’t cover compiling OpenCV apps with g++ here.

      • ibrahim Sholeh April 22, 2015 at 5:21 am #

        please adrian give me a tips… 🙁 should i install gcc and g++ compiler through apt-get install build-essential… please help me… 🙁

        • Adrian Rosebrock April 22, 2015 at 6:05 am #

          Yes, you’ll need gcc/g+++ to compile OpenCV through the build-essential package. I discuss this at the top of the article. Just follow the steps one at a time as I have detailed them.

  53. Happysangs April 21, 2015 at 6:22 am #

    I have a question.
    when I install opencv and python , I have to use only pi cam?
    can I use the web cam?

    • Adrian Rosebrock April 21, 2015 at 6:28 am #

      You certainly could, although you would have to setup and install the drivers required for the Pi to read frames from your web cam. I personally have not done that so I’m not sure about the best way to go about it.

  54. ibrahim sholeh April 21, 2015 at 8:49 pm #

    By the way, you have tested it can run motion detection project at 32 fps/640x480p on raspberry pi 2 … I think it is very fast… Does it mean opencv 2.4.10 can utilize quad core cpu of raspberry pi 2???

    • Adrian Rosebrock April 22, 2015 at 6:07 am #

      The reason the Pi 2 is able to achieve such high frame rate is because it is significantly faster than it’s predecessors. The raw processing power itself makes it more capable of higher frame rather. And since it has more cores, less time is wasted doing context switching.

  55. srikanth April 23, 2015 at 12:52 pm #

    hello adrain.i have a problem with pi using in windows through u have any idea of connecting in this way?

    • Adrian Rosebrock April 23, 2015 at 1:04 pm #

      Hey Srikanth, I only use OSX and Ubuntu, I do not have a Windows machine. Hopefully another PyImageSearcher reader can help you though!

    • Jonathan July 3, 2015 at 8:24 am #

      Hi Srikanth.

      If you have access to your Pi directly, in a terminal run ifconfig to find out the Pi’s ip address then simply connect to that via Putty. If taht doesn’t work then you may need to connect via a specific port. Try :1 after the ip address or :22 or :5901 which have all worked for me at various times. Hope that helps.

      Thanks Adrian for the great tutorial. Just working through it.

      • Adrian Rosebrock July 3, 2015 at 10:29 am #

        Hey Jonathan, the standard port for SSH is port 22, so you should always try to connect to the Pi via port 22 over SSH. SSH also defaults to port 22 so this shouldn’t be an issue. When I ssh via my terminal, my command looks like:

        $ ssh pi@pi_ip_address

        Where “pi_ip_address” is replaced with the IP address of your Pi.

  56. Cyb April 24, 2015 at 6:14 am #

    After reading all post, I wrote the new lines of .profile at the beginning instead of the end of the file, and mkvirtualenv is working now.

    • Adrian Rosebrock April 24, 2015 at 6:35 am #

      No problem, I’m glad it’s working for you!

  57. vix April 25, 2015 at 5:10 pm #

    i get reply
    -bash: cmake: comment not found
    how to solve?

    • Adrian Rosebrock May 1, 2015 at 7:08 pm #

      You need to install cmake first. Please see Step 1 of this tutorial.

      • Daniel Kim May 17, 2015 at 12:41 am #

        sudo apt-get install cmake? will that solve?

        • Adrian Rosebrock May 17, 2015 at 7:21 am #

          Yes, as per the instructions in Step 1, that command will install cmake for you.

  58. Markus Flaig April 27, 2015 at 9:56 am #

    I used “make -j4” instead of just “make”, which finished the build process in less than an hour on my Raspberry Pi 2.

  59. Tushar April 27, 2015 at 12:51 pm #

    Hello, Adrian.
    This is an awesome tutorial. I have a question. I need to use the gpio pins while I’m running the cv code. Therefore, I need to use the sudo. But it doesn’t work in the cv. Gives my a python error that cv2 is not installed. What should I do?


    • Adrian Rosebrock May 1, 2015 at 7:04 pm #

      Unfortunately, I do not have any experience working with the GPIO pins so I’m not the right person to answer this question. Hopefully another PyImageSearcher reader can jump in and help!

    • Aron May 17, 2015 at 11:40 am #

      Tushar, I’m facing the same problem. Did you find the solution?



  60. sovann April 28, 2015 at 7:29 am #

    Dear Adrian Rosebrock

    I don’t know how to do step 7 in LXTerminal. Could you screenshot the command line again, please?

    • Adrian Rosebrock May 1, 2015 at 7:02 pm #

      All you need to do is use your favorite editor, such as vi or emacs, and copy + paste the content into your ~/.profile file.

  61. SAS April 29, 2015 at 4:39 am #

    install matplotlib using pip on raspberry pi 2

    1) sudo rapi-config => memory split => choose lowest level 16 MB => reboot
    2) login to your pi => no need to startx (graphical ) yet
    3) source ~/.profile
    4) workon cv
    5) pip install matplotlib

    hope this help

    • Adrian Rosebrock May 1, 2015 at 6:59 pm #

      This is incredible. Thanks for the tip!

      • karthik December 24, 2015 at 5:16 am #

        install matplotlib on rasp 2

        I tried this and worked for me

        • Adrian Rosebrock December 24, 2015 at 12:05 pm #

          Thanks for sharing Karthik!

  62. Paula April 29, 2015 at 4:05 pm #

    Hi Adrian, thanks for the tutorial, I just have a few questions:
    1- After creating my virtual environment in Step 7, should I stay in cv for the rest of the steps?
    2- If so, why in step 11 the first line is “workon cv”, can’t I just type python after step 10?
    3- Is it necessary to reboot in the middle of the process, or just follow the steps?
    4- I’m using a 8GB SD with Raspbian, do you think it’s enough to run real-time computer vision applications or should I get a 16GB?

    (5) Sorry for bothering.
    Thanks in advance!

    • Adrian Rosebrock May 1, 2015 at 6:58 pm #

      Hey Paula, to answer your questions:

      1. Yes, you should stay in the cv environment for the rest of the steps.
      2. The workon cv step is included as a manner of completeness. If you were to reboot your system, you would need to re-enter the virtual environment using the workon command.
      3. I would only reboot when prompted to when upgrading the system in Step 0.
      4. 8gb is more than likely enough, but it’s always helpful to have extra space.

  63. HYCCC April 30, 2015 at 10:26 am #

    Hi Adrian,
    Thanks so much!! Indeed, it’s the best tutorial for installing opencv!!!

    But I have a problem and maybe you can help me to figure out, when I launch for face detection. I get this error msg ”

    GLib-GObject-WARNING **: Attempt to add property GtkSettings::gtk-label-select-on-focus after class was initialised”

    I am using 2015-2 raspibian img, is this error is a bug from the new camera module driver?
    I am looking forward to hear back from you

    Thank you very much!!


    • Adrian Rosebrock May 1, 2015 at 6:55 pm #

      The GTK error is actually related to the libgtk2.0-dev package. It will not impact your OpenCV usage.

  64. Kevin May 1, 2015 at 2:22 am #

    Hi Adrian,

    Thanks for the great tutorial. I seem to have, after following all the steps, encountered an error when trying to import cv2 in the virtual environment. I’ve read all the comments regarding the importing of cv2 but I believe I have done everything correctly. However, if this is of any use, I can successfully import cv2 when I’m not in the (cv) virtual environment (i.e. when I just launch python from bootup and import cv2). Where could the problem lie? Thanks.



    • Adrian Rosebrock May 1, 2015 at 6:52 pm #

      Hi Kevin, based on your comment it sounds like OpenCV may not be symbolically linked into your cv virtual environment correctly. Go back to Step 10 and make sure that everything is sym-linked into your virtual environment.

  65. Harini May 1, 2015 at 8:49 am #

    I have a problem with step 9 and 10, opencv not installed in the folder /usr/local/lib/python2.7/site-packages

    and i do not know why, i’ve followed the steps exactly and it shows me no module found when i write import cv2.

    please help, I’m entirely new to this.

    • Adrian Rosebrock May 1, 2015 at 6:49 pm #

      Check and see if it’s in the dist-packages directory. Also be sure to read through all the comments on this post as other readers have had similar problems.

  66. Morganna May 3, 2015 at 9:05 am #

    It works perfectly. Do you ever encounter a project that calculate the angle between two objects using these python,opencv and pi cam?

    • Adrian Rosebrock May 3, 2015 at 9:09 am #

      If you know the location of the two objects, then you an apply basic triangle properties and compute the angle between them. I do not have any code on the PyImageSearch blog on doing this, but I’ll make note about potentially doing one in the future.

      • Morganna May 4, 2015 at 4:23 am #

        i saw one post from PyImage on the distant calculation, i wonder can the idea be done in measuring the angle as well?

  67. Sergio May 6, 2015 at 12:51 pm #

    404 not found when trying to download at step 9 : (

    • Adrian Rosebrock May 6, 2015 at 2:45 pm #

      Hey Sergio, make sure you are using the entire command: wget -O

      • Sergio May 8, 2015 at 7:10 am #

        Working now thanks !

        Problem is, Im accesing my raspberry pi 2 trough SSH from my Windows 8.1 PC and every time y try executing a python script which includes cv2.imshow i get this error:

        (image:24957): GLib-GObject-WARNING **: Attemp to add propery GtkSettings::gtk-button-images after class was initialised

        Im googled it and found some solutions which did not worked for me, so right now I`m a bit stuck since I cannot even display the result of any image processing.

        Hope u can help

        Thnks !

        • Adrian Rosebrock May 8, 2015 at 8:37 am #

          Hey Sergio, please see my previous comment on this blog post: “The GTK error is actually related to the libgtk2.0-dev package. It will not impact your OpenCV usage.” Your image will still display to your screen, but you might see that error message related to GTK. And if you’re SSH’ing into the Pi, you need to make sure you are using X11 forwarding so your window will show up.

  68. Martin Mayr May 7, 2015 at 7:23 am #

    Hello Adrian,
    I followed all the 11 steps and in step 11 i’m struggling now:
    the command workon cv worked (I think) since there is no reply I don’t really know ….)
    the python command opens Python 2.7.3…. (looks fine too

    But then the >>> import cv2 replies:
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    What did I miss ????

    Thanks for any help

    Best regards


    • Adrian Rosebrock May 7, 2015 at 7:43 am #

      Hey Martin, there are a lot of things that could have gone wrong, such as not properly sym-linking the OpenCV files into your virtual environment, not updating your .profile file correctly, or even forgetting to run make install. Read through the rest of the comments on this post (I know, there are a lot of them), but they address many different areas as to where the problem could be.

  69. Martin Mayr May 7, 2015 at 10:58 am #

    Hello … I redid step 10 after deleting the files and (which were displayed red in the directory ~/.virtualenvs/cv/lib/python2.7/site_packages/ before then I recreated the ln … commands and now the two files are colored cyan …
    Since that the problem of the import mentioned before has disappeared …

    No idea why ?

    Best regards

    • Adrian Rosebrock May 7, 2015 at 12:01 pm #

      If they were red, then the sym-link did not point to an existing file. If they are cyan now, then the sym-link does point to a valid file.

  70. Andre May 9, 2015 at 1:11 am #

    Hi Adrian

    Thank you for a very comprehensive tutorial.
    I am a new to Pi & Programming. I have followed every step but when I type in: workon cv I get: workon: command not found.
    Any sugestions?

    Thank you in advance


    • Adrian Rosebrock May 9, 2015 at 7:50 am #

      Hey Andre, definitely take a few minutes and read through the comments on this post (I know there are a lot of them). Other readers have had issues like this and have posted their solutions. The main reasons why the workon fails is because (1) either your .profile was not updated correctly or (2) you did not source ~/.profile after updating it.

      • Andre May 11, 2015 at 2:58 am #

        Hi Adrian
        My mistake was to edit vi filename instead of vi ~/.profile
        Hopefully everything will work now.

        • Adrian Rosebrock May 11, 2015 at 6:20 am #

          Nice, congrats on getting it to work! 🙂

  71. Max Web May 9, 2015 at 3:49 am #

    do it also work with the Raspberry Pi B Model?

    • Adrian Rosebrock May 9, 2015 at 7:48 am #

      Theoretically yes, but I have not personally tried with the B model: only the Pi 2 and B+.

  72. leonard May 10, 2015 at 6:26 pm #

    Hello Adrian , first of all thanks a lot for this tutorial , am kinda new at raspberry and opencv and such … can you please explain how to make the step 7 am stuck at /.profile update thingy , i dont get it at all . i mean where is it how can i modify it and put those lines that you say ?
    Second after this tut i can run any code with python for color tracking and controlling 2 servos for tracking the moving color ?
    Thanks again

    • Adrian Rosebrock May 11, 2015 at 6:24 am #

      Hey Leonoard, the .profile thingy is actually a (hidden) file that resides on your disk. The ~/ is actually just shorthand for our “home” directory. We use the vi command to open up our text editor and insert the virtualenv lines. However, you could use any text editor that you want — including the graphical text editor shipped with Ubuntu. If you’re struggling a bit with vi then try using nano: nano ~/.profile — the nano editor should be a bit easier for you to use and insert the required lines.

      As for your question, once you have followed the tutorial you will absolutely be able to perform color tracking on the Pi, provided that you have a camera connected to it of course. I actually over color/object tracking inside my Case Studies my book, so that might be worth checking out as well!

  73. Sam May 11, 2015 at 12:08 pm #

    I followed your tutorial and installed openCV along with the virtual environment.

    But I want to run my python program at START-UP of the raspberry pi.

    How can I do that?

    • Adrian Rosebrock May 11, 2015 at 12:54 pm #

      Hey Sam, you’ll want to look into using crontab to schedule a task whenever you Pi boots up.

  74. sar1 May 12, 2015 at 6:00 am #


    I want to install the openCV in my raspberry pi Model B (512Mb) . Can I fallow the same steps as you mentioned in tutorial ?

    • Adrian Rosebrock May 12, 2015 at 6:41 am #

      I have never tried to install OpenCV on the Pi B, but theoretically yes, you can follow the same steps detailed on this tutorial.

  75. juan christophel May 12, 2015 at 10:36 am #

    hey buddy, it’s works, so helpful for me.. i so appreciate.. thank you so much..God bless you more

    • Adrian Rosebrock May 12, 2015 at 8:45 pm #

      Very nice Juan, I’m happy it worked for you! 🙂

  76. Luiz Felipe May 12, 2015 at 11:15 pm #

    Hi there! Thanks for this excellent guide…worked flawlessly.

    The only question I have is if it’s possible to run the “source ~/.profile” and “workon cv” commands on boot/startup.


    • Adrian Rosebrock May 13, 2015 at 6:40 am #

      Nice! I’m glad the guide did the trick for you. As for your question, I would suggest putting the contents of ~/.profile followed by workon cv inside your ~/.bashrc file. Anytime you startup, reboot, or open a terminal window the configurations in .bashrc will be executed.

      • Luiz Felipe May 14, 2015 at 5:06 pm #

        Hello, Adrian. Sorry for my delayed feedback.

        Thank your for the reply, it worked worked exactly as I expected/wanted.

        Unfortunately, I thought this trick would solve my main issue, but it didn’t.
        The python script works perfectly when I launch it directy from the terminal, but I need to execute it indirectly, using a bash file e,g or more specifically (my project), I need to run it from the Mathematica/Wolfram software. This sofware has a function called Run[], and it works with any other shell command.

        Can you help me?


        • Adrian Rosebrock May 15, 2015 at 7:27 am #

          I’m actually not familiar using Mathematica, but if you want to launch your Python script from a bash file, you would follow the same process. Just have the bash script call the workon cv before calling your Python script. That will make the bash session drop into the cv virtual environment prior to calling your Python script.

  77. Andrew Stroz May 14, 2015 at 8:13 am #

    Hey Adrian, do you have have some recommendations on how to get this working with python 3 and opencv 3?

    • Adrian Rosebrock May 14, 2015 at 9:06 am #

      Hey Andrew, I actually have not tried to setup OpenCV 3 on the Raspberry Pi just yet. OpenCV 3 is still in RC phase and until the final version is fully released I’m not planning on doing any tutorials on OpenCV 3 and Python 3. But in general, you should be able to follow the steps I outlined, only using Python 3.

      As a side note, if you decide to go with OpenCV 2.4.X, you’ll need to use Python 2.7 as OpenCV 2.4.X does not support Python 3.

  78. Akshit Tyagi May 16, 2015 at 12:39 am #

    Hey. I am new to RaspPi B+ . In my installation process the file ~/.profile doesnt exist and hence I couldnt update it to new virtualenvs. What should I do?

    • Adrian Rosebrock May 16, 2015 at 7:18 am #

      If the ~/.profile file does not exist, just create it.

  79. eliaz May 17, 2015 at 8:30 am #

    Bello Adrián! Thank you so much for the tutorial ¿ this aplication, open cv & phyton is it compatible with banana pi m2?

    • Adrian Rosebrock May 17, 2015 at 9:31 am #

      Hi Eliaz, I do not own a Banana Pi M2 so I have not been able to test out this tutorial with it. Perhaps another PyImageSearch reader can help you with this question.

  80. Dot May 18, 2015 at 2:26 pm #

    Question: I am successfully going into my virtual environment with the commands:
    $ source ~/.profile
    $ workon cv

    However I have to type
    $ cd ~/.virtualenvs

    in order to import cv2 in python. Additionally, this means that I can’t access any of the sample codes stored in the directory

    because when I try to run a sample script (for instance python I get the error
    ” File “”, line 4, in
    import as cv
    ImportError: No module named

    Any ideas? Thanks!

    • Adrian Rosebrock May 18, 2015 at 2:35 pm #

      Hey Dot, you definitely should not have to be in the .virtualenvs directory to import OpenCV. Go back to Step 10 and ensure that your sym-links are properly setup.

  81. Austin Crane May 20, 2015 at 10:17 am #

    Thanks for this! 1+

    • Adrian Rosebrock May 20, 2015 at 11:59 am #

      No problem Austin, I’m glad you found it useful!

  82. phu May 20, 2015 at 1:08 pm #

    i use GPIO library, but GPIO.setup running as root. Can you help me?

    • Adrian Rosebrock May 20, 2015 at 1:13 pm #

      Hey Phu, as I’ve mentioned in previous comments, I don’t have any experience with the GPIO library so I’m not the right person to ask. However, it seems to be that you should be able to execute the commands detailed in this tutorial under the root account rather than the pi user and they should still work. That would also allow you to install GPIO under root.

  83. abanoub emil May 21, 2015 at 9:14 am #

    hi Adrian i have a problem when i write command workon cv it say command not found .thanks !

    • Adrian Rosebrock May 21, 2015 at 10:01 am #

      Take a look at Step 7 again. You likely did not update your .profile file or source it after updating.

  84. Nithin May 22, 2015 at 4:40 pm #

    Hi Adrian,

    Thanks for your awesome detailed installation guide.

    I have followed all of the steps and finished the make install, only to find that the and the are in the dist-packages folder and not the site-packages folder. And this causes the import cv2 command to fail. Is this then a simple copy and past into the site-packages folder or have done something wrong.

    I have an updated ./profile file and and am in cv.


    • Adrian Rosebrock May 23, 2015 at 7:13 am #

      It’s strange that the binaries are in the dist-packages directory. I would suggest going back to Step 10 and update the sym-links to use your dist-packages directory rater than the site-packages directory. You could also manually move the binaries into the site-packages directory.

  85. Nukul Sehgal May 26, 2015 at 10:21 pm #

    Thanks a lot for this wonderful compilation of tutorial and help provided in the comments !!

    • Adrian Rosebrock May 27, 2015 at 6:44 am #

      No problem Nukul, I’m glad it was helpful for you! 🙂

  86. nikko May 27, 2015 at 3:41 am #

    Hi adrian, how to fix this?

    ” source ~/.profile

    workon cv

    -bash: workon: command not found”

    • Adrian Rosebrock May 27, 2015 at 6:45 am #

      Go back to Step 7 and ensure (1) virtualenv and virtualenvwrapper are properly installed and (2) you have properly updated your .profile file.

  87. Joe Landau June 1, 2015 at 8:38 pm #

    In step 9, the admonition to “Important: Make sure you’re in the cv virtual environment ” will be most unclear to those who are not familiar with virtual environments. They won’t know how to “make sure”, won’t have a reference, and will have to go hunting for it.

    just sayin’

    • Adrian Rosebrock June 2, 2015 at 6:45 am #

      Thanks for the feedback Joe, you’re definitely right. I’ll make sure to make this point more clear in future revisions.

  88. Neal Yang June 2, 2015 at 11:02 am #

    cmake: commend not found

    How can I solve this problem?Thanks a lot!

    • Adrian Rosebrock June 2, 2015 at 12:45 pm #

      Please see Step 1 of this tutorial. You need to install cmake.

      • Neal Yang June 2, 2015 at 9:28 pm #

        Thanks a lot! I have another problem here, when I do the “pip install numpy” ,there is something like” insecure platform worning” message,,and the install just stopped somewhere, I can’t fix it ,have to go on the the next step and have a try . As a result, I can’t find the two files anywhere, both site and pisc-pakage…….tried several times even erase the SD card .what should I do ?Thanks a lot.I’m using pi 2 by the way

        • Adrian Rosebrock June 3, 2015 at 6:41 am #

          The insecure warning is related to the urllib3 that pip uses to download packages to install. That all said, it’s just a warning and should not stop the NumPy package from installing. NumPy can definitely take a few minutes to install, so I would make sure that it’s compiling before quitting the script.

  89. orta June 2, 2015 at 6:51 pm #

    Hi adrian~
    I faced this problem:

    ln -s /usr/local/lib/python2.7/site-packages/
    ln: failed to create symbolic link `’: File exists


    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    Could you please give me some sugguestions?

    • Adrian Rosebrock June 3, 2015 at 6:44 am #

      Please see my response to Wei’s comment (June 2, 2015 at 6:54 pm) below, he asked the exact same question.

  90. Wei June 2, 2015 at 6:54 pm #

    Hello, there are two problems for help~

    ln: failed to create symbolic link `’: File exists

    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    • Adrian Rosebrock June 3, 2015 at 6:44 am #

      If the symbolic link already exists, I assume that you tried to create it before? Just use rm in the virtual environment directory to delete the sym-link and then re-create it. Give that a try and see if it fixes your issue.

  91. spex June 3, 2015 at 8:49 am #

    Hi Adrian

    After checking on the web my 153.3kib file is a little bit on the small side, not sure how that happened, i’ve tried the instruction maybe 5 times. Anyway i downloaded it [OpenCV] in a browser 87.4 mib and copied it across, writing over the old one. Currently building, all looking well. 😉

    • Adrian Rosebrock June 3, 2015 at 9:46 am #

      Awesome, glad to hear it! Make sure you are copying the complete URL from the wget command, it sounds like only part of the URL was copied and pasted into your terminal.

  92. Mike Brandt June 3, 2015 at 9:47 am #

    Nevermind. I got it working. Thanks for such a fun a detailed tutorial.

    • Adrian Rosebrock June 3, 2015 at 8:26 pm #

      Awesome, I’m happy to hear that everything is working for you Mike!

  93. Kronos June 4, 2015 at 1:26 pm #

    Now that Open-CV has just released 3.0 with support for Python 3.0, does this process change much for installing on the RasPi using Python 3.x?

    • Adrian Rosebrock June 4, 2015 at 7:40 pm #

      It changes a little bit, mainly to the cmake command and constructing your virtual environment, but that’s it. I’ve already installed OpenCV 3.0 on my Raspberry Pi, so in the coming weeks I’ll have a new tutorial published. Be sure to look out for the blog post on Monday, June 8th that will detail the changes to the PyImageSearch blog now that OpenCV 3.0 is released.

  94. karthik June 7, 2015 at 2:36 pm #

    hey Adrian,
    I’ve have successfully installed opencv on my pi! thank you for the tutorial, I would like to know if i need to create a virtual environment every time i login to my pi to use openCV? also is there a way to use opencv in python on the raspbian GUI…?

    • Adrian Rosebrock June 7, 2015 at 2:50 pm #

      Hey Karthik, you don’t need to create a virtual environment each time you login to your pi, just use the workon command to access the cv environment, like this:

      $ workon cv

      And this will drop you into your cv virtual environment. And you can certainly access the cv environment from the GUI, but you’ll still want to use the terminal application.

  95. Myrijam June 8, 2015 at 8:52 am #

    Hear Adrian,
    thanks a lot for this tutorial – i was able to go through it without any errors. But I am stuck on 2 aspects involved:
    a) installing PyFirmata unter the virtual environment so that I can send commands to an Arduino
    b) using that virtual environment inside LDXE. I tried updating the profile as described in the posts, but it does not seem do work.

    For Firmata I used

    sudo apt-get update && sudo apt-get install python-serial git-core
    git clone git://
    cd pyFirmata
    sudo python install

    but id did not work 🙁
    Any help would be greatly appreciated… 😉 Thanks

    • Adrian Rosebrock June 8, 2015 at 9:09 am #

      Hi Myrijam, thanks for the comment. But unfortunately I’m not familiar with PyFirmata or LDXE. Perhaps another PyImageSearch reader will be able to help you out with that 🙂

      Is it possible to install PyFirmata without the sudo command and install it into the cv virtual environment? That would make OpenCV available to PyFirmata.

      If not, you can just create a new virtual environment under the root user — that way OpenCV is exposed to the root account as well.

  96. Alain June 11, 2015 at 2:38 am #

    Hi Adrian,

    I’ve been following your blog posts and it’s been great so far. However on step 9 as I was trying to setup the build (typing up line 3) it gives me a CMake Error: The source directory “home/pi” does not appear to contain CMakeLists.txt.

    What can I do to resolve this? Thanks again for sharing your knowledge.

    • Adrian Rosebrock June 11, 2015 at 6:48 am #

      Hey Alain, be sure to go back to Step 9. You need to be in the build directory before running the cmake command.

      • Alain June 14, 2015 at 6:58 pm #

        All sorted now! I did not unzip the OpenCV file. Thanks for the two tutorials I have gotten everything working. Also just bought your book to read, so far so good!

        • Adrian Rosebrock June 15, 2015 at 6:23 am #

          Nice, congrats on getting OpenCV installed Alain! And enjoy the books! 😀

  97. Igor June 11, 2015 at 2:53 pm #

    very nice! thank you very much!

    • Adrian Rosebrock June 11, 2015 at 3:31 pm #

      No problem, I’m happy it worked for you!

  98. Tyrone June 13, 2015 at 12:38 pm #

    Hello I’m new with the raspberrpi, and I’m looking forward to buying you’re book so I can understand opencv with the raspberrypi. But first I want to install open cv and test it with the pi cam. I’m stuck on Step 7 where it’s saying to update you’re -/.profile file to include the following lines:
    How do you access the -/.profile to add 1-3. I’m sure this is easy. But I can use your help.
    Thank you.

    • Adrian Rosebrock June 13, 2015 at 9:28 pm #

      You can edit the ~/.profile file using your favorite editor such as vi, emacs, or any other editor listed here.

  99. Stefano June 15, 2015 at 8:49 am #

    Hi…you are my hero?
    I have only question…
    All operation after step7 (install virtual env) must be on CV virtual env?

    • Adrian Rosebrock June 15, 2015 at 9:31 am #

      Correct, executing your Python scripts or installing new packages via pip must be done within the cv virtual environment.

      • Stefano June 16, 2015 at 6:28 am #

        Perfect Works for me….Perfect tutorial

        • Adrian Rosebrock June 16, 2015 at 6:40 am #

          Awesome — I’m happy it worked for you Stefano!

  100. Njin June 16, 2015 at 12:22 pm #

    Thank you for this tutorial page.

  101. Cathush June 17, 2015 at 8:10 am #

    hello Adrian! I followed your steps carefully but i have not updated .profile to include those two lines you mentioned in step 7. But um sure open CV compiled with in cv virtual environment. open cv got installed successfully but when i cant import cv2 in python shell. So i tried to include those two lines in the .profile later but um not sure where to put exactly because there are ‘if[]’ commands also. If i paste those lines just above the ‘if []’ will it be okay ? and i couldnt figure out how to save that file.
    Do i have to follow the other steps after step 7 again or will it work just updating .profile ?

    • Adrian Rosebrock June 17, 2015 at 8:13 am #

      I would suggest placing the edits to the ~/.profile file at the bottom. After you make the edits and save the file, you’ll need to re-run the commands following Step 7, including sym-linking the file into your virtual environment.

  102. Remi Theriault June 19, 2015 at 11:20 am #

    Hi Adrian,

    Step 10: Sym-link OpenCV into the ‘site-packages’ directory. First command (cd) results in: bash: cd: /home/pi/.virtualenvs/cv/lib/python2.7/site-packages/: No such file or directory.

    OpenCV installed in expected directory. Tried to reinstall virtualenv and virtualenvwrapper but I get: Requirement already satisfied. ~/.profile also updated successfully to include the three relevant lines, AND reloaded. Computer vision virtual environment created (command prompt displays: (cv)pi@raspberrypi ~/opencv-2.4.10/build $).

    I feel like this is a stupid mistake but can’t seem to find a solution.

    However, I noticed that files and folders containing virtualenv and virtualenvwrapper can be found in /usr/local/lib/python2.7/dist-packages.

    • Adrian Rosebrock June 19, 2015 at 11:53 am #

      That’s interesting that the virtual environment directory cannot be found. Try doing echo $VIRTUAL_ENV in your terminal while in the cv virtual environment and try to figure out where your virtual environment was installed at.

      • Remi Theriault June 19, 2015 at 12:15 pm #

        Thanks Adrian, that did it (I had made the following typo while editing the .profile file: …HOME/.virtuallenvs). I normally copy paste directly from your tutorial in my terminal to avoid this kind of mistake, but I was not able to copy paste adequately from an external application in vi (new at vi), so I had written it manually. Thanks again for this quick answer and awesome tutorial 😉

        • Adrian Rosebrock June 19, 2015 at 12:31 pm #

          No problem, I’m glad it’s working for you now! 🙂

  103. Vin July 2, 2015 at 4:04 am #

    Hi Adrian

    I finally got everything installed properly and everything works. But I have a question about the workon cv command. Is it necessary to always enable the virtual environment before using python? I see I can run python and import the cv2 without any problems, without the virtual environment.


    • Adrian Rosebrock July 2, 2015 at 6:41 am #

      Hey Vin, congrats on getting OpenCV installed and working, that’s great. When installing OpenCV, the file (your OpenCV bindings) are installed in the site-packages directory of Python that is system-wide. However, I do still recommend using virtual environments and the workon command, just in case you ever want to install any other packages into the cv virtual environment. This will ensure that your system Python is kept neat and tidy.

      • Vin July 2, 2015 at 8:21 am #

        Oh I see, so its for better practice. I was thinking without the virtual environment I wouldn’t be able to do certain things as well, but thanks for clearing that up.

        Oh one thing though, in my case the and files were installed in the dist-packages directory. I had to copy them manually to the site-packages folder in order to carry on with your steps. Would this cause any problems in future? And does it mean I did something wrong? I was so sure I followed every step you showed exactly as it were, and I did it all in a single session.

        Thanks again

        • Adrian Rosebrock July 2, 2015 at 8:28 am #

          You can do anything inside a virtual environment that you can using the system version of Python — the added benefit is the better organization, the ability to have multiple Python environments (and versions), and completely independent and non-conflicting libraries.

          As for the dist-packages question, other readers have ran into this as well. It’s definitely not a problem, just for whatever reason cmake generated build files that instructed the system to install OpenCV into the dist-packages directory rater than site-package. You likely did not do anything wrong, it’s likely just a peculiarity of your system. Regardless, you did the right thing by moving it to the site-packages directory.

          • Vin July 2, 2015 at 5:19 pm #

            Ah ok, I just found it a bit tedious repeatedly typing the source profile and workon cv commands every time i wanted to work on something. But I understand the benefit now.

            Awesome, so no problems then.

            Thank you for responding!

          • Adrian Rosebrock July 2, 2015 at 6:34 pm #

            No problem, glad to help out 🙂

  104. jack July 2, 2015 at 4:33 am #

    Hi !
    I have a little problem.
    I did the tutorial, and at the end, it worked well !
    However, I closed the window, and opened an other, and tried :
    source ~/.profile
    workon cv
    –> bash: workon: command not found

    And I do not understand why it worked one time…
    Do you have an idea ?

    • Adrian Rosebrock July 2, 2015 at 6:42 am #

      Hey Jack, that is quite strange. But it sounds like your ~/.profile file was not updated properly. Definitely check again and make sure that the changes have taken affect.

      • jack July 2, 2015 at 2:24 pm #

        What do I see Inside ?

        • Adrian Rosebrock July 2, 2015 at 5:11 pm #

          See Step 7. You should have added the following lines to the your ~./profile file so that virtualenv and virtualenvwrapper can be found:

          # virtualenv and virtualenvwrapper
          export WORKON_HOME=$HOME/.virtualenvs
          source /usr/local/bin/

          • Jack July 6, 2015 at 4:18 am #

            Thanks a lot ! It is working now ! Maybe, if you can write it in the step 7, as you explain perfectly for me 😉
            Otherwise very good tutorial !!

  105. Neilesh July 3, 2015 at 12:43 pm #

    Hey Adrian,
    Thank you for the tutorial. I am trying to install opencv on my B+ but on step 10 when I use the ln commands, I get the error that is not a directory and is not a directory. Any tips would help.

    • Adrian Rosebrock July 3, 2015 at 12:48 pm #

      Hey Neilesh, it sounds like the paths to your and files are incorrect. Check to see where they are installed (you should be able to find out by looking at the output of cmake). You’ll also want to make sure you run make install after OpenCV is compiled using the make command.

      • Neilesh July 3, 2015 at 3:07 pm #

        Thank you for the fix. I just went back and tried make install again and it all worked.

        • Neilesh July 3, 2015 at 3:12 pm #

          Another question. What are the steps required to use a USB webcam, Logitech c525, that I got working with guvcview without drivers, with opencv.

          • Adrian Rosebrock July 3, 2015 at 4:12 pm #

            I haven’t played around with any Logitech cameras on the Pi just yet, so I’m not sure about that one. My Logitech C920 just arrived yesterday, so as soon as I get a chance, I’ll be playing around with it. Once I get a chance to set it up, I’ll have some more meaningful comments.

            EDIT: My mistake, I thought you were asking how to install the drivers to get the Pi to access your camera. If you are looking to access your webcam, take a look at any of my articles that utilize the cv2.VideoCapture function. I also detail how to access the webcam inside my book, Practical Python and OpenCV.

        • Adrian Rosebrock July 3, 2015 at 4:11 pm #

          Awesome, glad to hear it! 🙂

      • cipri_tom July 9, 2015 at 4:44 am #

        I’m sorry, I used openCV3, so there is no I can’t wait for the new tutorial haha.

        However, when I say import cv2 my python crashes with “illegal instruction” :(. Any ideas ?

        p.s. you can remove the prev comment

        • Adrian Rosebrock July 9, 2015 at 6:25 am #

          No worries! I’ll have a new OpenCV 3 install tutorial with both Python 2.7 and Python 3+ support online within the next few weeks 🙂

  106. Bogomil July 3, 2015 at 5:32 pm #

    Any plans for updating this excellent guide to include installation of openCV 3 to raspberry pi 2?

    • Adrian Rosebrock July 4, 2015 at 7:40 am #

      You bet! I’ll have a new OpenCV 3.0 install tutorial for the Raspberry Pi 2 online within the next 2-3 weeks. This guide won’t be updated, but I will be creating a new one. Keep an eye on the PyImageSearch blog as well as this page for more information on OpenCV 3 tutorials, how-to’s, and install guides.

      • cipri_tom July 8, 2015 at 9:22 am #

        Cool! I’m killing myself now with getting cmake detect everything nicely.

  107. florin July 4, 2015 at 3:42 pm #

    Cannot find the /.profile file ….

    • Adrian Rosebrock July 4, 2015 at 6:08 pm #

      Hey Florin, if your ~/.profile file does not exist, create it.

  108. Matt July 5, 2015 at 10:47 pm #

    Thanks for the tutorial and all the great Q&A! I am having a problem when running the I get the following output (“Example:2828): Gtk-WARNING **: cannot open display:” I’ve double checked my ~/.profile and ensured that I have my CV virtual environment active. I’ve also used the image viewer in X Server to view the image file to ensure it seems okay. This was a completely new install from NOOB. I’m not sure where to look from here. Thanks Adrian.

    • Adrian Rosebrock July 6, 2015 at 6:16 am #

      Hey Matt, take a look at the other comments on this thread. You’ll need to SSH into your Pi with X11 forwarding. Your command should look something like this:

      $ ssh -X pi@your_pi_ip_address

      Notice how i have specified the

  109. PABLITO July 7, 2015 at 9:01 pm #

    Hi Adrian, I want to use RPi.GPIO library with opencv but I can’t , I need to access as root but if I do that I can’t use opencv.
    I have this error:

    RuntimeError: No access to /dev/men. Try running as root!

    • Adrian Rosebrock July 8, 2015 at 6:15 am #

      Hey Pablo, take a look at the other comments on this post. A few other readers are looking to have access to GPIO and OpenCV via root as well. I personally do not have any experience with GPIO so I unfortunately cannot address that question. But I can help with OpenCV: In general, just follow the same install process, but perform the steps under the root account and in the /root directory — that way you’ll have an OpenCV install setup for root.

      • PABLITO July 19, 2015 at 2:23 pm #

        Hi Adrian, I’m so grateful for your answer because everything worked fine, i could join RPiGpio library and Opencv. I have another question about how eliminate noise in an image.I’m working with video stream and i need to recognize three figures (square, circle and triangle) in a not controled environment. so I have noise if the brightness change. I’m working with hsv space color and i separate “h” canal to do a mask.
        I’m sure that you know how can i solve my problem.

        Again Thanks for the last answer.

        • Adrian Rosebrock July 20, 2015 at 8:38 am #

          A Gaussian blur is normally used to reduce noise. After reducing noise, I would probably suggest using a simple contour approximation to identify each of the shapes.

        • Izwan August 27, 2015 at 7:22 am #

          Can you explain how to do it? join RPiGpio library and Opencv

          • Adrian Rosebrock August 27, 2015 at 6:26 pm #

            Please see the other comments on this blog post related to GPIO and OpenCV as I’ve answered this question multiple times. All you need to do is launch a root shell, create the virtual environment under the root account, and then install the GPIO library.

          • Izwan August 28, 2015 at 8:02 pm #

            In general, just follow the same install process, but perform the steps under the root account and in the /root directory — that way you’ll have an OpenCV install setup for root?

            then install gpio library?

            i will try this but something to make sure, this will make opencv working together with gpio?

          • Adrian Rosebrock August 29, 2015 at 7:07 am #

            Yep, that’s correct! And if you already have OpenCV installed, all you need to do is create a virtual environment for the root user, install NumPy, and then sym-link the file into the site-packages of your virtual environment. Lastly, you can install GPIO as well and you’ll be able to use both GPIO and OpenCV together.

  110. mizz1205 July 9, 2015 at 2:53 am #

    Hello,please help me.
    I am step9 did $make, however I have opened new window of raspberry LXterminal before this step and started step9.
    Is this mean that I am not in cv virtual environment? How do you know if you are in virtual CV?

    (your comment…)
    “important: Make sure you’re in the cv virtual environment so OpenCV is compiled against the virtual environment Python and NumPy. Otherwise, OpenCV will be compiled against the system Python and NumPy which can lead to problems down the line.”

    Raspberry is now doing compiling like below..

    “[ 5%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/undistort.cpp.o
    [ 5%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/thresh.cpp.o
    [ 5%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/sumpixels.cpp.o”

    I am wondering if I am on right truck.

    • Adrian Rosebrock July 9, 2015 at 6:27 am #

      Anytime you open up a new terminal, login, reboot, etc. you’ll need to ensure you are in the cv virtual environment by using the following command:

      $ workon cv

      If done correctly, your terminal will change slightly and you’ll see the text cmake inside the cv virtual environment — you can run make outside of the virtual environment, that shouldn’t be an issue.

  111. Jack July 13, 2015 at 10:34 am #

    I would do a dist-upgrade in step zero rather than an upgrade. The difference is that dist-upgrade will remove packages and update all packages.

  112. Girish Kumar July 16, 2015 at 6:40 am #

    Hi Adrian,

    HAve you tried Open CV on Raspiberry PI-2 B+ with more than one Web-cam ? I would like to try this out, just want to check whether you had attemted this before or not

    Purpose is just motion detection


    • Adrian Rosebrock July 16, 2015 at 7:37 am #

      Hey Girish, I have not used the Raspberry Pi with more than one webcam. But it should be pretty simple using the cv2.VideoCapture function. You can use cv2.VideoCapture(0) to access the first webcam, cv2.VideoCapture(1) to access the second, and so on.

  113. Nichten July 17, 2015 at 4:48 pm #


    I have done all steps twice and still get nothing. I have clear install of Raspbian and every thing works fine if i have close terminal window. after closing it and reopen i cannot type workon cv. I have tried everything what was said in comments. What i am doing wrong?

    I have also tried creating new virtual enviroment named cv and i get error that comment not exist :/.

    • Adrian Rosebrock July 18, 2015 at 7:35 am #

      Hi Nichten, it really sounds like your ~/.profile file was not updated correctly. Go back to Step 7 and ensure you have (1) updated it correctly and (2) have reloaded it using the source command.

      • Nichten July 18, 2015 at 8:03 am #

        Unfortunately I have tried it again :/. I have no idea what to do more… I have also tried modifying ~/.profile with vi and nano but nothing helps me…

        • Adrian Rosebrock July 19, 2015 at 7:47 am #

          Hi Nichten, this could be a silly question, but have you made sure virtualenv and virtualenvwrapper installed? If after updating your ~/.profile file with the correct contents (and you still can’t access the virtual environment), then the only remaining suggestion is that virtualenv and virtualenvwrapper have not been installed correctly.

  114. Mesut July 19, 2015 at 7:15 pm #

    Hello Adrian
    I preformed all steps by copying the exact bash codes you mentioned above. step by step without modifying every single word, I even compiled the openCV library three times but when I’m going to give a test it says:

    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    I’m now going to hit my head to a wall! I can’t realize what’s the problem could you please help me?

    • Adrian Rosebrock July 20, 2015 at 8:37 am #

      Hey Mesut — please see the other comments on this blog post. The most likely cause is (1) your ~./profile was not updated correctly, (2) the ~/.profile was not reloaded, (3) the and files were not correctly sym-linked into the cv virtual environment, and (4) not being in the cv virtual environment when trying to import OpenCV. Please see the other comments for more information on each of these cases.

      • Mesut July 20, 2015 at 12:26 pm #

        Thanks and hi again, I almost read all of the comments on this page, and I updated my profile as you mentioned. Used source ~/.profile many times and and are both symbolic-linked perfectly because now I tried again and it said: “failed to create symbolic link `’: File exists”
        I have done these all in cv virtualenv because the workon command works correctly and I see a ‘(cv)’ right behind of my username shown in PuTTY.
        Also after opening ‘Python->help()->modules()’ there are cv and cv2 modules but still can’t import’em 🙁
        So what’s wrong with this?

        • Adrian Rosebrock July 20, 2015 at 7:29 pm #

          Hey Mesut, I’m sorry to hear about the continued problems, that’s super frustrating. It definitely sounds like your virtual environments are working which is a great start. If the symlink already exists, I would suggest deleting it and ensuring the path to the files are indeed correct. It could be possible that the and files could have different paths on your system.

  115. Josh July 20, 2015 at 6:07 pm #

    Hello Adrian,

    Very nice tutorial, thank you for doing this. When on step 11, I get the error “bash: workon: command not found.” I have tried in the ~./virtualenvs/cv/lib/python2.7/site/packages cd and in the regular pi@raspberrypi – $ cd. I have also tried with source ~/.profile. Any help is greatly appreciated. Thank you.

    • Adrian Rosebrock July 20, 2015 at 7:31 pm #

      Hey Josh — please see the other comments on this blog post, other readers have had similar problems. In most cases the ~/.profile has not been updated properly.

      • Josh July 21, 2015 at 11:10 am #

        Hello Adrian,

        Thank you for the response, I was able to load the workon command and work withing the cv. However, now when I try to load ~/.profile, it tells me permission denied. Any thoughts?

        • Adrian Rosebrock July 21, 2015 at 2:20 pm #

          It sounds like you may had edited and saved the file as root. You’ll likely need to change the permissions of the file back to the pi user. Read up on the chown command for more information.

          • Josh July 21, 2015 at 6:39 pm #

            Hello Adrian,

            Don’t know how I messed up, but I ended up formatting the SD card and starting from scratch. Got all the way through with no hiccups this time. Thanks a lot for your help! Looking forward to ordering the camera and setting up the motion detector.

          • Adrian Rosebrock July 21, 2015 at 6:55 pm #

            Nice, congrats on getting it to work!

  116. Sahil Naran July 20, 2015 at 10:18 pm #

    This is very strange, I could not find my and file in neither the dist-package nor the site-packages
    However, I located in /home/pi/opencv-2.4.10/build/usr/local/lib/python2.7/site-packages/
    I then tried did step 10 the symbolic linking with /home/pi/opencv-2.4.10/build/usr/local/lib/python2.7/site-packages/
    and it worked.
    Will this solution lead to problems ahead?

    • Adrian Rosebrock July 21, 2015 at 6:37 am #

      That will most certainly work; however, if you ever delete your opencv-2.4.10 directory than your symlinks will be invalid (since the files they point to don’t exist). In your case, I would suggest explicitly copying the and files into the site-packages cirectory.

      • Sahil Naran July 21, 2015 at 6:59 am #

        Thanks for the speedy reply.

  117. Joseph July 21, 2015 at 5:54 am #

    Hello Adrian! Thanks for your unique way of passing information efficiently to your readers. However, at step 10, the command cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ it said no such file or directory but when i checked the path cv was installed in it says the directory exists. I’m wondering why i cannot utilize the cv virtual environment.
    also, the workon cv command did not work.

    • Adrian Rosebrock July 21, 2015 at 6:34 am #

      Hey Joseph, I am sorry to hear about the error. However, I would take a few minutes and read the rest of the comments on this post. If the workon command does not work, then it’s likely that either virtualenv and virtualenvwrapper have not been installed, or your ~/.profile has not been properly updated, or it needs to be reloaded.

  118. jack July 22, 2015 at 9:47 am #

    hello adrian,
    i have a problem, i can not import “serial” library in opencv virtual environnement. Do you know why ?

    have a good day !


    • Adrian Rosebrock July 22, 2015 at 9:26 pm #

      Hey Jack, I don’t use the serial Python package, but it sounds like you have not installed it inside the cv virtual environment. Popping into the cv virtual environment and then installing it via pip should take care of the issue:

      $ workon cv
      $ pip install pyserial

      • Jack July 24, 2015 at 10:48 am #

        Thanks a lot Adrian ! It is working ! 🙂
        Have a good day !

  119. John July 23, 2015 at 2:17 am #

    hi Adrian, I have installed openCv sucessfully with this tutorial! I want to implement opencv with a PWM driver which uses smbus module. However, the virtual environment which opencv was operating in does not include smbus module.
    Is there a method to add smbus into existing virtualenv?

    • Adrian Rosebrock July 23, 2015 at 6:42 am #

      Hey John, take a look at the other comments on this blog post. I would also read up a bit on Python virtual environments.

      The cv virtual environment is entirely independent of your system install and you can install whatever Python packages you want into it. I have never used smbus before, but if I wanted to, I would just enter the cv virtual environment and let pip install it:

      $ workon cv
      pip install cffi

      • John July 23, 2015 at 10:08 pm #

        Hey Adrian! I searched for a solution for days, and your reply just inspired me to reach a solution!! Many thinks!

        The code I have used:
        $ workon cv
        pip install smbus-cffi

        • John Tran January 28, 2016 at 1:52 am #

          Mine doesn’t work until I’ve used:

          workon cv
          sudo apt-get install libffi-dev
          pip install cffi


  120. Antonio July 23, 2015 at 6:16 am #

    The workon command does not work

    • Adrian Rosebrock July 23, 2015 at 6:38 am #

      Hey Antonio, please see the other commends in this post — there are many reasons why the workon command is not working, the most likely ones being that your ~/.profile file has not been updated and reloaded.

  121. Cédric Verstraeten July 23, 2015 at 10:16 am #

    Instead of motion you can also use, it’s also open-source and a lot more user friendly to install and configure. You can find more information on the website.

    • Adrian Rosebrock July 24, 2015 at 6:09 am #

      Wow, Kerberos is pretty awesome — thanks for mentioning it.

  122. Mike July 23, 2015 at 1:19 pm #


    Very nice tutorial. All set up perfectly. Is there any way to set it up so i can leave it in my house to record all day and have access to the live feed via cell phone?


    • Adrian Rosebrock July 24, 2015 at 6:08 am #

      Hey Mike, that’s certainly possible! You might want to take a look at this post on home surveillance to get you started.

      • Mike July 24, 2015 at 11:28 am #

        Hi Adrian,

        This info is awesome. Thanks a lot! Just a few questions, nothing to pressing. 1) is there a certain order I should follow? In the article you directed me to, there were like 5 links to other pages all setting this up, Should I do 1 thing before the next? I don’t want to mess the order up and screw everything up. 2) is it possible to set this up BEFORE getting my camera? I am about to order it, but would like to plug and play. If I have to wait to install the camera prior to coding, that’s perfectly fine, just want to make sure. Thanks a lot for your help.


        • Adrian Rosebrock July 24, 2015 at 3:06 pm #

          Hey Mike, the first step you’ll need to do is setup your Raspberry Pi and the camera module. This step needs to be done first, you really can’t move forward without first installing OpenCV on your Pi and confirming you have access to the camera. Form there, the tutorial is entirely self-contained and you’ll be able to follow it straight through.

  123. Talha July 28, 2015 at 2:16 am #

    Hey man thanks for the tutorial.

    Running into an issue when running the long cmake command. I made sure to have the two dots to check in the previous dir but it says “/home/pi” does not appear to contain CMakeLists.txt

    Went back up to step one and tried reinstalling it but realized im getting two errors like so:

    E: Unable to locate package build
    E: Unable to locate package essential

    Any help?

    • Adrian Rosebrock July 28, 2015 at 6:32 am #

      Hey Talha, I would suggest going back and making sure you are copying and pasting your commands correctly. There is supposed to be a dash in between the name: build-essential, not build essential.

  124. Paul Conroy July 28, 2015 at 10:05 am #


    Awesome tutorial! I’ve never used a Raspberry Pi before and am new to Linux, but the whole install and setup ran pretty seamlessly.

    One thing I note, for other users. I didn’t have a USB keyboard or USB mouse, so I had to do a “headless” setup of the Raspberry Pi initially. This entailed first editing the NOOBS setup SD to just select Raspberian OS and do a Silent install.

    Next, I used SSH to login to the RasPi and do the setup of OpenCV. the only issue I had was that the SSH connection kept timing out on the Make. Each time it did, I went back in and did:

    Workon cv
    cd cd opencv-2.4.10
    cd build
    cmake xxx

    and make picked up where it had left off.

    Just out of curiosity, is it possible to stop the SSH session from dying? I edited the .SSH on my Mac and the SSH_Config on the RasPi with what was supposed to be variables to send a ping every 2 minutes, but it didn’t work?


  125. Aaron July 31, 2015 at 3:50 am #


    I am having problems at Step: 9. After entering the “cmake” command with all of the options…I get a whole lot of errors of this sort:

    “CMake Error: Cannot open file for write: /home/pi/opencv-2.4.10/build/samples/cpp/CMake Files/tutorial_hull_demo.dir/build.make.tmp
    Cmake Error: : System Error: No such file or directory”

    I can’t seem to figure out what I did wrong…none of the other posts deal with this issue. Obviously I am new to Unix, but excited to get started learning OpenCV…any help would be great.

    • Aaron August 2, 2015 at 4:20 am #

      Reinstalled OS…everything working fine now. Problem solved.

      • Adrian Rosebrock August 2, 2015 at 5:23 am #

        Nice, congrats on fixing the problem Aaron 🙂

  126. Yousif July 31, 2015 at 7:28 pm #

    Hi Adrian,

    I am trying to import cv2 as shown in step 11 but the module doesn’t seem to exist. I tried running cmake in step 9 but that doesn’t seem to work either. Do you know what the problem might be?



    • Adrian Rosebrock August 1, 2015 at 6:44 am #

      Hey Yousif — there could be a lot of reasons why the module is not importing. I could read through the rest of the comments on this post and see which ones apply to you. If CMake is not working, then I would consider restarting from Step 0 and following the instructions exactly.

  127. Johnny August 4, 2015 at 9:18 am #

    Nice Adrian. I appreciate the details.


  128. Sahil Naran August 5, 2015 at 6:19 am #

    Good day
    I have a file named
    when I use : python my opencv works
    when I use sudo python I get import error: No module named cv2. I need to use sudo to control my pins

    • Adrian Rosebrock August 5, 2015 at 6:22 am #

      Please read through the other comments on this post related to GPIO and sudo. The trick is that you need to launch a sudo shell and re-create the cv virtual environment, but for the root user instead. From there, you can just open up a root shell, enter the cv environment and run python (again, as root) and the script will work for you.

      • Sahil Naran August 5, 2015 at 6:38 am #

        Thank you Sir!
        Just to be clear –
        I enter the sudo shell by using sudo su and start with step 7?
        Sorry, linux is not my strong point

        • Adrian Rosebrock August 6, 2015 at 6:27 am #

          Yes, that is correct. Start at Step 7 in your root shell, create your virtual environment, install NumPy, skip Step 9 since OpenCV is already installed on your system, and then do Step 10. When do you Step 10 make sure you are creating the sym-link in the virtual environment for the root user.

      • Sahil Naran August 5, 2015 at 10:05 am #

        In shell:
        root@raspberrypi: python works
        “Use in cv”
        root@raspberrypi: source ~/.profile
        root@raspberrypi: workon cv
        (cv)root@raspberrypi: python
        (cv)root@raspberrypi: No module named RPi.GPIO

        • Adrian Rosebrock August 6, 2015 at 6:23 am #

          You need to install the GPIO package into the cv virtual environment for the root user:

          $ pip install rpi.gpio

  129. Humberto August 11, 2015 at 12:43 pm #

    Hi, excellent tutorial!!
    I´m a question, how to setup python-smbus and 12c-tools in the virtual env?

    • Adrian Rosebrock August 11, 2015 at 2:46 pm #

      Hey Humberto — please see the previous comments on this post. I mentioned to another reader how to install python-smbus inside the virtual environment.

  130. jpiat August 17, 2015 at 9:21 am #

    To get max performance out ot the PI2 with openCV built-in functions your need to add the :


    to the Step 9 “cmake” command. On the B+ the flag :


    could also improve built-in functions results. Beware that with those flags you won’t be able to move your install from a B+ to a PI 2. Those flag can make a bigg difference depending on the functions.

    • Adrian Rosebrock August 18, 2015 at 6:49 am #

      Thanks for the tip, I’ll be sure to try these out!

    • Antonio May 12, 2016 at 8:53 am #

      Is this actual on the Pi 3?

  131. ilan August 23, 2015 at 7:10 am #

    Thanks for the incredibly in depth tutorial. If I can make one recommendation, step 7 is a bit confusing for people new to linux that might not realize it is in fact a text file that needs to be edited. At least it was for me.

    I do have a question: After completing all steps successfully, this whole installation uses up 1.9GB of space on my SD card, 1.7GB of which is used by the “build” folder we created. Is this folder still necessary after installation or can we delete this to save space? I am using an 8GB card.

    • Adrian Rosebrock August 24, 2015 at 6:45 am #

      After you have completed the installation you can delete the build directory and the source code directory itself.

  132. Tang August 25, 2015 at 7:25 pm #

    Hi Adrian,

    I can’t seem to get pass the mkvirtualenv command. Somehow it says command is not found.

    pi@raspberrypi ~ $ mkvirtualenv cv
    bash: mkvirtualenv: command not found

    *** for the moment I have skipped that step and is now at Step 9.
    How do I get the mkvirtualenv to work properly?

    Thanks in advance.

    • Adrian Rosebrock August 26, 2015 at 6:23 am #

      Hey Tang, it sounds like your .profile file was not updated correctly. Go back to Step 7, ensure it’s updated, and then run the source command. Also, you do not want to run the cmake command in Step 9 if you are not in the cv virtual environment.

    • Francois November 8, 2015 at 9:35 pm #

      Hey Tang,
      I’m having the same problem, I looked at Adrian’s response, I edited my profile but I have no idea how to reload it, do you remember how to do that?

      Also when I use this command : sudo rm -rf ~/.cache/pip
      nothing happens
      did you have the same issue?

      • Adrian Rosebrock November 9, 2015 at 6:28 am #

        Pleas see Step 7 of this post. Reloading your profile can be done via:

        $ source ~/.profile

    • PzCampus November 26, 2016 at 1:15 pm #

      make sure you are not running “source .profile” command with sudo

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

        You can use the sudo command, but I would just make sure it’s the full path to the .profile file:

        $ sudo source /home/pi/.profile

  133. haim August 26, 2015 at 2:50 pm #


    i follow the install tutorial and all went well.
    then i run the picamera tutorial, it worked well at first but after i terminate the script i got error when i tried to access the camera again.
    (not related but it was: mmal: mmal_vc_component_enable: failed to enable component: ENOSPC – if you know somethiong about this…)

    so i reboot the device and after the reboot i can’t get “workon cv” to work.
    i get error: “-bash: workon: command not found”
    i tried: “source ~/.profile” but it doesn’t help.
    this make me wonder:
    A. did all the work and hours in compiling are lost?
    B. why i need all this virtual thing?
    C. how can i fix this?

    some background:
    raspberry model B+, kernel update to 4.X, raspberian OS, working via Putty ssh and Xming.

    • Adrian Rosebrock August 27, 2015 at 6:24 am #

      That is very strange behavior — I would check your ~/.profile file and ensure the changes made from Step 7 are still present. It would be very odd, but it sounds like during the reboot the contents of your ~/.profile file were reverted or changed.

      OpenCV and the associated packages are still installed, I wouldn’t worry about that. I would just make sure that your .profile file didn’t change somehow.

  134. Won Kyoung Jeong August 28, 2015 at 12:35 am #

    Hello Adrian!

    I have a problem with step 7

    You said that “update your ~/.profile file to include the following lines.”

    # virtualenv and virtualenvwrapper
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/

    How can I do? Should I type those through python, Lxterminal??

    Where should I type those??

    • Adrian Rosebrock August 28, 2015 at 6:57 am #

      Open up your ~/.profile using your favorite text editor such as vi, vim, emacs, etc. and then insert those lines.

      • Won Kyoung Jeong August 28, 2015 at 7:04 pm #

        Oh! I’m glad to thank to you.
        I did it yesterday. And I had to know how to do with vi editor.
        So I found it and I went to step 10.

      • Won Kyoung Jeong August 29, 2015 at 7:58 am #

        Thank you very much…
        How can I operate this program ,opencv2.4.10 ?? Just through Lxterminal??
        And where did you get the

        • Adrian Rosebrock August 30, 2015 at 7:57 am #

          Yes, you use your terminal to execute Python scripts that import OpenCV. The script was created by writing a simple script to load an image from disk and display it to my screen. I would suggest reading this post to get you started.

  135. Girwan September 1, 2015 at 11:10 am #

    Hello Adrian,
    I followed your steps and completed all.Updated the ~/.profile,there are two files named cv,py and on the directory: usr/local/lib/python2.7/site-packages but when i execute pa python script i get an error ImportError: No module Named cv2.
    what should i do about it? Please help.

    • Adrian Rosebrock September 1, 2015 at 8:10 pm #

      Make sure you are in the cv virtual environment before trying to import OpenCV.

      • Girwan September 5, 2015 at 2:39 am #

        Yes,it shows the (cv)….after creating virtual environment. Then i am trying to run program from the python ide pre installed on the raspbian and get the same error.Or I should run .py file from the virtual environment itself if so how do do it please give detailed steps ..i am
        new to python and raspberry pi.

        • Adrian Rosebrock September 5, 2015 at 5:20 am #

          If you are in the cv virtual environment and still having issues importing OpenCV, I would go back to Step 7 and make sure that the and files are properly sym-linked into the virtual environment site-packages directory. It sounds like you have everything working okay, it’s just that the sym-link are not in the virtual environment.

          • Girwan September 5, 2015 at 1:57 pm #

            I got it. I was trying to run .py files from the system python IDE not the virtual (cv) environment…

            …and why don’t we install the open cv and other dev tools against the system python..??..being curious…

            And Thank you so much for great tutorial.

          • Adrian Rosebrock September 6, 2015 at 6:54 am #

            You certainly can install OpenCV and other Python packages against the system Python; however, I don’t recommend it. It’s much wiser to use Python virtual environments that are entirely sequestered from each other. For example, suppose you installed v1.8.1 of into your system Python. However, 10 months later, there is some computer vision or machine learning package that requires NumPy 1.7.8. Do you change your current system setup? Risk breaking scripts? Of course not! Just create a new virtual environment install the older verison of NumPy, and you’re ready to go!

  136. armin September 4, 2015 at 1:07 pm #

    Have anyone used sift algorithm related functions implemented in opencv in RPi2?
    Is it possible to process more than one VGA image per second with sift and keypoint matching related functions ?

    • Adrian Rosebrock September 5, 2015 at 5:30 am #

      Are you referring to the DoG keypoint detector or the SIFT local invariant descriptor? In either case, both are unsuited for real-time matching, especially with the Pi. Detectors like FAST and binary descriptors like BRIEF, BRISK, ORB, and FREAK are much better suited for this type of task.

      I think this question could also make for a good blog post in the future 😉

      • armin September 7, 2015 at 4:25 am #

        My final goal is finding matched key points between two captured images for some specific use.
        I don’t have access to RPI2 now, in fact i want to choose between some of available sbcs(beagle bone, RPI, cubie board,…) for doing this task.
        how can i decide between single and multi core processors for this task?will multi core processing be helpful for me?

        • Adrian Rosebrock September 7, 2015 at 8:20 am #

          Multi-core is really only useful if you can efficiently distribute your process between multiple tasks (obviously). When researching between the Raspberry Pi, Beagle Bone, etc. I would also keep in mind the problem you are trying to solve. If you can efficiently distribute the process over multi-cores, then the Pi 2 would be a great route to go.

          • Izwan September 17, 2015 at 4:36 am #

            How i can do features matching with this setup (opencv 2.4.10)?
            match? or…..?

            Please advice.

          • Adrian Rosebrock September 17, 2015 at 7:34 am #

            I cover feature matching applied to recognizing the covers of books inside Practical Python and OpenCV.

          • pradeep November 27, 2015 at 7:13 pm #

            Hello Adrain,

            Could you please advice, how to efficiently distribute the process over multi-cores on raspberry pi2.? If there is any video link, that would be very helpful…

          • Adrian Rosebrock November 28, 2015 at 6:48 am #

            It’s a bit of a pain, but you can use TBB when compiling OpenCV to utilize multiple-cores. Another great method is to utilize “threading” where you distribute certain processes (such as reading frames from the camera) across multiple cores. I’ll try to do a tutorial on threading and how it can optimize various operations within OpenCV in the future.

  137. mobin September 9, 2015 at 2:42 am #

    Hi adrian
    I want use autostart,i have to use it code:
    sudo nano /etc/xdg/lxsession/LXDE/autostart
    and edit:
    @lxpanel –profile LXDE
    @pcmanfm –desktop –profile LXDE
    @xscreensaver -no-splash
    @/usr/bin/python /home/pi/
    but befor run,i how ran workon cv?

    • Adrian Rosebrock September 9, 2015 at 6:42 am #

      I’m not familiar with autostart, but you just need to (1) run workon cv to drop into the cv virtual environment, (2) change directory to where your code lives, and (3) execute it.

      • John Tran January 28, 2016 at 6:08 pm #

        Hello Adrian,

        I have similar concern as mobin. I’d like to make Pi to plug & play a python script (eg. taking a photo in every a min). Normally, I need to open the terminal and type:
        @ source ~/.profile
        @ workon cv
        then I have (cv) @ : python

        Since your instructions on using opencv + python is very rock and special, I haven’t found any info to solve this problem. I’ve found many way to run a script at start up, but none about auto accessing to virtual environment.
        Would you give some advice?


        • Adrian Rosebrock January 28, 2016 at 6:22 pm #

          Having a script run at startup is actually quite simple. The first step is to create a cronjob that runs on startup. Have the job start a script named

          Inside, source the .profile, run the workon command, and then execute your Python script.

          The trick is that instead of calling your Python script at startup, you’re instead calling a shellscript.

          I hope that helps!

          • John Tran January 30, 2016 at 1:23 am #

            Thanks for great advice. I’ve tried that, but I might be having problem with opencv. I tested with many my python code, it was just working good for not using import cv2. For eg.,

            well-worked script:

            source ~/.profile
            workon cv

            NOT working script:

            source ~/.profile
            workon cv

            However, once I ran the shellcript manually (by typing /home/pi/, the 2 cases worked fine.

            Would you give some idea?

          • Adrian Rosebrock January 30, 2016 at 11:59 am #

            Check to see if your shellscript is being executed as root or the pi user. If it’s being executed as root, you’ll need to update the shellscript to switch to the pi user. From there, the source and workon commands will work.

          • John Tran January 31, 2016 at 4:21 am #

            Hi Adrian,

            I’ve finally figure out the problem. First, the method to add “source ~/.profile and workon cv” along to the script is failed since we’re using the virtual len. Here is my solution:
            create a contains those lines:

            export WORKON_HOME=$HOME/.virtualenvs
            source /usr/local/bin/
            workon cv
            python /home/pi/
            THEN go to file manager–>Ctrl + H to show hidden files. Open .bashrc file using any text editor. Add this line add the very bottom:
            source ~/
            Finally, save the file, and using the crontab to executive the lxterminal at start up.

          • Adrian Rosebrock January 31, 2016 at 8:55 am #

            Thanks for sharing John! It’s interesting that the .bashrc file had to be edited rather than .profile for this particular issue.

  138. Vivek September 10, 2015 at 3:59 pm #

    After Step 6,
    I ended up tying:
    $ sudo pip install virtualenv virtualenvwrapper
    $ sudo rm -rf ~/

    And now I cannot access anything. What am I supposed to do? Also can you elaborate upon how to edit the profile file?

    • Adrian Rosebrock September 11, 2015 at 6:30 am #

      Hey Vivek — Ouch, if you executed sudo rm -rf ~/ then you wiped out your entire home directory. It’s not a good idea to use sudo and rm in conjunction with each other unless you are sure of what you’re doing. I would suggest reinstalling NOOBS/Raspbian on your Pi at this point.

      As for editing the ~/.profile file, any text editor will do such as vi, emacs, or an editor with a GUI.

      • Tang September 12, 2015 at 3:28 am #


        I think vi is difficult for person who has just stated playing with linux.
        When I first tried to edit the .profile, I had used leafpad. But leafpad had picked up the wrong .profile file.

        I think it is best that you recommend nano…. by issuing sudo nano immediately after the LXTerminal is launched.

        … it is just my suggestion.

  139. Tang September 11, 2015 at 11:12 am #

    Hi Adrian,

    can i run python idle from the virtual environment?

    • Adrian Rosebrock September 11, 2015 at 6:19 pm #

      Of course! Just start the cv virtual environment and then open up Python:

      • Tang September 12, 2015 at 3:19 am #


        But that is not running from idle.
        That is running from the command prompt.

        When I was in the CV environment, I typed idle to launch the python idle.
        Then I keyed in
        import cv2

        and the response inside idle was that there is no such library

        • Adrian Rosebrock September 12, 2015 at 6:53 am #

          Oh, you’re talking about the GUI version of IDLE, not the command line version of IDLE. I honestly never use the GUI version of IDLE, so I’m not sure about that. If you would like to use a GUI along with the OpenCV library, I suggest installing ipython notebooks which is basically a super-charged version of IDLE that can run in your browser.

        • Oren June 1, 2017 at 2:29 am #

          when your on the workon (cv) prompt type
          python -m idlelib.idle


  140. Girwan September 14, 2015 at 11:16 am #

    Hello Adrian,
    I am facing problems on getting access to the GPIO pins from the virtual environment (cv).I am not being able to run a .py program with import GPIO as GPIO from inside the (cv) if i do not use sudo command and if i do so i can not import cv2 library…

    Thanks in advance.

    • Adrian Rosebrock September 15, 2015 at 6:07 am #

      I answer this question multiple times in multiple comments on this post. Please see the other comments. (cmd + f/ctrl + f for mentions of GPIO).

  141. Fran September 15, 2015 at 4:23 pm #

    Thanks! It was very useful. I am wondering how neat would be a tutorial using miniconda instead.

  142. Kister Genesis Jimenez September 22, 2015 at 1:56 pm #

    Have you tried adding -j4 in building openCV?
    I used “make -j4” just trying out if parallel support is available in the build script. it did not produce an error and I think it is faster than when I tried without the -j4.

    • Adrian Rosebrock September 23, 2015 at 6:43 am #

      Indeed adding -j4 during your make command can increase compilation speeds since it allows the Pi to use multiple cores.

  143. Ragav October 2, 2015 at 4:57 am #

    ur procedure worked succesfully… after doin tht i closed the terminal and again opened the terminal and did the step 11.. it shows bash: workon: command not found

    • Adrian Rosebrock October 2, 2015 at 7:05 am #

      Each time you open up a terminal you need to run source ~/.profile to ensure its loaded correctly. From there, workon will work without an issue.

  144. Ken October 7, 2015 at 2:50 pm #

    Hi Adrian,

    I have been working through your excellent tutorial on installing OpenCV with Python 2.7 on a Raspberry Pi 2. One issue that no one has mentioned is way back at step 2. When I try to install the libraries listed I get the following:

    libtiff4-dev has no installation candidate

    When I try to substitute with libtiff5-dev I run into dependency issues. I am new to the raspberry pi, but I am really surprised that no one has brought this up, since I am starting with a clean version of raspbian and I can’t believe I am the only one this has happened to.

    • Adrian Rosebrock October 7, 2015 at 3:58 pm #

      Hey Ken — are you by chance using Raspbian Jesse? I have personally not had a chance to try Jesse out yet, but I have received a couple issues about the error related to libtiff5-dev. I’ll try to get Jesse installed on one my micro-SD cards this weekend and see what the error is.

      That said, if you can install Wheezy on your card, this tutorial will work without an issue 🙂

      • Ken October 8, 2015 at 11:52 am #

        Hi Adrian,

        You were right; Raspbian Wheezy worked just fine. I originally installed Raspbian using NOOBS, and Jesse is the version that is highlighted. Its probably worth mentioning that at the start of the tutorial.



  145. Dat October 9, 2015 at 8:07 am #

    Hi Adrian
    why i can’t not “workon cv”?
    everytime i type : workon cv
    then: bash workon command not found
    i google it but cant figure that out
    would you help me?
    thank you

    • Adrian Rosebrock October 10, 2015 at 6:48 am #

      Hey Dat — I’ve covered this question many times on the comment thread to this post. Please do a ctrl+f and search for “workon” on this page. Again, I’ve addressed the many reasons why in other comments.

  146. decoo October 11, 2015 at 9:26 am #

    can you update your tutorial to work with newest rasbian image?

    • Adrian Rosebrock October 12, 2015 at 6:55 am #

      Just to clarify: are you referring to Raspbian Jesse?

  147. Bruno Cesar October 19, 2015 at 12:49 pm #

    Hey, I’d like to know if this would be possible with a USB camera.
    Where I live the CSI camera is pretty expensive, and I’d like to avoid having to buy it.

    Great post, thanks for your work.

    Bruno Cesar

    • Adrian Rosebrock October 20, 2015 at 6:15 am #

      You can use a USB camera with the Raspberry Pi, but keep in mind that only a few of them work right out of the box without installing any extra drivers. This list on verified webcams should help get you started.

  148. Rusyadi October 26, 2015 at 11:37 am #

    Hi Adrian,

    I just want to know, can I use raspberry pi and openCV for detect an object ?


    • Adrian Rosebrock October 27, 2015 at 4:53 am #

      You most certainly can! Be sure to start off by reading