使用python和opencv保存视频时出错 [英] Error During Saving a video using python and opencv

查看:398
本文介绍了使用python和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()

当我在python中运行它时,会出现以下错误

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 <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:

Python / OpenCV 2.4.9 doesn't support cv2.VideoWriter_fourcc, which is version 3.x. If you're using 2.4.x:

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

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

此处的好例子如何使用OpenCV和Python录制视频 转载供参考:

Good example here How to Record Video Using OpenCV and Python Reproduced for reference:

#!/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和opencv保存视频时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