-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathImageSourceSolution.cs
131 lines (106 loc) · 3.3 KB
/
ImageSourceSolution.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
// 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.Collections;
using UnityEngine;
namespace Mediapipe.Unity
{
public abstract class ImageSourceSolution<T> : Solution where T : GraphRunner
{
[SerializeField] protected Screen screen;
[SerializeField] protected T graphRunner;
[SerializeField] protected TextureFramePool textureFramePool;
private Coroutine _coroutine;
public RunningMode runningMode;
public long timeoutMillisec
{
get => graphRunner.timeoutMillisec;
set => graphRunner.timeoutMillisec = value;
}
public override void Play()
{
if (_coroutine != null)
{
Stop();
}
base.Play();
_coroutine = StartCoroutine(Run());
}
public override void Pause()
{
base.Pause();
ImageSourceProvider.ImageSource.Pause();
}
public override void Resume()
{
base.Resume();
var _ = StartCoroutine(ImageSourceProvider.ImageSource.Resume());
}
public override void Stop()
{
base.Stop();
StopCoroutine(_coroutine);
ImageSourceProvider.ImageSource.Stop();
graphRunner.Stop();
}
private IEnumerator Run()
{
var graphInitRequest = graphRunner.WaitForInit(runningMode);
var imageSource = ImageSourceProvider.ImageSource;
yield return imageSource.Play();
if (!imageSource.isPrepared)
{
Logger.LogError(TAG, "Failed to start ImageSource, exiting...");
yield break;
}
// Use RGBA32 as the input format.
// TODO: When using GpuBuffer, MediaPipe assumes that the input format is BGRA, so the following code must be fixed.
textureFramePool.ResizeTexture(imageSource.textureWidth, imageSource.textureHeight, TextureFormat.RGBA32);
SetupScreen(imageSource);
yield return graphInitRequest;
if (graphInitRequest.isError)
{
Logger.LogError(TAG, graphInitRequest.error);
yield break;
}
OnStartRun();
graphRunner.StartRun(imageSource);
var waitWhilePausing = new WaitWhile(() => isPaused);
while (true)
{
if (isPaused)
{
yield return waitWhilePausing;
}
if (!textureFramePool.TryGetTextureFrame(out var textureFrame))
{
yield return new WaitForEndOfFrame();
continue;
}
// Copy current image to TextureFrame
ReadFromImageSource(imageSource, textureFrame);
AddTextureFrameToInputStream(textureFrame);
yield return new WaitForEndOfFrame();
if (runningMode.IsSynchronous())
{
RenderCurrentFrame(textureFrame);
yield return WaitForNextValue();
}
}
}
protected virtual void SetupScreen(ImageSource imageSource)
{
// NOTE: The screen will be resized later, keeping the aspect ratio.
screen.Initialize(imageSource);
}
protected virtual void RenderCurrentFrame(TextureFrame textureFrame)
{
screen.ReadSync(textureFrame);
}
protected abstract void OnStartRun();
protected abstract void AddTextureFrameToInputStream(TextureFrame textureFrame);
protected abstract IEnumerator WaitForNextValue();
}
}