How do I solve unspecified error with cv2.imshow? - python

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!

Related

Import "cv2" could not be resolved

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.

Problem importing modul OpenCV in Anaconda

I am a beginner in terms of using python. Currently, I got python 3.7 and I am using anaconda as IDE. For my project, I need to detect the location and brightness of points/dots in an external imported picture. I figure I could use the modul OpenCV. Unfortunately, I am stuck right now with the problem to import the module cv2. I already successfully installed the package using
python -m pip install OpenCV-python
on command prompt. OpenCV Version which was installed is 4.2.0.34. While compiling the test script of OpenCV on Spyder
import cv2
print(cv2.__version__)
I got this message
"ImportError : DLL load failed : The Modul was not found."
Can somebody let me know please how to fix this problem? Is somehow the version of OpenCV I got not compatible with the Python version? I already watched and followed many tutorial videos on Youtube, but I couldn't find the solution. I tried to copy the cv2.pyd to the site-packages folder in
C:/user/Anaconda3/Lib/site-packages directory
still it didn't fix the problem...
I would appreciate any answers and help I could get here. Thank you!
try installing it using these commands in the following order:
conda update anaconda-navigator
conda update navigator-updater
pip install opencv-python
It should work fine.
Try conda install opencv
or conda install -c menpo opencv

Miniconda does not install correctly

I've made multiple attempts to install Miniconda on my Macbook running Catalina. I've been following this guide on YouTube about how to install it. The area where I run into issues is when the installation completes and I have to restart the terminal. According to the video, this should allow me to use the conda command in the terminal itself, thus allowing me to do what I need to do. The error message I receive is below:
ERROR: The install method you used for conda--probably either pip
install conda or easy_install conda--is not compatible with using
conda as an application. If your intention is to install conda as a
standalone application, currently supported install methods include
the Anaconda installer and the miniconda installer. You can download
the miniconda installer from https://conda.io/miniconda.html.
What confuses me most about this is the fact that I have done this exact step. This current installation is from the link they provided. Has anyone else run into any issues like this?

Pycharm Can't install TensorFlow

I cannot install tensorflow in pycharm on windows 10, though I have tried many different things:
went to settings > project interpreter and tried clicking the green plus button to install it, gave me the error: non-zero exit code (1) and told me to try installing via pip in the command line, which was successful, but I can't figure out how to make Pycharm use it when it's installed there
tried changing to a Conda environment, which still would not allow me to run tensorflow since when I input into the python command line: pip.main(['install', 'tensorflow']) it gave me another error and told me to update pip
updated pip then tried step 2 again, but now that I have pip 10.0.1, I get the error 'pip has no attribute main'. I tried reverted pip to 9.0.3 in the command line, but this won't change the version used in pycharm, which makes no sense to me. I reinstalled anaconda, as well as pip, and deleted and made a new project and yet it still says that it is using pip 10.0.1 which makes no sense to me
So in summary, I still can't install tensorflow, and I now have the wrong version of pip being used in Pycharm. I realize that there are many other posts about this issue but I'm pretty sure I've been to all of them and either didn't get an applicable answer or an answer that I understand.
you can try to follow the steps from "Pycharm anaconda import tensor flow library issue" for solve your issue ("You need to do these following steps:")
what worked for is this;
I installed TensorFlow on the command prompt as an administrator using this command pip install tensorflow
then I jumped back to my pycharm and clicked the red light bulb pop-up icon, it will have a few options when you click it, just select the one that says install tensor flow. This would not install in from scratch but basically, rebuild and update your pycharm workspace to note the newly installed tensorflow

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.

Categories