2017-04-17 5 views
2

파이썬에서 내 웹캠을 읽고 창에 표시하는 스크립트가 있습니다.OpenCV로 파이썬에서 웹캠 비디오를 저장하는 방법

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라는 파일을 생성하지 않습니다,하지만 파일 (0 바이트) 빈 나는 다음과 같은 오류가 표시되는 명령 줄에 :

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)이므로 왜 일치하지 않을지 잘 모르겠습니다. 는 4 자리 FOURCC 코드가를 지정하는 데 사용되는 것을 말한다 튜토리얼에서는

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. 

: 다시 스크립트를 실행하면

는 (이 경우 이미 빈 output.avi에서)는 이러한 오류를 보여줍니다 비디오 코덱은 플랫폼에 따라 다르며 사용 가능한 코드 목록은 fourcc.org에서 찾을 수 있습니다. 나는 OSX에서 여러 코덱 코드 (DIVX, XVID, MJPG, X264, WMV1, WMV2)를 시도했다. 그러나 불행히도 그들 중 누구도 나를 위해 일하지 않습니다.

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 

사람이 여기 잘못 될 수 있는지 알고 있나요 : 그들은 모두 나에게 다음과 같은 오류를 제공하는 MJPG을 제외하고, 같은 오류를 준다? 모든 팁을 환영합니다!

답변

5

아마도 AVFoundation을 사용하여 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