【问题标题】:Stream images from python openCV with ffmpeg使用 ffmpeg 从 python openCV 流式传输图像
【发布时间】:2021-10-03 07:50:17
【问题描述】:

为了尝试嵌入式 AI,我想通过 rtsp 流传输图像数据集。 我试图做的是每 X 秒读取一张图像并将其发送到流并推断我的 AI。我尝试使用这个 github 存储库:https://gist.github.com/takidog/2c981c34d5d5b41c0d712f8ef4ac60d3#file-main-py

这是我迄今为止尝试过的:

import cv2
import time

import subprocess as sp 

import glob, os

__PATH = "./DATASET"


os.chdir(__PATH)
IMG_LIST = glob.glob("*.jpg")
IMG_LIST_LEN = len(IMG_LIST)
IMG_INDEX = 0
IMG_DELAY = 2

IMG_WIDTH = 1280
IMG_HEIGHT = 720

IMG_SIZE = str(IMG_WIDTH)+"x"+str(IMG_HEIGHT)
FPS = 5

RTSP_SERVER = "rtsp://localhost:31415/stream"

COMMAND = ['ffmpeg',
           '-re',
            '-s', IMG_SIZE,
            '-r', str(FPS),
            '-i', '-',
            '-bufsize', '64M',
            '-maxrate', "4M",
            '-rtsp_transport', 'tcp',
            '-muxdelay','0.1',
            RTSP_SERVER]

process = sp.Popen(COMMAND,stdin=sp.PIPE)

while(True):

    CURRENT_IMG = cv2.imread(IMG_LIST[IMG_INDEX])
    IMG_INDEX = (IMG_INDEX+1)%IMG_LIST_LEN
    while(CURRENT_IMG.shape[0]!=720): #We dump images with a bad format
        CURRENT_IMG = cv2.imread(IMG_LIST[IMG_INDEX])
        IMG_INDEX = (IMG_INDEX+1)%IMG_LIST_LEN

        
    _,FRAME = cv2.imencode('.png', CURRENT_IMG)

    process.stdin.write(FRAME.tobytes())

    time.sleep(1/FPS)

令人惊讶的是这不起作用并给了我这个错误:

Input #0, png_pipe, from 'pipe:':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgb24(pc), 1280x720, 25 fps, 25 tbr, 25 tbn, 25 tbc
[NULL @ 0x55ba3fe1b860] Unable to find a suitable output format for 'rtsp://localhost:31415/stream'
rtsp://localhost:31415/stream: Invalid argument
Traceback (most recent call last):
  File "main.py", line 47, in <module>
    process.stdin.write(FRAME.tobytes())
BrokenPipeError: [Errno 32] Broken pipe

【问题讨论】:

  • 添加 '-f', 'rtsp', 参数并重试。
  • 嗨,很抱歉回答迟了:当我添加这个参数时,它给了我:[tcp @ 0x5591ee1734c0] Connection to tcp://localhost:31415?timeout=0 failed: Connection refused Could not write header for output file #0 (incorrect codec parameters ?): Connection refused Error initializing output stream 0:0 -- Conversion failed! Traceback (most recent call last): File "main.py", line 47, in &lt;module&gt; process.stdin.write(FRAME.tobytes()) BrokenPipeError: [Errno 32] Broken pipe
  • 我建议你先试试UDP。 TCP 协议要求 TCP 服务器(接收大小)在流式传输开始之前启动。这可能是错误的原因(我不确定)。
  • 我尝试了-rtsp_transport udp,但错误仍然完全相同
  • 我发布了一个可重现的代码示例。如果它仍然无法正常工作,请尝试更新 FFmpeg 的版本。我的回答试图解决 FFmpeg 流媒体问题(不解决您的 AI 推理案例)。你可能可以用cap = cv2.VideoCapture替换FFplay(监听器),但我没有测试。

标签: python python-3.x opencv ffmpeg opencv-python


【解决方案1】:

