python video capture loop

有些话、适合烂在心里 提交于 2019-12-07 16:55:54

问题


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

回答1:


Try to use cam.getImage instead of cam.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:

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


来源:https://stackoverflow.com/questions/5595658/python-video-capture-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!