2017-04-17 3 views
2

私はウェブカメラを読み込んでウィンドウに表示するスクリプトをPythonで手に入れました。これは完全に新しいウィンドウで私のウェブカメラからのリアルタイムのビデオ映像を示しOpenCVでWebカメラビデオをPythonで保存する方法

import cv2 
import imutils 
camera = cv2.VideoCapture(0) 

# Define the codec and create VideoWriter object to save the video 
fourcc = cv2.VideoWriter_fourcc(*'XVID') 
video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) 

while True: 
    try: 
     (grabbed, frame) = camera.read() # grab the current frame 
     frame = imutils.resize(frame, width=640, height=480) 
     cv2.imshow("Frame", frame) # show the frame to our screen 
     key = cv2.waitKey(1) & 0xFF # I don't really have an idea what this does, but it works.. 
     video_writer.write(frame) # Write the video to the file system 
    except KeyboardInterrupt: 
     break 

# cleanup the camera and close any open windows 
camera.release() 
video_writer.release() 
cv2.destroyAllWindows() 
print "\n\nBye bye\n" 

:私は今、そうthis tutorial次、私は次のコードを書いて、結果を保存します。しかし、ビデオファイルを書くことは失敗するようです。それはoutput.aviというファイルを作成しますが、ファイルが空である(ゼロバイト)とコマンドラインで私は、次のエラーを参照してください。

OpenCV: Frame size does not match video size. 
OpenCV: Frame size does not match video size. 
OpenCV: Frame size does not match video size. 
etc. 

私ははっきりと私は保存するサイズにフレームのサイズを変更しますビデオ(640x480)だから私はなぜそれが一致しないか分からない。

私は(そう、この場合には、空のoutput.aviがすでに存在している)を再度スクリプトを実行すると、それはこれらのエラーを示していますチュートリアルで

2017-04-17 10:57:14.147 Python[86358:5848730] AVF: AVAssetWriter status: Cannot Save 
2017-04-17 10:57:14.332 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save 
2017-04-17 10:57:14.366 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save 
2017-04-17 10:57:14.394 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save 
etc. 

それが四桁のFourCCコードを指定するために使用されていることを述べていますビデオコーデックはプラットフォームに依存し、利用可能なコードのリストはfourcc.orgにあります。私はOSXを使っているので、DIVX、XVID、MJPG、X264、WMV1、WMV2のコーデックコードを試しました。しかし、残念なことにそれらのどれも私のために働いていません。彼らは皆、私に次のエラーを与える、MJPGを除いて、同じエラーを与える:

OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829 
Traceback (most recent call last): 
    File "store_video.py", line 15, in <module> 
    video_writer.write(frame) # Write the video to the file system 
cv2.error: /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write 

誰もがここで間違っている可能性が何を知っていますか?すべてのヒントは大歓迎です!

答えて

5

おそらくAVCoundationでOpenCVを構築し、XVIDや他のコーデックをサポートしていないからでしょう。 mp4vm4vエクステンションを試すことができます。他のノートで

import cv2 
camera = cv2.VideoCapture(0) 

# Define the codec and create VideoWriter object to save the video 
fourcc = cv2.VideoWriter_fourcc('m','p','4','v') 
video_writer = cv2.VideoWriter('output.m4v', fourcc, 30.0, (640, 480)) 

while True: 
     (grabbed, frame) = camera.read() # grab the current frame 
     frame = cv2.resize(frame, (640,480)) 
     cv2.imshow("Frame", frame) # show the frame to our screen 
     key = cv2.waitKey(33) & 0xFF # I don't really have an idea what this does, but it works.. 
     video_writer.write(frame) # Write the video to the file system 
     if key==27: 
      break; 

# cleanup the camera and close any open windows 
camera.release() 
video_writer.release() 
cv2.destroyAllWindows() 
print("\n\nBye bye\n") 

、エラー

OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829 

は、あなたが、私は私のコードで使用されるようにあなたがcv2.resizeを試すことができます

frame = imutils.resize(frame, width=640, height=480) 

と次元を台無しにすることを意味します。 cv2が既にそれを行うことができるときに別のライブラリを使う必要はありません。

+0

私のすべての問題を解決した 'imutils.resize()'の代わりに 'cv2.resize()'を使用して最後のヒントを表示します。私は今、コーデックのいずれかを使用してビデオを書き込むことができます。ありがとう! – kramer65

関連する問題