2017-11-17 21 views
1

私はOpenCV3(Python 3.6)でビデオを書き込もうとしています。私はこのコードがどこかに掲載されているのを発見したコードは機能しますが、ビデオを再生すると数秒ごとに間違ったフレームが挿入されるようです。シーケンスの最初のフレームのようです。ビデオの見た目は次のとおりです。ここで(Link to video埋め込みコードが実行されない場合)OpenCV 3 VideoWriter余分なフレームを挿入する

<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)) 

任意のポインタは、このコードの後に​​、あなたのコードでは

答えて

0

をapprecitedされます:

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を忘れないでください。

関連する問題