如何将VideoFrame或ImageFeatureValue调整为特定大小以匹配输入形状要求?

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

我的目标是使用tinyYolov3模型通过HoloLens实时执行对象检测。我想将模型作为ONNX文件直接包含在项目中,并在HoloLens自身内部计算预测。为此,我计划将Windows.media和Windows.AI.MachineLearning库用作相机和我的预测之间的管道。

跟随此tutorial,我可以将帧捕获为VideoFrame,并且可以在ImageFeatureValue中将其转换为符合我的输入类型要求的帧。我现在的问题是关于形状的要求。 Yolo模型需要3x416x416帧作为输入,我在网上找不到有关调整VideoFrame或ImageFeatureValue大小的任何文档。

非常感谢您的帮助。

using (var frameReference = CameraFrameReader.TryAcquireLatestFrame())
using (var videoFrame = frameReference?.VideoMediaFrame?.GetVideoFrame())
await ModelHelper.EvaluateVideoFrameAsync(videoFrame).ConfigureAwait(false);

public async Task EvaluateVideoFrameAsync(VideoFrame frame)
{
    if (frame != null)
    {
        try
        {
            ModelInput inputData = new ModelInput();
            inputData.image = ImageFeatureValue.CreateFromVideoFrame(frame);
            //TODO: CHANGE SIZE FRAME
            var output = await Model.EvaluateAsync(inputData).ConfigureAwait(false);
        }
    }
}
c# hololens onnx windows-machine-learning
1个回答
0
投票

我没有使用Windows Machine Learning API和ImageFeatureValue类的经验。但是,当我尝试从HoloLens调整帧大小时,我不得不使用SoftwareBitmap而不是VideoFrame。然后,我使用BitmapEncoder调整它们的大小,然后转换回VideoFrame:

    private async Task<SoftwareBitmap> ResizeBitmap(SoftwareBitmap softwareBitmap, uint width, uint height)
{
    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);

        encoder.SetSoftwareBitmap(softwareBitmap);

        encoder.BitmapTransform.ScaledWidth = width;
        encoder.BitmapTransform.ScaledHeight = height;
        encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.NearestNeighbor;

        await encoder.FlushAsync();

        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

        return await decoder.GetSoftwareBitmapAsync(softwareBitmap.BitmapPixelFormat, softwareBitmap.BitmapAlphaMode);
    }
}

var inputBitmap = frameReference.VideoMediaFrame.SoftwareBitmap;
var outputBitmap = ResizeBitmap(inputBitmap, your_width, your_height);

var outputVideoFrame = VideoFrame.CreateWithSoftwareBitmap(SoftwareBitmap);
© www.soinside.com 2019 - 2024. All rights reserved.