python opencv 录制视频_使用python和opencv保存视频时出错

该博客解决了在使用Python OpenCV保存视频时遇到的`cv2.VideoWriter_fourcc`错误。问题在于所使用的OpenCV版本不支持此方法。解决方案是使用`cv2.cv.CV_FOURCC`代替,适用于OpenCV 2.4.x版本。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值