gpt4 book ai didi

python - 如何使用python opencv在jupyter笔记本上以正常速度播放mp4

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:24 27 4
gpt4 key购买 nike

有什么方法可以使用 python opencv 在 jupyter notebook 上以与实际文件相同的速度播放 mp4 视频?
当我制作 .py 文件并使用 cv2.imshow() 时,速度是正常的,但是当我在 jupyter notebook 上尝试下面的代码时,它变得非常慢。
我的代码:

import cv2
import matplotlib.pyplot as plt
from IPython import display
%matplotlib inline

cap = cv2.VideoCapture('video.mp4')

while(cap.isOpened()):
try:
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imshow(frame)

display.clear_output(wait=True)
display.display(plt.gcf())

except KeyboardInterrupt:
cap.release()

最佳答案

根据我的经验,即使您在 Jupyter Notebook 中工作,也最好使用 OpenCV 的显示功能。这可以这样实现:

fps = 30
title = 'normal speed video'
delay = int(1000 / fps)

for frame in frames(video):
cv2.imshow(title, frame)

if cv2.waitKey(delay) & 0xFF == ord('q'):
break

cv2.destroyAllWindows()

此处frames 生成器只是逐帧生成视频,可以是found here


也可以通过 Video 以编程方式在 Jupyter 中显示视频:

from IPython.display import Video

Video(‘https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4’)
Video(‘path/to/video.mp4’)
Video(‘path/to/video.mp4’, embed=True)
Video(b’raw-videodata’, embed=True)

See docs了解更多信息

关于python - 如何使用python opencv在jupyter笔记本上以正常速度播放mp4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com