I am taking pictures on my Raspberry Pi using an usb webcam and Pygame. These images will be used to track movement of an object, then rotate a motor. Therefore, a constant framerate would be nice. Unfortunately, taking pictures of dark object seem to take up almost 4x as much time as with bright objects. I suspect this is the result of a longer exposure time.
If this is indeed the problem, is there any way to set the exposure time to a fixed number? If not, what else can i do?
result of code below:
Dark: (aimed at a black wall)
- Duration: 14213 ms
- Min: 12 ms
- Max: 387 ms
- Avg: 142 ms
Bright: (aimed at a white wall)
- Duration: 3550 ms
- Min: 12 ms
- Max: 67 ms
- Avg: 35 ms
print "importing.."
import time
import pygame
import pygame.camera
from pygame.locals import *
# INITIALIZE CAMERA
print "\ninitializing.."
pygame.init()
pygame.camera.init()
camlist = pygame.camera.list_cameras()
cam = pygame.camera.Camera("/dev/video0", (320,240))
cam.start()
time.sleep(1)
# MEASURE TIME
print "running.."
begin = int(round(time.time() * 1000))
min = 1000
max = 0
for i in range(1, 100):
start = int(round(time.time() * 1000))
img = cam.get_image()
stop = int(round(time.time() * 1000)) - start
if(stop > max):
max = stop
if(stop < min):
min = stop
print "{}\t{} ms".format(i, stop)
duration = int(round(time.time() * 1000)) - begin
print "Duration: {} ms".format(duration)
print "Min:\t{} ms".format(min)
print "Max:\t{} ms".format(max)
print "Avg:\t{} ms".format(duration / 100)
As long as your required frame rate is low enough to accommodate the slowest exposure, you can sleep after processing each frame until it's time for a new frame. You've already got the time that you started the first frame. When the first frame is done, add the required interval to get the next start and calculate the sleep time to the next frame.