【问题标题】:Error During Saving a video using python and opencv使用 python 和 opencv 保存视频时出错
【发布时间】:2015-11-10 13:46:26
【问题描述】:

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

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'

请帮我解决这个错误

【问题讨论】:

  • 谢谢!!!!此链接帮助使用的是 open cv 2.4.9 它不支持fourcc = cv2.VideoWriter_fourcc('XVID') 功能,因此将其替换为fourcc = cv2.cv.CV_FOURCC('XVID') 并且它可以工作很好
  • 你们中的一个人应该将其作为答案发布,并且 OP 应该接受它。

标签: python opencv image-processing video-capture


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    也许这个问题很久以前就已经回答了。

    如果您在任何版本中遇到此问题,这里有一个解决方法。

    参考“opencv 文档”: https//github.com/opencv/opencv/search?q=CV_FOURCC&unscoped_q=CV_FOURCC

    #define CV_FOURCC_MACRO(c1, c2, c3, c4) \
    (((c1) & 255) + (((c2) & 255) << 8) + (((c3) & 255) << 16) + (((c4) & 255) << 24))
    

    所以简单定义

    def fourcc(a,b,c,d):
        return return ((ord(a) & 255) + ((ord(b) & 255) << 8) + ((ord(c) & 255) << 16) + ((ord(d) & 255) << 24))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多