Skip to content

Commit

Permalink
refactor(sample): more kind logging in the sample app (#1018)
Browse files Browse the repository at this point in the history
* call Debug.Log in the sample app

* make Bootstrap error message more suggestive

* update README
  • Loading branch information
homuler authored Sep 2, 2023
1 parent 059b0b2 commit 806ea46
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 62 deletions.
15 changes: 5 additions & 10 deletions Assets/MediaPipeUnity/Samples/Common/Scripts/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
// https://opensource.org/licenses/MIT.

using System.Collections;
using UnityEngine;

namespace Mediapipe.Unity
namespace Mediapipe.Unity.Sample
{
public static class AssetLoader
{
private static ResourceManager _ResourceManager;

public static void Provide(ResourceManager manager)
{
_ResourceManager = manager;
}
public static void Provide(ResourceManager manager) => _ResourceManager = manager;

public static IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = false)
{
if (_ResourceManager == null)
{
#if UNITY_EDITOR
Logger.LogWarning("ResourceManager is not provided, so default LocalResourceManager will be used");
Debug.LogWarning("ResourceManager is not provided, so default LocalResourceManager will be used");
_ResourceManager = new LocalResourceManager();
#else
throw new System.InvalidOperationException("ResourceManager is not provided");
Expand All @@ -31,9 +29,6 @@ public static IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool
return _ResourceManager.PrepareAssetAsync(name, uniqueKey, overwrite);
}

public static IEnumerator PrepareAssetAsync(string name, bool overwrite = false)
{
return PrepareAssetAsync(name, name, overwrite);
}
public static IEnumerator PrepareAssetAsync(string name, bool overwrite = false) => PrepareAssetAsync(name, name, overwrite);
}
}
4 changes: 3 additions & 1 deletion Assets/MediaPipeUnity/Samples/Common/Scripts/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private void OnEnable()

private IEnumerator Init()
{
Debug.Log("The configuration for the sample app can be modified using AppSettings.asset.");
#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
Expand Down Expand Up @@ -56,7 +57,8 @@ private IEnumerator Init()
AssetLoader.Provide(new LocalResourceManager());
break;
#else
Debug.LogError("LocalResourceManager is only supported on UnityEditor");
Debug.LogError("LocalResourceManager is only supported on UnityEditor." +
"To avoid this error, consider switching to the StreamingAssetsResourceManager and copying the required resources under StreamingAssets, for example.");
yield break;
#endif
}
Expand Down
16 changes: 8 additions & 8 deletions Assets/MediaPipeUnity/Samples/Common/Scripts/GraphRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

using Stopwatch = System.Diagnostics.Stopwatch;

namespace Mediapipe.Unity
namespace Mediapipe.Unity.Sample
{
public abstract class GraphRunner : MonoBehaviour
{
Expand Down Expand Up @@ -113,14 +113,14 @@ public virtual IEnumerator Initialize(RunningMode runningMode)
{
this.runningMode = runningMode;

Logger.LogInfo(TAG, $"Config Type = {configType}");
Logger.LogInfo(TAG, $"Running Mode = {runningMode}");
Debug.Log($"Config Type = {configType}");
Debug.Log($"Running Mode = {runningMode}");

InitializeCalculatorGraph();
_stopwatch = new Stopwatch();
_stopwatch.Start();

Logger.LogInfo(TAG, "Loading dependent assets...");
Debug.Log("Loading dependent assets...");
var assetRequests = RequestDependentAssets();
yield return new WaitWhile(() => assetRequests.Any((request) => request.keepWaiting));

Expand All @@ -129,7 +129,7 @@ public virtual IEnumerator Initialize(RunningMode runningMode)
{
foreach (var error in errors)
{
Logger.LogError(TAG, error);
Debug.LogError(error);
}
throw new InternalException("Failed to prepare dependent assets");
}
Expand All @@ -155,7 +155,7 @@ public virtual void Stop()
}
catch (BadStatusException exception)
{
Logger.LogError(TAG, exception.Message);
Debug.LogError(exception);
}

try
Expand All @@ -164,7 +164,7 @@ public virtual void Stop()
}
catch (BadStatusException exception)
{
Logger.LogError(TAG, exception.Message);
Debug.LogError(exception);
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ protected void SetImageTransformationOptions(PacketMap sidePacket, ImageSource i
inputVerticallyFlipped = !inputVerticallyFlipped;
}

Logger.LogDebug($"input_rotation = {inputRotation}, input_horizontally_flipped = {inputHorizontallyFlipped}, input_vertically_flipped = {inputVerticallyFlipped}");
Debug.Log($"input_rotation = {inputRotation}, input_horizontally_flipped = {inputHorizontallyFlipped}, input_vertically_flipped = {inputVerticallyFlipped}");

sidePacket.Emplace("input_rotation", new IntPacket((int)inputRotation));
sidePacket.Emplace("input_horizontally_flipped", new BoolPacket(inputHorizontallyFlipped));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void SetPixels32(Color32[] pixels)
if (!RevokeNativeTexturePtr())
{
// If this line was executed, there must be a bug.
Logger.LogError("Failed to revoke the native texture.");
Debug.LogError("Failed to revoke the native texture.");
}
}

Expand Down Expand Up @@ -287,15 +287,15 @@ public static void OnReleaseTextureFrame(uint textureName, IntPtr syncTokenPtr)

if (!isIdFound)
{
Logger.LogError(_TAG, $"nameof (name={textureName}) is released, but the owner TextureFrame is not found");
Debug.LogError($"nameof (name={textureName}) is released, but the owner TextureFrame is not found");
return;
}

var isTextureFrameFound = _InstanceTable.TryGetValue(_instanceId, out var textureFrame);

if (!isTextureFrameFound)
{
Logger.LogWarning(_TAG, $"nameof owner TextureFrame of the released texture (name={textureName}) is already garbage collected");
Debug.LogWarning($"nameof owner TextureFrame of the released texture (name={textureName}) is already garbage collected");
return;
}

Expand All @@ -318,7 +318,7 @@ private static bool AcquireName(uint name, Guid ownerId)
if (ownerId != id && _InstanceTable.TryGetValue(id, out var _))
{
// if instance is found, the instance is using the name.
Logger.LogVerbose($"{id} is using {name} now");
Debug.Log($"{id} is using {name} now");
return false;
}
var _ = _NameTable.Remove(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void OnTextureFrameRelease(TextureFrame textureFrame)
if (!_textureFramesInUse.Remove(textureFrame.GetInstanceID()))
{
// won't be run
Logger.LogWarning(_TAG, "The released texture does not belong to the pool");
Debug.LogWarning("The released texture does not belong to the pool");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ private IEnumerator GetPermission()
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Logger.LogWarning(_TAG, "Not permitted to use Camera");
Debug.LogWarning("Not permitted to use Camera");
yield break;
}
#elif UNITY_IOS
if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) {
Logger.LogWarning(_TAG, "Not permitted to use WebCam");
Debug.LogWarning("Not permitted to use WebCam");
yield break;
}
#endif
Expand Down Expand Up @@ -246,7 +246,7 @@ private IEnumerator WaitForWebCamTexture()
{
const int timeoutFrame = 2000;
var count = 0;
Logger.LogVerbose("Waiting for WebCamTexture to start");
Debug.Log("Waiting for WebCamTexture to start");
yield return new WaitUntil(() => count++ > timeoutFrame || webCamTexture.width > 16);

if (webCamTexture.width <= 16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private IEnumerator Run()

if (!imageSource.isPrepared)
{
Logger.LogError(TAG, "Failed to start ImageSource, exiting...");
Debug.LogError("Failed to start ImageSource, exiting...");
yield break;
}

Expand All @@ -76,7 +76,7 @@ private IEnumerator Run()
yield return graphInitRequest;
if (graphInitRequest.isError)
{
Logger.LogError(TAG, graphInitRequest.error);
Debug.LogError(graphInitRequest.error);
yield break;
}

Expand Down
3 changes: 1 addition & 2 deletions Assets/MediaPipeUnity/Samples/Scenes/AppSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,4 @@ MonoBehaviour:
- width: 1280
height: 720
frameRate: 0
_availableVideoSources:
- {fileID: 32900000, guid: 56e0345cf6a6fcda3a013db92f9d6c01, type: 3}
_availableVideoSources: []
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
var calculatorOptions = new CalculatorOptions();
calculatorOptions.SetExtension(FaceDetectionOptions.Extensions.Ext, new FaceDetectionOptions { MinScoreThresh = minDetectionConfidence });
calculator.Options = calculatorOptions;
Logger.LogInfo(TAG, $"Min Detection Confidence ({calculator.Calculator}) = {minDetectionConfidence}");
Debug.Log($"Min Detection Confidence ({calculator.Calculator}) = {minDetectionConfidence}");
}

using (var validatedGraphConfig = new ValidatedGraphConfig())
Expand All @@ -106,7 +106,7 @@ private PacketMap BuildSidePacket(ImageSource imageSource)
SetImageTransformationOptions(sidePacket, imageSource);
sidePacket.Emplace("model_type", new IntPacket((int)modelType));

Logger.LogInfo(TAG, $"Model Selection = {modelType}");
Debug.Log($"Model Selection = {modelType}");

return sidePacket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
var calculatorOptions = new CalculatorOptions();
calculatorOptions.SetExtension(TensorsToDetectionsCalculatorOptions.Extensions.Ext, opt);
calculator.Options = calculatorOptions;
Logger.LogInfo(TAG, $"Min Detection Confidence = {minDetectionConfidence}");
Debug.Log($"Min Detection Confidence = {minDetectionConfidence}");
break;
}
}
Expand All @@ -163,7 +163,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
var options = calculator.Options.GetExtension(ThresholdingCalculatorOptions.Extensions.Ext);
options.Threshold = minTrackingConfidence;
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
Debug.Log($"Min Tracking Confidence = {minTrackingConfidence}");
}
}
calculatorGraph.Initialize(cannonicalizedConfig);
Expand All @@ -186,8 +186,8 @@ private PacketMap BuildSidePacket(ImageSource imageSource)
sidePacket.Emplace("num_faces", new IntPacket(maxNumFaces));
sidePacket.Emplace("with_attention", new BoolPacket(refineLandmarks));

