Is there an easy way to install OpenCV on Raspberry pi - opencv

There are many steps while installing OpenCV on Raspberry pi . Is there any chance to install by using a single command. I tried sudo apt-get install python3-opencv but that doesn't work.

The main one liner is with pip The Python Package Installer but to make it work you also need some dependencies that may or may not be installed already from other related tools.
$ pip3 install opencv-python
$ sudo apt-get install libatlas-base-dev libjasper-dev libqtgui4 python3-pyqt5 libqt4-test libilmbase-dev libopenexr-dev libgstreamer1.0-dev libavcodec58 libavformat58 libswscale5
In addition with the current newest version you need to set an env variable to run it, LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0 referenced issue
$ LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0 python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
If you are really opposed to that you can use a previous version that doesn't have this issue. Don't forget to uninstall the other version if you already installed it pip3 uninstall opencv-python
$ pip3 install opencv-python==4.1.0.25
On a newly flashed 2019-09-26-raspbian-buster-lite img after adding ssh access and connecting that way.
system update and install pip3
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install python3-pip
version check
$ python3 --version
Python 3.7.3
$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
install openCV and check version
$ pip3 install opencv-python
$ pip3 freeze | grep opencv-python
opencv-python==4.1.1.26
test and fail,
$ python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: libwebp.so.6: cannot open shared object file: No such file or directory
install dependencies
$ sudo apt-get install libatlas-base-dev libjasper-dev libqtgui4 python3-pyqt5 libqt4-test libilmbase-dev libopenexr-dev libgstreamer1.0-dev libavcodec58 libavformat58 libswscale5
test with env variable set
$ LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0 python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

The easiest way to do this is using .whl files. When you want to install a package with pip, it searches for a precompiled version of that package that is compatible with your system.
In your case, pip is looking for an OpenCV Python package compatible with armv7l platform, if it cannot find a precompiled .whl file to install, it tries to download the source as .tar.gz and compile it by itself on Raspberry Pi (which is a real pain.)
armv7l compatible .whl files are available for OpenCV Python now and you can use pip to install it directly with pip install opencv-python command but sometimes pip tries to install the latest package and sometimes it is not armv7l compatible. I suggest you check packages and put version specifications to the command.
To handle this situation a lot easier with installations of prerequisites and preparation of the installation environment; you can use complete guides like in this blog post for beginners

Related

Couldn't install opencv-python with python version Python 3.6.5 :: Anaconda, Inc

I have darknet and opencv4.2.0 with Ubuntu 18.04
however I couldn't import CV2 while using python (darknet works well)
when i use sudo pip3 install opencv-python
terminal shows
Installing through navigator doesn't work either

Installing opencv-python on raspberry pi 3, Raspbian Stretch

As above, trying to install opencv-python.
Normally this would be a simple pip install opencv-python, but it doesn't seem to work. Instead, opencv install to the miniconda directory
/home/pi/miniconda3/lib/python3.5/site-packages
instead of global
/usr/lib/python3.7
Trying to add opencv-python in Thorny through the application fails. In the python program...
import sys
sys.path.append('/home/pi/miniconda3/lib/python3.5/site-packages')
import cv2
results in a
No module named 'cv2.cv2'
I've tried adding it to path also (export PYTHONPATH=/home/pi/miniconda3/lib/python3.5/site-packages:$PYTHONPATH) with no luck.
Because pip is linked to your default Python and I think this default is your miniconda. A better approach is to call the Python version directly with pip. Additionally #Dave W. Smit mentioned that you should better install opencv-python-contrib to use the full OpenCV package (but don´t use both packages!).
$ python-3.7 -m pip install opencv-python-contrib
Or you can use the pip version (if pip is at least version 0.8)
$ pip-3.7 install opencv-python-contrib
Or with pip version 1.5+
$ pip3.7 install opencv-python-contrib

Install packages from PYPI in Raspberry Pi

