Import "cv2" could not be resolved - python

I've installed python and opencv by using the correct commands in the cmd, but when I type in 'import cv2' in a python then I get the error in visual studio code and I can't find any solution online how to fix this
Import "cv2" could not be resolved Pylance (reportMissingImports)
More of the error in the terminal
ModuleNotFoundError: No module named 'cv2'

It just happened to me and I solved it installing both opencv-python and opencv-python-headless with pip and reloading the Visual Studio Code window right after it.
To install the needed packages, just run this command in the terminal:
$ pip install opencv-python opencv-python-headless

Make sure that its using the correct version of Python; e.g., I installed Python from the windows store, which is working:
However, if I use another one, it does not work:

I noticed that Visual Studio does not support the latest Python 3.8, so I had to use PyCharm as an IDE.

I had a similar issue so I installed opencv-python-headless (install opencv-python if not earlier) and reloaded the VScode window.
pip install opencv-python-headless
However, some methods like imshow() kept failing:
cv2.error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
This was resolved by installing opencv-contrib-python. You can install it by running the following code in your terminal:
pip install opencv-contrib-python

In your terminal (cmd if Windows) run the following (make sure that you're on the current working environment):
pip install opencv-python
then:
pip install opencv-python-headless

I just uninstalled the older python version and installed a new one again. That is probably the simplest and the most hassle-free one in my mind. Stick to version 3.8.8, that should do the trick. Also no worries about maintaining multiple versions of python in the system. It is indeed true that the lack of support from VS is causing this issue.

I am on a mac and struggle with this and this is the best way to do it, when ur in your .py file, on the lower left part of your vscode windown u will see the version of python you are using, click that and try different versions until your open cv command works, in my case, 3.8.8 worked for me.

Related

How do I solve unspecified error with cv2.imshow?

My code keeps breaking only at points where I use the cv2.imshow() function. It displays this error:
error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
It was working fine until after I installed another library, but I'm not sure how that would mess it up. I tried uninstalling that extra library but that didn't do anything to fix the issue. Any ideas? Some of the solutions I've found online are to install with pip install opencv-python, but that's how I installed it initially. I have also tried uninstalling and reinstalling opencv to no avail.
Additional details based on follow-up questions: I'm on a Windows computer using the Pycharm IDE, and I've been installing libraries using pip in the Miniconda app.
Here is what the code looks like. The error happens anytime I try to use imshow, so here's one example of code I run that causes the error:
import cv2
img = cv2.imread("Tropical-tree.jpg")
cv2.imshow("Image", img)
cv2.waitKey(0)
For future reference if anyone else has this issue, here's how I solved it
I had been using an environment I made in the Miniconda3 program. To fix the issue, I made a new environment (also in Miniconda) using this command:
conda create --name new_env python=3.6
Opencv works best with python 3.6 rather than the newest update, so it's important to specify the version when creating the environment. Then I went in the environment and reinstalled opencv using pip install opencv-python, and then in Pycharm I switched to use that environment and it worked fine. Hope this helps anyone else who runs into this!

ICan't Do Anything With Pip because it won't install anything

Any time i try to install anything with pip i would get this type of error. I've updated my computer, updated visual studios, pip 20.0.2, python 3.8.2, and still nothing works, I've even tried reinstalling everything.
ERROR: https://i.stack.imgur.com/m8Yqv.png
You need to install PortAudio on your system to use one of the libraries you're trying to install.
try using pip3 instead of pip or check paths

Error in installing OpenCV with python (MAC OSX) and PyCharm IDE

I want to install the OpenCV to then include it in PyCharm on my Mac, and
I have started with the terminal following this guideline: click here
the problem is when I follow the third step and write this code:
$ brew install opencv3 --with-contrib --with-python3
this error has shown:
Error: invalid option: --with-contrib
Of course I have installed also the PyCharm IDE for python and the latest verson of python (3.7) to be knowledged
please if you can help me I will appreciate that
This problem was solved! thank you
According to the formula, opencv3 no longer requires these flags.
CMake flags
-DOPENCV_ENABLE_NONFREE=ON
-DOPENCV_EXTRA_MODULES_PATH=#{buildpath}/opencv_contrib/modules
enable support for contrib modules.
-DBUILD_opencv_python2=ON
-DBUILD_opencv_python3=ON
-DPYTHON2_EXECUTABLE=#{which "python"}
-DPYTHON2_LIBRARY=#{py2_lib}/libpython2.7.dylib
-DPYTHON2_INCLUDE_DIR=#{py2_prefix}/include/python2.7
-DPYTHON3_EXECUTABLE=#{which "python3"}
-DPYTHON3_LIBRARY=#{py3_config}/libpython#{py3_version}.dylib
-DPYTHON3_INCLUDE_DIR=#{py3_include}
provide support for Python3 as well as Python2.
Just run
brew install opencv3

OpenCV: AttributeError: module 'cv2' has no attribute 'face'

I am creating a face recognition system using Python and OpenCV on these versions:
Python 3.6.2 :: Anaconda custom (64-bit)
Anaconda 4.3.23
OpenCV 3.3.0
When I try to train the face recognizer:
face_recognizer = cv2.face.createLBPHFaceRecognizer()
I get this error:
AttributeError: module 'cv2' has no attribute 'face'
Update:
I've tried to do this:
pip install opencv_python‑3.3.0+contrib‑cp36‑cp36m‑win_amd64.whl
Also:
conda install -c menpo opencv3=3.3.0
And I still have the error.
The Menpo project does not have an installer for OpenCV 3.3. The Menpo project is up to 3.1 on macOS and Windows, and 3.2 on Linux. See the Anaconda package for that description and also the list of files for the installer versions. Actually, you can check out the GitHub repo for Menpo's OpenCV3 build and grab the files yourself. You can change the build files to suit your system if needed.
I'm not sure if your pip attempt includes a typo or not---the correct PyPI package wheel file with the contrib module is opencv_contrib_python not opencv_python+contrib, as shown at PyPI. Note that if you're not using Windows the GUI features of OpenCV will not work with the pip installer, including imshow() and other similar features.
You'd be better served just removing and reinstalling fresh with the contrib modules instead of trying to build them in later.
While I was looking for the same solution, I tried out many methods which don't work well with successfully installing OpenCV along with the extra modules i.e., OpenCV Contrib.
Apparently, while using pip install opencv-python windows platforms usually download only OpenCV without the extra modules!
What works, is stable and easy to install:
Download the integrated 'whl' file containing both OpenCV and it's Contrib files, which would be like 'opencv_python‑3.4.3+contrib‑cp36‑cp36m‑win_amd64.whl' which can be downloaded from here.
Install using pip install <whl filename>
I have tried other methods which are unreliable such as the solution suggested by #RoyaumeIX, however ended up with failure.
So is it with using
pip install opencv
pip install opencv-contrib
Installing opencv-contrib does not properly register the opencv package.
I strongly suggest that you directly download the official whl file and install it.
I also had same problem but it got resolved by following these steps:
start anaconda navigator
open CMD.exe prompt, hope you see this **(base) C:\Users\acer>** this may be different for you
write these command >>>**pip install opencv-contrib-python**
Now you can run your code as check!! I have runned my code in Spyder in base enviroment and it worked for me!
my using python 2.7 and opencv 3.3.0
working in code
cv2.face.LBPHFaceRecognizer_create();
Solution, as were found at OpenCV forum (and same at StackOverflow), works well for me:
pip install opencv-python
pip install opencv_contrib_python
And in cv2 version 4.0.0 face recogniser can be created by using different function name, as mentionted above:
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer = cv2.face.EigenFaceRecognizer_create()
face_recognizer = cv2.face.FisherFaceRecognizer_create()
this should fix the problem
pip install opencv-python-headless
pip install opencv-contrib-python-headless
I found the solution to my issue, you have to follow this tutorial OpenCV with extra modules.
The essential steps are:
Go to C:/PythonXX/lib/site-packeges (the site-packages folder where your python is installed), and delete cv2.pyd if present.
Download OpenCV with extra modules
Create a Visual Studio project with CMake
Open Python IDLE and enter import cv2. If there is no error, then the installation is successful.

Python 2.7.11 pip not installed

I have Python 2.7.11 installed on my machine which to my understanding should come with pip, however when I check the C:\Python27\Tools\Scripts\ directory there is no pip.exe present.
I have tried completely removing and reinstalling Python 2.7.11 without success. Running the installer pip is set to be installed, but after the install pip is nowhere to be found.
I also have Python 3.4 installed which has pip as expected. Any thoughts?
I encountered the same problem - pip not installed - with python-2.7.16, Win10, installing for 'all users'. It was resolved when I allowed the MSI installer to target the default location (C:\Python27) rather than changing it to under Program Files (x86). I've no clue why this changed anything.
python2.7.11 should install pip into c:\python27\scripts, you can take a look here
I used https://pip.pypa.io/en/stable/installing/ to make it install. Odd that an outside body has to make a tool to get pip to install. There is no mention of pip.pypa.io on the Python web site.
Although I got error messages, it did install, so check carefully.
You also may see a message suggesting that you upgrade to pip 9.0 with the command 'pip install --upgrade.' I highly recommend that.
Also, make sure you are in the directory where pip.exe is located when you run the commands. They don't tell you that, because they assume you know that, but we're not all geeks.
For people who are running into this issue, I have a Windows 10 x86 dev box that I use for exploit development, Python 2.7 was installed due to Immunity Debugger, this install did not include the new SSL package and did not include "pip".
There was no "C:\Python27\Scripts" folder which included pip. I did have a "C:\Python27\Tools\Scripts" folder which did not have pip installed.
I tired to install pip as suggested but did not work. Best way is to uninstall Python and install newest version, currently mine is 2.7.15 which came with pip as an option. This is what solved my issue, any older version of Python will need upgraded to support the SSL packages. You will receive the same errors when you try to install pip on an older version.
If you have reinstalled Python which included the SSL package and it still does not have pip, trying installing pip this way:
1. curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
2. python get-pip.py
https://pip.pypa.io/en/stable/installing/
This should work if you have a newer version of Python.
Once you have installed pip or newer version of python, go to "C:\Python27\Scripts" and you should see pip.exe.
I have installed "python-2.7.16" in my windows 10 PC. PIP is installed under "C:\Python27\Scripts". One can add the "C:\Python27\Scripts" path to environment variable and then access pip command from command prompt.
To add the path in environment variable:
Control Panel\System and Security\System --> Advanced System Settings --> Advanced --> enviroment variables --> under system variable --> Path(variable name)
Had the issue where no matter which version of python 2.7 I installed on windows 10 there was no pip.exe generated in the "Scripts" folder.
I solved it by ensuring that that MSI Installer file had Admin privileges before installing
if pip is not installed you can use easy_install.exe to install pip and other modules
you can find easy install in
python -m easy_install pip
I had the same issue:
Installed Python 27
Tried to use pip, but failed with unrecognized command error
Checked installation: no "C:\Python27\Scripts", only "C:\Python27\Tools\Scripts"
This issue happens only on some versions of Windows.
HOW TO FIX IT:
Uninstall Python 27
Reinstall Python 27, but unselect "pip" feature
Check installation: no "C:\Python27\Scripts" as expected
Start the installer again and use "Change Python"
Set "pip" and "Add Python.exe to Path" features to be installed
Check installation: "C:\Python27\Scripts" is now correctly present
So for some unknown reason, pip is not correctly installed on some versions of Windows if it is installed during default Python 27 setup. To fix this issue, pip must be installed afterwards using the "Change Python" setup.
I meet the same issue when install Python 2.7.9. The installer does not come with pip.
Solution:
Install newer python2 x86 version: https://www.python.org/downloads/release/python-2718/
Then pip is located at C:\Python27\Scripts\pip.exe
It happens on windows as you should have admin rights to install anything on disk C.
I have the same issue Scripts folder was not installed. I would sugest to instal it on disk D.

Categories