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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53888878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:23:10  来源:igfitidea点击:

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

pythonimagecv2

提问by Elephant

I am trying to take a picture from the defualt carmera with python, to do this I am using openCV (import cv2from python shell). However, when I attempt to disable the camera it closes but with the error [ WARN:0] terminating async callback.

我正在尝试使用 python 从默认的 carmera 中拍照,为此我使用 openCV(import cv2来自 python shell)。但是,当我尝试禁用相机时,它会关闭但出现错误[ 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

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

采纳答案by Aprajita Verma

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!

我希望这有帮助!

回答by Djotchuang Tamo

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.

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

回答by Sumit kumar

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

cv2.destroyAllWindows()

回答by robot liu

  1. first:add cv2.destroyAllWindows()
  2. second:the camera permission you have banned,and then check it.
  1. 第一:添加 cv2.destroyAllWindows()
  2. 第二:你已经禁止的相机权限,然后检查它。

回答by Joe Llerena

It works for me as indicated Sumit Kumar

正如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()

回答by Brady R

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

回答by ARUN AK

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

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

回答by Little_being

I did this & I don't see that warning there after.(only for Windows OS)

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

Open cmd and type:

打开cmd并输入:

setx OPENCV_VIDEOIO_PRIORITY_MSMF 0