简体   繁体   English

python视频捕获循环

[英]python video capture loop

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

Try to use cam.getImage instead of cam.saveSnapshot . 尝试使用cam.getImage而不是cam.saveSnapshot cam.getImage returns PIL image, thus you are able to determine whether any frame has been actually grabbed or not. cam.getImage返回PIL图像,因此您可以确定是否实际抓取了任何帧。 The folloing code hasnt been tested: 以下代码尚未经过测试:

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)

  return cam.getImage(), b

while True:
  im, b = capt()
  if im:
    break
im.save('%s.png' % (b), 'JPEG')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM