import cv2
import numpy as np
cap = cv2.VideoCapture('1.MP4')
fourcc = cv2.FOURCC(*'XVID')
out = cv2.videoWriter('output.avi',fourcc,20.0,(640,480))
while (1):
#Capture frame-by-frame
#ret = cap.set(3,960),cap.set(4,720)
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
如上所示,这是opencv文档中的演示。虽然它可以在我的PC上运行。 像这样的追溯:
Traceback (most recent call last):
File "E:\opencv\Pic\test.py", line 34, in <module>
fourcc = cv2.cv.FOURCC(*'XVID')
AttributeError: module 'cv2.cv2' has no attribute 'cv'
我还看到一些解决方案,例如将cv2.cv更改为cv2,而下面的追溯:
Traceback (most recent call last):
File "E:\opencv\Pic\test.py", line 34, in <module>
fourcc = cv2.FOURCC(*'XVID')
AttributeError: module 'cv2.cv2' has no attribute 'FOURCC'
我非常感谢任何温暖的解决方案。
答案 0 :(得分:0)
链接@Spence wetjen帖子适用于旧的OpenCV,在OpenCV 3.x中无法使用。
这是我的代码:
环境:Python3 + OpenCV 3.3
#!/usr/bin/python3
import cv2
## opening videocapture
cap = cv2.VideoCapture(0)
## some videowriter props
sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = 20
#fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
#fourcc = cv2.VideoWriter_fourcc('m', 'p', 'e', 'g')
fourcc = cv2.VideoWriter_fourcc(*'mpeg')
## open and set props
vout = cv2.VideoWriter()
vout.open('output.mp4',fourcc,fps,sz,True)
cnt = 0
while cnt<20:
cnt += 1
print(cnt)
_, frame = cap.read()
cv2.putText(frame, str(cnt), (10, 20), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,0), 1, cv2.LINE_AA)
vout.write(frame)
vout.release()
cap.release()
结果:
答案 1 :(得分:0)
也导入contrib库。
pip install opencv-contrib-python
或
pip3 install opencv-contrib-python