Overview
There are already several posts about how to install OpenCV on a Rasberry Pi 3 Ex. [1] and [2], yet no one has made use of conda - a really powerful open source package management system. It would save your hours of building time and provide a error-free experience. Thus this post will show you how to install miniconda on your Raspberry PI and use conda to install OpenCV.
Install miniconda on Rasberry pi
Conda is an open source package management system and environment management system that runs on Windows, macOS and Linux. Conda quickly installs, runs and updates packages and their dependencies and easily creates, saves, loads and switches between environments on your local computer. By using it we can install thousands of packages on pi with one line of command and customize different environments. This post choose to use miniconda.
PS: Miniconda is a distribution of conda that includes only conda and its dependencies. It has a much smaller size conpared with Anaconda which has over 150 scientific packages included.
Miniconda installation follows this stackoverflow [question].
wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-armv7l.sh
sudo md5sum Miniconda3-latest-Linux-armv7l.sh
sudo /bin/bash Miniconda3-latest-Linux-armv7l.sh
When installing change the default dir installation from /root/miniconda3 to:
/home/pi/miniconda3(Or to your own target location)
Conda would ask if you want it to add environment variable to root path.
Normally it's a good idea to not pollute your root environment. Otherwise you would run into weird compile error by linking against the wrong library or a headless runtime error in the future. Add the environment variable later by yourself is a smart move.
Edit the .bashrc file
```
sudo nano /home/pi/.bashrc
...and add the following line to the end of the file
export PATH=/home/pi/miniconda3/bin:$PATH
Save and close this file & reboot Raspberry pi
sudo reboot -h now
After reboot enter the following command "python --version" which should give you:
Python 3.4.3 :: Continuum Analytics, Inc.
If you are new to conda, I recommend read this [conda tutorial] .
Install OpenCV via conda
Congruations! You've seccessfully installed conda. Now let's move to OpenCV.
Firstly, I would recommend calling sudo chown -R <user-name> miniconda
where normally user-name is *pi* here. It's because the owner of miniconda is set to root instead of pi during installation. If you see an error message as `Error: Missing write permissions in: <path-to-your-conda>`, you should try the this command.
## Search available opencv packages for armv71
Due to the fact that raspberry pi has a CPU architecture as armv71, we need to find a available package first.
Type anaconda search -t conda opencv
you would see a list of available packages:
After several trials I choose to use the **poppy-project/opencv3** package whose OpenCV version is 3.1.0 and needs Python 2.7. You are welcome to try other packages listed. Please leave a comment if you find a package work with python 3!
First, we need to create a python 2.7 conda environment.
conda create --name python2 python=2
source activate python2
Now you should see (python2) is prefixed before your user name.
Run anaconda show poppy-project/opencv3
then conda would guide you to use
conda install --channel https://conda.anaconda.org/poppy-project opencv3
to install the package.
Type python
, import cv
2 then cv2.__version__
to verify your installation:
If you see 3.1.0 then you nail it!
During my trial, I ran into an error as `ImportError: libjpeg.so.8: cannot open shared object file: No such file or directory`. It's because the `LD_LIBRARY_PATH` on your system is not set properly so that the dynamic linking goes south.
A quick solution would be add the directory that contains `libjpeg.so.8` to your `/etc/ld.so.conf` file, and then run `ldconfig`.
Comments