I am trying to take a picture from the defualt carmera with python, to do this I am using openCV (import cv2
from python shell). However, when I attempt to disable the camera it closes but with the error [ WARN:0] terminating async callback
.
This is code I am trying to run:
import cv2
camera_port = 0
camera = cv2.VideoCapture(camera_port)
return_value, image = camera.read()
cv2.imwrite("image.png", image)
camera.release() # Error is here
The code outputs the desired result- it takes a saves an image but I do not understand why the error message occures or how to remove it
It's probably showing a warning because you're not releasing the handle to the webcam.
try adding this to the end of the code
camera.release()
cv2.destroyAllWindows()
I hope this helps!
I had the same warning. Just modify the line camera = cv2.VideoCapture(camera_port) to camera = cv2.VideoCapture(camera_port, cv2.CAP_DSHOW) and add cv2.destroyAllWindows() as the last line of your code.
camera = cv2.VideoCapture(camera_port,cv2.CAP_DSHOW)
cv2.destroyAllWindows()
It works for me as indicated Sumit Kumar
camera_port = 0
#camera = cv2.VideoCapture(camera_port)
camera = cv2.VideoCapture(camera_port,cv2.CAP_DSHOW)
# Check if the webcam is opened correctly
if not camera.isOpened():
raise IOError("Cannot open webcam")
return_value, image = camera.read()
print("We take a picture of you, check the folder")
cv2.imwrite("image.png", image)
camera.release() # Error is here
cv2.destroyAllWindows()
- first:add
cv2.destroyAllWindows()
- second:the camera permission you have banned,and then check it.
I did this & I don't see that warning there after.(only for Windows OS)
Open cmd and type:
setx OPENCV_VIDEOIO_PRIORITY_MSMF 0
来源:https://stackoverflow.com/questions/53888878/cv2-warn0-terminating-async-callback-when-attempting-to-take-a-picture