OpenCV error on spyder - python

I want to use OpenCV with python on spyder.
But error occured when I run a simple code.
import cv2
img = cv2.imread('pi.png',0)
cv2.imshow('image', img)
cv2.waitKey(0)
dcv2.destroyAllwindows()
Error
This application failed to start because it could not find or load the Qt platform plugin "xcb".
Reinstalling the application may fix this problem.
The error is occured at this line.
cv2.imshow('image', img)
Details: Ubuntu 14.04(LTS), OpenCV 2.4.13, Spyder 2.3.9(Python 2.7)
Please Tell me in detail what should I do.

I didn't understand the answer.( stackoverflow.com/questions/30483753/python-app-xcb-plugin-fail )
But, the problem is solved from changing condition when I reinstall OpenCV
Installation is referred to this page.
The key of the solution in this method is simple!
Change the condition from 'WITH_QT=ON' to 'WITH_QT=OFF' in the script. (opencv.sh)
I don't have any solution without reinstalling because I'm inexperienced.
If you know about solution that change the condition without reintalling or reason why the problem is occured, give me a feedback please.
Thank you.

Related

cv2.waitKey(0) blocks the program

Here is a simple script:
import cv2
img = cv2.imread("img.png", 0)
cv2.imshow("Test", img)
print("Before")
cv2.waitKey(0)
print("After")
After closing the Test window, the output of terminal was like:
$ python test.py
Before
It seems that cv2.waitKey(0) blocks the program and I cannot even kill the process by typing ctrl+c in the terminal. What's wrong with it?
PS: It works properly before but today there is some system upgrade being done and this kind of issue happened. I'm using ubuntu 18.04.
waitKey() is supposed to break/return once all windows have been closed, and also return immediately if no windows are currently open. This is regardless of the desired amount of delay.
On Windows 10, I can't reproduce what you describe. That is to say, it's probably specific to your environment.
Your described behavior is probably a bug due to bad interaction with a window manager/GUI toolkit.
Please check https://github.com/opencv/opencv/labels/category%3A%20highgui-gui for any obvious previous reporting.
Before opening a new bug, make sure it's reproducible and comes with a description of the environment that is required to reproduce it. That includes all versions of everything you think is relevant, including OpenCV's version.
After checking issues of opencv, I do find the same problem that has already been posted:https://github.com/opencv/opencv/issues/20822.
It is a bug due to the Qt Gui backend and the issue is still open.

How do I get Pillow ImageOps.contain to work when inside VS Code?

I have VS Code on a Mac.
Pillow is installed and version verified as 8.3.2 via Pip list in the terminal window of VS Code. I have confirmed via the pillow docs that the ImageOps.contain() is part of 8.3.
My problem is that when I use the terminal, type python and run the following, it works perfectly:
from PIL import Image, ImageOps
im = Image.open("images/Barcelona.jpg")
print(im.format, im.size, im.mode)
im = ImageOps.contain(im, (800, 800), method=3)
im.show()
Preview pops right up and shows me the picture.
When I put the exact code into VS Code or build a .py file with Nano, I get an error message which is shown in this image:
I've verified the right version of Python, Pillow, and such. Any help or pointers would be greatly appreciated.
This turns out to be a problem with what I can only call nested environments and/or a conflict between Anaconda and Workspaces. I'm not really sure but when I used the _version import to figure out a) what VS Code thought and then b) figured out the environment Pip was reporting on, I deactivated up one level, upgraded the (base) to the 8.3 version and all ran fine. The knowledge on importing the version variable to see precisely what the code is importing came from the question asked below and was invaluable.

Problem displaying image opencv+python using imshow + waitKey

I have recently upgraded my python/opencv for a project to python 3.7 + opencv 4.3.0 and now I have an issue with opencvs imshow. I am running Ubuntu 18.04 and am using conda venvs.
I tried to rerun this piece of code multiple times and half the time it correctly displays the white image, and half the time it displays the image below (1). The printed output (2) is always the same. I tried changing it from waitKey(0) to waitKey(1000) but that doesn't make a difference. Still about half the time a tiny black image is all I see.
Does anybody know how to debug this? I tried looking at Pycharms log files but they dont have any more details. I also tried running it straight from the command line but that gives the same issue. Also tried to remove the environment and created a fresh one, reinstalled opencv and got the same issues. When I create a 3.6 environment I don't have the issue, but that's no longer an option. I need python3.7 for some other packages that don't support certain features in 3.6.
I received a warning that libcanberra gtk was missing, and found that in another post that it could cause issues. So I installed it using sudo apt install libcanberra-gtk-module libcanberra-gtk3-module and the warning went away. Sadly the issue did not...
import numpy as np
import cv2
if __name__ == '__main__':
img = np.ones((255, 255, 3), dtype=np.uint8)*255
print(img.shape)
print(img.dtype)
print(img.min())
print(img.max())
cv2.imshow("i", img)
cv2.waitKey(0)
screenshot of the code + result
console output
I think there is no problem. The left-upper part of the image is white as it is supposed to be. The rest is undefined. I recommend equating green and blue channels to zero and leaving only red channel as 255 in order to make sure that is the case.
I solved it in this way:
if cv2.waitKey(0):
pass

