python opencv 录制视频_使用python和opencv保存视频时出错
This is the code to save video from the web camimport numpyimport cv2cap = cv2.VideoCapture(0)# Define the codec and create VideoWriter objectfourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWri

This is the code to save video from the web cam
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()
When I Run It In python it gives the following 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'
Please help me solve this error
解决方案
Python / OpenCV 2.4.9 doesn't support cv2.VideoWriter_fourcc, which is version 3.x. If you're using 2.4.x:
replace
fourcc = cv2.VideoWriter_fourcc(*'XVID')
with
fourcc = cv2.cv.CV_FOURCC(*'XVID')
#!/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()
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)