-
-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathDemoGraph.cs
104 lines (80 loc) · 3.03 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
using Mediapipe;
using System;
using System.Collections.Generic;
using UnityEngine;
public abstract class DemoGraph : MonoBehaviour, IDemoGraph<PixelData> {
[SerializeField] protected TextAsset config = null;
protected const string inputStream = "input_video";
protected CalculatorGraph graph;
protected GlCalculatorHelper gpuHelper;
void OnDestroy() {
Stop();
}
public 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(SidePacket sidePacket);
/// <summary>
/// Convert <paramref name="colors" /> to a packet and send it to the input stream.
/// </summary>
public Status PushInput(PixelData pixelData) {
int timestamp = System.Environment.TickCount & System.Int32.MaxValue;
var imageFrame = ImageFrame.FromPixels32(pixelData.Colors, pixelData.Width, pixelData.Height, true);
if (!IsGpuEnabled()) {
var packet = new ImageFramePacket(imageFrame, timestamp);
return graph.AddPacketToInputStream(inputStream, packet);
}
var status = gpuHelper.RunInGlContext(() => {
var texture = gpuHelper.CreateSourceTexture(imageFrame);
var gpuFrame = texture.GetGpuBufferFrame();
UnsafeNativeMethods.GlFlush();
texture.Release();
return graph.AddPacketToInputStream(inputStream, new GpuBufferPacket(gpuFrame, timestamp));
});
imageFrame.Dispose();
return status;
}
public abstract void RenderOutput(WebCamScreenController screenController, PixelData pixelData);
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.GetValue();
}
/// <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;
}
}