In this blog post I’ll demonstrate how to install OpenCV 3 on the Raspberry Pi Zero.
Since I’ve covered how to install OpenCV on the Raspberry Pi in multiple, previous blog posts, I’ll keep this post on the shorter side and detail only the relevant commands necessary to get OpenCV up and running. For a more thorough discussion on how to install OpenCV 3 on your Pi (along with a 22-minute video installation guide), please refer to this post.
I’ll also be making the following assumptions in this installation guide:
- You are using Raspberry Pi Zero hardware (so the timings supplied with each command will match up).
- You have Raspbian Jessie installed on your Pi Zero.
- You want to install OpenCV v3.0 with Python 2.7 bindings (for Python 3 support, see this post).
Again, I have already covered installing OpenCV on multiple Raspberry Pi platforms and Raspbian flavors — the primary goal of this tutorial is to get OpenCV up and running on your Pi Zero so you can get started learning about computer vision, image processing, and the OpenCV library.
Installing OpenCV on your Raspberry Pi Zero
If you haven’t seen the Raspberry Pi Zero yet, it’s a really cool piece of hardware. It packs a single core 1GHz ARM processor. 512mb of RAM. And it’s smaller than a credit card.
But the best part?
It’s only $5!
While the Pi Zero isn’t quite fast enough for advanced video processing, it’s still a great tool that you can use to learn the basics of computer vision and OpenCV.
Step #1: Expand filesystem
If you’re using a brand new install of Raspbian Jessie, then the first thing you should do is ensure your filesystem has been expanded to include all available space on your micro-SD card:
$ sudo raspi-config
Select the first option “1. Expand Filesystem”, arrow down to “Finish”, and reboot your Pi:
After rebooting, your filesystem will have be expanded to include all available space on your micro-SD card.
Step #2: Install dependencies
I’ve discussed each of these dependencies are in previous posts, so I’ll just provide a brief description, the command(s) themselves, along with the amount of time it takes to execute each command so you can plan your OpenCV install accordingly (the compilation of OpenCV alone takes 9+ hours).
First, we need to update and upgrade our existing packages:
$ sudo apt-get update $ sudo apt-get upgrade
Timing: 2m 29s
Install our developer tools:
$ sudo apt-get install build-essential cmake pkg-config
Timing: 49s
Let’s grab the image I/O packages and install them:
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
Timing: 36s
Along with some video I/O packages (although it’s unlikely that you’ll be doing a lot of video processing with the Raspberry Pi Zero):
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev
Timing: 36s
We’ll need to install the GTK development library for OpenCV’s GUI interface:
$ sudo apt-get install libgtk2.0-dev
Timing: 2m 57s
Let’s also pull down a couple routine optimization packages leveraged by OpenCV:
$ sudo apt-get install libatlas-base-dev gfortran
Timing: 52s
Lastly, let’s install the Python 2.7 headers so wen can compile our OpenCV + Python bindings:
$ sudo apt-get install python2.7-dev
Timings: 55s
Note: I’ll only be covering how to install OpenCV 3 with Python 2.7 bindings in this post. If you would like to install OpenCV 3 with Python 3 bindings, please refer to this post.
Step #3: Grab the OpenCV source
At this point, all of our dependences have been installed, so let’s grab the 3.0.0
release of OpenCV from GitHub and pull it down:
$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip $ unzip opencv.zip
Timing: 1m 58s
Let’s also grab the opencv_contrib repository as well:
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.0.0.zip $ unzip opencv_contrib.zip
Timing: 1m 5s
It’s especially important to grab the opencv_contrib
repo if you want access to SIFT and SURF, both of which have been removed from the default install of OpenCV.
Now that opencv.zip
and opencv_contrib.zip
have been expanded, let’s delete them to save space:
$ rm opencv.zip opencv_contrib.zip
Step #4: Setup Python
The first step in setting up Python for the OpenCV build is to install pip
, a Python package manager:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
Timing: 49s
Let’s also install virtualenv
and virtualenvwarpper
, allowing us to create separate, isolated Python environments for each of our future projects:
$ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/.cache/pip
Timing: 30s
Note: I’ve discussed both virtualenv
and virtualenvwrapper
many times on the PyImageSearch blog. If this is your first time using them, I suggest referring to this blog post on installing OpenCV 3 on Raspbian Jessie.
To complete the install of virtualenv
and virtualenvwrapper
, open up your ~./profile
:
$ nano ~/.profile
And append the following lines to the bottom of the file:
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
Now, source
your ~/.profile
file to reload the changes:
$ source ~/.profile
Let’s create a new Python virtual environment appropriately named cv
:
$ mkvirtualenv cv
Timing: 31s
The only requirement to build Python + OpenCV bindings is to have NumPy installed, so let’s use pip
to install NumPy for us:
$ pip install numpy
Timing: 35m 4s
Step #5: Compile and install OpenCV for the Raspberry Pi Zero
We are now ready to compile and install OpenCV. Make sure you are in the cv
virtual environment by using the workon
command:
$ workon cv
And then setup the build using CMake:
$ cd ~/opencv-3.0.0/ $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \ -D BUILD_EXAMPLES=ON ..
Timing: 4m 29s
Now that the build is all setup, run make
to start the compilation process (this is going to take awhile, so you might want to let this run overnight):
$ make
Timing: 9h 42m
Assuming OpenCV compiled without error, you can install it on your Raspberry Pi Zero using:
$ sudo make install $ sudo ldconfig
Timing: 2m 31s
Step #6: Finishing the install
Provided you completed Step #5 without an error, your OpenCV bindings should now be installed in /usr/local/lib/python2.7/site-packages
:
$ ls -l /usr/local/lib/python2.7/site-packages total 1640 -rw-r--r-- 1 root staff 1677024 Dec 2 08:34 cv2.so
All we need to do now is sym-link the cv2.so
file (which are our actual Python + OpenCV bindings) into the site-packages
directory of the cv
virtual environment:
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
Step #7: Verifying your OpenCV install
All that’s left to do now is verify that OpenCV has been correctly installed on your Raspberry Pi Zero.
Whenever you want to use OpenCV, first make sure you are in the cv
virtual environment:
$ workon cv
And from there you can fire up a Python shell and import the OpenCV bindings:
$ workon cv $ python >>> import cv2 >>> cv2.__version__ '3.0.0' >>>
Or you can execute a Python script that imports OpenCV.
Once OpenCV has been installed, you can remove both the opencv-3.0.0
and opencv_contrib-3.0.0
directories, freeing up a bunch of space on your filesystem:
$ rm -rf opencv-3.0.0 opencv_contrib-3.0.0
But be cautious before you run this command! Make sure OpenCV has been properly installed on your system before blowing away these directories, otherwise you will have to start the (long, 9+ hour) compile all over again!
Troubleshooting
If you ran into any error installing OpenCV 3 on your Raspberry Pi Zero, I would suggest consulting the Troubleshooting section of this post which goes into added detail for each installation step.
The post also includes a complete 22 minute video where I demonstrate how to run each command to flawlessly get OpenCV 3 installed on your Raspberry Pi:
What's next? We recommend PyImageSearch University.
86 total classes • 115+ hours of on-demand code walkthrough videos • Last updated: October 2024
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
This post detailed how to install OpenCV 3 on your Raspberry Pi Zero. The purpose of this blog post was to provide accurate timings that you can use when planning your own install of OpenCV on your Pi Zero.
In order to get OpenCV up and running, I made the following assumptions:
- You are running Raspbian Jessie on your Raspberry Pi Zero.
- You are installing OpenCV v3.
- You want to use Python 2.7 with your OpenCV bindings.
If you would like to use Python 3+ with your OpenCV bindings, please consult this post, where I have elaborated more on each step, provided more detailed information, and included a 22 minute video that walks you through step-by-step on installing OpenCV 3 on your Raspberry Pi.
Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF
Enter your email address below to join the PyImageSearch Newsletter and download my FREE 17-page Resource Guide PDF on Computer Vision, OpenCV, and Deep Learning.
Sol
PiCamera?
Adrian Rosebrock
There actually isn’t picamera module hookup on the Pi Zero. You’ll need to use a USB camera. I prefer the Logitech C920 since it is plug-and-play capable with the Pi.
Jacob
there is on pi zero w
Marek
I had to install python-dev before installing numpy.
Adrian Rosebrock
Thanks for pointing this out Marek! I’ve updated the installation instructions to include installing the Python headers.
Serban Balamaci
Hi Adrian, cool site, I’m wondering about the hardware setup for PiZero you have there. I see it has two micro usbs – one will be used for power I think-. To me it’s a pity they did not drop the hdmi in favor of another microusb, I don’t understand how you can use it. You attach the usb/network camera either to the other microusb but then how do you control it – you either need a keyboard attached(and the cool looking small hdmi monitor you have) or a usb->network adapter or usb wifi. Do you have an usb hub?
Adrian Rosebrock
Indeed, it has two micro-USB ports. The first one is for power. And the second one is used for any peripherals. I ended up plugging in a USB hub, which then allows me to connect USB wifi, keyboard, mouse, and webcam. The HDMI is also min-HDMI.
Sam Fattah
Hi Andrian, What type of USB hub did you used? bz mine dose not work for the USB cam.
Adrian Rosebrock
I ended up going with this one from AdaFruit.
Loek
Hi Adrian,
I have installed OpenCV because I’m interested in exploring the possibilities of robot vision. The tests and demo’s work fine. There is, however, one thing I would like to be clarified on. When doing Python stuff I like to work with IDLE. I have started it from the command line in the virtual cv environment with IDLE &. Import numpy works fine, but when I try to import cv2 I get “Import error: No module named cv2. When I start Python from the command line importing cv2 works fine. But then, of course I miss all the advantages of IDLE. Is there a way of fixing this? I already tried sudo apt-get install python from within the virtual environment, but the message I get says the the latest version is already installed.
Thank you
Adrian Rosebrock
I don’t personally use IDLE (other than the command line version), so I’m not sure about this problem. If you like using the GUI version of IDLE, I would suggest installing IPython Notebooks, that way you can code similar IDLE, only in your browser. Plus, this has the advantage of working with the Python virtual environment.
Loek
Hi Adrian,
Thank you very much for your reply. Meanwhile I investigated the matter a little bit further and found out that putting the symbolic link ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so in the /usr/bin/ directory because that’s where the Python executable is. This solves the problem and I am now able to use IDLE within the virtual environment.
Kind regards
Adrian Rosebrock
Great, thanks for sharing Loek!
Randy Marsh
Have you tried running make -j to speed up the build ?
Adrian Rosebrock
Yes, you can use the
-j
flag to speedup the build; however this is only useful on the Pi 2 and Pi 3 where there are multiple cores. On the Pi Zero, this will not speedup the build.jonasdig
Hi, do you think the Zero is powerful enough to handle motion detection with OpenCV and the camera module v2?
Adrian Rosebrock
For very basic motion detection, you can use the Zero, but you’ll struggle to get more than a couple frames per second processed (if that). I would suggest using this tutorial as a starting point.
zook
Don’t know why but in my case cv2 go to /usr/local/lib/python2.7/dist-packages not /usr/local/lib/python2.7/site-packages
Frank
Is it possible to have an image of raspbian + opencv for raspberry pi zero, 2 and 3 ready to download and flash it on the SD please ?
I did try to install open cv following your tutorial on raspberry pi 2 and zero but there is always something wrong somewhere.
For example, right now I have issues on cmake, it failed for many reasons and it will take huge amount of time for a newbie to understand what’s wrong.
Please, I think that is the fasted and more helpful way.
Adrian Rosebrock
Hey Frank — I actually just announced a Raspbian img file with OpenCV pre-configured and pre-installed. Be sure to take a look!
Edison
Hi. Adrian.
What about using cross compiling to speed up make?
For the RPi zero, avoid virtual environment wouldn’t be better?
Adrian Rosebrock
I haven’t tried cross-compiling before for the Raspberry Pi, it might be something worth looking into. As for avoid Python virtual environments on the Pi Zero, I’m not sure what the end goal would be? That wouldn’t impact performance.
Edison
Thank you.
I’ll try cross-compiling. If I succeed I’ll come back with the results.
Rodo
Hi Adrian. Thanks for the post. I suggest people compiling it on a headless pi (using SSH) to call make as:
$make > progress.txt 2>&1 &
It runs make in background, writing the stdout and stderr output to a file avoiding the typical issues with the SSH connections and allowing to close the session. To check the progress, open a new SSH and call:
$tail -f progress.txt
Kind regards
Adrian Rosebrock
I would also recommend using
screen
as well.Charles Schultz
Actually, I would recommend installing ‘screen’ and run it that way, especially if your connection is short-lived for any reason (intentional or accidental).
sudo apt-get screen
screen
^A^D to leave screen while it is still running
screen -r to get back to your screen session, even from a completely different ssh session.
Adrian Rosebrock
The
screen
command is an excellent utility, thanks for mentioning it Charles!hashbang
Hello
for a lane detection project with opencv in python do you believe pi zero w is good choice or i should get pi 3?
Adrian Rosebrock
The Pi Zero realistically isn’t suitable for working with video. I would recommend you use the Pi 3.
Pranav Jain
Hi,
I tried installing opencv along with opencv_contrib , i did not run into any issue and it compiled , i am able to import opencv in my python environment but the modules from the opencv_contrib (xfeatures2d etc) are not available. I followed all the procedures listed here exactly. Do i need to add something extra in cmake statement . Any ideas as to how i can resolve this ?
Adrian Rosebrock
Double-check your CMake and specifically check the “Modules to be Compiled” section. You’ll likely find that
xfeatures2d
was not included. 99% of the time that is due to your path to theopencv_contrib/modules
directory being incorrect.Charles Schultz
Adrian,
Thanks for putting this up, and your other posts as well – I am really digging the tutorial/”learning by example” approaches. This is excellent stuff!
Just curious, beyond object detection, have you used CV for “objects in motion” detection? I know you have done some stuff with people/facial recognition, but I am gearing towards controlling a small quad-copter (orange cheerson cx-10). I am completely new to CV and still have a lot to learn, but I am trying to figure out the best way to determine 3d movement from a 2d image stream – obviously, directions parallel to the plane of viewing is pretty clear (up, down, left, right), but along the plane of view seems more of a challenge (forward, backward). Have you tackled anything like that?
Adrian Rosebrock
If both your camera and your object is moving then things become a bit more challenging. Methods like correlation tracking would work better in that situation.
Ansh
Oh I came across a similar situation in tracking objects and associating a control logic for the motors. The x and y as you said is pretty simple. For the depth (forward and backward), what worked for me was the area of ROI rectangle. What I mean –
The recognized object can encapsulated in a rectangle (see Adrian’ previous blogs)~cv2.rectangle from where one is getting – x,y,w,h values. w*h provides the area of the rectangle. So as the object moves away from the camera the area of rectangle decreases and as the camera moves closer the area of the rectangle increases. Also at each depth plane the value of this area is unique. This value of the area can be interpreted as Z value and combined with X and Y values can provide variables to do design a control logic.
I have yet to play with DLIB library and correlation tracking etc and maybe that will be better. But the above mentioned approach works for me just fine.
Ansh
Hi Adrain,
Do you have comments/adivce on overclocking the Zero for a better Open CV performance? I am just using for Face Recog using LBPHFace Detector. If my application needs, do you recommend some safe way/settings for the same?
~Ansh Verma
Adrian Rosebrock
I really don’t recommend the Zero for real-time computer vision applications. Use the Raspberry Pi 3 instead. It’s much faster and better suited for CV applications.
Ansh
Also, I understand that we can run OpenCV applications via the GPU of the raspberry pi for better performance – it is suppose to be faster?
Do you have any comments on it? Say instead of overclock the pi. Will python codes run via GPU and how does one do that?
Carlos Simoes
Hi Adrian,
I am from Portugal, do you have programming for recognize one label in glass from car .
my ideia is install two camera in park and they see the glass front and back and recognize the label if the car have this label. if not create alarme.
can you help me about this idea .
thanks your attention .
best regards.
Carlos
Adrian Rosebrock
Hi Carlos — I’m not sure what you mean by “recognize one label in glass from car”. Can you please elaborate?
Rui L.
I just followed this post and successfully compiled opencv-3.0.0 on C.H.I.P headless 4.4. The final build took a little bit more than 5 hours. I didn’t need an external USB drive as the build only needed about 2.3 GB free space.
Thanks Adrian!
Adrian Rosebrock
Congrats on getting OpenCV installed, Rui — nice job!
Mohamed Abdelkader
Thanks for the tutorial. What is the expexted frame rate if I want to do blob detection, just simple color detection, on Pi zero?
Adrian Rosebrock
The Pi Zero really isn’t suitable for doing real-time frame processing. I would highly suggest that you use a Pi 2 or a Pi 3 instead.
NVibe
Thanks for this write up. After going through the compilation process of 3.2.0 a few times, I keep getting an “illegal instruction” message when in python, importing cv2 on the Pi Zero. I must confess I had followed another tutorial, so will give your a go.
On another note, I am curious to know what can be realistically achieved on a PI Zero in small project terms. I am a newbie to the topic.
Is basic face tracking &/or recognition possible?
Is basic object tracking & /or recognition possible?
How about multiple faces (counts &/or recognition)?
You mention real time frame processing not suitable on the platform, does that discount using the above methods entirely?
Do you have a guide for the types of applications (examples) best suited to each Pi platforms limitations in plain speak?
Thanks again for your write up.
Adrian Rosebrock
In all honesty, I do not recommend the Pi Zero for anything other than super, super basic techniques. It’s more of a “learning platform” for basic OpenCV operations. Face tracking and face recognition will be far too slow on the Pi. I would highly encourage you to use a Pi 3 instead. The speed boost from the 4 cores is well worth it.
For what it’s worth, both the Quickstart Bundle and Hardcopy bundle of my book, Practical Python and OpenCV, include a pre-configured Raspbian .img file with OpenCV pre-installed. Simply download the .img, flash it to your Pi, and boot. It’s by far the fastest way to get up and running with OpenCV on your Pi.
khaled
when I ran : ls -l /usr/local/lib/python2.7/site-packages
I get total of 0
there were not any problem with build and make
Adrian Rosebrock
Check the output of your
build/lib
directory to see if yourcv2.so
bindings are there. If not, then your CMake command was configured improperly.Sam B
I got a fatal error for math.h
JanakaS
Hey, I had the same problem.. this has got to do with gcc using precompiled headers..to correct this do the following:
In the cmake command in Step#4 include another ‘segment’ at the end as:
The time it takes to compile not is much greater… maybe 20 hrs.. but, it compiles all the way!
Found it from:https://github.com/opencv/opencv/issues/6517
Cheers,
Graham
Thank you, thank you, thank you 🙂
Danish
Thanks JanakaS m looking for this solution from past 2 days and finally it’s compiling
Vixel
hey, the power to the raspberry pi zero got interrupted and now the putty became inactive! and it was 31%. I was doing it for four hours on that ‘make’ command and its all gone now!!! Is there any easier way? like, imaging it in the sd card directly? Will it again run for 9hrs or will there be any data already present to speed up upto 31%??
Adrian Rosebrock
I have two questions:
1. Use the “screen” command to multiplex your terminal. This way if you lose network connection to the Pi the compile is not interrupted. If you lose power; however, there is not much you can do. You will need to restart the compile. In general restarting an interrupted compile will lead to errors (not always, but it does happen).
2. I actually offer a pre-configured Raspbian .img file inside the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV. Just download the .img file, flash it to your card, and boot. Be sure to take a look!
I hope that helps.
Vixel
Hey, is it possible to bring Raspbian OS on a VMware or something, install opencv (the long ever make command), make an image file and put it on the Pi board. That way you can use the processor of our laptop. Is it possible????
Adrian Rosebrock
You can now install Raspbian on your laptop/desktop and play around with it, but you cannot configure Raspbian on your laptop, build the .img, and then move it to your Pi.
Rich1812
Hello Adrian, I installed opencv on the pi Zero successfully. However I only have python2 installed in the virtual environment, because at the time I wasn’t sure if it will work, but it does. Now I would like to add python3 capability, I would like to add with python3 binding.
my question is, after opencv is complied, can I simply install python3? Do I need to re-compile the whole thing again? I use a pi Zero W, It took about 32 hours for me to download all dependencies to fully compile and install, it was painfully slow, time consuming! I really don’t want to have to go through it again. Thanks.
Adrian Rosebrock
Unfortunately you would need to recompile OpenCV for Python 3 all over again. I know that’s a bummer but unfortunately there isn’t a way around it at this point.
Amir
Also is it possible to recompile opencv from the very start of this tutorial again without having to reformat the card and lose everything else? Like I can access the files via VNC viewer so i’m wandering if i delete the opencv folder completely in VNC, would I be able to run everything again from the start of this tutorial from step 1 again or no? If not whats the easiest way?
Thanks
Adrian Rosebrock
Yes, just delete your OpenCV or “build” directories and re-run CMake and make.
Amir
I have also tried copying this CMAKE command from another forum which is suggested for openCV installation on Jessie but thought I’d give it a go but now whenever I try running the make command the build starts from 3% every time…and crashes on 3% with the same fatal math error as before after a few minutes! 🙁
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
-D CMAKE_SIZEOF_VOID_P=4 \
-D BUILD_opencv_python3=TRUE \
-D PYTHON3_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.4m.so \
-D BUILD_EXAMPLES=ON ..
Cheers
Adrian Rosebrock
I think you need to turn off pre-compiled headers in the CMake command by adding the following switch:
-D ENABLE_PRECOMPILED_HEADERS=OFF
Daniel
Hi Adrian, Thanks for your great tutorial. I was wondering if running a virtual environment would also cause an increase in power consumption? I am running a headless setup with the pi camera as a motion sensing camera, but need it to be as power efficient as possible. Thanks again!
Adrian Rosebrock
No, a Python virtual environment would have zero increase in power consumption. A Python virtual environment is not a “virtual machine” that has heavy implications on your CPU/RAM/power consumption. A Python virtual environment, in essence, just creates a new Python install in your home directory, under a new name, allowing you to have multiple environments on your Pi.
Daniel
Thanks for the explanation!
winn
Would it make more sense to follow this tutorial or the Raspberry Pi 3 tutorial if I wanted to install OpenCV on a Pi Zero W (latest version). The main difference I see is this tutorial is not for Stretch and also doesn’t have you expand the swap file.
I’m building a small robot and I want it to have two “eyes” (each of which is an entire a Pi Zero W with camera module, one NoIR the other normal), and also a Pi 3 that’s in charge of getting visual data from the “eyes” and doing all the other basic processing needed for the robot (motion, navigation, higher-level image processing). I know you said the Pi Zero W isn’t great for real-time processing, but I assume it can do some low-level pre-processing to take the load off the Pi 3?
Adrian Rosebrock
I really, really do not recommend using the Pi Zero, even for this. You’ll do with network and I/O latency transmitting the frame to the Pi 3. Why not just use the Pi 3 to start? Is there a particular reason you are using 3 Raspberry Pis for this project?
Bob Colwell
actually, yes…Mr. “MickMake” has a PCB that connects the Pi Zero to the TI DLP2000 picoprojector. Works pretty well. He’s working on a version of the adapter board that will handle Pi 2 and Pi 3, but until then…
Which leads to my question. I can see the Raspbian GUI displayed on a screen by the TI DLP2000 after boot. After considerable futzing around, I can ssh to the Pi Zero. What I want to do next is start playing with the opencv code examples you provided in the Practical Python and OpenCV book. But I get an error message: Gtk-WARNING **: cannot open display:
How do I get those code examples to direct their output to the TI DLP? thanks.
Adrian Rosebrock
Hi Bob — I unfortunately do not have any experience with a picoprojector so I’m not sure what is going on there. My guess is that somehow the X server is not running on the Pi or you have not enabled X11 forwarding if you are using SSH.
Bob Colwell
after lots more experimentation, I tried the obvious:
$ export DISPLAY=:0
That did it! Now I’m running the opencv code from the command line of an ssh, and the images are being superimposed on the usual Raspbian desktop.
Back in business.
Adrian Rosebrock
Awesome, congrats on resolving the issue Bob!
Tom
sudo make install should probably read 2 hours not 2 minutes
judson antu
hey adrian,
i am more interested in the screen you have used for the rpi. looks like it is compact and portable. can you please tell me where to buy it and how is it , good or bad?
Adrian Rosebrock
I got my screen from AdaFruit (sorry I cannot find the exact model). I really like it.
Aluizio Rocha
Hi Adrian,
I am trying to install OpenCV 3.3.0 on my new Raspberry Pi Zero W first following the instructions of your post “Optimizing OpenCV on the Raspberry Pi”, when you suggest using some optimization architecture extension for ARM processors. After 12 hours of compilation I tried to import cv2 library in Python 3.5 but it crashes with a “illegal instruction” message. Then, I run a “make clean” and a cmake command without the flags to enable NEON and VPFV3. This second compilation have the same result: “illegal instruction”.
Is there any issue about running OpenCV 3.3 on a Raspberry Pi Zero?
Adrian Rosebrock
Hey Aluizio — I actually haven’t compiled OpenCV 3.3 on a Raspberry Pi Zero so I’m not sure why that error is happening. Can you double-check your cmake output and ensure the Python version you are trying to “import cv2” into matches the Python version that cmake + make compiled the bindings for?
Guy
Hi Adrian
Thanks to your concise detailed tutorials I successfully set up a security camera on R-Pi B+ a few years ago – works like a charm.
I am now trying to get opencv installed on a raspberry pi zero w but after multiple attempts I run into this problem during the make process:”virtual memory exhausted: Cannot allocate memory”. This happens after about 18 hours! and 85% complete.
BTW – raspian stretch is installed on the pi.
Adrian Rosebrock
Congrats on your security camera project! Compiling OpenCV on a Pi Zero is a real pain. I would suggest increasing your swap size as I do in this tutorial.
CJ
It would have been a great help to include the swap file size issue for the Zero as I had the exact same error after starting it early afternoon and waking in the morning at 86% complete with the same error!!!! Live and learn I guess. I’m doing it on the new Pi Zero W. does that have multiple cores? Can I do MAKE 4 or MAKE 2 etc?
Adrian Rosebrock
On the Pi Zero W I would just use a single core via “make”. It will take a long time to compile, it’s a limitation of the Pi Zero. In general I do not recommend using the Pi Zero or Zero W for computer vision unless it’s an incredibly simplistic pipeline.
Mel Cushnahan
Hi Adrian,
this tutorial is great but I have run into an issue with the make command
when I first ran it I got a fatal error with maths.h but after reading the comments I found a solution to that, however the make is still failing now I am getting a fatal error with
modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
Anyone else seen anything like this, it happens around 9% of the way through the make I think
Thanks
Mel
Adrian Rosebrock
Hi Mel — does the compile hang or does it exit with an actual error? If it exits with an actual error try adding the following switch to disable FFMPEG:
-D WITH_FFMPEG=OFF
CJ
Also 2 more questions. Since it aborted, how and what point do I start the compile again after I change the swap size. 1) If I’m still powered up? (hopefully my power didn’t go off.
2) If I have to start from after a reboot?
Thanks.
Adrian Rosebrock
I would suggest starting your compile from scratch. Once you have updated your swap delete your “build” directory, re-create it, re-run “cmake” to configure the OpenCV build, and then compile it.
Kartik Desai
Hey adrian,
I have return a code similar to you home pi surveillance.
It works fine with pi 2 and 3. but not in zero.
I feel like there is the version in pip python compatibility. So can you please help.
when run code in pi 2 and 3 its working fine,
but in pi zero i am getting:
import dropbox
ImportError: No module named dropbox
and if i comment : import dropbox
then I get following error:
import imutils
ImportError: No module named imutils.
So can you help how to figure out pip version error or is there something else to be done??
Adrian Rosebrock
You need to install the libraries first:
$ pip install dropbox imutils
Kartik Desai
Hey Adrian ,resending the comment again as there was some error in comment,
I installed imutils and drop box after that I was facing python site-packages2.7 error.
But later I resolved it my adding:
————————————————————————————-
import sys
sys.path.append(‘/usr/local/lib/python2.7/site-packages’)
in my python application.
————————————————————————————-
may be it could be helpful for others..
I have installed opencv 3.0 on pi zero w and my code for motion detection camera
is working fine. Thanks to your home surveillance tutorial for the reference.
Just one thing to ask is some times my pi zero gets hang after running for hours and needs reboot.Well this problem isn’t faced in Pi 3 its running code from over a month in spite of many other applications installed. where as there is no other application installed on pi zero except opencv and ftp server.(almost free space is 10 gb) in pi zero .
——————————————————————————————————————-
Any suggestions if you can provide????
Adrian Rosebrock
1. Regarding your Dropbox issue — somewhere along the line you likely installed the ‘dropbox’ package into a different Python version than you are importing it into. If you followed a PyImageSearch guide to installing OpenCV my guess is that you forgot to use Python virtual environments.
2. The Pi Zero is a single core machine with limited computational power. The Pi 3 is still limited but it at least has four cores to distribute computation and can more easily run any user code (like this script) and perform system operations. My guess is that your Pi Zero is just becoming too overloaded and crashes. Use a Pi 3 if at all possible.
Rohit sharma
Hey Adrian,
I have installed opencv3.0.0 successfully on pi zero.
But there is pip version incompatibility with python 2.7 or python 3 i have both. I guess i am running different version of pip and python .
any idea regarding this.??
Adrian Rosebrock
It sounds like you have both Python 2.7 and Python 3 installed which would be normal for the Raspberry Pi. Which version of Python did you compile OpenCV for?
Jacob
in case anyone has an issue with an error that says not enough memory, just do the following, you can open a new cmd window and type this in and then in your cv environment just type make again and it will continue from where it left it.
$ cd /
$ sudo dd if=/dev/zero of=swapfile bs=1M count=3000
To set it as 1GB, change the count value (3000 in the example above) to 1000, 1500 for 1.5GB etc.
Now change the file created to a swap file with the command below.
$ sudo mkswap swapfile
Turn on the swap file with the command,
$ sudo swapon swapfile
To ensure that the swap file is turned on automatically at system startup, open fstab.
$ sudo nano etc/fstab
And add the line given below. Save and close.
/swapfile none swap sw 0 0
That is it. You can check if the system is using the swap file you created with the command
$ cat /proc/meminfo
ragul
hi adrian i have raspberry pi zero w.i very interested in image processing using open cv.the raspberry pi is suitable for opencv software?also,refer the steps of installation process
Adrian Rosebrock
I would not recommend the Raspberry Pi Zero W for computer vision and image processing. A Raspberry Pi 3 is a good option but the Pi Zero W is too slow. You can find my OpenCV + Raspberry Pi install guides on this page.
kingslime
Hi
Thank you for writing this article.
I have a question about opencv.
I made a face recognition in raspberry 3 b +.
I want to run it from raspberrypi zero w.
An illegal instruction error occurred when raspberrypi zero w was used on the microSD card used in Raspberry 3b +.
Can you fix this error?
Adrian Rosebrock
What face recognition code were you trying to run?
Yazan Yaseen
Ive followed up the instructions and its installed without errors..
But i cant use SIFT and SURF why ?
I used cv2.SIFT but it doesn’t work
Adrian Rosebrock
SIFT and SURF are now in the “xfeatures2d” submodule. See this tutorial.
William
Thanks for the great tutorial. Can you suggest a better raspberry pi alternative hardware for openCV under $50 at 2019? Thank you.
Saurabh Chauhan
Hello Adrian,
I want to run object detection SSD based algorithm on Raspyberry pi zero. So I followed your tutorial and installed OpenCV 3.0 and I realised that DNN module which is needed for the object detection algorithm is available on OpenCV 3.3 onwards so I installed OpenCV 4 on Raspyberry pi zero but I got the error message that “OpenCV is not built for the current hardware i.e. raspberry pi zero”. Then I installed OpenCV 3.4 and when I imported cv2 then I got error message “illegal instruction”.
I spent nearly a week but couldn’t able to do progress.
Could you please guide me? Is it possible to run SSD model on Raspyberry pi zero?
Thanking you,
Saurabh
Adrian Rosebrock
I would suggest you read Deep Learning for Computer Vision with Python. That book will teach you how to run object detectors on the RPi.
Biswajit Ghosh
Hi Adrian,
I’m not able to run the Cmake command at all, it says configuration incomplete, errors occurred! I did all the previous step as you described, are there any more configuration I missed?
Adrian Rosebrock
Take a look at the output of the “cmake” command — it will tell you what the error is.
Filipe
trys to updat the cmake library
here is a link:http://osdevlab.blogspot.com/2015/12/how-to-install-latest-cmake-for.html
Nurzhan Amantay
Thank you very much, Adrian, for your post!
For them who have problem with activating virtualenv, ”
environment does not contain an activate script”
use manually this code for activating your venv
$ source /home/pi/.virtualenvs/cv/bin/activate