Logger.LogInfo(TAG, $"Max Num Faces = {maxNumFaces}");
Logger.LogInfo(TAG, $"Refine Landmarks = {refineLandmarks}");
Debug.Log($"Max Num Faces = {maxNumFaces}");
Debug.Log($"Refine Landmarks = {refineLandmarks}");

return sidePacket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Mediapipe.Unity.Sample.HairSegmentation
{
Expand Down Expand Up @@ -96,7 +97,7 @@ private PacketMap BuildSidePacket(ImageSource imageSource)
sidePacket.Emplace("output_horizontally_flipped", new BoolPacket(outputHorizontallyFlipped));
sidePacket.Emplace("output_vertically_flipped", new BoolPacket(outputVerticallyFlipped));

Logger.LogDebug($"output_rotation = {outputRotation}, output_horizontally_flipped = {outputHorizontallyFlipped}, output_vertically_flipped = {outputVerticallyFlipped}");
Debug.Log($"output_rotation = {outputRotation}, output_horizontally_flipped = {outputHorizontallyFlipped}, output_vertically_flipped = {outputVerticallyFlipped}");
return sidePacket;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
var options = calculator.Options.GetExtension(TensorsToDetectionsCalculatorOptions.Extensions.Ext);
options.MinScoreThresh = minDetectionConfidence;
Logger.LogInfo(TAG, $"Min Detection Confidence = {minDetectionConfidence}");
Debug.Log($"Min Detection Confidence = {minDetectionConfidence}");
}
}

Expand All @@ -200,7 +200,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
var options = calculator.Options.GetExtension(ThresholdingCalculatorOptions.Extensions.Ext);
options.Threshold = minTrackingConfidence;
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
Debug.Log($"Min Tracking Confidence = {minTrackingConfidence}");
}
}
calculatorGraph.Initialize(cannonicalizedConfig);
Expand Down Expand Up @@ -235,8 +235,8 @@ private PacketMap BuildSidePacket(ImageSource imageSource)
sidePacket.Emplace("model_complexity", new IntPacket((int)modelComplexity));
sidePacket.Emplace("num_hands", new IntPacket(maxNumHands));

Logger.LogInfo(TAG, $"Model Complexity = {modelComplexity}");
Logger.LogInfo(TAG, $"Max Num Hands = {maxNumHands}");
Debug.Log($"Model Complexity = {modelComplexity}");
Debug.Log($"Max Num Hands = {maxNumHands}");

return sidePacket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
var options = calculator.Options.GetExtension(TensorsToDetectionsCalculatorOptions.Extensions.Ext);
options.MinScoreThresh = minDetectionConfidence;
Logger.LogInfo(TAG, $"Min Detection Confidence = {minDetectionConfidence}");
Debug.Log($"Min Detection Confidence = {minDetectionConfidence}");
}
}

Expand All @@ -256,7 +256,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
var options = calculator.Options.GetExtension(ThresholdingCalculatorOptions.Extensions.Ext);
options.Threshold = minTrackingConfidence;
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
Debug.Log($"Min Tracking Confidence = {minTrackingConfidence}");
}
}
calculatorGraph.Initialize(cannonicalizedConfig);
Expand Down Expand Up @@ -287,19 +287,19 @@ private PacketMap BuildSidePacket(ImageSource imageSource)
sidePacket.Emplace("output_horizontally_flipped", new BoolPacket(outputHorizontallyFlipped));
sidePacket.Emplace("output_vertically_flipped", new BoolPacket(outputVerticallyFlipped));

