Opencv imshow crashes python launcher on macOS 11.0.1 (Big Sur) - python

I'm trying to run some old code from gaussian filter when I find out that python launcher gets stuck trying to do the imshow function.
I tried:
Used Matplotlib to display a graph to see if the python launcher was the problem but no, graph showed up fine.
Remove process in between just to have the image read and display in fear that something in my code was breaking the launcher but no success.
Reinstalled opencv-python but no success.
Also saw one question like this in the google search but OP deleted it.
Has anyone encounter this issue or has any fix for this?
Example code:
import cv2 as cv
filename = 'chessboard.png'
img = cv.imread(filename)
cv.imshow('dst',img)
cv.waitKey(0)
OS: MacOS Big Sur (11.0.1)

I resolved the issue with below steps:
Install the anaconda.
Install the libraries needed.
Run the script, there is an error appeared as below:
You might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
then I installed two libraies:
pip install opencv-python opencv-python-headless
Retry run the script, the image can be shown on the left top of the monitor.

I was also facing the same problem. I solved it by just installing opencv-python-headless. use:
pip install opencv-python-headless

I also encounter this problem when I upgraded to Big Sur.
Uninstall anaconda(every package), and reinstall python.
pip install opencv-python opencv-python-headless
This does help me with imshow() but I can't run cv.face. This attribute is not found.
This solve my problem
pip install --force-reinstall opencv-contrib-python==4.1.2.30
This downgrade gets my code working again.

I naturally have my environment installed in anaconda.
I had to use the package
opencv-python-headless.
It is now running again.
Also the
cv.imread(...)
method

I also ran into this problem after installing macOS Big Sur. It was not only cv2.imshow() that did not respond but also cv2.namedWindow()
cv2.imread() was working however.
Solution was to install opencv-python-headless as others have said before

I had the same problem as you. I solved it by installing anaconda and using anaconda's virtual environment(PyCharm)

import cv2 as cv
import matplotlib.pyplot as plt
filename = 'chessboard.png'
img = cv.imread(filename)
cv.imshow('dst',img)
cv.waitKey(0)
I also found that pc hangs up at cv.imread(...).
Nothing was happened.
So I added line 2 import matplotlib.pyplot ...,
the image was displayed.

It seems to be a package reference issue. My solution solves the issue:
re-create conda env
re-install package, incl. opencv-python-headless

This is the snap of the error that I face (Click this link to watch)
This is common problem in Mac, not only in the Big Sur but also in the Catalina and others too.
and I solved this using a single command.
pip install opencv-python-head
And here watch the problem is solved now (Click this link to watch)

Related

Face recognition opencv problem . I tried to run my code on jupyter and getting this error [duplicate]

