2017-09-21 9 views
0

私は以下のコードを実行してビデオをフレームに変換しています。問題は、0 KBのサイズの画像ファイルを作成していて、開いたときに何も表示されていないということです。問題の原因を理解できません。 Imageコーデックをインストールする必要はありますか?openCV imwriteは0kbのイメージフレームを書きます

''' 
    Using OpenCV takes a mp4 video and produces a number of images. I am using OpenCV 3.3.0 version and Python 2.7 


    Which will produce a folder called data with the images, There will be 2000+ images for example.mp4. 
    ''' 
    import cv2 
import numpy as np 
import os 

# Playing video from file: 

try: 
     cap = cv2.VideoCapture('aa.mkv') 
except: 
     print "Could not open video file" 
     raise 
print cap.grab() 

try: 
    if not os.path.exists('data'): 
     os.makedirs('data') 
except OSError: 
    print ('Error: Creating directory of data') 

currentFrame = 0 
while(True): 
    # Capture frame-by-frame 
    ret, frame = cap.read() 

    if not frame is None: 
     # Saves image of the current frame in jpg file 
     name = './data/frame' + str(currentFrame) + '.jpg' 
     print ('Creating...' + name) 
     cv2.imwrite(name, frame) 
     #cv2.imshow(name, frame) 
    else: 
     break 

    # To stop duplicate images 
    currentFrame += 1 

# When everything done, release the capture 
cap.release() 
cv2.destroyAllWindows() 
+2

imwriteコマンドはどこですか? –

+0

'frame'をチェックすると、それは空白になります。代わりに、あなたの 'ret'をチェックして、それが' False'であること、つまりフレームが読み込まれていないことを確認することができます。 –

+0

retも何も返さない。 –

答えて

1

フレームを書き込むためにimwrite関数を使用していません。また、imshow関数名のスペルが間違っています。私はあなたのコードを変更しました。試してみてください:

import cv2 
import numpy as np 
import os 

# Playing video from file: 
cap = cv2.VideoCapture('aa.mkv') 

try: 
    if not os.path.exists('data'): 
     os.makedirs('data') 
except OSError: 
    print ('Error: Creating directory of data') 

currentFrame = 0 
while(True): 
    # Capture frame-by-frame 
    ret, frame = cap.read() 

    if not frame is None: 
     # Saves image of the current frame in jpg file 
     name = './data/frame' + str(currentFrame) + '.jpg' 
     print ('Creating...' + name) 
     cv2.imwrite(name, frame) 
     cv2.imshow(name, frame) 
    else: 
     break 

    # To stop duplicate images 
    currentFrame += 1 

# When everything done, release the capture 
cap.release() 
cv2.destroyAllWindows() 
+0

私は、OPがある時点でコード内に 'imwrite'を持っていて、' w'がどこから来たのか推測するつもりです。 –

+0

このエラーが発生しています。cv2.error:C:\ build \ master_winpack-bindings-win32-vc14-static \ opencv \ modules \ highgui \ src \ window.cpp:325:エラー:(-215)size.width> 0 && size.height> function cv :: imshow –

+0

私はコードを変更しました。今すぐチェックしてください。また、ビデオへのパスが正しいかどうかを確認してください。 – Jazz

関連する問題