Logger.LogDebug($"output_rotation = {outputRotation}, output_horizontally_flipped = {outputHorizontallyFlipped}, output_vertically_flipped = {outputVerticallyFlipped}");
Debug.Log($"outtput_rotation = {outputRotation}, output_horizontally_flipped = {outputHorizontallyFlipped}, output_vertically_flipped = {outputVerticallyFlipped}");

sidePacket.Emplace("refine_face_landmarks", new BoolPacket(refineFaceLandmarks));
sidePacket.Emplace("model_complexity", new IntPacket((int)modelComplexity));
sidePacket.Emplace("smooth_landmarks", new BoolPacket(smoothLandmarks));
sidePacket.Emplace("enable_segmentation", new BoolPacket(enableSegmentation));
sidePacket.Emplace("smooth_segmentation", new BoolPacket(smoothSegmentation));

Logger.LogInfo(TAG, $"Refine Face Landmarks = {refineFaceLandmarks}");
Logger.LogInfo(TAG, $"Model Complexity = {modelComplexity}");
Logger.LogInfo(TAG, $"Smooth Landmarks = {smoothLandmarks}");
Logger.LogInfo(TAG, $"Enable Segmentation = {enableSegmentation}");
Logger.LogInfo(TAG, $"Smooth Segmentation = {smoothSegmentation}");
Debug.Log($"Refine Face Landmarks = {refineFaceLandmarks}");
Debug.Log($"Model Complexity = {modelComplexity}");
Debug.Log($"Smooth Landmarks = {smoothLandmarks}");
Debug.Log($"Enable Segmentation = {enableSegmentation}");
Debug.Log($"Smooth Segmentation = {smoothSegmentation}");

return sidePacket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
var options = calculator.Options.GetExtension(TensorsToDetectionsCalculatorOptions.Extensions.Ext);
options.MinScoreThresh = minDetectionConfidence;
Logger.LogInfo(TAG, $"Min Detection Confidence = {minDetectionConfidence}");
Debug.Log($"Min Detection Confidence = {minDetectionConfidence}");
}
}

Expand All @@ -186,7 +186,7 @@ protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
var options = calculator.Options.GetExtension(ThresholdingCalculatorOptions.Extensions.Ext);
options.Threshold = minTrackingConfidence;
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
Debug.Log($"Min Tracking Confidence = {minTrackingConfidence}");
}
}
calculatorGraph.Initialize(cannonicalizedConfig);
Expand Down Expand Up @@ -228,17 +228,17 @@ private PacketMap BuildSidePacket(ImageSource imageSource)
sidePacket.Emplace("output_horizontally_flipped", new BoolPacket(outputHorizontallyFlipped));
sidePacket.Emplace("output_vertically_flipped", new BoolPacket(outputVerticallyFlipped));

Logger.LogDebug($"output_rotation = {outputRotation}, output_horizontally_flipped = {outputHorizontallyFlipped}, output_vertically_flipped = {outputVerticallyFlipped}");
Debug.Log($"output_rotation = {outputRotation}, output_horizontally_flipped = {outputHorizontallyFlipped}, output_vertically_flipped = {outputVerticallyFlipped}");

sidePacket.Emplace("model_complexity", new IntPacket((int)modelComplexity));
sidePacket.Emplace("smooth_landmarks", new BoolPacket(smoothLandmarks));
sidePacket.Emplace("enable_segmentation", new BoolPacket(enableSegmentation));
sidePacket.Emplace("smooth_segmentation", new BoolPacket(smoothSegmentation));

Logger.LogInfo(TAG, $"Model Complexity = {modelComplexity}");
Logger.LogInfo(TAG, $"Smooth Landmarks = {smoothLandmarks}");
Logger.LogInfo(TAG, $"Enable Segmentation = {enableSegmentation}");
Logger.LogInfo(TAG, $"Smooth Segmentation = {smoothSegmentation}");
Debug.Log($"Model Complexity = {modelComplexity}");
Debug.Log($"Smooth Landmarks = {smoothLandmarks}");
Debug.Log($"Enable Segmentation = {enableSegmentation}");
Debug.Log($"Smooth Segmentation = {smoothSegmentation}");

return sidePacket;
}
Expand Down
Loading

0 comments on commit 806ea46

Please sign in to comment.