Python video capture loop

I wrote a simple script to continuously capture snapshots from my webcam. My only problem is that the video capture module does not always capture an image, which in turn leads to a program crash. I think I could solve this using an infinite loop, but I'm not sure how to do 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()
+3
source share
1 answer

Try using cam.getImageinstead cam.saveSnapshot. cam.getImagereturns a PIL image, so you can determine if a frame is actually captured or not. The following code has not 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')
+4
source

All Articles