Spyder doesn't show image with OpenCV

I am just trying to perform the easiest task in opencv library which is opening and showing an image. I was doing it before on windows computer and it was working perfectly, however when I try to perform the same task on my MacBook it doesn't work. The code is simple:
import cv2
img = cv2.imread('/Users/michalsniady/Desktop/IFE/4th semester/Image Processing And Computer Graphics/nature.jpg', 1)
cv2.imshow('window', img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
When I was downloading opencv I used simply pip install opencv-python if I recall correctly. Maybe the version is wrong? I'm using Python 3.7.6
When I run this program it has two outcomes:
- kernel died
- nothing happens
I am using Spyder with Anaconda
Thank you for any help.
I faced somewhat similar issues.
I reinstalled my open CV and then it worked correctly!

OpenCV 4.0.0 SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set

Hello I am trying to create a facial recognition program but I have a peculiar error:
here is my code:
import cv2 as cv
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
face_cascade = cv.CascadeClassifier("lbpcascade_frontalface.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
and this error is the output
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
I have "lbpcascade_frontalface.xml" in the working directory so that shouldn't be an issue
if it helps when I enter
cv.__version__
I get
'4.0.0'
New Answer
OpenCV seems to now have a directory dedicated to cascades, they are placed in data and I am seeing something like this floating around in tutorials now
haar_cascade_face = cv.CascadeClassifier('data/haarcascade/haarcascade_frontalface_default.xml')
You may have to find where data is on your machine or the above my work. I have not touched this project since I finished it in early 2019.
Bear in mind this only works for frontal face, if you want to use Haar's Cascade for eyes that is a separate file.
old answer
Turns out I didn't need to download another file and use it because opencv comes with it this little bit of code worked
cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_default.xml")
Well I was in this same problem, as #TylerStrouth mentioned this code snippet doesn't work :
cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_default.xml")
because there are no haarcascades files in data directory if you have just installed opencv in a standard format of pip install opencv-python or sudo apt-get install python3-opencv
You will get an error something similar to this stackoverflow question, therein is the mentioned solution that worked for me, that is if you're using python3 then you need also to install opencv-contrib-python before running the above code snippet.
pip install opencv-contrib-python
which has full package (contains both main modules and contrib/extra modules)
As explained by #TylerStrouth above opencv has a directory of cascades in which the cascade files are available, I also faced the same problem while running the code for face detection on Ubuntu 16.04 and solved it as follows
Get the location of opencv using
whereis opencv
Mine was in /usr/share/opencv
Check whether the cascades are present in that location and copy paste the location in cv2.CascadeClassifier along with the required haarcascade
I have encountered same issue in little different way.
I was using Jupiter notebook to execute code here
I copied XML file from here and created a XML file in current Jupiter directory, when loading this files using below:
classifier = CascadeClassifier('haarcascade_frontalface_default.xml')
Its returned me error :
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
So, I tried other way, removed this file, and downloaded actual file as XML format in current directory, which resolved my issue.
I was having the same error when i was using hogcascade_pedestrians.xml to detect pedestrians from a local video and i was reading the hogcascade_pedestrians.xml as follows:
pedestrainsClassifier = cv2.CascadeClassifier("hogcascade_pedestrians.xml")
Of which you should read it as follows:
pedestrainsClassifier = cv2.CascadeClassifier(f"{cv2.data.haarcascades}hogcascade_pedestrians.xml")
Alternatively you can do it as follows:
pedestrainsClassifier = cv2.CascadeClassifier(cv2.data.haarcascades +"hogcascade_pedestrians.xml")
Good luck
Change your code as follows, this worked for me
har_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'har.xml')
On version 3.4.9.33 of opencv-python (pip show opencv-python, Windows) the below line works fine: trained_face_data = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

Categories