How to import OpenCV into python - python

I have downloaded Anaconda on my Windows PC, and I have been using Spyder IDE. Now I want to do a project with OpenCV.
However, I have tried to install OpenCV using the Command Prompt and typing the following in:
pip install opencv-python
This only gives an error message and says "pip" is not recognized as a command.
Furthermore I tried to import OpenCV into Python using the following:
import cv2
This also doesn't work
I would appreciate any help in getting OpenCV working.

It depends on Your OS. Check Your pip installation with cmd: pip -V and check python with: python -V
It seems like You don't have pip installed at all.
Than import cv2 should work.

Related

How to resolve this error of "Cannot find reference 'imread' in '__init__.py" in python after installing opencv?

I am new to python and installed opencv by typing: pip install opencv-contrib-python in the command prompt.
Below I have shown a very simple example of reading in an image and showing it on the screen:
However, I get this error:
When I hover over the imread method in PyCharm, it says Cannot find reference 'imread' in '__init__.py'
It was showing import and runtime errors when I typed import cv2 and import numpy in the command prompt.
I tried going back to the previous version of NumPy by typing pip install --force-reinstall numpy=1.19.3. So, now when I type import cv2 and import numpy in the command prompt, it shows no error but it says it still can't find a reference to it in PyCharm.
I am using Python 3.9.1 with pip 20.2.3.
If you are using python interpreter with anaconda change it to default python one. if you have not installed python separately then install it and assign the path to it.
somewhat this will be the path
c://user/APpData/Local/Programs/Python/python39/python.exe
The NumPy multicore import error is a result of installing NumPy on Windows that is incompatible with the version of OpenCV that you have. Judging from your screenshots as well as the error, you are running Windows. Try installing Christoph Gohlke's NumPy libraries instead that were built with multicore support enabled.
First do:
pip install pipwin
Then:
pipwin install numpy
This should hopefully settle the OpenCV dependency problem you have.
To solve it, you should upgrade numpy installation. Try:
pip install -U numpy
For information here
Instead of doing:
import cv2
try doing:
from cv2 import cv2
This worked for me after a lot of troubleshooting also using pycharm. I now have access to all of the regular cv2 methods.

How can I fix "no module named cv2"?

Yesterday it seems like my OpenCV is working fine, but today when I reinstall and hit repair the python that I was using. I can't access import cv2 anymore. I tried to reinstall python and reinstall OpenCV but it's not working too. How can I fix this? When I import cv2 at cmd it seems to be working (cmd screenshot) but when I used python at the visual studio 2019 it says "no module named cv2" visual studio screenshot. How can I fix this? Please help.
If you're using Python 2 enter the following:
pip show opencv-python
If you're using Python 3 enter the following:
pip3 show opencv-python
Can you see information about opencv-python package? (name, version, summary..etc)
If yes then ensure Python is added to PATH and you're running the Python version where the opencv package is installed.
If no then you didn't install opencv-python.
If you're using Python 2 enter the following:
pip install opencv-python
If you're using Python 3 enter the following:
pip3 install opencv-python

Python Import Error, Symbol not found: _iconv Referenced from:

I've successfully running Python 2.7.15 (default one) on macOS Sierra 10.12.6.
I have also installed OpenCV using pip of version 3.4.0, using this tutorial.
Screenshot: Python IDLE Screenshot showing OpenCV version on macOS
When I try to import cv2 from python IDLE it gets successfully imported, but when I try import cv2 from a python script and run as cgi script, it shows the following error:
Error Screenshot: OpenCV import error while importing and running from browser
What does the error actually mean?
How can I solve this?
Note: I've no errors while running python as CGI scripts (without importing opencv).
Note: I've even tried of installing OpenCV using Homebrew but it still produces the same error while importing OpenCV as a CGI script.
Very common error with OpenCV, the best move you can do is to install OpenCV using Anaconda.
I recommend you to fully uninstall Python and install Anaconda Python, it comes with the conda package manager that will allow you to install opencv--python easily.
https://www.anaconda.com/download/
Once Anaconda Python is installed, try the following commands:
conda install -c conda-forge opencv
conda install -c menpo opencv3

Unable to install cv2 on windows

I am trying to install opencv in python on my windows machine but I am unable to do so. I have python 2.7.11::Anaconda 2.4.1 <32-bit>
Here is what I have tried till now -
pip install cv2 on command line gives the error :
could not find a
version that satisfies the requirement cv2
I downloaded the package from sourceforge site, followed the steps
and pasted cv2.pyd in C:\Python27\Lib\site-packages but still it is
not working. I get the following error message
ImportError: No
module named cv2
(I already have numpy installed and it works just fine).
pip install opencv-python
you can type this instead of
pip install cv2
it also works with anaconda pro
I was able to solve the error.
If you are using python version 3 , sometimes you have to use pip3.
pip3 install opencv-python
Make sure you are using python 3 , it won't work for python 2.
Try
pip install opencv-python==4.3.0.36
So I was using PyCharm, and what worked for me was to install it directly from file->settings, Project:your-project-name->Python Interpreter list

installing opencv for python in Ubuntu 14.04

I ran the following command on terminal
$ sudo apt-get install libopencv-dev python-opencv
This installed opencv version 2.4.10.
After that I open python in terminal and try to import opencv as follows
>> import cv2
This gives me an error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2
I also tried using import cv, import opencv, etc. but I am getting the same error.
Do I need to follow some more steps to configure opencv for python ??
This happens when python cannot refer to your default site-packages folder where you have kept the required python files or libraries
Add these lines in the code:
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
or before running the python command in bash move to /usr/local/lib/python2.7/site-packages directory. This is a work around if you don't want to add any thing to the code.
OR
try adding the following line in ~/.bashrc
export PATH=/usr/local/lib/python2.7/site-packages:$PATH
This happens when python cannot refer to your default site-packages folder where you have kept the required python files or libraries
Add these lines in the code:
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
or before running the python command in bash move to /usr/local/lib/python2.7/site-packages directory. This is a work around if you don't want to add any thing to the code.
There is an installer for Ubuntu 16.04, and it may work well on Ubuntu 14.04, you could have a try. I have used it to install on Ubuntu 16.04 and it succeed!
An interactive installing script for install openCV on Ubuntu 16.04 LTS
The Opencv version(2.4.10) installed is for python2x version.
I think you are trying to use cv2 in python3x version (which might be set as default for python)
Open python2 on terminal (use command python2 instead of python)
>> import cv2
This will work.
I think it is better that you just install Anaconda python distribution.
https://www.continuum.io/downloads
You can find wealth of tutorials in the internet on how to install it in your system. And trust me, it is VERY EASY to install.
After you have install your Anaconda python distribution, you can install OpenCV 3.1 by the following commands. Note that you should have an internet connection.
# if you are using Anaconda for Python 2.7
conda install -c menpo opencv
The above code should install OpenCV 3.1 in your anaconda python 2.7
# if you are using Anaconda for Python 3.5
conda install -c menpo opencv3
The above code should install OpenCV 3.1 in your anaconda python 3.5
Then to verify that you have successfully install OpenCV 3.1 in your system, you can issue the following command in the python interpreter:
# import the opencv library
import cv2
# prints the version of the OpenCV installed in your system
cv2.__version__
That's it. I hope that helped you =)
Try using this:
sudo apt-get install python-opencv opencv-dev python-numpy python-dev

Categories

Resources