tensorflow - 从gstreamer缓冲区创建输入张量

问题描述 投票:2回答:1

我目前正在编写一个拦截帧的Gstreamer1.0插件,使用tensorflow执行某些任务,在帧上写入数据并将其注入。

我在C / C ++中这样做,当数据必须在Gstreamer和Tensorflow之间流动时,我正面临一个问题。

我在GstBuffer对象中有一个框架,我必须从中提取数据并构造输入Tensor。格式始终相同,UINT8 RGB矩阵[宽度,高度,3]

/* extract raw data from gstreamer buffer */
gpointer bytes;
gst_buffer_extract_dup(outbuf, 0, size, &bytes, &copied);

使用字节指针我现在必须构造:

Tensor input(tensorflow::DT_UINT8, tensorflow::TensorShape(cwidth, cheight, 3));

我不知道我应该怎么做。 我找不到任何有关如何使用gpointer和tensorflow的信息或示例我只能真正找到使用文件作为源的示例,这根本不是我的情况。

任何线索或洞察力将不胜感激。

c++ tensorflow gstreamer gstreamer-1.0
1个回答
2
投票

我找到了一种方法但它只在你的缓冲区是RGB 3通道帧缓冲​​区时才有效。

Tensor ConvertRGBBufferToInputTensor(gpointer buffer, gint cwidth, gint cheight, gint channel) {

    Tensor input_tensor(tensorflow::DT_UINT8, tensorflow::TensorShape({1, cwidth, cheight, channel}));
    auto input_tensor_mapped = input_tensor.tensor<uint8_t, 4>();

    uint8_t * uintdata = (uint8_t*)buffer;

    for (int y = 0; y < cheight; ++y) {
        const uint8_t* source_row = uintdata + (y * cwidth * channel);
        for (int x = 0; x < cwidth; ++x) {
            const uint8_t* source_pixel = source_row + (x * channel);
            for (int c = 0; c < channel; ++c) {
                const uint8_t* source_value = source_pixel + c;
                input_tensor_mapped(0, y, x, c) = *source_value;
            }
        }
    }

    return input_tensor;
}

当你有一个GstBuffer时,从gstreamer使用它

/* extract raw data from the buffer and convert it to an input tensor */
gst_buffer_extract_dup(outbuf, 0, size, &bytes, &copied);
GST_INFO("Extracted %" G_GSIZE_FORMAT " from buffer" , copied);
Tensor input_tensor = ConvertRGBBufferToInputTensor(bytes, cwidth, cheight);
g_free(bytes);
© www.soinside.com 2019 - 2024. All rights reserved.