这是一个可重现的示例 - 希望您可以复制粘贴并执行,但没有任何承诺...

该示例应用以下阶段:

  • ./test_dataset 文件夹中创建 10 张合成 JPEG 图像,用作输入。
  • 作为 RTSP 监听器执行 FFplay 子进程。
    使用 TCP 协议时,我们应该首先启动 TCP 服务器(FFplay 被用作 TCP 服务器,以防万一)。
    我们还需要接收进程,因为没有它,FFmpeg 流媒体进程会在第一帧后停止。
  • 为 RTSP 流执行 FFmpeg 子进程。
    循环读取 JPEG 图像到 NumPy 数组(BGR 颜色格式),并将数组作为原始视频帧写入标准输入管道。
    注意:写入原始视频帧比将每个帧编码为 PNG(如您的参考示例所使用的那样)更有效。

代码如下:

import cv2
#import time
import subprocess as sp 
import glob
import os

img_width = 1280
img_height = 720


test_path = './test_dataset'  # Folder with synthetic sample images.

os.makedirs(test_path, exist_ok=True)  # Create folder for input images.

os.chdir(test_path)

ffmpeg_cmd = 'ffmpeg'  # May use full path like: 'c:\\FFmpeg\\bin\\ffmpeg.exe'
ffplay_cmd = 'ffplay'  # May use full path like: 'c:\\FFmpeg\\bin\\ffplay.exe'


# Create 10 synthetic JPEG images for testing (image0001.jpg, image0002.jpg, ..., image0010.jpg).
sp.run([ffmpeg_cmd, '-y', '-f', 'lavfi', '-i', f'testsrc=size={img_width}x{img_height}:rate=1:duration=10', 'image%04d.jpg'])


img_list = glob.glob("*.jpg")
img_list_len = len(img_list)
img_index = 0

fps = 5

rtsp_server = 'rtsp://localhost:31415/live.stream'

# You will need to start the server up first, before the sending client (when using TCP). See: https://trac.ffmpeg.org/wiki/StreamingGuide#Pointtopointstreaming
ffplay_process = sp.Popen([ffplay_cmd, '-rtsp_flags', 'listen', rtsp_server])  # Use FFplay sub-process for receiving the RTSP video.


command = [ffmpeg_cmd,
           '-re',
           '-f', 'rawvideo',  # Apply raw video as input - it's more efficient than encoding each frame to PNG
           '-s', f'{img_width}x{img_height}',
           '-pixel_format', 'bgr24',
           '-r', f'{fps}',
           '-i', '-',
           '-pix_fmt', 'yuv420p',
           '-c:v', 'libx264',
           '-bufsize', '64M',
           '-maxrate', '4M',
           '-rtsp_transport', 'tcp',
           '-f', 'rtsp',
           #'-muxdelay', '0.1',
           rtsp_server]

process = sp.Popen(command, stdin=sp.PIPE)  # Execute FFmpeg sub-process for RTSP streaming


while True:
    current_img = cv2.imread(img_list[img_index])  # Read a JPEG image to NumPy array (in BGR color format) - assume the resolution is correct.
    img_index = (img_index+1) % img_list_len  # Cyclically repeat images

    process.stdin.write(current_img.tobytes())  # Write raw frame to stdin pipe.

    cv2.imshow('current_img', current_img)  # Show image for testing

    # time.sleep(1/FPS)
    key = cv2.waitKey(int(round(1000/fps)))  # We need to call cv2.waitKey after cv2.imshow

    if key == 27:  # Press Esc for exit
        break


process.stdin.close()  # Close stdin pipe
process.wait()  # Wait for FFmpeg sub-process to finish
ffplay_process.kill()  # Forcefully close FFplay sub-process
cv2.destroyAllWindows()  # Close OpenCV window

【讨论】:

    猜你喜欢
    • 2022-06-23
    • 2020-10-27
    • 2011-11-26
    • 1970-01-01
    • 2016-03-14
    • 2016-10-23
    • 2018-08-07
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多