CV2: "[ WARN:0] terminating async callback" when attempting to take a picture

65,699

Solution 1

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.

Solution 2

camera = cv2.VideoCapture(camera_port,cv2.CAP_DSHOW)

cv2.destroyAllWindows()

Solution 3

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!

Solution 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
Share:
65,699
Elephant
Author by

Elephant

I'm a hobbyist programmer and I have learnt everything I know for fun :)

Updated on July 28, 2022

Comments

  • Elephant
    Elephant almost 2 years

    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

  • isaac weathers
    isaac weathers over 4 years
    This solution worked but defining port instead cv2.VideoCapture(0, cv2.CAP_DSHOW)
  • Otto medina
    Otto medina about 4 years
    Using cv2.CAP_DSHOW removes the warning, but slows down my frame rate from 30fps to 7fps, on Windows.
  • Otto medina
    Otto medina about 4 years
    Using cv2.CAP_DSHOW removes the warning, but slows down my frame rate from 30fps to 7fps, on Windows
  • Otto medina
    Otto medina about 4 years
    On Windows 10 with Python 3.8 and OpenCV 4.2, this does not solve the problem. The warning is also displayed on the OpenCV sample programs, e.g. samples/python/video.py.
  • Engr Syed Rowshan Ali
    Engr Syed Rowshan Ali almost 4 years
    Correct Answer, don't know why, but mentioning the 2nd parameter solves the warning
  • Mrigank Pawagi
    Mrigank Pawagi almost 4 years
    Goodness! Thank you so much!
  • Goigle
    Goigle about 3 years
    It's worth noting that DirectShow is a legacy API and is currently deprecated
  • Peter Wood
    Peter Wood about 3 years
    @Goigle what would you use instead of CAP_DSHOW?
  • Goigle
    Goigle about 3 years
    @PeterWood The default on windows should be fine (CAP_MSMF), assuming your Win10 is up to date. On Win10 20H2 I experience no issues (that warning just means the stream ended). The only people on my team who have issues are on outdated versions of Windows. This makes sense as DSHOW used to be the default and MSMF is slowly replacing DSHOW
  • Christoph Rackwitz
    Christoph Rackwitz over 2 years
    destroyAllWindows() is absolutely not relevant to this question.