I was working on a sign language detection project on jupyter notebook. While running the code for live detection I encountered an error as shown below:
OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-1drr4hl0\opencv\modules\highgui\src\window.cpp:651: 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'
The code that caused this error is:
while True:
ret, frame = cap.read()
image_np = np.array(frame)
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes']+label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=5,
min_score_thresh=.5,
agnostic_mode=False)
cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
break
NB: I installed OpenCV using using pip install.
Edit: This solution seems to work for a majority of users, but not
all. If you are in this case, see the proposed answer by
Sachin Mohan
I had the exact same error using yolov5, on windows 10. Rebuilding the library by typing
pip uninstall opencv-python
then
pip install opencv-python
worked for me.
Few frustration hours later, saw this solution under the comment of the first answer by Karthik Thilakan
pip uninstall opencv-python-headless -y
pip install opencv-python --upgrade
This worked for me in the conda environment. Thanks Karthik! :)
I installed another GPU and finally upgraded to Tensorflow 2 this week and suddenly, the same issue arose. I finally found my mistake and why uninstalling and reinstalling opencv works for some people. The issue is stated clearly in a text file in your opencv-python dist-packages named METADATA.
It states;
There are four different packages (see options 1, 2, 3 and 4 below) and you should SELECT ONLY ONE OF THEM. Do not install multiple different packages in the same environment.
Further, the file says that;
You should always use these packages if you do not use cv2.imshow et al. or you are using some other package (such as PyQt) than OpenCV to create your GUI.
referring to
Packages for server (headless) environments ... (with) no GUI library dependencies
So, if you run;
pip list | grep opencv
and the result is more than one opencv version, you've likely found your problem. While an uninstall & reinstall of opencv might solve your problem, a more masterful solution is to simply uninstall the headless version as that is the one that does not care about GUIs, as it should be used in server environments.
I had the same problem when I wrote a similar program, but issue was with different versions of opencv packages.
You can check them with the command:
pip list | grep opencv
My output was:
opencv-contrib-python 4.5.5.62
opencv-python 4.5.5.62
opencv-python-headless 4.5.4.60
And it turned out that opencv-python-headless must be version 4.5.4 for the program to run properly. So the solution was to change the opencv-python version to be the same as opencv-python-headless. So in that case you can run:
pip install opencv-python==4.5.4.60
worked for me.
I had this exact same issue a few weeks back and I'd like to perhaps complement some of the answers touching the headless elephant in the room.
My complex project incorporates a few in-house subprojects by other colleagues. These tend to be developed and tested independently, so no cross-contamination occurs. However, since one of them used opencv-python and another went with opencv-python-headless, the final build installed both.
THIS IS THE PROBLEM!
Whenever I had both, a number of functions, particularly those pertaining visualisation, now failed. Worse: pip list revealed both opencv- versions installed! To make matters worse, whenever I uninstalled and installed again opencv-python (a simple --upgrade never worked, as it claimed the latest version was there and nothing needed upgrading), then it started working. We all hate witchcraft, so...
I went down the compilation rabbit hole and obviously nothing good was there to be found.
How does pip know it?
if you check into your .venv\Lib\site-packages, you'll find the following two folders:
opencv_python-4.5.4.60.dist-info
opencv_python-headless-4.5.4.60.dist-info
or whatever your version might be. These are the folders where pip gets its metadata from, but not where the actual code is. In fact, you don't do import opencv-..., but rather import cv2.
You do import cv2 in both cases! In fact, -headless is a crippled drop-in for the real thing. So, if you look up in your list, you'll find a cv2 folder. Both libraries deposit their code in this folder. As we know, when it comes to saving files, the last on the scene wins.
Order! Order!
(Ok, I miss John Bercow.)
Now, both libraries saving to the same folder, what is the order? Since they don't depend on one another, and in my case where poetry is being used to manage dependencies, alphabetical order is the default, and (drumroll) -headless comes last.
Solution
At some point, I just decided to go nuts and remove -headless altogether. I am not the CV dev in the team, so I was just grasping for straws , but... it worked! That's when I looked int the whole drop-in thing.
My colleagues were developing with a simple requirements.txt file, so when it came to gathering requirements in a nice proper pyproject.toml file, I just left the -headless option out.
Bottom line
You can't have both. Whenever you have multi-part projects, I highly advise to run through the pip list after the environment is built and check for the couple. If you find both, always remove the -headless, as it is a subset of the main one.
Achtung: check your .venv\pyvenv.cfg for a line with:
include-system-site-packages = true
This line means your project will be importing any libraries (other than the standard ones) from your global Python install and if you happen to have the -headless in the global environment, you're still in trouble.
This error is mostly with Pycharm Ide , I resolved it by changing the project interpreter None of the given solution in the internet worked for me.
This solved the issue for me:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip install opencv-contrib-python
pip install opencv-python
I was trying to move a set of files to my Windows10 from Ubuntu 18.04 LTD, and running a cli for inference and the same error as mentioned in the opening post cropped up......I was checking on the versions of Open-CV and Open-CV Headless in both Ubuntu and Windows and they were exactly the same......While it was executing on Ubuntu, it threw the error in Windows......I removed Open-CV Headless and upgraded the Open-CV, and used the same set of commands and Windows started to execute the CLI for inferencing.......
for streamlit cloud use opencv-python-headless
but in other platforms use opencv-python
try:
pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless
and then reinstall the OpenCV:
pip uninstall opencv-python
pip install opencv-python
you can also save image with single command and then open it from drive.
cv2.imwrite("TestImage.jpg",img)
No need to waste time on cv2.imshow()

problem installing and importing modules in python

