【问题标题】:Set other video on video end (cv2)在视频端设置其他视频(cv2)
【发布时间】:2022-10-04 19:22:11
【问题描述】:

我正在尝试在 cv2 的视频端设置其他视频。

我希望它设置其他视频(我提供的),但实际结果是没有的。窗口刚刚关闭并在命令行中显示错误。

错误是:

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

我试图通过重新定义 cap 变量来做到这一点。 这是代码:

import numpy as np
import cv2 as cv
cap = cv.VideoCapture('video.mp4')
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        cap = cv.VideoCapture('video2.mp4')
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('q'):
        break
cap.release()
cv.destroyAllWindows()

【问题讨论】:

    标签: python opencv video


    【解决方案1】:

    为 video2.mp4 设置 cap 后,您需要读取 video2 的第一帧。
    框架对象和之前的cap.read()还是一样的(无效的)
    您还应该在打开新的捕获对象之前释放旧的捕获对象。
    试试下面的代码:

    import numpy as np
    import cv2 as cv
    cap = cv.VideoCapture('video.mp4')
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            cap.release() # release old Capture object
            cap = cv.VideoCapture('video2.mp4')
            ret, frame = cap.read() # read first frame of new video
        cv.imshow('frame', frame)
        if cv.waitKey(1) == ord('q'):
            break
    cap.release()
    cv.destroyAllWindows()
    

    【讨论】:

    • 谢谢你!!代码有效!
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多