Python Opencv2 imshow is closing immediately even with waitKey(0) - python

I'm using wsl2 and VScode as the editor. The code in question is simply:
image = cv2.imread('sample.png')
cv2.imshow('image', image)
cv2.waitKey(0)
The first run goes smoothly and lets me inspect the image until I press a button. However after the first run the picture shows up for a quarter of a sec and then disappears. Any idea what could be causing this?

However after the first run the picture shows up for a quarter of a sec and then disappears.
This appears to be a problem triggered by the first run. Could it be that you'll need to add cv2.destroyAllWindows() to the end of your code?
import cv2
image = cv2.imread('sample.png')
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

I don't know the problem but a workaround worked for me.
I don't press any key to close the OpenCV window. I kill the terminal using the 🗑️ button in the VSCode terminal.
Please consider if even 1 time you press any key to close the OpenCV window, then you have to restart your WSL. So, from the beginning, just kill the terminal instead of pressing any key.
But this is just a temporary workaround. I hope other people can help to find the root cause.

I had the same issue and was able to fix it by updating my WSL version here:
aka.ms/wslstorepage

Related

OpenCV is not loading my webcam in PyCharm

I am just trying a simple OpenCV code test in PyCharm but every time I run the code it just ends. I know it is not an issue with my webcam as I have run the code using Anaconda's command prompt and it works though it does take some time to start up.
The code I am using is:
import cv2
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
cv2.imshow('Image', img)
cv2.waitKey(1)
I feel that OpenCV is taking time to open up my webcam and so the first few reads are coming back as false and causing the code to end before my webcam can show but I don't know how to fix it. I tried googling around but I was not able to find anything. If anyone knows how I can fix this I would really appreciate your help!
Edit: nevermind I was being dumb. PyCharm was not executing the file I was on! 😅
I don't know if the problem is hardware related but, you can try something like this:
import cv2
cap = cv2.VideoCapture(0)
while True:
try:
success, img = cap.read()
cv2.imshow('Image', img)
cv2.waitKey(1)
except Exception as e:
print("Exception happened: {}".format(e))
continue
This basically tries to execute the code inside of try and if an exception happens it executes the code inside of except. It also informs what the error is. You can also add some sleep using time.sleep(), before trying to read the webcam again.
For time.sleep(), you need to import time.
Hopefully this helps. :)
I think your error can be from the your interpreter.Or you can try kick in your code and run it. Dont use the triangle symbol in the above right corner for the first attemp
If you are running on Mac, the PyCharm usually does not have access to webcam. So, its best you run your application from the terminal when you need to access the webcam.

Trying to make the python shell go full screen upon launch and also make a popup image go straight into full screen

I'm working on some extra curricular work for school and attempting to implement images into my game of hangman , the main issues I am having is when opening the image it is opening behind the shell and as such can't be noticed. I'm hoping to be doing it with the subprocess library - subprocess.call("taskkill /f /im Microsoft.Photos.exe" , shell=True) - in a similar way to how the window is shut afterwards.
Along with this , when using - print('\n' * 100) - to hide the word which the users will guess , my text begins to go offscreen which is easily fixed by making the window fill the screen but just wondering if there is an easier , automated way to do this through python ?
You can try using cv2 library, for example :
import cv2
image = cv2.imread("face.jpg")
cv2.imshow("Gray", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
This will show the photo on the screen untill you press a key to destroy it.
You can also change the photo resolution with cv2.resize(image,x,y) and check the image current resolution with image.shape()

pyautogui.locateOnScreen() Returns... Nothing?

I'm trying to use PyAutoGui's image recognition functions. (OS X)
Needless to say, I'm running into some slight issues that I can't seem to solve myself no matter where I look or what I do. I'm attempting to have PyAutoGui click on the Chrome shortcut based off a .png screenshot saved to my desktop.
Here's my code in terminal:
>>>import pyautogui
>>>chrome = pyautogui.locateOnScreen('/Users/ianscalzo/Desktop/chrome.png")
>>>
I get no backfire on my filepath, but it causes my shell/terminal to return nothing but go to a new line. (As shown in the code example above - Just causes terminal to go to a blank ">>>")
I don't really understand why it doesn't do anything but go to a new line, so any insight would be greatly appreciated.
Thank you so much!
After struggling with this forever also, finally figured out that you either use command line to take the screenshot or using the screenshot button with windows key. It doesn't work with the snipping tool.
so try:
image = pyautogui.screenshot()
image.save('testing.png')
Go and crop testing.png as small as possible so that locateOnScreen works faster. Then go back to the terminal and type:
pyautogui.locateOnScreen('testing.png')

Automatically refresh an image in ubuntu raspberry pi

I am streaming and writing an image to a particular location in a raspberry pi. Every time a new image comes it overwrites the previous one. Now if i keep that image file open, it does not get automatically updated.I have to close and reopen it for the update to happen.Is there anyway i can automatically refresh it.
I tried implementing a python code to continuously read and show the image. But still i have to refresh the window for the image to get updated. Below is the code that i used.
img = cv2.imread("Filename",1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Please suggest any alternatives. I just need to preview the stream.
You could use a simple loop as you can call imshow repeatedly without destroying it.
while True: #Find something to get out of here
img = cv2.imread("Filename",1)
cv2.imshow('image', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
due to opencv documenation:
For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed.
So you have to set time in milliseconds instead of 0.

How can I make SimpleCV show image?

I am using the following code from Simple CV tutorial
logo = Image("simplecv")
logo.show()
But then it shows up a small window but blank, without image.
Could you help me?
Thank you
just add a simple line in your code and it will work if you are running this code with a script file, otherwise just save an image on your desktop and provide the path of that image to your code "Image("path")" if still you are gating the same error please install it again.
logo = Image("simplecv")
logo.show()
raw_input("Press Enter for EXIT")
This link helped me: Image.show() won't display the picture
For linux users: if you installed simpleCV from Software Center, then check if you have sampleimages folder in
/usr/lib/pymodules/python2.7/SimpleCV/
If it's not there, obtain it from
https://github.com/sightmachine/SimpleCV#ubuntu-1204

Categories

Resources