Skip to content

Commit

Permalink
fix: modelAssetBuffer cannot be specified (#1084)
Browse files Browse the repository at this point in the history
* fix: model asset path is always set

* test: add basic FaceDetector tests

* refactor: DetectionResult.Empty

* refactor: set ImageProcessingOptions null by default if possible

* ci: cache PackageResources

* test: read textures synchronously

* refactor: cache an empty result on the caller site
  • Loading branch information
homuler committed Jan 1, 2024
1 parent bcce0e9 commit 3ee44bd
Show file tree
Hide file tree
Showing 14 changed files with 565 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ jobs:
with:
ref: ${{ inputs.ref }}



# Cache built libraries
- name: Concat native library source files
run: |
Expand All @@ -34,6 +32,8 @@ jobs:
Packages/com.github.homuler.mediapipe/Runtime/Plugins/libmediapipe_c.so
Packages/com.github.homuler.mediapipe/Runtime/Plugins/Protobuf/*.dll
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Protobuf/**/*.cs
Packages/com.github.homuler.mediapipe/PackageResources/MediaPipe/*.bytes
Packages/com.github.homuler.mediapipe/PackageResources/MediaPipe/*.txt
key: libs-ubuntu-20.04-v1-${{ hashFiles('cache_key.txt') }}

# Setup build tools
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/macos-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
Packages/com.github.homuler.mediapipe/Runtime/Plugins/libmediapipe_c.dylib
Packages/com.github.homuler.mediapipe/Runtime/Plugins/Protobuf/*.dll
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Protobuf/**/*.cs
Packages/com.github.homuler.mediapipe/PackageResources/MediaPipe/*.bytes
Packages/com.github.homuler.mediapipe/PackageResources/MediaPipe/*.txt
key: libs-macos-11-v1-${{ hashFiles('cache_key.txt') }}

# Setup build tools
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/windows-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
Packages/com.github.homuler.mediapipe/Runtime/Plugins/mediapipe_c.dll
Packages/com.github.homuler.mediapipe/Runtime/Plugins/Protobuf/*.dll
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Protobuf/**/*.cs
Packages/com.github.homuler.mediapipe/PackageResources/MediaPipe/*.bytes
Packages/com.github.homuler.mediapipe/PackageResources/MediaPipe/*.txt
key: libs-windows-2019-v1-${{ hashFiles('cache_key.txt') }}

- name: Remove cache_key.txt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected override IEnumerator Run()
else
{
// clear the annotation
_detectionResultAnnotationController.DrawNow(FaceDetectionResult.Empty);
_detectionResultAnnotationController.DrawNow(default);
}
break;
case Tasks.Vision.Core.RunningMode.VIDEO:
Expand All @@ -112,7 +112,7 @@ protected override IEnumerator Run()
else
{
// clear the annotation
_detectionResultAnnotationController.DrawNow(FaceDetectionResult.Empty);
_detectionResultAnnotationController.DrawNow(default);
}
break;
case Tasks.Vision.Core.RunningMode.LIVE_STREAM:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ internal DetectionResult(List<Detection> detections)

public void Clear() => detections.Clear();

public static readonly DetectionResult Empty = new DetectionResult(new List<Detection>());
public static DetectionResult Empty => Alloc(0);

public static DetectionResult Alloc(int capacity) => new DetectionResult(new List<Detection>(capacity));

public static DetectionResult CreateFrom(List<Mediapipe.Detection> detectionsProto)
internal static DetectionResult CreateFrom(List<Mediapipe.Detection> detectionsProto)
{
var result = Alloc(detectionsProto.Count);
Copy(detectionsProto, ref result);
return result;
}

public static void Copy(List<Mediapipe.Detection> source, ref DetectionResult destination)
internal static void Copy(List<Mediapipe.Detection> source, ref DetectionResult destination)
{
var detections = destination.detections;
if (source.Count < detections.Count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum Delegate
public string modelAssetPath { get; } = string.Empty;
public byte[] modelAssetBuffer { get; } = null;

public BaseOptions(Delegate delegateCase = Delegate.CPU, string modelAssetPath = "", byte[] modelAssetBuffer = null)
public BaseOptions(Delegate delegateCase = Delegate.CPU, string modelAssetPath = null, byte[] modelAssetBuffer = null)
{
this.delegateCase = delegateCase;
this.modelAssetPath = modelAssetPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public sealed class FaceDetector : Core.BaseVisionTaskApi
private readonly NormalizedRect _normalizedRect = new NormalizedRect();
private readonly List<Detection> _detectionsForRead;

private readonly FaceDetectionResult _emptyResult = FaceDetectionResult.Alloc(0);

private FaceDetector(
CalculatorGraphConfig graphConfig,
Core.RunningMode runningMode,
Expand Down Expand Up @@ -99,12 +101,13 @@ public static FaceDetector CreateFromOptions(FaceDetectorOptions options)
/// frame of reference coordinates system, i.e. in `[0,image_width) x [0,
/// image_height)`, which are the dimensions of the underlying image data.
/// </returns>
public FaceDetectionResult Detect(Image image, Core.ImageProcessingOptions? imageProcessingOptions)
public FaceDetectionResult Detect(Image image, Core.ImageProcessingOptions? imageProcessingOptions = null)
{
using var outDetectionsPacket = DetectInternal(image, imageProcessingOptions);
if (outDetectionsPacket.IsEmpty())
{
return FaceDetectionResult.Empty;
_emptyResult.Clear();
return _emptyResult;
}
outDetectionsPacket.GetDetectionList(_detectionsForRead);
return FaceDetectionResult.CreateFrom(_detectionsForRead);
Expand Down Expand Up @@ -167,12 +170,13 @@ private Packet DetectInternal(Image image, Core.ImageProcessingOptions? imagePro
/// frame of reference coordinates system, i.e. in `[0,image_width) x [0,
/// image_height)`, which are the dimensions of the underlying image data.
/// </returns>
public FaceDetectionResult DetectForVideo(Image image, int timestampMs, Core.ImageProcessingOptions? imageProcessingOptions)
public FaceDetectionResult DetectForVideo(Image image, int timestampMs, Core.ImageProcessingOptions? imageProcessingOptions = null)
{
using var outDetectionsPacket = DetectVideoInternal(image, timestampMs, imageProcessingOptions);
if (outDetectionsPacket.IsEmpty())
{
return FaceDetectionResult.Empty;
_emptyResult.Clear();
return _emptyResult;
}
outDetectionsPacket.GetDetectionList(_detectionsForRead);
return FaceDetectionResult.CreateFrom(_detectionsForRead);
Expand Down Expand Up @@ -258,6 +262,7 @@ private static Tasks.Core.TaskRunner.PacketsCallback BuildPacketsCallback(FaceDe
}

var result = FaceDetectionResult.Alloc(options.numFaces);
var emptyResult = FaceDetectionResult.Alloc(0);

return (PacketMap outputPackets) =>
{
Expand All @@ -277,8 +282,9 @@ private static Tasks.Core.TaskRunner.PacketsCallback BuildPacketsCallback(FaceDe
if (outDetectionsPacket.IsEmpty())
{
emptyResult.Clear();
resultCallback(
FaceDetectionResult.Empty,
emptyResult,
image,
(int)timestamp);
return;
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.

Loading

0 comments on commit 3ee44bd

Please sign in to comment.