forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBootstrap.cs
More file actions
160 lines (140 loc) · 4.23 KB
/
Copy pathBootstrap.cs
File metadata and controls
160 lines (140 loc) · 4.23 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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 System.IO;
using UnityEngine;
namespace Mediapipe.Unity
{
public class Bootstrap : MonoBehaviour
{
[System.Serializable]
public enum AssetLoaderType
{
StreamingAssets,
AssetBundle,
Local,
}
private const string _TAG = nameof(Bootstrap);
[SerializeField] private ImageSourceType _defaultImageSource;
[SerializeField] private InferenceMode _preferableInferenceMode;
[SerializeField] private AssetLoaderType _assetLoaderType;
[SerializeField] private bool _enableGlog = true;
public InferenceMode inferenceMode { get; private set; }
public bool isFinished { get; private set; }
private bool _isGlogInitialized;
private void OnEnable()
{
var _ = StartCoroutine(Init());
}
private IEnumerator Init()
{
Logger.SetLogger(new MemoizedLogger(100));
Logger.MinLogLevel = Logger.LogLevel.Debug;
Protobuf.SetLogHandler(Protobuf.DefaultLogHandler);
Logger.LogInfo(_TAG, "Setting global flags...");
GlobalConfigManager.SetFlags();
if (_enableGlog)
{
if (Glog.LogDir != null)
{
if (!Directory.Exists(Glog.LogDir))
{
Directory.CreateDirectory(Glog.LogDir);
}
Logger.LogVerbose(_TAG, $"Glog will output files under {Glog.LogDir}");
}
Glog.Initialize("MediaPipeUnityPlugin");
_isGlogInitialized = true;
}
Logger.LogInfo(_TAG, "Initializing AssetLoader...");
switch (_assetLoaderType)
{
case AssetLoaderType.AssetBundle:
{
AssetLoader.Provide(new AssetBundleResourceManager("mediapipe"));
break;
}
case AssetLoaderType.StreamingAssets:
{
AssetLoader.Provide(new StreamingAssetsResourceManager());
break;
}
case AssetLoaderType.Local:
{
#if UNITY_EDITOR
AssetLoader.Provide(new LocalResourceManager());
break;
#else
Logger.LogError("LocalResourceManager is only supported on UnityEditor");
yield break;
#endif
}
default:
{
Logger.LogError($"AssetLoaderType is unknown: {_assetLoaderType}");
yield break;
}
}
DecideInferenceMode();
if (inferenceMode == InferenceMode.GPU)
{
Logger.LogInfo(_TAG, "Initializing GPU resources...");
yield return GpuManager.Initialize();
if (!GpuManager.IsInitialized)
{
Logger.LogWarning("If your native library is built for CPU, change 'Preferable Inference Mode' to CPU from the Inspector Window for Bootstrap");
}
}
Logger.LogInfo(_TAG, "Preparing ImageSource...");
ImageSourceProvider.ImageSource = GetImageSource(_defaultImageSource);
isFinished = true;
}
public ImageSource GetImageSource(ImageSourceType imageSourceType)
{
switch (imageSourceType)
{
case ImageSourceType.WebCamera:
{
return GetComponent<WebCamSource>();
}
case ImageSourceType.Image:
{
return GetComponent<StaticImageSource>();
}
case ImageSourceType.Video:
{
return GetComponent<VideoSource>();
}
case ImageSourceType.Unknown:
default:
{
throw new System.ArgumentException($"Unsupported source type: {imageSourceType}");
}
}
}
private void DecideInferenceMode()
{
#if UNITY_EDITOR_OSX || UNITY_EDITOR_WIN
if (_preferableInferenceMode == InferenceMode.GPU) {
Logger.LogWarning(_TAG, "Current platform does not support GPU inference mode, so falling back to CPU mode");
}
inferenceMode = InferenceMode.CPU;
#else
inferenceMode = _preferableInferenceMode;
#endif
}
private void OnApplicationQuit()
{
GpuManager.Shutdown();
if (_isGlogInitialized)
{
Glog.Shutdown();
}
Protobuf.ResetLogHandler();
Logger.SetLogger(null);
}
}
}