OpenCV:尝试拍照时"[ WARN:0] terminating async callback"



我试图用python从Defualt Carmera拍摄照片,为此,我正在使用OpenCV(来自Python Shell的import cv2 as cv)。但是,当我尝试禁用相机时,它会关闭,但使用错误[ WARN:0] terminating async callback

这是我要运行的代码:

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

代码输出所需的结果,它拍摄并保存图像,但是我不明白为什么发生错误消息或如何 emove it it

我有相同的警告。

只需修改行

camera = cv.VideoCapture(camera_port)

to

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

它可能会显示警告,因为您没有将手柄释放到网络摄像头上。

尝试将其添加到代码的末尾

camera.release()
cv2.destroyAllWindows()

我希望这会有所帮助!

camera = cv.VideoCapture(camera_port, cv.CAP_DSHOW)
cv.destroyAllWindows()

我做了这件事&我在那里看不到那个警告。(仅适用于Windows OS)

打开CMD和类型:

setx OPENCV_VIDEOIO_PRIORITY_MSMF 0

这似乎是MSMF后端的错误。

如果您使用的是Windows,则可以将后端更改为DirectShow后端。

所以,这样更改VideoCapture

captureDevice = cv.VideoCapture(0, cv.CAP_DSHOW)

它对我有效,如所示 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()

嘿,嗨,伙计们发现解决方案pip pip opencv-contrib-python == 3.4.7.28尝试这样的尝试,我们必须专门说这个版本的版本较小版本是4.x,所以我做到了,所以我做到了,没有错误弹出

  1. 首先:添加cv.destroyAllWindows()
  2. 第二:您已禁止的相机许可,然后检查。
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

最新更新