frame_to_video Python程序

时间:2018-09-14 04:21:07

标签: python opencv image-processing video-processing

我正在尝试编写将一组框架转换为图像的程序。但是,我没有得到相同的流畅视频,而是在各帧之间连续跳跃。我所做的最初是将视频转换为帧,然后再将帧转换回视频。我的视频长约1分钟,大约有1500张照片(30FPS),因此我认为视频到帧程序没有任何问题。无论如何,我已经在下面附加了我的代码。我这样做的原因是将FACE API应用于每个帧并模糊每个面孔(出于保密目的)。有没有更好的办法?还是我的程序有问题?

import cv2
import argparse
import os

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-ext", "--extension", required=False, default='png', help="extension name. default is 'png'.")
ap.add_argument("-o", "--output", required=False, default='output.mp4', help="output video file")
args = vars(ap.parse_args())

# Arguments
dir_path = '******************'
ext = args['extension']
output = args['output']

images = []
for f in os.listdir(dir_path):
    if f.endswith(ext):
        images.append(f)

# Determine the width and height from the first image
image_path = os.path.join(dir_path, images[0])
frame = cv2.imread(image_path)
cv2.imshow('video',frame)
height, width, channels = frame.shape

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case
out = cv2.VideoWriter(output, fourcc, 20.0, (width, height))

for image in images:

    image_path = os.path.join(dir_path, image)
    frame = cv2.imread(image_path)

    out.write(frame) # Write out frame to video

    cv2.imshow('video',frame)
    if (cv2.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit
        break

# Release everything if job is finished
out.release()
cv2.destroyAllWindows()

print("The output video is {}".format(output))

0 个答案:

没有答案