CV2:尝试拍照时“[WARN:0] 终止异步回调”

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

我正在尝试使用 python 从 defualt carmera 拍照,为此我正在使用 openCV(import cv2 来自 python shell)。但是,当我尝试禁用相机时,它会关闭但出现错误 [ WARN:0] terminating async callback

这是我正在尝试的代码 运行:

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

代码输出所需的结果 - 它需要保存图像,但我不明白为什么会出现错误消息或如何删除它

它可能会显示警告,因为您没有释放网络摄像头的句柄。

尝试将其添加到代码末尾

camera.release()
cv2.destroyAllWindows()

希望对您有所帮助!

  1. 首先:添加cv2.destroyAllWindows()
  2. 其次:你禁止的摄像头权限,然后查看。

我这样做了,之后我没有看到那个警告。(仅适用于 Windows OS)

打开 cmd 并输入:

setx OPENCV_VIDEOIO_PRIORITY_MSMF 0
camera = cv2.VideoCapture(camera_port,cv2.CAP_DSHOW)

cv2.destroyAllWindows()

它对我有用 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()

我有同样的警告。 只需将行 camera = cv2.VideoCapture(camera_port) 修改为 camera = cv2.VideoCapture(camera_port, cv2.CAP_DSHOW) 并添加 cv2.destroyAllWindows() 作为代码的最后一行。

camera = cv2.VideoCapture(camera_port, cv2.CAP_DSHOW) # Added cv2.CAP_DSHOW
return_value, image = camera.read()
cv2.imwrite("image.png", image)
camera.release()
cv2.destroyAllWindows() # Handles the releasing of the camera accordingly

嘿伙计们找到了解决方案 pip install opencv-contrib-python==3.4.7.28 像这样尝试我们必须特别说明版本尝试较小版本我的是 4.x 所以我做到了并且没有弹出错误

这似乎是 opencv 的 MSMF 后端中的一个错误。

如果您使用的是 windows,那么您可以将后端更改为 DirectShow 后端。

所以,像这样更改 VideoCapture

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