1) If you're doing this on an HDMI display, you're suggested to plug in the 3.5inch display into the Raspberry Pi. After the driver is installed and the Raspberry Pi is rebooted, you can see the 3.5 screen shows the desktop of the Raspberry Pi, while the HDMI displays the CLI (command line interface).
By on April 18, 2016 in,Can you believe it’s been over four years since the was released? Back then the Pi Model B shipped with only 256MB of RAM and a 700MHz single core processor.Just over one year ago the was unleashed on the world.
And man, for something called a “Pi”, this beast made an impact on the computer world like an asteroid. $ unzip opencvcontrib.zipTiming: 43sYou might need to expand the command above using the “” button during your copy and paste. Zip in the3.1.0.zip may appear to be cutoff in some browsers. The full URL of the OpenCV 3.1.0 archive is:Note: Make sure youropencv andopencvcontrib versions are the same (in this case,3.1.0 ). If the versions numbers do not match up, then you’ll likely run into either compile-time or runtime.
Step #4: Python 2.7 or Python 3?Before we can start compiling OpenCV on our Raspberry Pi 3, we first need to installpip, a Python package manager. $ sudo python get - pip.pyTiming: 20sIf you’re a longtime PyImageSearch reader, then you’ll know that I’m a huge fan of both. Installing these packages is not a requirement and you can absolutely get OpenCV installed without them, but that said, I highly recommend you install them as other PyImageSearch tutorials in the future will also leverage Python virtual environments. I’ll also be assuming that you have bothvirtualenv andvirtualenvwrapper installed throughout the remainder of this guide.So, given that, what’s the point of usingvirtualenv andvirtualenvwrapper?First, it’s important to understand that a virtual environment is a special tool used to keep the dependencies required by different projects in separate places by creating isolated, independent Python environments for each of them.In short, it solves the “Project X depends on version 1.x, but Project Y needs 4.x” dilemma.
It also keeps your globalsite - packages neat, tidy, and free from clutter.If you would like a full explanation on why Python virtual environments are good practice, absolutely.It’s standard practice in the Python community to be using virtual environments of some sort, so I highly recommend that you do the same. $ mkvirtualenv cv - p python3Again, I can’t stress this point enough: thecv Python virtual environment is entirely independent and sequestered from the default Python version included in the download of Raspbian Jessie. Any Python packages in the global site - packages directory will not be available to thecv virtual environment.
Similarly, any Python packages installed insite - packages ofcv will not be available to the global install of Python. Keep this in mind when you’re working in your Python virtual environment and it will help avoid a lot of confusion and headaches. How to check if you’re in the “cv” virtual environmentIf you ever reboot your Raspberry Pi; log out and log back in; or open up a new terminal, you’ll need to use theworkon command to re-access thecv virtual environment. In previous blog posts, I’ve seen readers use themkvirtualenv command — this is entirely unneeded! Themkvirtualenv command is meant to be executed only once: to actually create the virtual environment.After that, you can useworkon and you’ll be dropped down into your virtual environment. Figure 3: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and need to run “source” and “workon” to resolve this issue.To fix this, simply execute thesource andworkon commands mentioned above. Installing NumPy on your Raspberry PiAssuming you’ve made it this far, you should now be in thecv virtual environment (which you should stay in for the rest of this tutorial).
Our only Python dependency is, a Python package used for numerical processing. $ pip install numpyTiming: 9m 39sBe sure to grab a cup of coffee or go for a nice walk, the NumPy installation can take a bit of time.Note: Another question I’ve often seen is “Help, my NumPy installation has hung and it’s not installing!” Actually, it is installing, it just takes time to pull down the sources and compile. The Raspberry Pi isn’t as fast as your laptop/desktop.
Step #5: Compile and Install OpenCVWe are now ready to compile and install OpenCV! Double-check that you are in thecv virtual environment by examining your prompt (you should see the( cv ) text preceding it), and if not, simply executeworkon. Figure 5: Checking that Python 3 will be used when compiling OpenCV 3 for Raspbian Jessie on the Raspberry Pi 3.Again, theInterpreter points to ourpython3. 4 binary located in thecv virtual environment whilenumpy points to our NumPy install.In either case, if you do not see thecv virtual environment in these variables paths, it’s almost certainly because you are NOT in thecv virtual environment prior to running CMake!If this is the case, access thecv virtual environment usingworkon cv and re-run thecmake command outlined above.Finally, we are now ready to compile OpenCV. $ make - j4Timing: 1h 12mNote: Compiling OpenCV in 72 minutes on the Raspberry Pi 3 is a 24% improvement over the previous 95 minutes for the Raspberry Pi 2. That extra 300MHz makes a big difference!The- j4 command controls the number of cores to leverage when compiling OpenCV 3.
The Raspberry Pi 3 has four cores, thus we supply a value of4 to allow OpenCV to compile faster.However, due to race conditions, there are times whenmake errors out when using multiple cores. If this happens to you, I suggest starting the compilation over again and using only one core.
$ rm - rf opencv - 3.1.0 opencvcontrib - 3.1.0However, be cautious with this command! Make sure OpenCV has been properly installed on your system before blowing away these directories. A mistake here could cost you hoursin compile time.
Troubleshooting and FAQQ. When I try to executemkvirtualenv andworkon, I get a “command not found error”.A.
There are three reasons why this could be happening, all of them related to Step #4:. Make certain that you have installedvirtualenv andvirtualenvwrapper viapip. You can check this by runningpip freeze and then examining the output, ensuring you see occurrences of bothvirtualenv andvirtualenvwrapper.
You might not have updated your /. Profile correctly. Use a text editor such asnano to view your /. Profile file and ensure that the properexport andsource commands are present (again, check Step #4 for the contents that should be appended to /. Profile.
You did notsource your /. Profile after editing it, rebooting, opening a new terminal, etc. Any time you open a new terminal and want to use a virtual environment, make sure you executesource /. Profile to load the contents — this will give you access to themkvirtualenv andworkon commands.Q. After I open a new terminal, logout, or reboot my Pi, I cannot executemkvirtualenv orworkon.A. See reason #3 from the previous question.Q. When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error:ImportError: No module named cv2.A.
Unfortunately, this error is extremely hard to diagnose, mainly because there are multiple issues that could be causing the problem. To start, make sure you are in thecv virtual environment by usingworkon cv. If theworkon command fails, then see the first question in this FAQ. If you’re still getting an error, investigate the contents of thesite - packages directory for yourcv virtual environment. You can find thesite - packages directory in /. Virtualenvs / cv / lib / python2. 7 / site - packages / or /.
Virtualenvs / cv / lib / python3. 4 / site - packages / (depending on which Python version you used for the install). Make sure that your sym-link to thecv2. So file is valid and points to an existing file. 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 on accessing your Raspberry Pi Camera with thepicamera 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,.
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 — to learn more about the real-world projects you can solve using your Raspberry Pi + Practical Python and OpenCV. SummaryIn this blog post, we learned how to install OpenCV 3 with either Python 2.7 or Python 3 bindings on your Raspberry Pi 3 running Raspbian Jessie.If you are running a different version of Raspbian (such as Raspbian Wheezy) or want to install a different version of OpenCV (such as OpenCV 2.4), please consult the following tutorials:.But before you goI tend to utilize the Raspberry Pi quite a bit on this blog, so if you’re interested in learning more about the Raspberry Pi + computer vision, enter your email address in the form below to be notified when these posts go live! Hi Adrian,I have a few question1.if 64 bit OS released for Pi 3 so what is 64 bit advantages?i read somewhere that you can use int64 but your program need more RAM to runbut i cant find anything that show clearly how much improve OpenCV programs speed?2.in previous tutorial you say: “In order to build OpenCV 3.1.0, you need to set -D INSTALLCEXAMPLES=OFF (rather than ON ) in the cmake command. ”but in this tutorial you not mention it.when i run make command i get error in 93% so i add this to cmake and run again.finished without error but im not sure the reason is for that.another thing i want to say,the Pi3 has the same RAM and GPU but the clock frequency of them increased so i think the 33% increasing in CPU clock is not the only improvementand i test a several code with both Pi3 and Pi2the FPS increase about 45 to 50% for mefor example,a python script with only imshow (+ your perfect imutils library) from 40 in Pi2 reached to 80 FPS! (use raspberry camera)thanks Adrian. Thanks Adrian,Following your response, I’m trying to investigate how to use multicore and multithreading on RPi with opencv to increase the performance.
Some sources say that it has to be recompiled with WITHTBB or WITHOPENMP flag enabled. My guess is the your installation procedure doesn’t do that, correct?When I started with running guvcview on my RPi3, it gives me a discouraging 2fps with 480 x 320 resolution, yet to see what I can get with opencv.If anyone did a similar project already and can give some pointers, it would be highly appreciated.
Adrian,First a thanksawesome install instructions. Awesome books! Awesome videos! I’m completely new to python and opencv and you have made it dead simple. I’m up and running in virtualbox on my Macbook Air and my Pi3 as well.Only problem is I’m having trouble with matplotlib on the Pi3.
I used pip to install it and it appears to runi.e. The histogram programs from Chapter 7 run but the windows with the plots of the histograms don’t show up. Since there doesn’t seem to be any error I haven’t been able to track down what I’m missing.Any thoughts?Regards,Jeff. Is it only my lack of understanding or is the virtualenv approach a total waste of time if I’m preparing a device for production?
In other words, I developed an opencv app that runs on RaspberryPi and I found a client for it. The app runs on system startup (crontab pointing to a launcher script) Did I just make a bloody mess on a brand new RPi3’s SD disk by creating a virtualenv there and installing opencv in it? Apparently now I cant access opencv from outside that virtualenv so I need to compile it again, this time outside. I have installed everything following the procedures in your tutorial and everything appears to have worked well, thank you, except for one thing. When I try to use cv2.SURF I get an error that there is no such module.Looking through the contents and readme’s in the installed and unzipped folder there does not appear to be a SURF module in the list of modules.
Am I missing something?I am currently using opencv 2.9.1 in my robot and use SURF to recognise images which guide it. I would like to upgrade to a later version to see if its “hit rate” can be improved.I have made the installation on a Pi2 with the very latest (as of yesterday) upgraded copy of Jessie but I can’t see that this would be an issue. Hello Adrian, I am having the same problem as Charles. Unfortunate, all this is new to me and I could not really understand how Charles solved the problem nor your response. I followed all the steps and checked the output on each step. I checked /home/pi/.virtualenvs/cv/lib/python2.7/site-packages to make sure that I have the cv2.so file there.
I also tried doing the symbolic link again and got a message that the link already exists. I am really stuck. Any further pointer you can provide is much appreciated. Hi Adrian,As requested, i made a try on Jessie Lite.It runs like a charm by following your tutorial (very well and sufficiently documented) step by step.
(on Pi2 & py27)No information returned about compiling time. Approximately 2h (3h from the beginning)The purpose of Jessie lite is to build a headless version of Pi.So the result is that direct video stream thru X11 forwarding or VNC is not efficient, as exactly i expected ?Now I will give a look on your home monitoring and motion detection tuto(include or add secure remote access to a real realtime stream thru http and messaging alert)Many Thanks. I cloned the latest OpenCV and OpenCVContrib gits and performed a make -j4.
It’s been going for over 12 hours at 100% CPU Load. Is this normal? Instructables says that for a Pi model b (the original) it should take 10hrs.Could the kernel I am using be nit-so-good? Or maybe I ran out of RAM and it is now performing the switching operations? (I have 256mb allocated to the GPU in my current setup)Thanks for the tutorial! Otherwise very easy to follow.Funny thing about your RC post and Cat detection posts the other day: I would like to do exactly that, have a cheap rc toy to interact with my cats!Shoot, sorry, I should add that this is an RPI 3 Model b.
Thanks in advance! It should definitely not take 12 hours to compile OpenCV on the Raspberry Pi 3.
In fact, it should take less than 2 hours. I think there might be an issue with the multi-processing compile and somewhere a race-condition has been caused, making the compile infinitely spin.Instead, I would suggest stopping the compile, deleting your “build” directory, re-creating it, and re-running CMake. From there, just compile with a single thread using:$ makeThis will take longer to compile, but it should ideally avoid any type of threading issues you may have encountered. Hi there,I’ve been trying to install OpenCV on my Pi 3 according to these instructions and ran out of space on the Pi’s 8GB SD card.
I’m guessing this isn’t normal. I’m using standard NOOBS and there’s some pre-installed stuff I’ve now purged (libreoffice., penguinpuzzle, etc) to make more room. From df -h I now see the card has 3.2G available – is that enough?
Or should I mount something externally to store the build files in? (Or get a bigger SD card, sigh)Success!
Looks like removing those extra unecessary packages did the trick. I imagine in future it would be easiest to start out with a ‘light’ copy rather than full NOOBS which is what I was using.For reference, I ended up purging:libreoffice.smartsimpenguinpuzzledillominecraft-pipython-minecraft-pisonic-piwolfram-engineHope this helps someone else. Thanks for the great guide!I ran into a problem while installing NumPy which took some time to figure out, so I’ll share here in case anyone else runs into the same issue.The installation for numpy was failing with an error code 1What I finally realized was that my /tmp directory was running out of space since I had previously turned it into a tmpfs RAM filesystem with a 80mb limit.To fix I just updated my “/etc/fstab” file to remove the line I added that defined /tmp as a RAM filesystem and rebooted my Pi. The numpy install went smoothly after that. There is something strange:At the end of the tutorial you say one can delete the opencv-3.1.0 and opencvcontrib directories with rm -rf folders. Where is then OpenCV installed?
We built Opencv into the build-folder. But after having deleted the folders, there is no build-folder left. How can it be, that Opencv still works? Where is opencv located?If I would want to make little customizations to the opencv source, I have no idea where to find the files, and how to remake them.kind regards from germany,amrosik. Your tutorial are the best in this world.I have a simple question on my mind. You said:“Any Python packages in the global site-packages directory will not be available to the cv virtual environment. Similarly, any Python packages installed in site-packages of cv will not be available to the global install of Python.”I didn’t install opnenCV on my Global environment but on the environment named “cv” i installed python 3 and open cv.
![3.5 3.5](/uploads/1/2/5/5/125505213/751005925.jpg)
But when I open a fresh terminal and type (without the workon command):$ pythonpython 2.7.9 (default, Mar 8 2015, 00:52:26)which makes sense, cause my global python is 2.7.9 where as the one in the virtual environment is 3.4.2 import cv2 cv2.version‘3.1.0’NO ERRO!! Shouldn’t it pop an error message saying “cv2 not understood” as we are working on global environment?no error occurs. When you installed OpenCV, it was installed in /usr/local/lib/python2.7/site-packages — this directory is the global directory for your Python install. It does indeed work outside the virtual environment.
We then use the sym-link command to move the cv2.so bindings into the “cv” virtual environment.That said, I’m not sure how you have OpenCV bindings for both Python 2.7 and Python 3 installed on the same system without explicitly doing so. Compiling OpenCV against a different version of Python than it’s imported into would cause an error, so I’m not particularly sure how your system is setup.
About halfway through the tutorial, you mention modifying the /.profileI can’t figure out where the.profile file is. If I type /.profile, I get nothing. If I try nano /.profile, it creates a new file. How do I get to this file to edit it?See below————————————————————————————————————–Now that both virtualenv and virtualenvwrapper have been installed, we need to update our /.profile file to include the following lines at the bottom of the file:# virtualenv and virtualenvwrapperexport WORKONHOME=$HOME/.virtualenvssource /usr/local/bin/virtualenvwrapper.sh. Adrian congratulations for your posts and your book!I have installed opencv with python2.7 correctly and everything is fine. Then, I tried to make the same steps for python3 from the point: mkvirtualenv cv -p python3 onwards in the same or in a different terminal window.
The installation cannot be done. When I am trying to import cv2, it says it cannot find numpy. Why numpy is not in the site-packages although I have followed exactly the same steps as for python2.7?Thank you anyway for your valuable presence! Hi Adrian,Appreciated with your post, it really helped plenty of people who wants to install opencv on their Raspberry Pi, however there is one question would like to ask:Although my Pi 3 has SUCCESSFULLY installed the opencv 3.1.0 and also showed the correct version in Python 3 as Step #7 illustrated; however, when i verify with the ls command, by type in:ls -l /usr/local/lib/python3.4/site-packages/It returns the following:-rw-r-r- 1 root staff 1480296 Oct 27 06:08 cv2.cpython-34m.soKnowing some files are missing as for the fact, but will opencv operate properly with missing files? Please advice, thank you very much.
Hi Adrian,I followed your guide to install opencv on a RPi 3, but with Jessie Lite and with no virtual environment. Cmake failed in that opencv only pointed to python2.7, when I want to be using python3.Therefore, I started again and only installed the python3-dev, and also used sudo pip3 for numpy. However, the packages path points to lib/python3.4/dist-packages, rather than site packages. Is this a problem?I can import cv2 and numpy into python3 and they seem to be working fine, but will future packages be installed into site-packages and will having two locations for packages cause an issue?I don’t really understand it, but it is discussed here. Hi Adrain thanks for the great tutorials, I’ve found many of them to be quite helpful.I am having some trouble on step 5 of this tutorial where we configure the cmake options and confirm that the output shows the build is using the correct python interpreters.
Currently I have a python 3.5 virtualenv setup, have installed numpy and can confirm that “python” and “import numpy” pull the 3.5 interpreter and numpy successfully. However when I run cmake with your options it chooses the python 3 interpreter in /usr/bin/python3.4I tried adding these more explicit options into cmake:-D PYTHON3PACKAGESPATH=/.virtualenvs/py35-cv/lib/python3.5/site-packages -D PYTHON3INCLUDEDIR=/.virtualenvs/py35-cv/include/python3.5m But they didn’t seem to have an effect.Any advice?Note: I am working inside my py35-cv virtual environment when running the cmake commands. I had exactly the same issue – realised it was because I wasn’t following the instructions exactly. Rather than installing in my home directory (/) I was in a subdirectory (/opencvinstall/) this was causing problems with the opencv-contrib section. I changed the build command:-D OPENCVEXTRAMODULESPATH=/opencvcontrib-3.1.0/modulesto reflect this:-D OPENCVEXTRAMODULESPATH=/opencvinstall/opencvcontrib-3.1.0/modulesand the configuration worked, currently building!Whilst I’m here – thanks for this awesome guide ?. Adrian, This is a great tutorial and a must-have resource for starting with OpenCV and Python on Raspberry Pi! I have subscribed to both your 10 day and 21 day crash courses.
I have been having trouble trying to get my setup working on Raspberry Pi. I had two unused webcams (Phillips SPC900NC and Logitech Quickcam 400). I am trying to get OpenCV working on Raspberry Pi with an intention to use these two for stereo vision. Do you think its something worth attempting? I know people typically use same cameras – but theoretically I should be able to get this working? Awesome post!!Sadly though, I am having some trouble integrating it with my current project.I am able to install OpenCV3 fully and import/test it (step 7) successfully. However, when I exit out of python back to the normal command line and attempt to run my program I get “ImportErrorr: no module named cv2”.
I ran through all of the trouble shooting steps and can’t determine the problem. My “/.profile” is correct, it looks like the smy-link is correct, and I am able to run “workon cv”. I’m not sure where to go from here and any help would be appreciated.
Great instructions and everything works. Unfortunately my unfamiliarity with virtual environments is causing me a lot of issues. Looking up explanations of virtualenv elsewhere, they use different terminology/steps to install it (for instance they don’t use.profile etc). Therefore, I’m unable to understand what I did in this tutorial and I am having trouble adding packages to my virtual environment or creating new virtual environments.If I wanted to delete this virtual environment and install opencv the regular way, how do I go about uninstalling the cv virtual environment/ what folders do I delete? Thanks for the tutorial, Adrian! Got it all done and backed up, too.One question:Could the install have affected my RPi3 wifi?
I had trouble staying connected during some of the longer gets yesterday, and read that I could use ‘sudo wconfig lan0 power off’ to fix it, and it did- the rest of the installs went off without a hitch. But today, after a fresh boot, the same command (its also in rc.local) has no apparent affect. The wifi fails every time I use the internet. Each time I restart the wifi it goes down within a minute, which is much more frequently than when I was doing the installs. And I think it takes my router down with it, too.Any ideas? Hi Adrian,Thanks for the great tutorial.
I currently have opencv working on my pi 3, but when I try to run a python file that imports cv2 as sudo it cant find it.To be more specific when I run:python cvTest.pyeverything works and cv is properly imported and such, but when I run:sudo python cvTest.pyI get the error: ImportError: No module named cv2Do you have any idea why this is happening or how to solve it? I have to run my final program as sudo to use the pigpio that the project also uses.Thanks for the help! Hi Adrian,So this worked for me but I have other packages that were installed that aren’t working now because they aren’t installed into the virtual environment. How do I install a package into the virtual environment?
Specifically I’m trying to install the pigpio library into the virtual environment so that I can run that at the same time as running opencv.I tried reinstalling pigpio when I was in the virtual environment but the python file is still unable to import pigpio.If you have any recommendations on how to solve this that would be greatly appreciated!Thanks!-Breanne. Thanks for your brilliant tutorial.I am doing an image processing project.I am a beginner to raspberry pi and have written a python script which uses OpenCV libraries. I’ve tested the code and works well on a computer.I want it to run on Raspberry pi 3 model B.I want to run the script on pi’s reboot. I am using a USB camera and probably an LCD display along with tesseract ocr.And honestly speaking, as a beginner I don’t want to get into the virtual environment stuff.Can i follow your tutorial without using virtual environment skipping the virtual environment steps?
Do i have to make any changes in other steps?And after installing OpenCV without doing the virtual environment stuff, will I be able to execute the code in IDLE on the pi?Sorry for being such a noob.:pMany thanks!! Hi AdrianI followed the tutorial for the installation of opencv 3.0 and 3.1 in Raspbian, but I’m having a problem because in the idle of python 2 it’s being returned that there is no module called import cv2, but I’m getting it through python in workon cv. I’m developing a facial detection program, but when I ask to import the picamera with python accessed through the workon cv it is returned that there is no such module. I tried installing the picamera module plus the terminal warns that it is already installed.
I have been experiencing this problem since the last update of Raspbian, since previously I was able to install opencv without any problem following the tutorial and developed a facial detection program, I wish you could help me with this problem on opencv.