-
-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathPoseTrackingSolution.cs
143 lines (114 loc) · 5.04 KB
/
PoseTrackingSolution.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace Mediapipe.Unity.PoseTracking {
public class PoseTrackingSolution : Solution {
[SerializeField] RawImage screen;
[SerializeField] DetectionAnnotationController poseDetectionAnnotationController;
[SerializeField] NormalizedLandmarkListAnnotationController poseLandmarksAnnotationController;
[SerializeField] PoseTrackingGraph graphRunner;
[SerializeField] TextureFramePool textureFramePool;
Coroutine coroutine;
public RunningMode runningMode;
public PoseTrackingGraph.ModelComplexity modelComplexity {
get { return graphRunner.modelComplexity; }
set { graphRunner.modelComplexity = value; }
}
public bool smoothLandmarks {
get { return graphRunner.smoothLandmarks; }
set { graphRunner.smoothLandmarks = value; }
}
public override void Play() {
base.Play();
graphRunner.Initialize();
if (coroutine != null) {
StopCoroutine(coroutine);
}
coroutine = StartCoroutine(Run());
}
public override void Pause() {
base.Pause();
ImageSourceProvider.imageSource.Pause();
}
public override void Resume() {
base.Resume();
StartCoroutine(ImageSourceProvider.imageSource.Resume());
}
public override void Stop() {
base.Stop();
StopCoroutine(coroutine);
ImageSourceProvider.imageSource.Stop();
graphRunner.Stop();
}
IEnumerator Run() {
var imageSource = ImageSourceProvider.imageSource;
yield return imageSource.Play();
if (!imageSource.isPrepared) {
Logger.LogError(TAG, "Failed to start ImageSource, exiting...");
yield break;
}
screen.rectTransform.sizeDelta = new Vector2(imageSource.textureWidth, imageSource.textureHeight);
screen.texture = imageSource.GetCurrentTexture();
var graphRunner = gameObject.GetComponent<PoseTrackingGraph>();
Logger.LogInfo(TAG, $"Model Complexity = {modelComplexity}");
Logger.LogInfo(TAG, $"Smooth Landmarks = {smoothLandmarks}");
Logger.LogInfo(TAG, $"Running Mode = {runningMode}");
if (runningMode == RunningMode.Async) {
graphRunner.OnPoseDetectionOutput.AddListener(OnPoseDetectionOutput);
graphRunner.OnPoseLandmarksOutput.AddListener(OnPoseLandmarksOutput);
graphRunner.OnPoseWorldLandmarksOutput.AddListener(OnPoseWorldLandmarksOutput);
graphRunner.StartRunAsync(imageSource).AssertOk();
} else {
graphRunner.StartRun(imageSource).AssertOk();
}
// Decide which TextureFormat to use
if (graphRunner.configType == GraphRunner.ConfigType.OpenGLES) {
// Use BGRA32 when the input packet is GpuBuffer
textureFramePool.ResizeTexture(imageSource.textureWidth, imageSource.textureHeight, TextureFormat.BGRA32);
} else {
// Use RGBA32 when the input packet is ImageFrame
textureFramePool.ResizeTexture(imageSource.textureWidth, imageSource.textureHeight, TextureFormat.RGBA32);
}
poseDetectionAnnotationController.isMirrored = imageSource.isMirrored;
poseLandmarksAnnotationController.isMirrored = imageSource.isMirrored;
while (true) {
yield return new WaitWhile(() => isPaused);
var textureFrameRequest = textureFramePool.WaitForNextTextureFrame();
yield return textureFrameRequest;
var textureFrame = textureFrameRequest.result;
var currentTexture = imageSource.GetCurrentTexture();
// Copy currentTexture to textureFrame's texture
if (graphRunner.configType == GraphRunner.ConfigType.OpenGLES) {
textureFrame.ReadTextureFromOnGPU(currentTexture);
} else {
var textureType = currentTexture.GetType();
if (textureType == typeof(WebCamTexture)) {
textureFrame.ReadTextureFromOnCPU((WebCamTexture)currentTexture);
} else if (textureType == typeof(Texture2D)) {
textureFrame.ReadTextureFromOnCPU((Texture2D)currentTexture);
} else {
textureFrame.ReadTextureFromOnCPU(currentTexture);
}
}
graphRunner.AddTextureFrameToInputStream(textureFrame).AssertOk();
if (runningMode == RunningMode.Sync) {
// When running synchronously, wait for the outputs here (blocks the main thread).
var value = graphRunner.FetchNextValue();
poseDetectionAnnotationController.Draw(value.poseDetection);
poseLandmarksAnnotationController.Draw(value.poseLandmarks);
// Logger.LogDebug($"Pose World Landmarks: {value.poseWorldLandmarks}");
}
yield return new WaitForEndOfFrame();
}
}
void OnPoseDetectionOutput(Detection poseDetection) {
poseDetectionAnnotationController.Draw(poseDetection);
}
void OnPoseLandmarksOutput(NormalizedLandmarkList poseLandmarks) {
poseLandmarksAnnotationController.Draw(poseLandmarks);
}
void OnPoseWorldLandmarksOutput(LandmarkList poseWorldLandmarks) {
// Logger.LogDebug($"Pose World Landmarks: {poseWorldLandmarks}");
}
}
}