OPenCV に基づく遅いビデオ再生

tkinter、pygame、および OpenCV を使用してそれぞれビデオを再生すると、ビデオの再生速度が低下することがわかります。ソース コードは次のとおりです。tkinter、OpenCV を使用してビデオを再生します

from tkinter import *
import cv2
from PIL import Image, ImageTk

def video_play():
while video.isOpened():
ret, frame = video.read() # 写真を読む
# print('read successfully')
if ret == True:
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) # 再生時に元の色を維持するように色を変換
current_image = Image.fromarray(img).resize((1920,1080)) # 画像を Image オブジェクトに変換
imgtk = ImageTk.PhotoImage(image=current_image)
movieLabel.imgtk = imgtk
movieLabel.config(image=imgtk)
movieLabel.update() # 各実行は 1 つの画像のみを表示し、ビデオ再生を実現するにはウィンドウを更新する必要があります
else:
break

root = Tk()
root.overrideredirect(True)
root.state(“zoomed”) movieLabel = Label(root,width=1920,height=1080) #動画
を再生するためのラベル コンテナを作成します
movieLabel.pack()
while True : video
= cv2.VideoCapture(r"E:\Relax\xjj\Sniffer\Yuanshin\1612179174bfee75ebffaca79a.mp4_last.mp4") #opencv でローカル動画ファイルを開く video_play(
) #video_play を呼び出して動画再生を実現する

mainloop() は、
pygame、OpenCV を使用してビデオを再生します。

import pygame
import sys
import cv2
import numpy as np
import os
import moviepy.editor as mpy

if name == ' main ':
# バックグラウンド ミュージックを傍受
# audio_background = mpy.AudioFileClip('cs1.mp4')
#
# audio_background.write_audiofile('cs1.mp3')
pygame.init() # pygame
FPS を初期化 = 100
#Setウィンドウ位置
os.environ['SDL_VIDEO_WINDOW_POS']="%d,%d" % (5,30)
FPSClock = pygame.time.Clock()
size = width, height = 960, 540 # ウィンドウサイズを設定
screen = pygame. display.set_mode(size) # 表示ウィンドウの
色 = (255, 255, 255) # 色を設定
ogg=pygame.mixer.Sound(“cs1.mp3”)
#pygame.mixer.music.load(“”)

videoCapture = cv2.VideoCapture("cs2.mp4")
ogg.play()

while True:
    a=pygame.time.get_ticks()

    if videoCapture.isOpened():
        #从opncv读一段视频进来
        ret, frame = videoCapture.read()
        if ret :
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            # frame = np.asarray(frame.resize((1920, 1080)))
            frame = cv2.resize(frame,(960,540),interpolation=cv2.INTER_CUBIC)
            frame = np.rot90(frame, k=-1)
            frame = pygame.surfarray.make_surface(frame)

            frame=pygame.transform.flip(frame,False,True)

            screen.blit(frame, (0,0))

    for event in pygame.event.get():  # 遍历所有事件
        if event.type == pygame.QUIT:  # 如果单击关闭窗口,则退出
            sys.exit()
    pygame.display.flip()  # 更新全部显示
    FPSClock.tick(FPS)
    pygame.time.get_ticks()

OpenCV でビデオを再生します。

cv2 をインポート
numpy を np としてインポート

moviepy.editor を mpy としてインポート

インポート pygame
pygame.init()

ogg=pygame.mixer.Sound(“cs1.mp3”)

バックグラウンドミュージック

cap = cv2.VideoCapture(“cs1.mp4”)
fps = cap.get(cv2.CAP_PROP_FPS)

ogg.play()

while (True):
# フレームの読み取り
ret, frame = cap.read()
# 画像の表示
cv2.imshow(“video”, cv2.resize(frame,(960,540),interpolation=cv2.INTER_CUBIC))
# 画像圧縮のスケール, if cv2.waitKey(int(1000/fps)) &
0xFF == ord('q'): cap.release ()
を破るcv2.destroyAllWindows () . . . . . . . . . . . . . . . . . . . . . . . . . _ _ _ _ _ _ _


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

おすすめ

転載: blog.csdn.net/douyinbuwen/article/details/127155810