使用 cv2 从 gstreamer 中提取和显示 mp4 视频的帧

问题描述 投票:0回答:0

如标题所说,我有一个 mp4 视频,我为其设置了一个 gstreamer 管道,我正在尝试在 opencv 中打开和显示。

此代码仅用于显示没有 cv2 的视频

from threading import Thread
import time 
import gi
import cv2 
import numpy as np

gi.require_version("Gst", "1.0")
gi.require_version("GstApp", "1.0")

from gi.repository import Gst, GLib, GstApp

_ = GstApp


Gst.init()

main_loop = GLib.MainLoop()
thread = Thread(target=main_loop.run)
thread.start()


pipeline = Gst.parse_launch("filesrc location = video.mp4 ! decodebin ! videoconvert ! autovideosink")
appsink = pipeline.get_by_name("sink")

pipeline.set_state(Gst.State.PLAYING)

try: 
    while True:
        time.sleep(.1)
except KeyboardInterrupt:
    pass


pipeline.set_state(Gst.State.NULL)
main_loop.quit()
thread.join()
exit()

这是我尝试使用 cv2 显示帧的代码

import cv2

cap = cv2.VideoCapture("filesrc location = video.mp4 ! decodebin ! videoconvert ! video/x-raw,format=BGR ! appsink", cv2.CAP_GSTREAMER)

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

cap.release()

但是,当我运行它时,我得到了错误

'cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'imshow'

过载解析失败:

  • imshow() 缺少必需的参数“mat”(位置 2)
  • imshow() 缺少必需的参数“mat”(位置 2)
  • imshow() 缺少必需的参数“mat”(位置 2) '

如果我运行它而不是

while cap.isOpened():

代替

while True:

程序运行后立即停止

opencv video gstreamer mp4 cv2
© www.soinside.com 2019 - 2024. All rights reserved.