【问题标题】:In opencv is there a way to open an .avi that is being recorded and created real time?在 opencv 中,有没有办法打开实时记录和创建的 .avi?
【发布时间】:2020-05-02 11:07:52
【问题描述】:

我有一个摄像头录制应用程序,它接收摄像头图像并记录并将摄像头图像输出保存为 .avi 文件,一旦完成录制,我就可以播放和执行任何操作。我想创建一个实时的opencv代码,它可以获取这些实时创建的.avi文件,打开它,实时操作它做一些分类。无论如何,opencv 是否可以在编写这些 .avi 时打开它们?最好是python,还有C++实现?这将在 windows10 上完成。

编辑: 目前,当我尝试使用 CV2 作为输出 .avi 进行通用视频捕获时

cap = cv2.VideoCapture('out.avi')
ret, frame = cap.read()

while(True):
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

它给了我错误

Traceback (most recent call last):
  File "video_grab.py", line 15, in <module>
    cv2.imshow('frame', frame)
cv2.error: OpenCV(4.1.2) C:\projects\opencv- 
python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion 
failed) size.width>0 && size.height>0 in function 'cv::imshow'

断言最有可能失败的地方,因为在停止并保存捕获时视频具有 0 x 0 维度。

【问题讨论】:

    标签: windows opencv image-processing video avi


    【解决方案1】:

    以下解决方案可能有效或无效,具体取决于 AVI 文件的编解码器:

    • 验证ret 的值为True
    • waitKey 中的时间增加到大于 1 毫秒。
    import cv2
    
    cap = cv2.VideoCapture('out.avi')
    
    frame_period = 100  # 100msec - assume frame rate is about 10Hz
    
    while(True):
        ret, frame = cap.read()
    
        if ret:
            cv2.imshow('frame', frame)
    
        if cv2.waitKey(frame_period) & 0xFF == ord('q'):
            break
    

    我使用“Motion JPEG”编解码器对其进行测试,当以比实际帧速率更快的速度读取时,我收到一条警告消息,例如:[mjpeg @ 000002a22394b0e0] overread 8

    你也可以试试,快点开始阅读,当ret = False时降低速率:

    frame_period = 1  # Start reading fast (wait only 1msec)
    
    while(True):
        ret, frame = cap.read()
    
        if ret:
            cv2.imshow('frame', frame)
        else:
            frame_period = 100 # Reduce the rate to 10Hz when reaching end of file.  
    
        if cv2.waitKey(frame_period) & 0xFF == ord('q'):
            break
    

    如果您有一些迹象表明已捕获新帧,我认为它会更好地工作。
    我无法通过使用 OpenCV “查询” AVI 文件来找到解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2012-07-16
      • 1970-01-01
      • 2015-02-25
      • 2010-11-11
      相关资源
      最近更新 更多