-
-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathDemoGraph.cs
123 lines (98 loc) · 3.95 KB
/
DemoGraph.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
using Mediapipe;
using System;
using System.Collections.Generic;
using UnityEngine;
public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {
[SerializeField] protected TextAsset config = null;
protected const string inputStream = "input_video";
protected CalculatorGraph graph;
protected GlCalculatorHelper gpuHelper;
protected virtual void OnDestroy() {
Stop();
}
public virtual void Initialize() {
if (config == null) {
throw new InvalidOperationException("config is missing");
}
graph = new CalculatorGraph(config.text);
}
public void Initialize(GpuResources gpuResources, GlCalculatorHelper gpuHelper) {
this.Initialize();
graph.SetGpuResources(gpuResources).AssertOk();
this.gpuHelper = gpuHelper;
}
public abstract Status StartRun();
public virtual Status StartRun(Texture texture) {
return StartRun();
}
/// <summary>
/// Convert <paramref name="colors" /> to a packet and send it to the input stream.
/// </summary>
public Status PushInput(TextureFrame textureFrame) {
var timestamp = new Timestamp(System.Environment.TickCount & System.Int32.MaxValue);
ImageFrame imageFrame = null;
if (!IsGpuEnabled()) {
imageFrame = new ImageFrame(
ImageFormat.Format.SRGBA, textureFrame.width, textureFrame.height, 4 * textureFrame.width, textureFrame.GetRawNativeByteArray());
textureFrame.Release();
var packet = new ImageFramePacket(imageFrame, timestamp);
return graph.AddPacketToInputStream(inputStream, packet);
}
#if UNITY_ANDROID
var glTextureName = textureFrame.GetNativeTexturePtr();
return gpuHelper.RunInGlContext(() => {
var glContext = GlContext.GetCurrent();
var glTextureBuffer = new GlTextureBuffer((UInt32)glTextureName, textureFrame.width, textureFrame.height,
textureFrame.gpuBufferformat, textureFrame.OnRelease, glContext);
var gpuBuffer = new GpuBuffer(glTextureBuffer);
return graph.AddPacketToInputStream(inputStream, new GpuBufferPacket(gpuBuffer, timestamp));
});
#else
imageFrame = new ImageFrame(
ImageFormat.Format.SRGBA, textureFrame.width, textureFrame.height, 4 * textureFrame.width, textureFrame.GetRawNativeByteArray());
textureFrame.Release();
return gpuHelper.RunInGlContext(() => {
var texture = gpuHelper.CreateSourceTexture(imageFrame);
var gpuBuffer = texture.GetGpuBufferFrame();
Gl.Flush();
texture.Release();
return graph.AddPacketToInputStream(inputStream, new GpuBufferPacket(gpuBuffer, timestamp));
});
#endif
}
public abstract void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame);
public void Stop() {
if (graph != null) {
graph.CloseInputStream(inputStream).AssertOk();
graph.WaitUntilDone().AssertOk();
}
}
/// <summary>
/// Fetch next value from <paramref name="poller" />.
/// Note that this method blocks the thread till the next value is fetched.
/// If the next value is empty, this method never returns.
/// </summary>
public T FetchNext<T>(OutputStreamPoller<T> poller, Packet<T> packet, string streamName = null, T failedValue = default(T)) {
if (!poller.Next(packet)) { // blocks
if (streamName != null) {
Debug.LogWarning($"Failed to fetch next packet from {streamName}");
}
return failedValue;
}
return packet.Get();
}
/// <summary>
/// Fetch next vector value from <paramref name="poller" />.
/// </summary>
/// <returns>
/// Fetched vector or an empty List when failed.
/// </returns>
/// <seealso cref="FetchNext" />
public List<T> FetchNextVector<T>(OutputStreamPoller<List<T>> poller, Packet<List<T>> packet, string streamName = null) {
var nextValue = FetchNext<List<T>>(poller, packet, streamName);
return nextValue == null ? new List<T>() : nextValue;
}
protected bool IsGpuEnabled() {
return gpuHelper != null;
}
}