-
-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathOfficialDemoCPU.cs
57 lines (43 loc) · 1.61 KB
/
OfficialDemoCPU.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
using Mediapipe;
using System.Runtime.InteropServices;
using UnityEngine;
public class OfficialDemoCPU : DemoGraph {
private const string outputStream = "output_video";
private readonly object outputImageLock = new object();
private ImageFrame outputImage;
private GCHandle outputVideoCallbackHandle;
private SidePacket sidePacket;
public override Status StartRun() {
Debug.Log("This graph is for testing official examples. You can customize the graph by editing `official_demo_cpu.txt` (default is `hand_tracking_desktop.pbtxt`)");
graph.ObserveOutputStream<ImageFramePacket, ImageFrame>(outputStream, OutputVideoCallback, out outputVideoCallbackHandle).AssertOk();
sidePacket = new SidePacket();
sidePacket.Emplace("num_hands", new IntPacket(2));
return graph.StartRun(sidePacket);
}
protected override void OnDestroy() {
base.OnDestroy();
if (outputVideoCallbackHandle.IsAllocated) {
outputVideoCallbackHandle.Free();
}
}
public override void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame) {
lock (outputImageLock) {
if (outputImage == null) { return; }
screenController.DrawScreen(outputImage);
outputImage.Dispose();
outputImage = null;
}
}
private Status OutputVideoCallback(ImageFramePacket packet) {
var statusOrImageFrame = packet.Consume();
if (statusOrImageFrame.ok) {
lock (outputImageLock) {
if (outputImage != null) {
outputImage.Dispose();
}
outputImage = statusOrImageFrame.ConsumeValueOrDie();
}
}
return Status.Ok();
}
}