首页 > 解决方案 > 在 Streamlink 直播流上使用 OpenCV-Python?

问题描述

我需要使用 Streamlink 对从 Twitch 提取的实时流运行 OpenCV-Python 图像识别,而无需将流写入磁盘。我已经对所有图像识别进行了测试并准备就绪(我使用 win32api 屏幕截图进行了测试),并且我还使用 Streamlink 提供的 cli 命令成功地提取了流,但我需要能够分析流一帧一次在 Python 脚本中使用 OpenCV。

我的问题是:我将如何使用 OpenCV 分析 Streamlink 流的每一帧?

标签: python-3.xopencvcomputer-visionhttp-live-streamingstreamlink

解决方案


我认为这段代码给了你一个想法。您只需要通过streamlink获取流并通过openCV捕获

def stream_to_url(url, quality='best'):
    streams = streamlink.streams(url)
    if streams:
        return streams[quality].to_url()
    else:
        raise ValueError("No steams were available")

主要的

def main(url, quality='best', fps=30.0):
    stream_url = stream_to_url(url, quality)
    cap = cv2.VideoCapture(stream_url)

    frame_time = int((1.0 / fps) * 1000.0)

    while True:
        try:
            ret, frame = cap.read()

https://github.com/streamlink/streamlink/blob/6a30ed7524eff2cdb205e024294b187cb660e4e3/examples/opencv-face.py#L40


推荐阅读