Skip to content

Commit

Permalink
refactor!: make Status internal (#959)
Browse files Browse the repository at this point in the history
* refactor!: stop returning Status

* refactor!: make Status internal
  • Loading branch information
homuler committed Jul 21, 2023
1 parent 889e8ca commit 9b3d33c
Show file tree
Hide file tree
Showing 44 changed files with 410 additions and 453 deletions.
51 changes: 25 additions & 26 deletions Assets/MediaPipeUnity/Samples/Common/Scripts/GraphRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public virtual IEnumerator Initialize(RunningMode runningMode)
Logger.LogInfo(TAG, $"Config Type = {configType}");
Logger.LogInfo(TAG, $"Running Mode = {runningMode}");

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

Expand All @@ -139,7 +139,7 @@ public virtual IEnumerator Initialize(RunningMode runningMode)

protected void StartRun(PacketMap sidePacket)
{
calculatorGraph.StartRun(sidePacket).AssertOk();
calculatorGraph.StartRun(sidePacket);
_isRunning = true;
}

Expand All @@ -149,20 +149,22 @@ public virtual void Stop()
{
if (_isRunning)
{
using (var status = calculatorGraph.CloseAllPacketSources())
try
{
if (!status.Ok())
{
Logger.LogError(TAG, status.ToString());
}
calculatorGraph.CloseAllPacketSources();
}
catch (BadStatusException exception)
{
Logger.LogError(TAG, exception.Message);
}

using (var status = calculatorGraph.WaitUntilDone())
try
{
calculatorGraph.WaitUntilDone();
}
catch (BadStatusException exception)
{
if (!status.Ok())
{
Logger.LogError(TAG, status.ToString());
}
Logger.LogError(TAG, exception.Message);
}
}

Expand All @@ -180,7 +182,7 @@ public virtual void Stop()

protected void AddPacketToInputStream<T>(string streamName, Packet<T> packet)
{
calculatorGraph.AddPacketToInputStream(streamName, packet).AssertOk();
calculatorGraph.AddPacketToInputStream(streamName, packet);
}

protected void AddTextureFrameToInputStream(string streamName, TextureFrame textureFrame)
Expand Down Expand Up @@ -217,7 +219,7 @@ protected Timestamp GetCurrentTimestamp()
return microsec < 0 ? Timestamp.Unset() : new Timestamp(microsec);
}

protected Status InitializeCalculatorGraph()
protected void InitializeCalculatorGraph()
{
calculatorGraph = new CalculatorGraph();
_NameTable.Add(calculatorGraph.mpPtr, GetInstanceID());
Expand All @@ -229,19 +231,16 @@ protected Status InitializeCalculatorGraph()
// However, if the config format is invalid, this code does not initialize CalculatorGraph and does not throw exceptions either.
// The problem is that if you call ObserveStreamOutput in this state, the program will crash.
// The following code is not very efficient, but it will return Non-OK status when an invalid configuration is given.
try
var baseConfig = textConfig == null ? null : CalculatorGraphConfig.Parser.ParseFromTextFormat(textConfig.text);
if (baseConfig == null)
{
var baseConfig = textConfig == null ? null : CalculatorGraphConfig.Parser.ParseFromTextFormat(textConfig.text);
if (baseConfig == null)
{
throw new InvalidOperationException("Failed to get the text config. Check if the config is set to GraphRunner");
}
var status = ConfigureCalculatorGraph(baseConfig);
return !status.Ok() || inferenceMode == InferenceMode.CPU ? status : calculatorGraph.SetGpuResources(GpuManager.GpuResources);
throw new InvalidOperationException("Failed to get the text config. Check if the config is set to GraphRunner");
}
catch (Exception e)
ConfigureCalculatorGraph(baseConfig);

if (inferenceMode != InferenceMode.CPU)
{
return Status.FailedPrecondition(e.ToString());
calculatorGraph.SetGpuResources(GpuManager.GpuResources);
}
}

Expand All @@ -257,9 +256,9 @@ protected Status InitializeCalculatorGraph()
/// A <see cref="CalculatorGraphConfig" /> instance corresponding to <see cref="textConfig" />.<br />
/// It can be dynamically modified here.
/// </param>
protected virtual Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected virtual void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
return calculatorGraph.Initialize(config);
calculatorGraph.Initialize(config);
}

protected void SetImageTransformationOptions(PacketMap sidePacket, ImageSource imageSource, bool expectedToBeMirrored = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected override IList<WaitForResult> RequestDependentAssets()
};
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand All @@ -96,11 +96,8 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)

using (var validatedGraphConfig = new ValidatedGraphConfig())
{
var status = validatedGraphConfig.Initialize(config);

if (!status.Ok()) { return status; }

return calculatorGraph.Initialize(config);
validatedGraphConfig.Initialize(config);
calculatorGraph.Initialize(config);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void AddTextureFrameToInputStream(TextureFrame textureFrame)
return r1 || r2 || r3 || r4;
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand All @@ -133,9 +133,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)

using (var validatedGraphConfig = new ValidatedGraphConfig())
{
var status = validatedGraphConfig.Initialize(config);

if (!status.Ok()) { return status; }
validatedGraphConfig.Initialize(config);

var extensionRegistry = new ExtensionRegistry() { TensorsToDetectionsCalculatorOptions.Extensions.Ext, ThresholdingCalculatorOptions.Extensions.Ext };
var cannonicalizedConfig = validatedGraphConfig.Config(extensionRegistry);
Expand Down Expand Up @@ -168,7 +166,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
}
}
return calculatorGraph.Initialize(cannonicalizedConfig);
calculatorGraph.Initialize(cannonicalizedConfig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override IList<WaitForResult> RequestDependentAssets()
};
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand All @@ -69,7 +69,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
_hairMaskStream = new OutputStream<ImageFramePacket, ImageFrame>(calculatorGraph, _HairMaskStreamName, true, timeoutMicrosec);
}
return calculatorGraph.Initialize(config);
calculatorGraph.Initialize(config);
}

