gpt4 book ai didi

python - 使用 python 和 opencv 保存视频时出错

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:42 26 4
gpt4 key购买 nike

这是从网络摄像头保存视频的代码

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()

当我在 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'

请帮我解决这个错误

最佳答案

Python/OpenCV 2.4.9 不支持 cv2.VideoWriter_fourcc,即版本 3.x。如果您使用的是 2.4.x:

替换fourcc = cv2.VideoWriter_fourcc(*'XVID')

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

这里是个好例子 How to Record Video Using OpenCV and 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()

关于python - 使用 python 和 opencv 保存视频时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40138724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com