在Python中使用pyglet进行视频运动计算

发布于 2024-08-27 01:06:12 字数 1336 浏览 13 评论 0原文

我正在编写一个简单的运动检测程序,但我希望它是跨平台的,所以我使用 python 和 pyglet 库,因为它提供了一种简单的方法来加载不同格式的视频(特别是 wmv 和 mpeg)。到目前为止,我有下面给出的代码,它加载电影并在窗口中播放它。现在我需要: 1)在时间t和t-1抓取帧 2) 进行减法以查看哪些像素对于运动检测处于活动状态。

关于如何抓取帧和跳过帧的任何想法,是否可以将像素值放入 numpy 中的矩阵或直接从 pyglet 中放入矩阵中?或者应该考虑使用 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()  

I am writing a simple motion detection program but i want it to be cross platform so im using python and the pyglet library since it provides a simple way to load videos in different formats (specially wmv and mpeg). So far i have the code given below which loads the movie and plays it in a window. Now i need to:
1) grab frame at time t and t-1
2) do a subtraction to see which pixels are active for motion detection.

any ideas on how to grab frames and to skip over frames and is it possible to put the pixel values into a matrix in numpy or something directly from pyglet? or should look into using something other than pyglet?

thanks
kuaywai


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()  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-09-03 01:06:12

对我来说,您似乎必须跳过播放功能并手动逐步浏览视频/动画,也许使用 source.get_animation().frames 这是一个帧列表,其中每个帧都是一个简单的图像。我猜这对于大型视频来说并不实际,但这通常不应该在 Python 中处理。

To me it seems like you have to skip the play function and manually step through the video/animation, maybe using source.get_animation().frames which is a list of frames where each frame is a simple image. I'm guessing this isn't really going to be pracitical with large videos but that is generally not something you should be handling in Python anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文