-
-
Notifications
You must be signed in to change notification settings - Fork 447
/
MediaPipeVideoGraph.cs
128 lines (106 loc) · 3.98 KB
/
MediaPipeVideoGraph.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Mediapipe.Unity.MediaPipeVideo
{
public class MediaPipeVideoGraph : GraphRunner
{
public int maxNumHands = 2;
public event EventHandler<OutputEventArgs<ImageFrame>> OnOutput
{
add => _outputVideoStream.AddListener(value);
remove => _outputVideoStream.RemoveListener(value);
}
private const string _InputStreamName = "input_video";
private GpuBufferPacket _outputGpuBufferPacket;
private string _destinationBufferName;
private TextureFrame _destinationTexture;
private const string _OutputVideoStreamName = "output_video";
private OutputStream<ImageFramePacket, ImageFrame> _outputVideoStream;
public override void StartRun(ImageSource imageSource)
{
if (configType != ConfigType.OpenGLES)
{
_outputVideoStream.StartPolling().AssertOk();
}
StartRun(BuildSidePacket(imageSource));
}
public override void Stop()
{
_outputVideoStream.RemoveAllListeners();
_outputVideoStream = null;
base.Stop();
}
public override IEnumerator Initialize(RunningMode runningMode)
{
if (runningMode == RunningMode.Async)
{
throw new ArgumentException("Asynchronous mode is not supported");
}
return base.Initialize(runningMode);
}
public void SetupOutputPacket(TextureFrame textureFrame)
{
if (configType != ConfigType.OpenGLES)
{
throw new InvalidOperationException("This method is only supported for OpenGL ES");
}
_destinationTexture = textureFrame;
_outputGpuBufferPacket = new GpuBufferPacket(_destinationTexture.BuildGpuBuffer(GpuManager.GlCalculatorHelper.GetGlContext()));
}
public void AddTextureFrameToInputStream(TextureFrame textureFrame)
{
AddTextureFrameToInputStream(_InputStreamName, textureFrame);
}
public bool TryGetNext(out ImageFrame outputVideo, bool allowBlock = true)
{
return TryGetNext(_outputVideoStream, out outputVideo, allowBlock, GetCurrentTimestampMicrosec());
}
protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (configType == ConfigType.OpenGLES)
{
var sinkNode = config.Node.Last((node) => node.Calculator == "GlScalerCalculator");
_destinationBufferName = Tool.GetUnusedSidePacketName(config, "destination_buffer");
sinkNode.InputSidePacket.Add($"DESTINATION:{_destinationBufferName}");
}
if (runningMode == RunningMode.NonBlockingSync)
{
_outputVideoStream = new OutputStream<ImageFramePacket, ImageFrame>(
calculatorGraph, _OutputVideoStreamName, config.AddPacketPresenceCalculator(_OutputVideoStreamName), timeoutMicrosec);
}
else
{
_outputVideoStream = new OutputStream<ImageFramePacket, ImageFrame>(calculatorGraph, _OutputVideoStreamName, true, timeoutMicrosec);
}
return calculatorGraph.Initialize(config);
}
protected override IList<WaitForResult> RequestDependentAssets()
{
return new List<WaitForResult> {
WaitForAsset("hand_landmark_full.bytes"),
WaitForAsset("hand_recrop.bytes"),
WaitForAsset("handedness.txt"),
WaitForAsset("palm_detection_full.bytes"),
};
}
private SidePacket BuildSidePacket(ImageSource imageSource)
{
var sidePacket = new SidePacket();
SetImageTransformationOptions(sidePacket, imageSource, true);
sidePacket.Emplace("output_rotation", new IntPacket((int)imageSource.rotation));
sidePacket.Emplace("num_hands", new IntPacket(maxNumHands));
if (configType == ConfigType.OpenGLES)
{
sidePacket.Emplace(_destinationBufferName, _outputGpuBufferPacket);
}
return sidePacket;
}
}
}