Why
The default version installed using "sudo apt-get" is 2.4. Therefore, we need compile OpenCV3+ using source code.
Platform
raspberry pi (linux kernel)
Prepare
pi@raspberrypi:~/opencv-3.3.1 $ sudo apt-get install cmake-gui wget
How
Download the OpenCV source code
cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.1.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.1.zip
unzip opencv_contrib.zip
Install OpenCV on System
pi@raspberrypi:~/opencv-3.3.1 $ cmake-gui &
Click "Generate" after "Configure" completed.
make:
pi@raspberrypi:~/opencv-3.3.1/build $ sudo make & sudo make install
Install OpenCV using virtual environment on raspberry Pi
Create your Python virtual environment and install NumPy
We’ll be using Python virtual environments, a best practice when working with Python.
You can install pip, virtualenv, and virtualenvwrapper using the following commands:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py
sudo pip install virtualenv
sudo pip install virtualenvwrapper
sudo rm -rf ~/.cache/pip
Once both virtualenv and virtualenvwrapper have been installed, open up your~/.profile and append the following lines to the bottom of the file, using your favorite terminal-based text editor such as vim , emacs , or nano :
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
From there, reload your ~/.profile file to apply the changes to your current bash session:
You’ll need to run source ~/.profile each time you open a new terminal/SSH into your Pi to ensure your system variables have been set properly (it also loads this file on boot).
Next, create your Python 3 virtual environment:
mkvirtualenv cv -p python3
Here I am creating a Python virtual environment named cv using Python 3 (alternatively, you may also use Python 2.7 by changing the -p switch to python2 ).
You can name the virtual environment whatever you want, but I use cv as the standard naming convention here on PyImageSearch.
Finally, install NumPy into the Python virtual environment:
pip install numpy
Compile and install the optimized OpenCV library for Raspberry Pi
We’re now ready to compile and install the optimized version of Raspberry Pi.
Ensure you are in the cv virtual environment using the workon command:
workon cv
You can use 'deactivate' to exit python virtual environment:
deactivate
Here I use opencv-3.3.0.
$ cd ~/opencv-3.3.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..
Notice how the NEON and VFPV3 flags have been enabled.
If you’re compiling OpenCV for Python 3, check the “Python 3” output of CMake: (If you use Python Virtual Environment 'cv')
Notice how the Interpreter , Libraries , numpy , and packages path variables have been properly set.
Before you start the compile I would suggest increasing your swap space. This will enable you to compile OpenCV with all four cores of the Raspberry Pi without the compile hanging due to memory exhausting.
Open up your /etc/dphys-swapfile file and then edit the CONF_SWAPSIZE variable:
CONF_SWAPSIZE=1024
Notice that I’m increasing the swap from 100MB to 1024MB. This is the secret sauce to compiling OpenCV with multiple cores on the Raspbian Stretch.
If you do not perform this step it’s very likely that your Pi will hang.
From there, restart the swap service:
sudo /etc/init.d/dphys-swapfile restart
Note: Increasing swap size is a great way to burn out your Raspberry Pi microSD card. Flash-based storage have limited number of writes you can perform until the card is essentially unable to hold the 1’s and 0’s anymore. We’ll only be enabling large swap for a short period of time, so it’s not a big deal. Regardless, be sure to backup your .img file after installing OpenCV + Python just in case your card dies unexpectedly early. You can read more about large swap sizes corrupting memory cards on this page.
Assuming OpenCV compiled without error (as in my screenshot above), you can install your optimized version of OpenCV on your Raspberry Pi:
make -j2
Note:
System reboot when I use "make -j4" on raspberry 3. I also tried "make" on raspberry-zero board. It took about half a day to compile. Therefore, it's very useful to use multiple job tasks and this will save a lot of time.
Error:
[ 65%] Linking CXX static library ../../lib/libopencv_ts.a
[ 65%] Built target opencv_ts
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
I solved this error in this way:
- make clean
- use Cmake to generate Makefile
- make (not make -jx)
Of course, this will take a longer time to make.
Assuming OpenCV compiled without error (as in my screenshot above), you can install your optimized version of OpenCV on your Raspberry Pi:
sudo ldconfig
Don’t forget to go back to your /etc/dphys-swapfile file and:
- Reset CONF_SWAPSIZE to 100MB.
- Restart the swap service.
Testing your optimized OpenCV + Raspberry Pi install
I installed OpenCV directly on OS, rather than Python Virtual Environment.
Python2.7:
pi@raspberrypi:~/opencv-3.3.0/build $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> quit()
Python3:
pi@raspberrypi:~/opencv-3.3.0/build $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2'
>>> quit()
If you compiled OpenCV for Python 3, you need to issue the following commands to sym-link the cv2.so bindings into Python3:
pi@raspberrypi:~/opencv-3.3.0/build $ cd /usr/local/lib/py
pypy2.7/ python2.7/ python3.5/
pi@raspberrypi:~/opencv-3.3.0/build $ cd /usr/local/lib/python3.5/site-packages/
pi@raspberrypi:/usr/local/lib/python3.5/site-packages $ ls
cv2.cpython-35m-arm-linux-gnueabihf.so
pi@raspberrypi:/usr/local/lib/python3.5/site-packages $ sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so
pi@raspberrypi:/usr/local/lib/python3.5/site-packages $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> quit()
Test OpenCV with 'cv' Python3 virtual environment:
(cv) pi@raspberrypi:~ $ python
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2'
>>>
If you compiled OpenCV for Python 3, you need to issue the following commands to sym-link the cv2.so bindings into your cv virtual environment:
(cv) pi@raspberrypi:~ $ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
(cv) pi@raspberrypi:~/.virtualenvs/cv/lib/python3.5/site-packages $ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so
(cv) pi@raspberrypi:~/.virtualenvs/cv/lib/python3.5/site-packages $ cd ~
(cv) pi@raspberrypi:~ $ python
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.0'
Congratulations!!!
Reference:
- optimizing-opencv-on-the-raspberry-pi, https://www.pyimagesearch.com/2017/10/09/optimizing-opencv-on-the-raspberry-pi/