【问题标题】:Repeating a video on loop using OpenCV and Python使用 OpenCV 和 Python 循环播放视频
【发布时间】:2020-10-19 04:59:13
【问题描述】:

我正在尝试创建一个简单的程序,该程序将使用 OpenCV 重复播放视频,直到按下 waitKey。视频将播放一次,然后给出错误消息“(-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'”。我早些时候收到此消息,但通过更改文件位置修复了它。我几乎可以肯定这个问题来自这样一个事实,即当视频结束时下一帧不存在,因此无法读取,因为当帧为 None 时中断 while 循环不会产生错误。我尝试过的每个解决方案都失败了。有什么帮助吗?

import cv2
import numpy as np 

cap = cv2.VideoCapture('video_file_location')


while True:

     _, frame = cap.read()

     cv2.imshow('frame',frame)
     if cv2.waitKey(1) == 27:
         break



cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python python-3.x opencv video


    【解决方案1】:

    尝试将cv2.VideoCapture() 放入循环中,并且框架应该只在返回为True 时显示,这是您代码中的问题

    import cv2
    import numpy as np 
    
    
    
    
    while True:
        #This is to check whether to break the first loop
        isclosed=0
        cap = cv2.VideoCapture('videoplayback.mp4')
        while (True):
    
            ret, frame = cap.read()
            # It should only show the frame when the ret is true
            if ret == True:
    
                cv2.imshow('frame',frame)
                if cv2.waitKey(1) == 27:
                    # When esc is pressed isclosed is 1
                    isclosed=1
                    break
            else:
                break
        # To break the loop if it is closed manually
        if isclosed:
            break
    
    
    
    cap.release()
    cv2.destroyAllWindows()
    

    【讨论】:

      猜你喜欢
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2016-10-14
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多