Problems with OpenCV install on Raspberry Pi - python

I have a Raspberry Pi running Raspian 10. I installed OpenCV using this tutorial. The steps seemed to all complete successfully, but I've had some problems importing the python module. When I use Python 3.7.3, I get the following output:
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
Which is clearly not working. Using Python 2.7.16 however, it runs fine:
>>> import cv2
>>> cv2.__version__
'4.6.0-dev'
Any idea why this may be? It seems like the OpenCV Python module isn't installed globally for Python 3. Ideally, I'd like to be able to use it in a Python 3 virtual environment.
I do have a temporary workaround. If I cd into the build directory containing the module, I can get it to work:
pi#raspberrypi:~ $ cd opencv/build/python_loader/
pi#raspberrypi:~/opencv/build/python_loader $ ls
cv2 setup.py
pi#raspberrypi:~/opencv/build/python_loader $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.6.0-dev'
Obviously, this isn't ideal.

I've managed to answer my own question. Running:
pi#raspberrypi:~/opencv/build $ pip3 install -e python_loader
Installs the package from the folder containing the module. It's not the most elegant solution, and I'd still be interested to see if anyone knows why the original method didn't work.
Hope this can be of some help to someone.

Related

Numpy import error even after installing it with current python version

Note - I already check numpy import error related threads but none helped
I am using debian 8 where default python is 2.7.9. I installed python 3.4.2 and created virutal env.
Within virtual environment -
python -V
Python 3.4.2
pip -V
pip 1.5.6 from /path/venv34/lib/python3.4/site-packages (python 3.4)
I have python3 numpy package - python3-numpy_1.12.0-2~pn0_amd64.deb
which I have installed with sudo dpkg -i python3-numpy_1.12.0-2~pn0_amd64.deb
which successfully completed.
Now when I do
python
Python 3.4.2 (default, Feb 7 2019, 06:08:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
>>>
Any clue what's wrong here?
python3.4 -m pip install numpy==1.12.0-2
ok since my repo is less than 50 i can not add comments, so take this answer as a comment to your question.
I think numpy is installed but not in your virtualenv, make sure your virtualenv is active when you are trying to install any library, you will see virtualenv name in every command line if it is activated.
(venv) C:\Users\seventeen\sprint25>
Try python -m pip install numpy==1.12.0. This should help you.

Python 3.7 import gpiozero ModuleNotFoundError No module named 'gpiozero' on raspberry pi zero

Built Python 3.7 on my Raspberry pi zero in a attempt to upgrade from Python 3.5.3
The build was successful, ran into the module not found for smbus and switched that to smbus2, now when I import gpiozero I get Module not found. my DungeonCube.py program was working fine under Python 3.5.3, but now Python 3.7 seems to have trouble finding gpiozero
this is what I did to test:
python3
Python 3.7.0 (default, Sept 7 2018, 14:22:04)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiozero
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gpiozero'
>>>
anyone know how to get python 3.7 to see gpiozero module?
I had same problem, realized that i used pip3 to install the package, but I was trying to use it with python which invokes python2. I tried with python3 it works just fine.
did you download the gpiozero module onto the raspberry pi? it does not come preinstalled with python.
you could try to do "sudo python3 pip install gpiozero". if that doesnt work replace python3 with python #GarryOsborne .

ImportError: No module named tensorflow, but tensorflow does exist

I installed tensorflow with virtualenv on linux. There is a tensorflow package under sitepackage folder, but when I run the demo downloaded from Github, it shows:
(tensorflow) idc#idc-Hi-Fi-Z77X:~/tensorflow$ sudo python CNN_sentence_tensorflow-master/sentence_classfier_with_tensorflow.py
Traceback (most recent call last):
File "CNN_sentence_tensorflowmaster/sentence_classfier_with_tensorflow.py", line 13, in <module>
import tensorflow as tf
ImportError: No module named tensorflow
I do this at interface. It's ok:
(tensorflow) idc#idc-Hi-Fi-Z77X:~/tensorflow/multi-class-text-classification-cnn-master$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import tensorflow as tf
>>>
you are using sudo python CNN_sentence_tensorflow-master/sentence_classfier_with_tensorflow.py
if you use sudo i think it will use your main python version not the one in your virtualenv
Because sudo python basically means run python as some other user (root by default). That user may have a different set of environment variables, including $PATH.
Some of linux distributions use older Python version for root user,like centos.Have a look at the outputs of which python and sudo which python, you'll see they might be different.
[~]$ which python
/usr/local/bin/python
[~]$ sudo which python
/usr/bin/python
Maybe you don't need to use sudo,or you can set permissions to all files and folders by using chmod -R 755 /folder.
Hope this helps.

Cannot import matplotlib into Python 3

I am attempting to teach myself programming and keep running into problems downloading modules I need for basic tutorials.
My latest attempt has been to get the matplotlib module into my Python 3 environment. I have tried so many different install packages and so many advice I found on the internet that I cannot remember how I originally got the module. But it seemed that everything went well with the installation process.
I am using a Raspberry Pi2 throughout all of this with the Raspbian OS installed.
Python 3.2.3 (default, Mar 1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import matplotlib
ImportError: No module named matplotlib
It may be possible that you actually installed the library for python 2. I would use pip to install the modules that you need. If you don't already have it installed, run this first command in the shell:
sudo apt-get install python3-pip
This program can be called with "pip3" or maybe "pip-3.2" in the shell.
To install mathplotlib with it, try running:
pip3 install matplotlib
or
pip-3.2 install matplotlib
That should install the Python 3 module for you.

Python module for OpenCV requires a library that's unavailable to Ubuntu 14.04

I'm trying to use OpenCV from within Python on an system using Ubuntu 14.04. When I try to import cv2, I get the following error:
me#Bedrock1:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libavcodec.so.53: cannot open shared object file: No such file or directory
A search for libavcodec.so.53 reveals that my system has only libavcodec.so.54.
A search of the internet reveals that libavcodec.so.53 has been removed from the 14.04 repository (http://www.ubuntuupdates.org/package/core/trusty/universe/base/libavcodec53). This, probably, explains my inability to apt-get install this library:
me#Bedrock1:~$ sudo apt-get install libavcodec53 libavcodec-extra-53
...
E: Package 'libavcodec53' has no installation candidate
E: Package 'libavcodec-extra-53' has no installation candidate
Unfortunately, now I'm truly stuck.
Is there a way to get the cv2 module to use libavcodec.so.54, or a way to find & install libavcodec.so.53?
NOTE: I just tried reinstalling by following the instructions at https://help.ubuntu.com/community/OpenCV
but I still am getting the same libavcodec.so.53 error....
Try this script. Ubuntu man page has an old version of this script. This should work.
https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/2.4/opencv2_4_9.sh
Check if reinstalling opencv resolves the issue
sudo apt-get install --reinstall python-opencv
BTW how did you install it before ?
I got the same problem on Ubuntu 14.04 64bit, as reported here:
https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/1161485/comments/2
I tried downloading the source package and recompiling, in order to get the correct libraries linked, but with no luck.
At the end, I recompiled from source, following these instructions:
http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html
Everything goes fine except for the python module compilation, which was disable at configure time (listed in Opencv modules: Unavailable: section).
I used cmake-gui to configure the build and generate makefiles. The configuration step was correctly setting PYTHON2_INCLUDE_DIR, PYTHON2_INCLUDE_DIR2 and PYTHON2_LIBRARY, but it was NOT setting PYTHON_INCLUDE_DIR, PYTHON_INCLUDE_DIR2 and PYTHON_LIBRARY.
In order to actually build the python module, you should set these PYTHON_ variables equal to their corresponding PYTHON2_ variables. Then, the make step will build also cv2.so.

Categories

Resources