Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement BaseVisionTaskApi #956

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_property = false:suggestion

# Use expression body for methods (IDE0022)
csharp_style_expression_bodied_methods = when_on_single_line:suggestion

# Use expression body for operators (IDE0023 and IDE0024)
csharp_style_expression_bodied_operators = when_on_single_line:suggestion

# Use language keywords instead of framework type names for type references (IDE0049)
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion

Expand Down

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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;

// TODO: use System.MathF
using Mathf = UnityEngine.Mathf;

namespace Mediapipe.Tasks.Components.Containers
{
/// <summary>
/// A rectangle, used as part of detection results or as input region-of-interest.
///
/// The coordinates are normalized wrt the image dimensions, i.e. generally in
/// [0,1] but they may exceed these bounds if describing a region overlapping the
/// image. The origin is on the top-left corner of the image.
/// </summary>
public readonly struct RectF : IEquatable<RectF>
{
private const float _RectFTolerance = 1e-4f;

public readonly float left;
public readonly float top;
public readonly float right;
public readonly float bottom;

#nullable enable
public override bool Equals(object? obj) => obj is RectF other && Equals(other);
#nullable disable

bool IEquatable<RectF>.Equals(RectF other)
{
return Mathf.Abs(left - other.left) < _RectFTolerance &&
Mathf.Abs(top - other.top) < _RectFTolerance &&
Mathf.Abs(right - other.right) < _RectFTolerance &&
Mathf.Abs(bottom - other.bottom) < _RectFTolerance;
}

// TODO: use HashCode.Combine
public override int GetHashCode() => Tuple.Create(left, top, right, bottom).GetHashCode();
public static bool operator ==(RectF lhs, RectF rhs) => lhs.Equals(rhs);
public static bool operator !=(RectF lhs, RectF rhs) => !(lhs == rhs);
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) 2023 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 System.Collections.Generic;
using System.Linq;

namespace Mediapipe.Tasks.Core
{
internal class TaskInfo<T> where T : ITaskOptions
{
public string taskGraph { get; }
public List<string> inputStreams { get; }
public List<string> outputStreams { get; }
public T taskOptions { get; }

public TaskInfo(string taskGraph, List<string> inputStreams, List<string> outputStreams, T taskOptions)
{
this.taskGraph = taskGraph;
this.inputStreams = inputStreams;
this.outputStreams = outputStreams;
this.taskOptions = taskOptions;
}

public CalculatorGraphConfig GenerateGraphConfig(bool enableFlowLimiting = false)
{
if (string.IsNullOrEmpty(taskGraph) || taskOptions == null)
{
throw new InvalidOperationException("Please provide both `task_graph` and `task_options`.");
}
if (inputStreams?.Count <= 0 || outputStreams?.Count <= 0)
{
throw new InvalidOperationException("Both `input_streams` and `output_streams` must be non-empty.");
}

if (!enableFlowLimiting)
{
return new CalculatorGraphConfig()
{
Node = {
new CalculatorGraphConfig.Types.Node()
{
Calculator = taskGraph,
Options = taskOptions.ToCalculatorOptions(),
InputStream = { inputStreams },
OutputStream = { outputStreams },
},
},
InputStream = { inputStreams },
OutputStream = { outputStreams },
};
}

var throttledInputStreams = inputStreams.Select(AddStreamNamePrefix);
var finishedStream = $"FINISHED:{Tool.ParseNameFromStream(outputStreams.First())}";
var flowLimiterOptions = new CalculatorOptions();
flowLimiterOptions.SetExtension(FlowLimiterCalculatorOptions.Extensions.Ext, new FlowLimiterCalculatorOptions()
{
MaxInFlight = 1,
MaxInQueue = 1,
});

return new CalculatorGraphConfig()
{
Node = {
new CalculatorGraphConfig.Types.Node()
{
Calculator = "FlowLimiterCalculator",
InputStreamInfo = {
new InputStreamInfo()
{
TagIndex = "FINISHED",
BackEdge = true,
},
},
InputStream = { inputStreams.Select(Tool.ParseNameFromStream).Append(finishedStream) },
OutputStream = { throttledInputStreams.Select(Tool.ParseNameFromStream) },
Options = flowLimiterOptions,
},
new CalculatorGraphConfig.Types.Node()
{
Calculator = taskGraph,
InputStream = { throttledInputStreams },
OutputStream = { outputStreams },
Options = taskOptions.ToCalculatorOptions(),
},
},
InputStream = { inputStreams },
OutputStream = { outputStreams },
};
}

private static string AddStreamNamePrefix(string tagIndexName)
{
Tool.ParseTagAndName(tagIndexName, out var tag, out var name);
return $"{tag}:throttled_{name}";
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023 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.

namespace Mediapipe.Tasks.Core
{
internal interface ITaskOptions
{
CalculatorOptions ToCalculatorOptions();
}
}

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

Loading