简体   繁体   English

在Raspberry Pi和Python上控制USB网络摄像头照片捕获时间

[英]Controlling USB web camera photo capture time on Raspberry Pi and Python

I am taking pictures on my Raspberry Pi using an usb webcam and Pygame. 我正在使用USB网络摄像头和Pygame在Raspberry Pi上拍照。 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. 不幸的是,拍摄黑暗物体的照片似乎要花费明亮物体近4倍的时间。 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 -时长:14213 ms
- Min: 12 ms -最小值:12毫秒
- Max: 387 ms -最大值:387 ms
- Avg: 142 ms -平均:142毫秒

Bright: (aimed at a white wall) 明亮:(针对白墙)
- Duration: 3550 ms -持续时间:3550毫秒
- Min: 12 ms -最小值:12毫秒
- Max: 67 ms -最大值:67毫秒
- Avg: 35 ms -平均:35毫秒

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. 完成第一帧后,添加所需的时间间隔以开始下一次,并计算下一帧的睡眠时间。

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

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