OpenCV 3 VideoWriter插入额外的帧

问题描述 投票:1回答:1

我正在尝试在OpenCV3(Python 3.6)中编写视频。我发现这个代码贴在某处。代码有效,但是当我播放视频时,似乎每隔几秒就会插入错误的帧。它看起来像是序列的第一帧。以下是视频的外观。 (如果嵌入代码没有运行,Link to video

<iframe width="560" height="315" src="https://www.youtube.com/embed/J3HKaQlzS8Y" frameborder="0" gesture="media" allowfullscreen></iframe>

这是我在Windows 10(64位)#!/ usr / local / bin / python3上使用的代码

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='jpg', 
help="extension name. default is 'jpg'.")
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))

任何指针将不胜感激

python opencv
1个回答
0
投票

在您的代码中,在此代码之后:

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

只需添加:

 images = sorted(images, key=lambda x: (int(re.sub('\D','',x)),x))

这样我们就可以获得一个排序数据。因此,视频帧将全部设置在那里的位置。不要忘记将import re作为头文件。

© www.soinside.com 2019 - 2024. All rights reserved.