Opencv / pygame中的视频,帧速率问题

时间:2017-02-09 12:48:08

标签: python opencv video pygame open-sesame

我正在尝试使用opencv和pygame编写一个小文件来播放文件中的视频。只要按下按键,视频就会播放,并一直播放到最后。释放按键后,背景颜色会发生变化,并记录按键和按键释放之间的时间。

到目前为止一切顺利,似乎才能按预期工作。我唯一的问题是我无法按照自己的意愿设置帧速率。所有视频都具有相同的长度(正好是2500毫秒,20 fps),当我运行脚本时,视频太快了。我试过了很多东西(video.set(cv2.cv.CV_CAP_PROP_FPS,),...),但它没有用。我得到的最接近的是在循环中的某处添加self.sleep(50)。它似乎调整了帧速率,但由于每个视频的长度不一致,它看起来非常可靠。任何建议将不胜感激 !非常感谢!

import cv2
import numpy
import pygame

red = (255,0,0)

background_color = (0,0,0,)

#check whether the correct backend is being used (for OpenSesame)
if self.get('canvas_backend') == 'legacy':
    surface = exp.surface
elif self.get('canvas_backend') == 'xpyriment':
    surface = exp.window
else:
    raise Exception('This script requires legacy or xpyriment (no opengl)')


# Full path to the video file
path = pool[self.get('video')]
# Open the video and determine the video dimensions
video = cv2.VideoCapture(path)

#initiate the video with a keypress and start the clock
key = None
while key == None:
   for event in pygame.event.get():
       if event.type == pygame.KEYDOWN:
           start_time = self.time()
           key = event.key
           go = True

while go:

    # Get a frame and break the loop if we failed, usually because the video
    # ended.
    success, frame = video.read()
    if not success:
        break

    #for some reasons the frames appeared inverted
    frame = numpy.fliplr(frame)
    frame = numpy.rot90(frame)

        # The video uses BGR colors and PyGame needs RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Create a PyGame surface
    surf = pygame.surfarray.make_surface(frame)

    #set the frame rate to 20 fps ?? To be verified.
    self.sleep(50)

    # Fill the background color of the PyGame surface whenever a key is released
    for event in pygame.event.get():
        if event.type == pygame.KEYUP:
            background_color = red
            surface.fill(background_color)
            pygame.display.update
            end_time = self.time()  

    # Show the PyGame surface!
    surface.blit(surf, ((surface.get_rect().centerx-150),(surface.get_rect().centery-100)))
    pygame.display.flip()



#define the experimental variable
rt = end_time- start_time
exp.set('keyreleaseRT', rt)
print '%s was released after %d ms' % (key, rt)

0 个答案:

没有答案