Skip to content

Commit

Permalink
Fix critical memory leak (#22)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
adryzz and Speykious committed Feb 21, 2022
1 parent d0bbbe4 commit cc7ed1e
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
<PackageReference Include="Mediapipe.Net.Runtime.CPU" Version="0.8.9" />
<PackageReference Include="SeeShark" Version="2.2.0" />
<PackageReference Include="SeeShark" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Mediapipe.Net.Examples.FaceMesh/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ public class Options
{
[Option('c', "camera", Default = 0, HelpText = "The index of the camera to use")]
public int CameraIndex { get; set; }

[Option('f', "input-format", Default = null, HelpText = "The format of the camera input")]
public string? InputFormat { get; set; }

[Option('r', "fps", Default = null, HelpText = "The framerate of the camera input")]
public int? Framerate { get; set; }

[Option('w', "width", Default = null, HelpText = "The width of the camera input")]
public int? Width { get; set; }

[Option('h', "height", Default = null, HelpText = "The height of the camera input")]
public int? Height { get; set; }
}
}
60 changes: 35 additions & 25 deletions Mediapipe.Net.Examples.FaceMesh/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +26,15 @@ public static void Main(string[] args)
{
// 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");

Expand All @@ -32,7 +43,17 @@ public static void Main(string[] args)
{
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)
Expand All @@ -41,41 +62,30 @@ public static void Main(string[] args)
return;
}
}
camera.OnFrame += onFrame;

calculator = new FaceMeshCpuCalculator();
calculator.OnResult += handleLandmarks;
calculator.Run();
camera.StartCapture();

Console.CancelKeyPress += (sender, eventArgs) => exit();
Console.ReadLine();
}

private static void handleLandmarks(object? sender, List<NormalizedLandmarkList> landmarks)
{
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);
while (true)
{
var frame = camera.GetFrame();

// Don't use a frame if it's not new
if (e.Status != DecodeStatus.NewFrame)
return;
converter ??= new FrameConverter(frame, PixelFormat.Rgba);

Frame cFrame = converter.Convert(frame);
Frame cFrame = converter.Convert(frame);

ImageFrame imgframe = new ImageFrame(ImageFormat.Srgba,
using ImageFrame imgframe = new ImageFrame(ImageFormat.Srgba,
cFrame.Width, cFrame.Height, cFrame.WidthStep, cFrame.RawData);

using ImageFrame img = calculator.Send(imgframe);
imgframe.Dispose();
using ImageFrame img = calculator.Send(imgframe);
}
}

private static void handleLandmarks(object? sender, List<NormalizedLandmarkList> landmarks)
{
Console.WriteLine($"Got a list of {landmarks[0].Landmark.Count} landmarks at frame {calculator?.CurrentFrame}");
}

// Dispose everything on exit
Expand Down
1 change: 1 addition & 0 deletions Mediapipe.Net.Examples.FaceMesh/mediapipe
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
<PackageReference Include="Mediapipe.Net.Runtime.GPU" Version="0.8.9" />
<PackageReference Include="SeeShark" Version="2.2.0" />
<PackageReference Include="SeeShark" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Mediapipe.Net.Examples.FaceMeshGpu/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ public class Options
{
[Option('c', "camera", Default = 0, HelpText = "The index of the camera to use")]
public int CameraIndex { get; set; }

[Option('f', "input-format", Default = null, HelpText = "The format of the camera input")]
public string? InputFormat { get; set; }

[Option('r', "fps", Default = null, HelpText = "The framerate of the camera input")]
public int? Framerate { get; set; }

[Option('w', "width", Default = null, HelpText = "The width of the camera input")]
public int? Width { get; set; }

[Option('h', "height", Default = null, HelpText = "The height of the camera input")]
public int? Height { get; set; }
}
}
61 changes: 36 additions & 25 deletions Mediapipe.Net.Examples.FaceMeshGpu/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
using System.Collections.Generic;
using System.Runtime.Versioning;
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.FaceMeshGpu
Expand All @@ -26,6 +28,15 @@ public static void Main(string[] args)
{
// 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");

Expand All @@ -34,7 +45,17 @@ public static void Main(string[] args)
{
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)
Expand All @@ -43,41 +64,31 @@ public static void Main(string[] args)
return;
}
}
camera.OnFrame += onFrame;

calculator = new FaceMeshGpuCalculator();
calculator.OnResult += handleLandmarks;
calculator.Run();
camera.StartCapture();

Console.CancelKeyPress += (sender, eventArgs) => exit();
Console.ReadLine();
}

private static void handleLandmarks(object? sender, List<NormalizedLandmarkList> landmarks)
{
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);
while (true)
{
var frame = camera.GetFrame();

// Don't use a frame if it's not new
if (e.Status != DecodeStatus.NewFrame)
return;
converter ??= new FrameConverter(frame, PixelFormat.Rgba);

Frame cFrame = converter.Convert(frame);
Frame cFrame = converter.Convert(frame);

ImageFrame imgframe = new ImageFrame(ImageFormat.Srgba,
ImageFrame imgframe = new ImageFrame(ImageFormat.Srgba,
cFrame.Width, cFrame.Height, cFrame.WidthStep, cFrame.RawData);

using ImageFrame img = calculator.Send(imgframe);
imgframe.Dispose();
using ImageFrame img = calculator.Send(imgframe);
imgframe.Dispose();
}
}

private static void handleLandmarks(object? sender, List<NormalizedLandmarkList> landmarks)
{
Console.WriteLine($"Got a list of {landmarks[0].Landmark.Count} landmarks at frame {calculator?.CurrentFrame}");
}

// Dispose everything on exit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
</PropertyGroup>

<ItemGroup Label="Package References">
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
<PackageReference Include="Mediapipe.Net.Framework.Protobuf" Version="0.8.9" />
<PackageReference Include="Mediapipe.Net.Runtime.CPU" Version="0.8.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="ppy.osu.Framework" Version="2022.118.0" />
<PackageReference Include="SeeShark" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="ppy.osu.Framework" Version="2022.217.0" />
<PackageReference Include="SeeShark" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<ItemGroup Label="Project References">
<ProjectReference Include="..\Mediapipe.Net\Mediapipe.Net.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit cc7ed1e

Please sign in to comment.