从单个帧创建视频

Creating a video from individual frames

我能够成功地将视频拆分为其组成图像帧,并使用 keras RESNet50 模型对其进行分析。我还能够将预测的叠加层添加到这些单独的图像中。现在我想通过将这些经过处理的带有叠加层的图像重新组合成一个 mp4 文件来重新创建原始视频。

如何从单个 jpg 图像帧按顺序创建视频?

我正在尝试使用 cv2.VideoWriter 将这些图像写回到单独的视频文件中。

uname -a 给我以下输出

Linux myhost 4.15.0-1023-azure #24~16.04.1-Ubuntu SMP Wed Aug 29 12:54:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

我的前 11 帧被命名为 frame-0000.jpg 到 frame-0011.jpg

最简单的方法是在您的终端中像这样使用 ffmpeg

ffmpeg.exe -f image2 -r 30 -i frame-%04d.jpg -codec:v libx264 -crf 23 video.mp4

How can I create a video from individual jpg image frames in sequence?

您可以使用此代码从 jpg 图像逐帧创建视频。这些图像将从该脚本所在的文件夹中读取。

import cv2 #Import of openCV library
import os 
#Create video, with name 'video.mp4', MP4 codec, 60 fps, width 1280 and height of 1024
video = cv2.VideoWriter('video.mp4',cv2.VideoWriter_fourcc(*'MP4V'),60,(1280,1024))
for file in os.listdir('./'): #List every file in this folder
    if ".jpg" in file: #Filter only jpg files
        image = cv2.imread(file) #Load image from disk
        video.write(image) #Put image into video.
video.release() #Save video to disk.

您可以修改此代码以从您的应用程序或某个数组加载图像。

编辑:添加评论