19

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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Elephant
  • 446
  • 1
  • 7
  • 19
  • 2
    For whoever is looking for the reason, as Aprajita Verma mentioned, the handle to the webcam is not released which gives the error. I have used the webcam in and outside of a condition in my code for OCR application. When the handle is released, the error does not occur. – Siyahi Sep 15 '20 at 16:49

9 Answers9

36

I had the same warning.

Just modify the line

camera = cv.VideoCapture(camera_port)

to

camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
7

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!

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Aprajita Verma
  • 208
  • 1
  • 5
  • 4
    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``. – Janos Mar 01 '20 at 17:42
7
camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW)

cv.destroyAllWindows()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Sumit kumar
  • 79
  • 1
  • 1
  • Using cv2.CAP_DSHOW removes the warning, but slows down my frame rate from 30fps to 7fps, on Windows – Janos Mar 01 '20 at 17:13
  • bad answer. `destroyAllWindows` has nothing to do with cameras, and there are no windows to destroy. do not just copy a previous answer and repost it. – Christoph Rackwitz Jul 23 '22 at 15:54
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
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
1

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)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

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()
Joe Llerena
  • 144
  • 1
  • 5
  • bad answer. `destroyAllWindows` has nothing to do with cameras, and there are no windows to destroy. do not just copy a previous answer and repost it. – Christoph Rackwitz Jul 23 '22 at 15:54
-1
  1. first:add cv.destroyAllWindows()
  2. second:the camera permission you have banned,and then check it.
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
robot liu
  • 7
  • 3
  • bad answer. `destroyAllWindows` has nothing to do with cameras, and there are no windows to destroy. do not just copy a previous answer and repost it. – Christoph Rackwitz Jul 23 '22 at 15:53
-1
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
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Brady R
  • 190
  • 1
  • 3
  • bad answer. `destroyAllWindows` has nothing to do with cameras, and there are no windows to destroy. do not just copy a previous answer and repost it. – Christoph Rackwitz Jul 23 '22 at 15:53
  • Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jul 24 '22 at 06:41
-1

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