gpt4 book ai didi

python - 使用 ffmpeg 从 python openCV 流式传输图像

转载 作者:行者123 更新时间:2023-12-04 23:05:38 30 4
gpt4 key购买 nike

为了尝试嵌入式 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

最佳答案

这是一个可重现的示例-希望您可以复制粘贴并执行,但没有任何 promise ...
该示例应用以下阶段:

  • ./test_dataset 中创建 10 个合成 JPEG 图像文件夹,用作输入。
  • 执行 FFplay 子进程作为 RTSP 监听器。
    使用 TCP 协议(protocol)时,我们应该首先启动 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

    关于python - 使用 ffmpeg 从 python openCV 流式传输图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68545688/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com