python+opencv读取视频,调用摄像头碰到了一个代码错误,如下:
python文件:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20, (640, 480))
while(1):
# get a frame
ret, frame = cap.read()
# save a frame
out.write(frame)
# show a frame
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Traceback (most recent call last):
File "video_demo.py", line 31, in <module>
fourcc = cv2.cv.FOURCC(*'XVID')
AttributeError: 'module' object has no attribute 'cv'
这是因为我使用的是opencv 3而运行的代码是opencv2 版本的,所以代码换成opencv 3 版本就行了:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
参考:https://blog.csdn.net/w5688414/article/details/78366174