I am installing python on windows10 and trying to install the opencv and numpy extentions in the command window. I get no error installing them and it says it is successfully installed. But when I try to check the installation and import cv2 it does not recognize it and give me the error: no module named cv2.
can anybody help me with this problem? Is there something wrong in installation process or do I need to install something else?
I checked the newest version of each and used the compatible one with my system.
Thanks.
One solution could be that you have 2 versions of python. So, go to the specific python's scripts directory and run: pip install numpy
If that too doesn't work, you can find the answers to this question on Why can't I import opencv3 even though the package is installed?, as stated by #Cut7er.
I have tried the solutions given to the above stated question myself also. But, they didn't work for me. So, another thing that you could try to use is this IDE called PyCharm. It ofcourse is much more beautiful that the IDLE, but it also has an inbuilt GUI controlled installation of binaries or packages. That would make things a lot easier. I have faced a lot of issues with packages for python and this IDE made things a lot easier. You can find it on https://www.jetbrains.com/pycharm/download/#section=windows.
You can also use anaconda. But, I found it a little difficult to use since, it has similar issues.
EDIT:
Seems like you are using PyCharm. But, you are installing libraries from your command prompt. So, see the answer to: ImportError: No module named 'bottle' - PyCharm. This answer guides you through how to install a certain library through your PyCharm window itself. So,
1) Go to Files>Settings
2) Search for "Interpreter" from the searching tab. Open the interpreter
3) You can now see a plus sign on the right. A click on it will open up a section on the left.
4) In the searching tab, search for numpy or opencv. Click on whichever module you want to install. And then click on the "install package" button on the bottom left. This will install the package for you.
5) Then click save. And run your file that says import cv/cv2.
This should probably do the trick.
Hope it helps!
Is it possible that you have 2 versions of python on your machine and your native pip is pointing to the other one? (e.g. you pip install opencv which installs opencv for python 2, but you are using python 3). If this is so, then use pip3 install opencv
I removed the Anaconda version on my machine, so I just have python 3.7 installed. I removed the python interpreter(Pycharm) and installed it again and the problem got fixed somehow!
I suspect you have two versions of python and the one you're using doesn't have opencv on it, because pip pointed to the wrong one.
A pragmatic solution assuming you're using the python version with conda is to just use conda to install cv2:
conda install -c menpo opencv
A more careful solution is to figure out how to get the pip that points to the python version you're using. On linux I can check that my pip points to my python like this:
:~$ which python
/home/kpierce/anaconda3/bin/python
:~$ which pip
/home/kpierce/anaconda3/bin/pip
So you see the pip and python versions are associated. On windows I suspect you do an analogous thing on the command line like
where python
where pip
And if they don't match, you might try
where python
where pip3
to see if those match. You need to use the pip that points to the correct python version. You can view the python version by entering the python interpreter and running
import sys
sys.version

AttributeError: module 'cv2' has no attribute 'imread'

