python 视频捕获循环
我编写了一个简单的脚本来连续从网络摄像头捕获快照。我唯一的问题是视频捕获模块并不总是捕获图像,这反过来又导致程序崩溃。我想我可以通过使用无限循环来解决这个问题,但我不确定如何解决。这是脚本:
from VideoCapture import Device
import datetime
def capt():
a = datetime.datetime.now().strftime("%Y%m%dT%H%M%S%ms")
b = str(a)
cam = Device(devnum=0)
cam.setResolution(1280, 960)
cam.saveSnapshot('%s.png' % (b))
for i in range(1, 100000):
capt()
I've written a simple script to continuously capture snapshots from my webcam. My only issue is the videocapture module doesn't always grab an image which in turn crashes the program. I think I could solve this by using an infinite loop but I'm not real certain how to go about it. Here's the script:
from VideoCapture import Device
import datetime
def capt():
a = datetime.datetime.now().strftime("%Y%m%dT%H%M%S%ms")
b = str(a)
cam = Device(devnum=0)
cam.setResolution(1280, 960)
cam.saveSnapshot('%s.png' % (b))
for i in range(1, 100000):
capt()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 cam.getImage 而不是 cam.saveSnapshot 。
cam.getImage
返回 PIL 图像,因此您可以确定是否实际抓取了任何帧。以下代码未经测试:
Try to use
cam.getImage
instead ofcam.saveSnapshot
.cam.getImage
returns PIL image, thus you are able to determine whether any frame has been actually grabbed or not.The folloing code hasnt been tested: