Files
Toby 0388ceaa40 fix incorrect ImageFrame memory ownership handling (#29)
* fix incorrect ImageFrame memory ownership handling

* Update runtime package

* Fix PInvoke method name

* Clean ImageFrame code

* Update ImageFrameTest

* Update Mediapipe.Net package version

* Make osu!framework asynchronous

This is to compensate for the performance drop instated by this PR.
Other examples can stay as-is.
Fortunately Mediapipe handles asynchronous code perfectly well.

* Fix linting

Co-authored-by: Speykious <speykious@gmail.com>
2022-03-22 01:44:47 +08:00

82 lines
2.8 KiB
C#

// Copyright (c) homuler and The Vignette Authors
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
using Mediapipe.Net.Calculators;
using Mediapipe.Net.Framework.Format;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using SeeShark;
using SeeShark.Decode;
using SeeShark.Device;
using SixLabors.ImageSharp.PixelFormats;
using Image = SixLabors.ImageSharp.Image;
namespace Mediapipe.Net.Examples.OsuFrameworkVisualTests
{
public class MediapipeDrawable : CompositeDrawable
{
private Camera? camera;
private FrameConverter? converter;
private FaceMeshCpuCalculator? calculator;
private readonly Sprite sprite;
private Texture? texture;
public MediapipeDrawable()
{
Masking = true;
CornerRadius = 10;
AddInternal(sprite = new Sprite
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
FillMode = FillMode.Fit,
FillAspectRatio = 1,
});
}
#pragma warning disable IDE0051
[BackgroundDependencyLoader]
private void load(Camera camera, FrameConverter converter, FaceMeshCpuCalculator calculator)
{
this.camera = camera;
this.converter = converter;
this.calculator = calculator;
this.camera.OnFrame += onFrameEventHandler;
this.camera.StartCapture();
}
#pragma warning restore IDE0051
private unsafe void onFrameEventHandler(object? sender, FrameEventArgs e)
{
if (converter == null || calculator == null)
return;
if (e.Status != DecodeStatus.NewFrame)
return;
Frame frame = e.Frame;
Frame cFrame = converter.Convert(frame);
ImageFrame imgFrame = new ImageFrame(ImageFormat.Srgba,
cFrame.Width, cFrame.Height, cFrame.WidthStep, cFrame.RawData);
using ImageFrame? outImgFrame = calculator.Send(imgFrame);
if (outImgFrame == null)
return;
var span = new ReadOnlySpan<byte>(outImgFrame.MutablePixelData, outImgFrame.Height * outImgFrame.WidthStep);
var pixelData = Image.LoadPixelData<Rgba32>(span, cFrame.Width, cFrame.Height);
texture ??= new Texture(cFrame.Width, cFrame.Height);
texture.SetData(new TextureUpload(pixelData));
sprite.FillAspectRatio = (float)cFrame.Width / cFrame.Height;
sprite.Texture = texture;
}
}
}