mirror of
https://github.com/cosyneco/MediaPipe.NET.git
synced 2025-08-24 08:40:39 +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
@ -5,11 +5,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CommandLine;
|
||||
using FFmpeg.AutoGen;
|
||||
using Mediapipe.Net.Calculators;
|
||||
using Mediapipe.Net.External;
|
||||
using Mediapipe.Net.Framework.Format;
|
||||
using Mediapipe.Net.Framework.Protobuf;
|
||||
using SeeShark;
|
||||
using SeeShark.Device;
|
||||
using SeeShark.FFmpeg;
|
||||
|
||||
namespace Mediapipe.Net.Examples.FaceMesh
|
||||
@ -24,6 +26,15 @@ namespace Mediapipe.Net.Examples.FaceMesh
|
||||
{
|
||||
// Get and parse command line arguments
|
||||
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");
|
||||
|
||||
FFmpegManager.SetupFFmpeg("/usr/lib");
|
||||
Glog.Initialize("stuff");
|
||||
|
||||
@ -32,7 +43,17 @@ namespace Mediapipe.Net.Examples.FaceMesh
|
||||
{
|
||||
try
|
||||
{
|
||||
camera = manager.GetCamera(parsed.CameraIndex);
|
||||
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,
|
||||
});
|
||||
Console.WriteLine($"Using camera {camera.Info}");
|
||||
}
|
||||
catch (Exception)
|
||||
@ -41,15 +62,25 @@ namespace Mediapipe.Net.Examples.FaceMesh
|
||||
return;
|
||||
}
|
||||
}
|
||||
camera.OnFrame += onFrame;
|
||||
|
||||
calculator = new FaceMeshCpuCalculator();
|
||||
calculator.OnResult += handleLandmarks;
|
||||
calculator.Run();
|
||||
camera.StartCapture();
|
||||
|
||||
Console.CancelKeyPress += (sender, eventArgs) => exit();
|
||||
Console.ReadLine();
|
||||
while (true)
|
||||
{
|
||||
var frame = camera.GetFrame();
|
||||
|
||||
converter ??= new FrameConverter(frame, PixelFormat.Rgba);
|
||||
|
||||
Frame cFrame = converter.Convert(frame);
|
||||
|
||||
using ImageFrame imgframe = new ImageFrame(ImageFormat.Srgba,
|
||||
cFrame.Width, cFrame.Height, cFrame.WidthStep, cFrame.RawData);
|
||||
|
||||
using ImageFrame img = calculator.Send(imgframe);
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleLandmarks(object? sender, List<NormalizedLandmarkList> landmarks)
|
||||
@ -57,27 +88,6 @@ namespace Mediapipe.Net.Examples.FaceMesh
|
||||
Console.WriteLine($"Got a list of {landmarks[0].Landmark.Count} landmarks at frame {calculator?.CurrentFrame}");
|
||||
}
|
||||
|
||||
private static unsafe void onFrame(object? sender, FrameEventArgs e)
|
||||
{
|
||||
if (calculator == null)
|
||||
return;
|
||||
|
||||
var frame = e.Frame;
|
||||
converter ??= new FrameConverter(frame, PixelFormat.Rgba);
|
||||
|
||||
// Don't use a frame if it's not new
|
||||
if (e.Status != DecodeStatus.NewFrame)
|
||||
return;
|
||||
|
||||
Frame cFrame = converter.Convert(frame);
|
||||
|
||||
ImageFrame imgframe = new ImageFrame(ImageFormat.Srgba,
|
||||
cFrame.Width, cFrame.Height, cFrame.WidthStep, cFrame.RawData);
|
||||
|
||||
using ImageFrame img = calculator.Send(imgframe);
|
||||
imgframe.Dispose();
|
||||
}
|
||||
|
||||
// Dispose everything on exit
|
||||
private static void exit()
|
||||
{
|
||||
|
Reference in New Issue
Block a user