2

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

我在 C/C++ 中执行此操作,当数据必须在 Gstreamer 和 Tensorflow 之间流动时,我目前面临一个问题。

我在 GstBuffer 对象中有一个框架,我必须从中提取数据并构造一个输入张量。格式始终相同,一个 UINT8 RGB 矩阵 [width,height,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 的信息或示例,我只能真正找到使用文件作为源的示例,这根本不是我的情况。

任何线索或见解将不胜感激。

4

2 回答 2

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);
于 2017-12-18T12:56:35.653 回答
0

它已经为 gstreamer 应用程序开发人员实现和部署:https ://github.com/nnsuite/nnstreamer

您可以编写 GStreamer 管道,例如,

camsrc ! videoconvert ! videorate ! videoscale ! video/x-raw, whatever-format-you-need ! tensor_converter ! tensor_filter framework=tensorflow model=your-own-model-file.pb ! ....

如果你需要的是转置

camsrc ! videoconvert ! videorate ! videoscale ! video/x-raw, whatever-format-you-need ! tensor_converter ! tensor_transform mode=transpose option=1:2:0:3 ! tensor_filter framework=tensorflow model=your-own-model-file.pb ! ....

如果您想将结果导入您的应用程序,您可以添加 app_sink 或 tensor_sink 并在那里获取结果张量。

于 2019-12-13T03:56:53.507 回答