Error al guardar un video usando python y opencv

Este es el código para guardar el video de la cámara web.

import numpy import cv2 cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) while(cap.isOpened()): ret, frame = cap.read() if ret==True: frame = cv2.flip(frame,0) # write the flipped frame out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows() 

Cuando lo ejecuto en python da el siguiente error

 > raceback (most recent call last): File > "C:\Users\Prakash\Desktop\Image Proccessing\c.py", line 6, in  > fourcc = cv2.VideoWriter_fourcc(*'XVID') AttributeError: 'module' > object has no attribute 'VideoWriter_fourcc' 

Por favor ayúdame a resolver este error.

Python / OpenCV 2.4.9 no admite cv2.VideoWriter_fourcc, que es la versión 3.x. Si está utilizando 2.4.x:

Reemplace fourcc = cv2.VideoWriter_fourcc(*'XVID')

con fourcc = cv2.cv.CV_FOURCC(*'XVID')

Buen ejemplo aquí Cómo grabar un video usando OpenCV y Python Reproducido para referencia:

 #!/usr/bin/env python import cv2 if __name__ == "__main__": # find the webcam capture = cv2.VideoCapture(0) # video recorder fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist videoOut = cv2.VideoWriter("output.avi", fourcc, 20.0, (640, 480)) # record video while (capture.isOpened()): ret, frame = capture.read() if ret: videoOut.write(frame) cv2.imshow('Video Stream', frame) else: break # Tiny Pause key = cv2.waitKey(1) capture.release() videoOut.release() cv2.destroyAllWindows()