2017-09-12 9 views
1

なぜ次のコードはビデオを保存しないのですか? ウェブカメラのフレームレートがVideoWriterフレームサイズと正確に一致することも必須ですか?Opencv2:Python:cv2.VideoWriter

import numpy as np`enter code here 
import cv2 
import time 

def videoaufzeichnung(video_wdth,video_hight,video_fps,seconds): 
    cap = cv2.VideoCapture(6) 
    cap.set(3,video_wdth) # wdth 
    cap.set(4,video_hight) #hight 
    cap.set(5,video_fps) #hight 

    # Define the codec and create VideoWriter object 
    fps = cap.get(5) 
    fourcc = cv2.VideoWriter_fourcc(*'XVID') 
    out = cv2.VideoWriter('output.avi',fourcc,video_fps, (video_wdth,video_hight)) 
    #out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) 

    start=time.time() 
    zeitdauer=0 
    while(zeitdauer<seconds): 
     end=time.time() 
     zeitdauer=end-start 
     ret, frame = cap.read() 
     if ret==True: 
      frame = cv2.flip(frame,180) 
      # write the flipped frame 
      out.write(frame) 

      cv2.imshow('frame',frame) 
      if cv2.waitKey(1) & 0xFF == ord('q'): 
       break 
     else: 
      break 

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

videoaufzeichnung.videoaufzeichnung(1024,720,10,30) 

ご協力いただきありがとうございます。

+0

あなたのシステムには 'Xvid'コーデックがインストールされていますか? – zindarod

+0

はい、私はまだ同じ問題を抱えています – user8495738

答えて

1

あなたはビデオI/O用にOpenCVのlibv4lバージョンを使用していると思われます。 VideoCapture::setメソッドがビデオ解像度を変更できないようにするOpenCVのlibv4l APIにバグがあります。リンク1,2および3を参照してください。次の操作を行う場合:

... 
frame = cv2.flip(frame,180) 
print(frame.shape[:2] # check to see frame size 
out.write(frame) 
... 

フレームサイズが関数引数で指定された解像度に一致するように変更されていないことがわかります。この制限を克服する1つの方法は、解像度引数に一致するように手動でフレームのサイズを変更することです。

... 
frame = cv2.flip(frame,180) 
frame = cv2.resize(frame,(video_wdth,video_hight)) # manually resize frame 
print(frame.shape[:2] # check to see frame size 
out.write(frame) 
... 
関連する問題