欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pygame+opencv實(shí)現(xiàn)讀取視頻幀的方法示例

 更新時間:2021年12月03日 10:54:53   作者:老光頭_ME2CS  
由于pygame.movie.Movie.play()只支持MPEG格式的視頻,所以決定使用與opencv讀取視頻幀的畫面,本文就詳細(xì)的介紹了pygame+opencv實(shí)現(xiàn)讀取視頻幀,感興趣的可以了解一下

由于pygame.movie.Movie.play() 只支持MPEG格式的視頻,且 pygame版本大于1.9.5好像已經(jīng)不支持這個模塊了,所以決定使用與opencv讀取視頻幀的畫面,利用pygame的surface刷新窗口。

有基礎(chǔ)的小伙伴,代碼還是很好理解,直接上代碼

pygame.time.Clock()同步時間

import pygame
from pygame.locals import *
import cv2
import sys
import time

FPS = 30
FramePerSec = pygame.time.Clock()

video_path = './Selected Stimuli/noaudio_c_001_critical_swerve.mp4'
video = cv2.VideoCapture(video_path)

pygame.init()
pygame.display.set_caption('OpenCV Video Player on Pygame')

screen = pygame.display.set_mode((1280, 720), 0, 32)
screen.fill([0,0,0])
num = 0

while True :

    T1 = time.time()
    ret, frame = video.read()
    if ret == False:
        print('Total Time:', time.time()-T0)
        sys.exit()

    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame = cv2.transpose(frame)
    frame = pygame.surfarray.make_surface(frame)
    screen.blit(frame, (0,0))
    if num == 0:
        T0 = time.time()
    pygame.display.update()
    FramePerSec.tick(FPS)

    num += 1
    print('freq time:{}, frame num: {}'.format(time.time()-T1, num))

    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

?但是存在一些問題,時間戳的耗時比視頻默認(rèn)時間更長。

按理說FramePerSec = pygame.time.Clock()是能夠很好的控制總的時長,但是發(fā)現(xiàn)視頻越長播放器的延遲時間越長

換成Ubuntu系統(tǒng)后,發(fā)現(xiàn)以上延遲的問題得到緩解,推測可能與Windows系統(tǒng)中的進(jìn)程管理有關(guān)。但是視頻時差別很明顯,比如120s視頻,實(shí)際播放時間只用了118.8s。推測可能是pygame.time.Clock()是確保單幀的刷新率與預(yù)設(shè)相同,但是由于每一幀都存在相同的時間誤差,就導(dǎo)致誤差累加的問題明顯。

自編時間控制

由于以上原因無法解決,增加了一個簡單的控制邏輯后可有效控制視頻播放的時間戳問題

import pygame
from pygame.locals import *
import cv2
import sys
import time

video_path = 'out1.avi'
video = cv2.VideoCapture(video_path)

FPS = int(round(video.get(cv2.CAP_PROP_FPS)))

FramePerSec = pygame.time.Clock()

Width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
Height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))

pygame.init()
pygame.display.set_caption('OpenCV Video Player on Pygame')

screen = pygame.display.set_mode((Width, Height), 0, 32)
screen.fill([0,0,0])
num = 0

while True :

    if num == 0:
        T0 = time.time()

    if time.time()-T0 > num*(1./FPS):

        ret, frame = video.read()
        TimeStamp = video.get(cv2.CAP_PROP_POS_MSEC)

        if ret == False:
            print('Total Time:', time.time()-T0)
            pygame.quit()
            sys.exit()

        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = cv2.transpose(frame)
        frame = pygame.surfarray.make_surface(frame)
        screen.blit(frame, (0,0))

        pygame.display.update()
        
        num += 1

    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

到此這篇關(guān)于pygame+opencv實(shí)現(xiàn)讀取視頻幀的方法示例的文章就介紹到這了,更多相關(guān)pygame opencv讀取視頻幀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論