[英]Error During Saving a video using python and opencv
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当我在 python 中运行它时出现以下错误
> raceback (most recent call last): File
> "C:\Users\Prakash\Desktop\Image Proccessing\c.py", line 6, in <module>
> 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. Python / OpenCV 2.4.9 不支持版本 3.x 的 cv2.VideoWriter_fourcc。 If you're using 2.4.x:
如果您使用的是 2.4.x:
replace fourcc = cv2.VideoWriter_fourcc(*'XVID')
替换
fourcc = cv2.VideoWriter_fourcc(*'XVID')
with fourcc = cv2.cv.CV_FOURCC(*'XVID')
与
fourcc = cv2.cv.CV_FOURCC(*'XVID')
Good example here How to Record Video Using OpenCV and Python Reproduced for reference:这里的好例子如何使用 OpenCV 和 Python 录制视频转载以供参考:
#!/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()
Maybe the question is answered long ago.也许这个问题很久以前就得到了回答。
Here is a workaround solution if you met this problem in whatever version.如果您在任何版本中遇到此问题,这里都有一个解决方法。
Refer to "opencv doc": https//github.com/opencv/opencv/search?q=CV_FOURCC&unscoped_q=CV_FOURCC参考“opencv doc”: https : //github.com/opencv/opencv/search?q=CV_FOURCC&unscoped_q=CV_FOURCC
#define CV_FOURCC_MACRO(c1, c2, c3, c4) \
(((c1) & 255) + (((c2) & 255) << 8) + (((c3) & 255) << 16) + (((c4) & 255) << 24))
So simply define所以简单定义
def fourcc(a,b,c,d):
return return ((ord(a) & 255) + ((ord(b) & 255) << 8) + ((ord(c) & 255) << 16) + ((ord(d) & 255) << 24))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.