【发布时间】:2020-07-23 12:59:04
【问题描述】:
我尝试编写一个小型 Python 程序,它可以让我启动网络摄像头并捕获图像并将其保存到 .png 文件中:
import cv2
cap = cv2.VideoCapture(0)
for i in range(3):
ret, frame = cap.read()
cap.release()
if ret == True:
cv2.imwrite(str(i) + 'image.png', frame)
else:
print("Webcam not working")
print(ret)
但是当我执行它时,它只会在0image.png 下保存一次图像,然后在控制台中显示它:
Webcam not working
False
Webcam not working
False
我做错了什么?
【问题讨论】:
-
因为你在读完第一帧之后就明确地
release了。 -
@DanMašek 谢谢,我将 cap.release() 移到循环之外并将它放在最后并且它起作用了
标签: python file opencv image-processing webcam