generated from vignetteapp/dotnet-template
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Program.cs
102 lines (88 loc) · 3.59 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 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
{
[SupportedOSPlatform("Linux")]
public static class Program
{
private static Camera? camera;
private static FrameConverter? converter;
private static FaceMeshGpuCalculator? calculator;
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");
// Get a camera device
using (CameraManager manager = new CameraManager())
{
try
{
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)
{
Console.Error.WriteLine($"No camera exists at index {parsed.CameraIndex}.");
return;
}
}
calculator = new FaceMeshGpuCalculator();
calculator.OnResult += handleLandmarks;
calculator.Run();
Console.CancelKeyPress += (sender, eventArgs) => exit();
while (true)
{
var frame = camera.GetFrame();
converter ??= new FrameConverter(frame, PixelFormat.Rgba);
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);
}
}
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
private static void exit()
{
Console.WriteLine("Exiting...");
camera?.Dispose();
converter?.Dispose();
calculator?.Dispose();
}
}
}