Skip to content

Commit

Permalink
feat(sample)!: remove Start Scene (#1017)
Browse files Browse the repository at this point in the history
* remove console window button

* refactor: change sample scripts namespace

* refactor: use AppSettings to configure the sample app

* refactor!: ImageSource stops inheriting MonoBehaviour

* refactor: remove ImageSource components from scenes

* refactor: make Bootstrap a prefab

* remove Global Config button

* add AppSettings.asset

* remove Start Scene

* remove MemoizedLogger
  • Loading branch information
homuler committed Sep 1, 2023
1 parent 07970c1 commit 059b0b2
Show file tree
Hide file tree
Showing 89 changed files with 1,732 additions and 5,123 deletions.
3 changes: 1 addition & 2 deletions Assets/MediaPipeUnity/Editor/PackageExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public static void Export()

var pluginAssets = EnumerateAssets(Path.Combine("Packages", "com.github.homuler.mediapipe"))
.Append(Path.Combine("Packages", "com.github.homuler.mediapipe", "Runtime", "Plugins", "iOS", "MediaPipeUnity.framework"));
var sampleAssets = EnumerateAssets(Path.Combine("Assets", "MediaPipeUnity", "Samples"), new string[] { ".cs", ".unity" })
.Where(x => Path.GetFileName(x) != "Start Scene.unity"); // exclude the 'Start Scene'
var sampleAssets = EnumerateAssets(Path.Combine("Assets", "MediaPipeUnity", "Samples"), new string[] { ".cs", ".unity" });
var tutorialAssets = EnumerateAssets(Path.Combine("Assets", "MediaPipeUnity", "Tutorial")); // export all the files
var assets = pluginAssets.Concat(sampleAssets).Concat(tutorialAssets).ToArray();

Expand Down
87 changes: 87 additions & 0 deletions Assets/MediaPipeUnity/Samples/Common/Scripts/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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;
using UnityEngine;
using UnityEngine.Video;

namespace Mediapipe.Unity.Sample
{
[Serializable]
public class AppSettings : ScriptableObject
{
[Serializable]
public enum AssetLoaderType
{
StreamingAssets,
AssetBundle,
Local,
}

[SerializeField] private ImageSourceType _defaultImageSource;
[SerializeField] private InferenceMode _preferableInferenceMode;
[SerializeField] private AssetLoaderType _assetLoaderType;
[SerializeField] private Logger.LogLevel _logLevel = Logger.LogLevel.Debug;

[Header("Glog settings")]
[SerializeField] private int _glogMinloglevel = Glog.Minloglevel;
[SerializeField] private int _glogStderrthreshold = Glog.Stderrthreshold;
[SerializeField] private int _glogV = Glog.V;

[Header("WebCam Source")]
[Tooltip("For the default resolution, the one whose width is closest to this value will be chosen")]

[SerializeField] private int _preferredDefaultWebCamWidth = 1280;
[SerializeField] private ImageSource.ResolutionStruct[] _defaultAvailableWebCamResolutions = new ImageSource.ResolutionStruct[] {
new ImageSource.ResolutionStruct(176, 144, 30),
new ImageSource.ResolutionStruct(320, 240, 30),
new ImageSource.ResolutionStruct(424, 240, 30),
new ImageSource.ResolutionStruct(640, 360, 30),
new ImageSource.ResolutionStruct(640, 480, 30),
new ImageSource.ResolutionStruct(848, 480, 30),
new ImageSource.ResolutionStruct(960, 540, 30),
new ImageSource.ResolutionStruct(1280, 720, 30),
new ImageSource.ResolutionStruct(1600, 896, 30),
new ImageSource.ResolutionStruct(1920, 1080, 30),
};

[Header("Static Image Source")]
[SerializeField] private Texture[] _availableStaticImageSources;
[SerializeField] private ImageSource.ResolutionStruct[] _defaultAvailableStaticImageResolutions = new ImageSource.ResolutionStruct[] {
new ImageSource.ResolutionStruct(512, 512, 0),
new ImageSource.ResolutionStruct(640, 480, 0),
new ImageSource.ResolutionStruct(1280, 720, 0),
};

[Header("Video Source")]
[SerializeField] private VideoClip[] _availableVideoSources;

public ImageSourceType defaultImageSource => _defaultImageSource;
public InferenceMode preferableInferenceMode => _preferableInferenceMode;
public AssetLoaderType assetLoaderType => _assetLoaderType;
public Logger.LogLevel logLevel => _logLevel;

public void ResetGlogFlags()
{
Glog.Logtostderr = true;
Glog.Minloglevel = _glogMinloglevel;
Glog.Stderrthreshold = _glogStderrthreshold;
Glog.V = _glogV;
}

public WebCamSource BuildWebCamSource() => new WebCamSource(
_preferredDefaultWebCamWidth,
_defaultAvailableWebCamResolutions
);

public StaticImageSource BuildStaticImageSource() => new StaticImageSource(
_availableStaticImageSources,
_defaultAvailableStaticImageResolutions
);

public VideoSource BuildVideoSource() => new VideoSource(_availableVideoSources);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 27 additions & 73 deletions Assets/MediaPipeUnity/Samples/Common/Scripts/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,13 @@
// https://opensource.org/licenses/MIT.

using System.Collections;
using System.IO;
using UnityEngine;

namespace Mediapipe.Unity
namespace Mediapipe.Unity.Sample
{
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;
[SerializeField] private AppSettings _appSettings;

public InferenceMode inferenceMode { get; private set; }
public bool isFinished { get; private set; }
Expand All @@ -38,109 +24,78 @@ private void OnEnable()

private IEnumerator Init()
{
Logger.SetLogger(new MemoizedLogger(100));
Logger.MinLogLevel = Logger.LogLevel.Debug;
#if !DEBUG && !DEVELOPMENT_BUILD
Debug.LogWarning("Logging for the MediaPipeUnityPlugin will be suppressed. To enable logging, please check the 'Development Build' option and build.");
#endif

Protobuf.SetLogHandler(Protobuf.DefaultLogHandler);
Logger.MinLogLevel = _appSettings.logLevel;

Logger.LogInfo(_TAG, "Setting global flags...");
GlobalConfigManager.SetFlags();
Protobuf.SetLogHandler(Protobuf.DefaultLogHandler);

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;
}
Debug.Log("Setting global flags...");
_appSettings.ResetGlogFlags();
Glog.Initialize("MediaPipeUnityPlugin");
_isGlogInitialized = true;

Logger.LogInfo(_TAG, "Initializing AssetLoader...");
switch (_assetLoaderType)
Debug.Log("Initializing AssetLoader...");
switch (_appSettings.assetLoaderType)
{
case AssetLoaderType.AssetBundle:
case AppSettings.AssetLoaderType.AssetBundle:
{
AssetLoader.Provide(new AssetBundleResourceManager("mediapipe"));
break;
}
case AssetLoaderType.StreamingAssets:
case AppSettings.AssetLoaderType.StreamingAssets:
{
AssetLoader.Provide(new StreamingAssetsResourceManager());
break;
}
case AssetLoaderType.Local:
case AppSettings.AssetLoaderType.Local:
{
#if UNITY_EDITOR
AssetLoader.Provide(new LocalResourceManager());
break;
#else
Logger.LogError("LocalResourceManager is only supported on UnityEditor");
Debug.LogError("LocalResourceManager is only supported on UnityEditor");
yield break;
#endif
}
default:
{
Logger.LogError($"AssetLoaderType is unknown: {_assetLoaderType}");
Debug.LogError($"AssetLoaderType is unknown: {_appSettings.assetLoaderType}");
yield break;
}
}

DecideInferenceMode();
if (inferenceMode == InferenceMode.GPU)
{
Logger.LogInfo(_TAG, "Initializing GPU resources...");
Debug.Log("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");
Debug.LogWarning("If your native library is built for CPU, change 'Preferable Inference Mode' to CPU from the Inspector Window for AppSettings");
}
}

Logger.LogInfo(_TAG, "Preparing ImageSource...");
ImageSourceProvider.ImageSource = GetImageSource(_defaultImageSource);
Debug.Log("Preparing ImageSource...");
ImageSourceProvider.Initialize(
_appSettings.BuildWebCamSource(), _appSettings.BuildStaticImageSource(), _appSettings.BuildVideoSource());
ImageSourceProvider.Switch(_appSettings.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");
if (_appSettings.preferableInferenceMode == InferenceMode.GPU) {
Debug.LogWarning("Current platform does not support GPU inference mode, so falling back to CPU mode");
}
inferenceMode = InferenceMode.CPU;
#else
inferenceMode = _preferableInferenceMode;
inferenceMode = _appSettings.preferableInferenceMode;
#endif
}

Expand All @@ -154,7 +109,6 @@ private void OnApplicationQuit()
}

Protobuf.ResetLogHandler();
Logger.SetLogger(null);
}
}
}
Loading

0 comments on commit 059b0b2

Please sign in to comment.