(i'm on mac os 10.8.5)
I'm using Python 3 (through jupyter notebook) and trying to import cv2
I did import cv2 succefully, but when I type im_g = cv2.imread("smallgray.png", 0) I get this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-5eb2880672d2> in <module>()
----> 1 im_g = cv2.imread("smallgray.png", 0)
AttributeError: module 'cv2' has no attribute 'imread'
I also checked dir(cv2) and I get:
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__'
I guess a lot of functions are missing...
Is it due to a wrong opencv installation ?
Actually I struggled a lot to get opencv and I guess I installed it 'too many times' and different ways via Terminal. (brew, pip)
Should I uninstall opencv and start over ? How can I do this properly ?
Thx in advance
It might be the case that you installed it in the wrong way. In my case as well, I installed OpenCV wrongly so I uninstalled it completely then reinstalled it which caused it to work.
Please notice the sequence of uninstalling and installing:
Uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
Install:
pip install opencv-contrib-python
pip install opencv-python
I was have this problem too, and i solved it, the problem was been in i named my script cv2.py, i'm just rename it to something else.
Reader's problem could be, that a wrong library (cv2 package) has been installed.
I installed opencv-python3 instead of opencv-python for example.
So in case you have PyCharm IDE, go to File->Settings->Project: *->Python Interpreter and see what you have listed there:
Make sure, you have installed opencv-python.
If you have also other similar names, it should be fine - in my case it works.
This might happen if you have named one of your files as 'cv2.py'. So when you 'import cv2', the system accesses your file instead of the actual library. So try renaming cv2.py' to any other name and your code should work fine.
I had the same issue, this turned out to solve the problem:
from cv2 import cv2
im_g=cv2.imread("smallgray.png", 0)
print(im_g)
Check:
brew list | grep opencv
If it doesn't installed then try:
brew install opencv
it works with me:
pip install opencv-python
pip install opencv-contrib-python
source : https://pypi.org/project/opencv-python/
Do cv2.cv2.imread()
This should work in most of the cases. Also, take a look here.
I faced Similar issue,On Ubuntu
I had installed open-cv using the comand:
sudo pip3 install opencv-python3
I Uninstalled opencv, By:
sudo pip3 uninstall opencv-python3
Then reinstalled it Using the following Code:
pip3 install opencv-python
I also faced this problem.
And get a solution by changing the current cv2.py to cv.py(You can name whatever you like except the name of packages in use).
Little Explanations
Saving the current working file as the name of cv2.py, when we try to run the file it imports the current file with import cv2 statement, not actual cv2 module. This whole thing is causing this whole Error.
The main problem is that you have your file or folder named "cv2", it is not permitted in Python. The way to solve is change the name and then your code will be run
The same issue has recently appeared within my code and had no idea of what's happening. Tried different methods such as uninstalling opencv-python.
Coming to the naming of the file that I am working with, I definitely had no problem that might cause a collapse with the name of the library.
I have deleted the opencv-python, cleaned all of the package-related caches, and then reinstalled the opencv-python and opencv-contrib-python. Now, it is working fine and I can say that no issues have appeared yet.
P.S I am using Arch Linux. If you wonder about how to clear caches, try looking at this source. I hope it helps. Keep learning and exploring, thanks!
I personally had to uninstall all previous installations dealing with opencv :
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless
Then I deleted all files that had to do with opencv too in site-package
after the uninstallation, files related to opencv like opencv_contrib_python-4.6.0.66.dist-info remained and caused errors.
Once all these files were deleted I reinstalled opencv:
pip install opencv-python
now it works for me
using cv2 on vscode
i was having this problem but nothing worked. Turns out that i named my file as "cv2.py" so it was importing itself
"Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error."
Perhaps you have just installed opencv-contrib-python and not opencv-python.
Try this out, it worked for me-
First uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
then install:
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16
You can refer here.

Importing Numpy results in error even though Anaconda says it's installed?

I signed up for a statistics udemy course which uses jupyter running the stock numpy package out of anaconda.
Numpy is working when I run python 3.4.4 in pycharm, but it will not work in either anaconda2 nor anaconda3.. this makes no sense because numpy comes stock as part of the anaconda library.
When I try importing numpy in jupyter, running a local instance of a python 2 script, I get this:
Thinking I could sidestep the error using a IDE, I tried pycharm and I got this:
Numpy is part of the anaconda default library which I'm running, so I checked and made sure the numpy package was there via the Anaconda Prompt using 'conda list'...
Why won't it import successfully?
Before I uninstall and reinstall everything, does anyone have any ideas?
Yayyyy. I figured it out. So I had several different python versions before I was running python through anaconda/jupyter. As a result, the tethering in-between the additional package libraries to the version of python running was shifty. For example, I may have had numpy working configured to 3.4 but not 2.7..
anyways, to break down the steps I took, I uninstalled all instances of python (both anaconda and normal versions). Then I deleted the old 3.4 libraries from my C drive. Then I installed anaconda 2.7.11 again, tested importing numpy and got the same error. Then I thought to myself, what if the packages are installed separate from the python library and their configuration didn't get reset via the reinstall.. so via the anaconda prompt I typed:
pip uninstall numpy
which removed the package I was having trouble with. and then :
pip install numpy
which downloaded and reset a whole new instance of the package. Then I tested importing numpy in both the anaconda prompt and jupyter = both worked.
I'm very happy that this ended up working out as I can continue on as planned. For anyone else who experiences a similar problem, I would try uninstalling, then reinstalling the problem causing package via pip commands - this step seemed to have the most impact on fixing the problem. Then if that doesn't work proceed to uninstall and reinstall the environments in intervals.
You can try using the following command:
pip install numpy --upgrade
This will uninstall old installed version of numpy and install a new version. This command solved my issue.
try activating you base conda environment before starting jupyter.
>activate root
>jupyter notebook

PyCharm does not recognize cv2 as a module

I am using OpenCV 3 and python 2.7 and coding using PyCharm. The code works fine but PyCharm does not recognize cv2 as a module. It underlines it with a red line, so it doesn't display its functions in the IntelliSense menu.
I tried to set an environment variable OPENCV_DIR but it didn't work
OpenCV is extracted in F:\opencv and Python is installed on C:\Python27
What is wrong?
here are proper instructions if you have not built opencv from source so that everyone can follow it
Click File menu > Settings
Search " Project Interpreter " in search bar
Click that + button to install packages or press Alt+Insert
now Available packages window will open and then search for " opencv-python " and click install package button.
enjoy and do +1
Try File->Invalidate Caches / Restart...
I can't say definitively why this works, but it may have something to do with the cached module definitions that PyCharm uses to provide code hints. In some cases they aren't updated or get corrupted. I just know that it's worked for me.
I've solved this problem. Hope this works for you also.
from cv2 import cv2
https://stackoverflow.com/a/61242587/8423105
Worked on Pycharm 4.0 version
Follow these steps:
Go to File>Settings in Pycharm IDE Window
Search Project Interpreter in search bar.
Click on any package from the available options
Package window will open from where you can install any packages.
I have installed opencv-python package to run opencv commands
Follow the link How to install OpenCV on Windows and enable it for PyCharm without using the package manager
Steps to follow:
Install Python 2.7.10
Install Pycharm(If you have not done it already)
Download and install the OpenCV executable.
Add OpenCV in the system path(%OPENCV_DIR% = /path/of/opencv/directory)
Goto C:\opencv\build\python\2.7\x86 folder and copy cv2.pyd file.
Goto C:\Python27\DLLs directory and paste the cv2.pyd file.
Goto C:\Python27\Lib\site-packages directory and paste the cv2.pyd file.
Goto PyCharm IDE and goto DefaultSettings>PythonInterpreter.
Select the Python which you have installed on Step1.
Install the packages numpy,matplotlib and pip in pycharm.
Restart your PyCharm.
PyCharm now has OpenCV library installed and working.
I have the same problem and I'm afraid that there isn't a solution for this at the moment. You can read more about the issue here
The problem is that OpenCV doesn't include the needed .py file to have the autocomplete which might be why PyCharm isn't picking up the package as well.
To be sure that we are on the same page, when you go File>Default Settings>Default Project>Project Interpreter and select the python 2.7 interpreter, does it list cv2 or opencv in the packages?
EDIT: I was able to get it working with anaconda instead of python. Just instaled anaconda and did everything the same as I would with python (copied cv2 where it would be in python). Hope it helps you!
Installing opencv-python package from pycharm setting worked for me.
I follow the steps in the webapp response and after that It does not work and I decided to reinstall the pycharm IDE, this works for me.
Hope it helps.
Just install opencv python package from settings.
What ended up working for me was uninstalling the package from pip and installing it from my package manager (apt):
python3 -m pip uninstall opencv-python
sudo apt install python3-opencv
You can install your existing libraries to pycharm by enabling the button "Inherit global site-packages" while creating project.
If you don't have installed libraries then you can install it by going to File>Settings>Project:your project name>project interpreter and then install your required package by searching that.
After installing OpenCV with pip and then pip3 in terminal. It would import when writing python in terminal, but not in PyCharm. I tried the invalidation of cache mentioned above, and it worked for a min until cache was warmed up. Same result....
I fixed it by going to:
PyCharm Menu
Preferences
Project (proj name) -> Project Interpreter
(this time instead of CV2)
Plus sign (to install packages)
searched for opencv-python
installed the package
I didn't even have to dot off that library, it then accepted the
"import cv2"
Just:
pip install opencv-python
solved for me on Windows and Linux.
While Installing pycharm,dont select virtual environment unless u want it,If you select it ,then it will create a venv file and you need to import all the module by command prompts.Tick the existing interpreter,it will make everything easy.

Categories

Resources