I want to install a precompiled version of OpenCV 3.4.2 in the Raspberry using the following commands:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install pip3
sudo pip3 install opencv-python
But it failed and the error message was: "Could not find a version that satisfies the requirement opencv-python (from versions: ) No matching distribution found for opencv-python"
Then tried the following commands but the result was the same:
sudo apt-get install python3-pip
sudo pip install opencv-python
I had not problems installing it on my PC with ubuntu 16 using the same commands. This is the page im using for reference https://pypi.org/project/opencv-python/
I'm using Raspbian Jessie Lite, Python 3.4
The list of downloadable files for opencv includes binary wheels for many Python versions but for limited set of architectures: MacOSX on Intel, Linux on Intel 32 and 64 bits, Windows on Intel 32 and 64 bits. Raspberry Pi processors absent and there is no source code so pip cannot compile it.
The FAQ recommends to look up Pi wheels at https://www.piwheels.org/.

Problems installing scikit-image in ubuntu 14.04

When I try to install scikit-image with pip2 there is show error. I have tried to find this problem solution.
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/scikit-image
Storing debug log for failure in /home/nshakib/.pip/pip.log
My guess is that you might have missing dependencies. I don't think Ubuntu 14.04 comes out of the box with any of the SciPy stack.
To answer your question try,
sudo apt-get install python-matplotlib python-numpy python-pil python-scipy
Then
sudo apt-get install build-essential cython
Finally
sudo apt-get install python-skimage
If that still doesn't work, you could try using the Anaconda Distribution for Python 2.7 from Continuum Analytics.
It comes with many of the major packages built-in and has a great package manager that manages dependencies.
https://docs.continuum.io/anaconda/install#anaconda-install
The conda package would be installed with conda install scikit-image

brew install python/matplotlib not working: No module named _tkinter

I am struggling to make basic plots in matplotlib. It looks like I'm not installing tkinter correctly, which I understand should be installed from the --with-tcl-tk flag. What could be causing my error? How can I better diagnose this problem? Downloading the ActiveState tkinter has not helped me.
I uninstalled then reinstalled everything I thought was relevant. No errors were raised by brew.
$ brew uninstall python
$ brew uninstall matplotlib numpy --force
$ brew install python --with-tcl-tk
$ brew install numpy matplotlib --with-tcl-tk
However, I get an error when I import pyplot:
$ python
Python 2.7.11 (default, Dec 22 2015, 12:47:31)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/local/lib/python2.7/site-packages/six.py", line 199, in load_module
mod = mod._resolve()
File "/usr/local/lib/python2.7/site-packages/six.py", line 113, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python2.7/site-packages/six.py", line 80, in _import_module
__import__(name)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
>>>
This is a little faster (and hopefully safer) than the uninstall method...
1) Ensure you have Brew's Tcl/Tk installed (it is kept separate from the MacOS one)
brew install tcl-tk
2) Now recompile Python with Tcl/Tk enabled
brew reinstall python --with-tcl-tk
In my case I solved this by uninstalling and reinstalling Python using homebrew. I hope there is a better solution that someone else can provide...
$ brew uninstall python#2
Uninstalling /usr/local/Cellar/python#2/2.7.14_3... (4,324 files, 77.4MB)
$ brew install python#2
==> Downloading https://homebrew.bintray.com/bottles/python#2-2.7.14_3.el_capita
######################################################################## 100.0%
==> Pouring python#2-2.7.14_3.el_capitan.bottle.2.tar.gz
==> /usr/local/Cellar/python#2/2.7.14_3/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python#2/2.7.14_3/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python#2/2.7.14_3/bin/python -s setup.py --no-user-cfg ins
==> Caveats
Pip and setuptools have been installed. To update them
pip install --upgrade pip setuptools
You can install Python packages with
pip install <package>
They will install into the site-package directory
/usr/local/lib/python2.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
🍺 /usr/local/Cellar/python#2/2.7.14_3: 4,669 files, 82.4MB
Pip kept everything, and the venv I was using was also still intact.

Resources