Jupyter NoteBook中播放视频
from IPython.display import clear_output, display, HTML
from PIL import Image
import matplotlib.pyplot as plt
import time
import cv2
import os
def show_video(video_path:str,small:int=2):
if not os.path.exists(video_path):
print("视频文件不存在")
video = cv2.VideoCapture(video_path)
current_time = 0
while(True):
try:
clear_output(wait=True)
ret, frame = video.read()
if not ret:
break
lines, columns, _ = frame.shape
if current_time == 0:
current_time = time.time()
else:
last_time = current_time
current_time = time.time()
fps = 1. / (current_time - last_time)
text = "FPS: %d" % int(fps)
cv2.putText(frame, text , (0,100), cv2.FONT_HERSHEY_TRIPLEX, 3.65, (255, 0, 0), 2)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (int(columns / small), int(lines / small)))
img = Image.fromarray(frame)
display(img)
time.sleep(0.02)
except KeyboardInterrupt:
video.release()
show_video('/home/tl/vechcle_infer/data/pred_road.mp4')
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0ByOnmXH-1626950190393)(output_2_0.png)]](https://i-blog.csdnimg.cn/blog_migrate/ff57e13ef43dd3959e7d9d9c4533ee89.png)