private PacketMap BuildSidePacket(ImageSource imageSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected override IList<WaitForResult> RequestDependentAssets()
};
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand Down Expand Up @@ -177,9 +177,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)

using (var validatedGraphConfig = new ValidatedGraphConfig())
{
var status = validatedGraphConfig.Initialize(config);

if (!status.Ok()) { return status; }
validatedGraphConfig.Initialize(config);

var extensionRegistry = new ExtensionRegistry() { TensorsToDetectionsCalculatorOptions.Extensions.Ext, ThresholdingCalculatorOptions.Extensions.Ext };
var cannonicalizedConfig = validatedGraphConfig.Config(extensionRegistry);
Expand All @@ -205,7 +203,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
}
}
return calculatorGraph.Initialize(cannonicalizedConfig);
calculatorGraph.Initialize(cannonicalizedConfig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private WaitForResult WaitForPoseLandmarkModel()
}
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand Down Expand Up @@ -229,9 +229,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)

using (var validatedGraphConfig = new ValidatedGraphConfig())
{
var status = validatedGraphConfig.Initialize(config);

if (!status.Ok()) { return status; }
validatedGraphConfig.Initialize(config);

var extensionRegistry = new ExtensionRegistry() { TensorsToDetectionsCalculatorOptions.Extensions.Ext, ThresholdingCalculatorOptions.Extensions.Ext };
var cannonicalizedConfig = validatedGraphConfig.Config(extensionRegistry);
Expand Down Expand Up @@ -261,7 +259,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
}
}
return calculatorGraph.Initialize(cannonicalizedConfig);
calculatorGraph.Initialize(cannonicalizedConfig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected override IList<WaitForResult> RequestDependentAssets()
};
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand All @@ -102,7 +102,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
_faceRectStream = new OutputStream<NormalizedRectPacket, NormalizedRect>(calculatorGraph, _FaceRectStreamName, true, timeoutMicrosec);
_faceLandmarksWithIrisStream = new OutputStream<NormalizedLandmarkListPacket, NormalizedLandmarkList>(calculatorGraph, _FaceLandmarksWithIrisStreamName, true, timeoutMicrosec);
}
return calculatorGraph.Initialize(config);
calculatorGraph.Initialize(config);
}

private PacketMap BuildSidePacket(ImageSource imageSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public bool TryGetNext(out ImageFrame outputVideo, bool allowBlock = true)
return TryGetNext(_outputVideoStream, out outputVideo, allowBlock, GetCurrentTimestampMicrosec());
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (configType == ConfigType.OpenGLES)
{
Expand All @@ -96,7 +96,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
_outputVideoStream = new OutputStream<ImageFramePacket, ImageFrame>(calculatorGraph, _OutputVideoStreamName, true, timeoutMicrosec);
}

return calculatorGraph.Initialize(config);
calculatorGraph.Initialize(config);
}

protected override IList<WaitForResult> RequestDependentAssets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected override IList<WaitForResult> RequestDependentAssets()
};
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand All @@ -67,7 +67,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
_outputDetectionsStream = new OutputStream<DetectionVectorPacket, List<Detection>>(calculatorGraph, _OutputDetectionsStreamName, true, timeoutMicrosec);
}
return calculatorGraph.Initialize(config);
calculatorGraph.Initialize(config);
}

private PacketMap BuildSidePacket(ImageSource imageSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected override IList<WaitForResult> RequestDependentAssets()
};
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand All @@ -163,9 +163,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)

using (var validatedGraphConfig = new ValidatedGraphConfig())
{
var status = validatedGraphConfig.Initialize(config);

if (!status.Ok()) { return status; }
validatedGraphConfig.Initialize(config);

var extensionRegistry = new ExtensionRegistry() { TensorsToDetectionsCalculatorOptions.Extensions.Ext, ThresholdingCalculatorOptions.Extensions.Ext };
var cannonicalizedConfig = validatedGraphConfig.Config(extensionRegistry);
Expand All @@ -191,7 +189,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
Logger.LogInfo(TAG, $"Min Tracking Confidence = {minTrackingConfidence}");
}
}
return calculatorGraph.Initialize(cannonicalizedConfig);
calculatorGraph.Initialize(cannonicalizedConfig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected override IList<WaitForResult> RequestDependentAssets()
};
}

protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
protected override void ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
if (runningMode == RunningMode.NonBlockingSync)
{
Expand All @@ -65,7 +65,7 @@ protected override Status ConfigureCalculatorGraph(CalculatorGraphConfig config)
{
_segmentationMaskStream = new OutputStream<ImageFramePacket, ImageFrame>(calculatorGraph, _SegmentationMaskStreamName, true, timeoutMicrosec);
}
return calculatorGraph.Initialize(config);
calculatorGraph.Initialize(config);
}

private PacketMap BuildSidePacket(ImageSource imageSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Mediapipe
{
public class BadStatusException : Exception
{
public Status.StatusCode statusCode { get; private set; }
public StatusCode statusCode { get; private set; }

public BadStatusException(string message) : base(message) { }

public BadStatusException(Status.StatusCode statusCode, string message) : base(message)
public BadStatusException(StatusCode statusCode, string message) : base(message)
{
this.statusCode = statusCode;
}
Expand Down
Loading

0 comments on commit 9b3d33c

Please sign in to comment.