Python macOS Mojave cv2.waitKey doesn't work - python

Since I updated my mac to Mojave, my python script doesn't work properly.
cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
key=cv2.waitKey(0) & 0xFF
print(key)
if key == ord('q'):
break
cv2.destroyAllWindows()
If I run this Script on my mac and press q, I will just get the mac beep sound and nothing happens. On Ubuntu instead it prints 113 and closes the window. Any ideas how I can fix this issue?

Related

Why does the OpenCV loop not end

i have a simple webcam opening using cv2 module:
cap=cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('webcam',img)
k=cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyWindows()
this program runs for ever, although i try to close it, the only way is closing vsCode
You can close the webcam window by clicking on your webcam window and press esc while if you want to close it with specific alphabet then you can use the below code to destroy the window by pressing q.
import cv2
cap=cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('webcam',img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Camera don't show image in openCV

I am using below code to access my laptop camera or usb webcam but it doesn't work. I am not sure how to debug this to get to root cause of this issue. Code and output:
import cv2
vid = cv2.VideoCapture(0)
print (vid)
print (vid.isOpened())
while(vid.isOpened()):
ret, frame = vid.read()
print (ret)
print (frame)
if not ret:
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()
The output of above is
<VideoCapture 0000000002DAE970>
True
False
None
When the cv2.VideoCapture(0) is executed I can see my laptop's camera light lit up and same happens when cv2.VideoCapture(1) is executed, I can see my webcam's light lit up. I am not sure why ret, frame = vid.read() ret is being return as False and image as None.
Please note that both cameras work in skype and other utilities. Even on VLC as well. I have checked in my device manager and they both show up. I am running this on Windows 7, openCV version is 4.5.5 and Python version is 3.8.6
Regards,
Yasar

OpenCV exit code -1073741819 (0xC0000005)

This is my program:
import numpy as np
import cv2
cap = cv2.VideoCapture(1)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
And the result:
exit code -1073741819 (0xC0000005)
I am running my program in Windows 7. My Python version is 3.6 in Pycharm.
I got this code to run on my computer by changing the 1 to a 0 in the line cap = cv2.VideoCapture(1). This integer value determines which camera is used to capture an image. Passing 0 tells OpenCV to use the first camera on your device. If your device, like mine, only has one camera, then telling it to use the second camera by passing 1 will result in errors.

OpenCV freezes while trying to run VideoCapture(0)

I am playing around with OpenCV. I am following the documentation example (link)
I installed GTK webcam application on Ubuntu to validate that my webcam works. I am able to start the webcam and see the video feedback in GTK.
I added some print message in the tutorial code to see where I get.
I added a print before and after this line: cap = cv2.VideoCapture(0)
All I get, when running the Python file, is the print that I added before the cap = cv2.VideoCapture(0) and nothing else.
I tried increasing the waitKey to 20, 40, 100 but it didn't help.
Does anyone know why it does not get further and display the frame?
My code:
import numpy as np
import cv2
videoFeed = cv2.VideoCapture(0)
while (True):
ret, frame = videoFeed.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Feed', frame_gray)
if cv2.waitKey(10) & 0xFF = ord("q"):
break
videoFeed.release()
cv2.destroyAllWindows()
My setup:
Windows 10 host
Ubuntu 18.04 guest host
Integrated Webcam
Using PIP to install python module (numpy, scipi, pillow, open_cv, etc.)
Using venv python
You have a bug in your code at if cv2.waitKey(10) & 0xFF = ord("q"):. You should've gotten a syntax error here though.
import numpy as np
import cv2
videoFeed = cv2.VideoCapture(0)
while (True):
ret, frame = videoFeed.read()
if ret == False:
print("Failed to retrieve frame")
break
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Feed', frame_gray)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
videoFeed.release()
cv2.destroyAllWindows()
Tested your code. Works fine. Only other suggestion is to check whether your Ubuntu guest has permission to access your webcam. If you're using VirtualBox I remember seeing an option for this in the interface

Window freezing even after using waitKey() and destroyAllwindows() in Opencv

ENVIRONMENT
OS- mint Linux,
using opencv3.1,using spyder through anaconda
ISSUE
The code mentioned below opens a window of name frame and display the video captured through laptop camera.But when I press 'q', as mentioned in code, it should stop and terminate the window. But,here the window stops to display any further frames captured and do not terminates.then manually I force Quit the process.
What is the problem, why is it not terminating the window?
CODE:-
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
There is an open bug with this issue:
https://github.com/opencv/opencv/issues/7343
There are also similar questions without a good solution:
opencv videocapture hangs/freeze when camera disconnected instead of returning "False"
Try the solutions here: DestroyWindow does not close window on Mac using Python and OpenCV Calling several times waitKey seems to work for many people. You can try without the release() as well
I use macOS BigSur and also had this issue, I ended my code with the lines below and had no problem later:
video = cv.VideoCapture("your_video.mp4") # or 0 for your camera
while(video.isOpened()):
ret, frame = video.read()
if ret:
# Your frame manipulations
if cv.waitKey(1) & 0xFF == ord('q'):
video.release()
cv.waitKey(0)
cv.destroyAllWindows()
break
else:
break
video.release()
cv.waitKey(0)
cv.destroyAllWindows()
This will destroy the freezed window of the video but you have to press 3 times Q. One por stopping the video, one more for closing the window and one last for releasing the kernel if you're running on the Jupiter Notebook.
I also recommend to restart and clear all outputs in the kernel tab after applying the code because it could not work if so.
apparently this solves the issue, just include this line on top:
import pyautogui

Categories