generated from RageAgainstThePixel/upm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
com.rest.huggingface 1.0.0-preview.7 (#5)
- added image segmentation task
- Loading branch information
1 parent
3defe14
commit 5161f27
Showing
10 changed files
with
139 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Runtime/Inference/ComputerVision/ImageSegmentation/ImageSegmentationResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using Utilities.WebRequestRest; | ||
|
||
namespace HuggingFace.Inference.ComputerVision.ImageSegmentation | ||
{ | ||
public sealed class ImageSegmentationResponse : B64JsonInferenceTaskResponse | ||
{ | ||
public ImageSegmentationResponse(string content, JsonSerializerSettings settings) : base(content, settings) | ||
{ | ||
Results = JsonConvert.DeserializeObject<IReadOnlyList<ImageSegmentationResult>>(content, settings); | ||
} | ||
|
||
public IReadOnlyList<ImageSegmentationResult> Results { get; } | ||
|
||
public override async Task DecodeAsync(CancellationToken cancellationToken = default) | ||
=> await Task.WhenAll(Results.Select(result => DecodeImageAsync(result, cancellationToken)).ToList()); | ||
|
||
private static async Task DecodeImageAsync(ImageSegmentationResult result, CancellationToken cancellationToken) | ||
{ | ||
await Rest.ValidateCacheDirectoryAsync(); | ||
|
||
if (!Rest.TryGetDownloadCacheItem(result.Blob, out var localFilePath)) | ||
{ | ||
await using var fileStream = new FileStream(localFilePath, FileMode.Create, FileAccess.ReadWrite); | ||
await fileStream.WriteAsync(Convert.FromBase64String(result.Blob), cancellationToken); | ||
} | ||
|
||
result.Mask = await Rest.DownloadTextureAsync(localFilePath, parameters: null, cancellationToken: cancellationToken); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Inference/ComputerVision/ImageSegmentation/ImageSegmentationResponse.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
Runtime/Inference/ComputerVision/ImageSegmentation/ImageSegmentationResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Newtonsoft.Json; | ||
using UnityEngine; | ||
|
||
namespace HuggingFace.Inference.ComputerVision.ImageSegmentation | ||
{ | ||
public sealed class ImageSegmentationResult | ||
{ | ||
[JsonConstructor] | ||
public ImageSegmentationResult( | ||
[JsonProperty("score")] double score, | ||
[JsonProperty("label")] string label, | ||
[JsonProperty("mask")] string blob) | ||
{ | ||
Score = score; | ||
Label = label; | ||
Blob = blob; | ||
} | ||
|
||
[JsonProperty("score")] | ||
public double Score { get; } | ||
|
||
[JsonProperty("label")] | ||
public string Label { get; } | ||
|
||
[JsonProperty("mask")] | ||
public string Blob { get; } | ||
|
||
[JsonIgnore] | ||
public Texture2D Mask { get; internal set; } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Inference/ComputerVision/ImageSegmentation/ImageSegmentationResult.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 5 additions & 3 deletions
8
Runtime/Inference/ComputerVision/ImageSegmentation/ImageSegmentationTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters