mirror of
https://github.com/cosyneco/MediaPipe.NET.git
synced 2025-08-06 15:08:53 +08:00
Fix critical memory leak (#22)
* fix examples (or at least try to) * stuff * Better dispose that * Add video input options because I'm selfish * THIS ONE MOTHERFUCKING FOR LOOP * Remove unnecessary using * Fix osu framework example * Use stable `Google.Protobuf` package * Add options to osu framework example * Use same options on all examples * Add framerate option * Use framerate option Co-authored-by: Speykious <speykious@gmail.com>
This commit is contained in:

committed by
GitHub

parent
d0bbbe4895
commit
cc7ed1ebd0
@ -2,17 +2,26 @@
|
||||
// This file is part of MediaPipe.NET.
|
||||
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
|
||||
|
||||
using System;
|
||||
using CommandLine;
|
||||
using FFmpeg.AutoGen;
|
||||
using Mediapipe.Net.Calculators;
|
||||
using osu.Framework;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osuTK;
|
||||
using SeeShark;
|
||||
using SeeShark.Device;
|
||||
|
||||
namespace Mediapipe.Net.Examples.OsuFrameworkVisualTests
|
||||
{
|
||||
public class OsuFrameworkVisualTestsGameBase : Game
|
||||
{
|
||||
private MediapipeDrawable? mediapipeDrawable;
|
||||
private Camera? camera;
|
||||
private FrameConverter? converter;
|
||||
private FaceMeshCpuCalculator? calculator;
|
||||
|
||||
protected override Container<Drawable> Content { get; }
|
||||
|
||||
private DependencyContainer? dependencies;
|
||||
@ -31,20 +40,47 @@ namespace Mediapipe.Net.Examples.OsuFrameworkVisualTests
|
||||
[BackgroundDependencyLoader]
|
||||
private void load()
|
||||
{
|
||||
mediapipeDrawable = new MediapipeDrawable
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Size = new Vector2(1280, 720),
|
||||
FillMode = FillMode.Fit
|
||||
};
|
||||
dependencies?.Cache(mediapipeDrawable);
|
||||
string[] args = Environment.GetCommandLineArgs();
|
||||
Options parsed = Parser.Default.ParseArguments<Options>(args).Value;
|
||||
|
||||
(int, int)? videoSize = null;
|
||||
if (parsed.Width != null && parsed.Height != null)
|
||||
videoSize = ((int)parsed.Width, (int)parsed.Height);
|
||||
else if (parsed.Width != null && parsed.Height == null)
|
||||
Console.Error.WriteLine("Specifying width requires to specify height");
|
||||
else if (parsed.Width == null && parsed.Height != null)
|
||||
Console.Error.WriteLine("Specifying height requires to specify width");
|
||||
|
||||
var manager = new CameraManager();
|
||||
camera = manager.GetDevice(parsed.CameraIndex,
|
||||
new VideoInputOptions
|
||||
{
|
||||
InputFormat = parsed.InputFormat,
|
||||
Framerate = parsed.Framerate == null ? null : new AVRational
|
||||
{
|
||||
num = (int)parsed.Framerate,
|
||||
den = 1,
|
||||
},
|
||||
VideoSize = videoSize,
|
||||
});
|
||||
dependencies?.Cache(camera);
|
||||
manager.Dispose();
|
||||
|
||||
var dummyFrame = camera.GetFrame();
|
||||
converter = new FrameConverter(dummyFrame, PixelFormat.Rgba);
|
||||
dependencies?.Cache(converter);
|
||||
|
||||
calculator = new FaceMeshCpuCalculator();
|
||||
calculator.Run();
|
||||
dependencies?.Cache(calculator);
|
||||
}
|
||||
#pragma warning restore IDE0051
|
||||
|
||||
protected override bool OnExiting()
|
||||
{
|
||||
mediapipeDrawable?.Dispose();
|
||||
calculator?.Dispose();
|
||||
converter?.Dispose();
|
||||
camera?.Dispose();
|
||||
return base.OnExiting();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user