使用Python中的Pyglet从视频计算运动

1 投票
1 回答
1268 浏览
提问于 2025-04-15 20:56

我正在写一个简单的运动检测程序,但我想让它可以在不同的平台上运行,所以我选择了Python和pyglet库,因为它可以很方便地加载不同格式的视频(特别是wmv和mpeg格式)。到目前为止,我已经写了下面的代码,可以加载电影并在窗口中播放。现在我需要做的是:

  1. 在时间t和t-1抓取帧
  2. 进行减法运算,以查看哪些像素在运动检测中是活跃的。

有没有什么想法可以帮助我抓取帧和跳过帧?还有,是否可以直接从pyglet把像素值放入numpy的矩阵中?或者我应该考虑使用其他库而不是pyglet?

谢谢!


import pyglet  
import sys  
window = pyglet.window.Window(resizable=True)  
window.set_minimum_size(320,200)  
window.set_caption('Motion detect 1.0')  
video_intro = pyglet.resource.media('movie1.wmv')  
player = pyglet.media.Player()  
player.queue(video_intro)  

print 'calculating movie size...'  
if not player.source or not player.source.video_format:  
    sys.exit  

myWidth = player.source.video_format.width  
myHeight = player.source.video_format.height  
if player.source.video_format.sample_aspect > 1:  
    myWidth *= player.source.video_format.sample_aspect  
elif player.source.video_format.sample_aspect < 1:  
    myHeight /= player.source.video_format.sample_aspect  

print 'its size is %d,%d' % (myWidth,myHeight)  
player.play()  

@window.event  
def on_draw():  
        window.clear()  
        (w,h) = window.get_size()  
        player.get_texture().blit(0, h-myHeight,  
                              width=myWidth,  
                              height=myHeight)  

pyglet.app.run()  

1 个回答

0

在我看来,你可能需要跳过播放功能,手动逐帧播放视频或动画。可以使用source.get_animation().frames,这个是一个帧的列表,每一帧都是一张简单的图片。我猜这样做对于大视频来说并不太实际,但其实在Python中处理大视频本来就不是一个好主意。

撰写回答