将OpenCV帧发送到OBS Studio

0lvr5msh  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(204)

我想从我的python脚本发送OpenCV格式的处理帧到OBS Studio进行显示。我尝试使用here概述的Flask方法,并将生成的网页作为“浏览器”添加到OBS,但在我当前的Python代码中使用时,它存在错误且速度缓慢。
有没有办法直接将OpenCV帧流传输到OBS?例如,生成一个RTSP流,然后我可以在OBS中读取“VLC视频源”输入?
任何帮助将不胜感激。
框架代码:

import cv2

output_to_obs = True
output_to_cv2 = True

camera = cv2.VideoCapture(0)

def frame_to_obs(frame):
    # ==========================
    # don't know what to do here
    # ==========================
    pass

def process_frame(frame):
    # just an example
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    return frame_gray

while (camera.isOpened()):
    ret, frame = camera.read()
    if ret == True:
        frame = process_frame(frame)
        if output_to_obs:
            frame_to_obs(frame)
        if output_to_cv2:
            cv2.imshow('frame',frame)
            if cv2.waitKey(1) == 27:
                # escape was pressed
                break
    else:
        break
tjvv9vkg

tjvv9vkg1#

试试这个:

import cv2
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst

output_to_obs = True
output_to_cv2 = True

camera = cv2.VideoCapture(0)

def frame_to_obs(frame):
    # Create a GStreamer pipeline
    pipeline = Gst.Pipeline.new("pipeline")

    # Create a source element that will read the OpenCV frames
    source = Gst.ElementFactory.make("appsrc", "source")
    source.set_property("format", Gst.Format.TIME)
    source.set_property("caps", Gst.Caps.from_string("video/x-raw, format=BGR"))
    source.set_property("is-live", True)
    source.connect("need-data", on_need_data, frame)

    # Create a sink element that will send the frames to OBS
    sink = Gst.ElementFactory.make("udpsink", "sink")
    sink.set_property("host", "127.0.0.1")
    sink.set_property("port", 5000)

    # Add the elements to the pipeline
    pipeline.add(source)
    pipeline.add(sink)

    # Link the elements
    source.link(sink)

    # Start the pipeline
    pipeline.set_state(Gst.State.PLAYING)

def on_need_data(source, user_data):
    # This function is called when the pipeline needs more data
    # to send to OBS. It will add the current frame to the pipeline
    # and then return.
    frame = user_data
    ret, buffer = cv2.imencode('.jpg', frame)
    if ret:
        source.emit("push-buffer", Gst.Buffer.new_wrapped(buffer.tobytes()))

def process_frame(frame):
    # just an example
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    return frame_gray

while (camera.isOpened()):
    ret, frame = camera.read()
    if ret == True:
        frame = process_frame(frame)
        if output_to_obs:
            frame_to_obs(frame)
        if output_to_cv2:
            cv2.imshow('frame',frame)
            if cv2.waitKey(1) == 27:
                # escape was pressed
                break
    else:
        break

相关问题