首页 文章

OpenCV写帧到文件python

提问于
浏览
1

嘿所以我开始玩OpenCV并且我无法将我的网络摄像头输出保存到文件中 . 这就是我所拥有的 . 运行正常,启动网络摄像头并创建“output.avi”问题是output.avi很小(414字节)和每次运行程序时相同的确切字节 . 我猜测问题是使用fourcc编码,但我还没能找到适用于我的情况 . 我在Mac OS X上运行 . 如果您需要更多信息,请告诉我 .

import numpy as np
import cv2

path = ('/full/path/Directory/output.avi')

cap = cv2.VideoCapture(0)
cap.set(1, 20.0) #Match fps
cap.set(3,640)   #Match width
cap.set(4,480)   #Match height

fourcc = cv2.cv.CV_FOURCC(*'XVID')
video_writer = cv2.VideoWriter(path,fourcc, 20.0, (640,480))

while(cap.isOpened()):
    #read the frame
    ret, frame = cap.read()
    if ret==True:
        #show the frame
        cv2.imshow('frame',frame)
        #Write the frame
        video_writer.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
video_writer.release()
cv2.destroyAllWindows()

3 回答

  • 4

    只需要改变

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

    fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')

    在这里找到答案:opencv VideoWriter under OSX producing no output

  • 1

    将代码组织到类中并分离清晰的函数,找到几个用于在API OpenCV中保存结果的函数,尝试其他格式并在多个OS上运行代码 .

    您也可以使用OpenCV转到C或Java / C#

    我想在计算机Vison书中有一章关于你的问题http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Dstripbooks&field-keywords=Cassandra%20NoSQL#/ref=nb_sb_noss_2?url=search-alias%3Dstripbooks&field-keywords=python+computer+vision+open+cv&rh=n%3A283155%2Ck%3Apython+computer+vision+open+cv

    这就是我能帮助你的全部

  • 1

    main problem 是你没有安全编码:

    path = ('/full/path/Directory/output.avi')
    
    cap = cv2.VideoCapture(0)
    if not cap:
        print "!!! Failed VideoCapture: invalid parameter!"
        sys.exit(1)
    
    cap.set(1, 20.0) #Match fps
    cap.set(3,640)   #Match width
    cap.set(4,480)   #Match height
    
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    
    video_writer = cv2.VideoWriter(path, fourcc, 20.0, (640,480))
    if not video_writer :
        print "!!! Failed VideoWriter: invalid parameters"
        sys.exit(1)
    
    # ...
    

    因此,当 VideoCapture()VideoWriter() 失败时,程序立即知道它无法继续 .

    另外,请注意 cv2.VideoWriter_fourcc() 如何替换旧版 cv2.cv.CV_FOURCC() 调用 . 我之所以这样做是因为this page显示了如何使用Python完成这些工作的最新示例 . 您也可以尝试使用all the FourCC codes,直到找到适合您系统的方式 .

    另一个需要注意的重要事项是,设置捕获界面的帧大小可能不起作用,因为相机可能不支持该分辨率 . 对于FPS也是如此 . Why is this a problem? 由于我们需要在 VideoWriter 构造函数中定义这些设置,因此发送到此对象 must 的所有帧都具有该精确维度,否则编写器将无法将帧写入该文件 .

    这就是你应该如何做到这一点:

    path = ('/full/path/Directory/output.avi')
    
    cap = cv2.VideoCapture(0)
    if not cap:
        print "!!! Failed VideoCapture: invalid parameter!"
        sys.exit(1)
    
    # The following might fail if the device doesn't support these values
    cap.set(1, 20.0) #Match fps
    cap.set(3,640)   #Match width
    cap.set(4,480)   #Match height
    
    # So it's always safer to retrieve it afterwards
    fps = cap.get(CV_CAP_PROP_FPS)
    w = cap.get(CV_CAP_PROP_FRAME_WIDTH);
    h = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    
    video_writer = cv2.VideoWriter(path, fourcc, fps, (w, h))
    if not video_writer :
        print "!!! Failed VideoWriter: invalid parameters"
        sys.exit(1)
    
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret == False:
             break
    
        cv2.imshow('frame',frame)         
        video_writer.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
             break
    
    cap.release()
    video_writer.release()
    

相关问题