所以我试图弄清楚如何使用带有Python绑定的OpenCV RC3.0 RC1正确确定实时网络摄像头源每秒捕获的帧数。
以下是我正在使用的代码:
import timeit
import cv2
fps_timer = timeit.Timer('[webcam.read() for x in range(30)]\nwebcam.release()', 'import cv2\nwebcam=cv2.VideoCapture(0)\n')
#time how long it takes to read 30 frames 10 times in a row
fps = (10*30)/fps_timer.timeit(10)
基于我在Stackoverflow和OpenCV论坛上使用的其他线程:
webcam=cv2.VideoCapture(0)
webcam.get(cv2.CAP_PROP_FPS)
无效。
问题:这是确定网络摄像头FPS的合理/准确方法吗?如果没有,有什么更好的选择?
为我想要实现的目标提供更多细节:
我想以这种方式弄清楚网络摄像头的FPS的原因是因为我通过子进程将webcam.read()的结果输出到ffmpeg所以我需要知道粗略的FPS。我正在寻找的核心功能是(实际的代码还有很多其他的东西,我觉得不相关):
FFMPEG_BIN = u'C:/FFMPEG/bin/ffmpeg.exe'
#Code to determine the FPS of the webcam would go here
ffmpeg_command = [ FFMPEG_BIN,
'-f', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', '{}x{}'.format(width,height), # size of one frame
'-r', '{}'.format(fps), # frames per second
'-i', '-', # The imput comes from a pipe
'-an', # Tells FFMPEG not to expect any audio
'-vcodec', 'libx264',
"output_video.avi" ]
video_writer = sp.Popen(ffmpeg_command, stdin=sp.PIPE)
while True:
ret, frame = webcam.read()
video_writer.stdin.write(frame.tostring())