I am trying to take a picture from the defualt carmera with python, to do this I am using openCV (import cv2 as cv 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 as cv
camera_port = 0
camera = cv.VideoCapture(camera_port)
return_value, image = camera.read()
cv.imwrite("image.png", image)
camera.release() # Error is here
The code outputs the desired result, it takes and saves an image, but I do not understand why the error message occurs or how to remove it
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I had the same warning.
Just modify the line
camera = cv.VideoCapture(camera_port)
to
camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW)
Method 2
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!
Method 3
camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW) cv.destroyAllWindows()
Method 4
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
Method 5
This seems to be a bug in MSMF backend of opencv.
If you are using windows then you can change the backend to DirectShow backend.
So, change VideoCapture like this:
captureDevice = cv.VideoCapture(0, cv.CAP_DSHOW)
Method 6
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()
Method 7
hey guys found the solution pip install opencv-contrib-python==3.4.7.28 try like this we have to specifically say the version try lesser version mine was 4.x so I did and no error popped up
Method 8
- first:add
cv.destroyAllWindows() - second:the camera permission you have banned,and then check it.
Method 9
camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW) # Added cv.CAP_DSHOW
return_value, image = camera.read()
cv.imwrite("image.png", image)
camera.release()
cv.destroyAllWindows() # Handles the releasing of the camera accordingly
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0