【发布时间】: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 <module> 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