-
-
Notifications
You must be signed in to change notification settings - Fork 567
matrix classification via tflite model #656
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
Merged
homuler
merged 35 commits into
homuler:feat/matrix-packet
from
mgarbade:matrix_packet_rebased_v3
Oct 15, 2022
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
21bb5cc
cc: matrix_frame as input to graph
mgarbade 537b83f
cc: float_vector_frame as input to graph
mgarbade 9e1334b
MatrixFramePacket - c# helper functions
mgarbade 69f705f
FloatVectorFramePacket - c# helper functions
mgarbade 4d43489
Unity: Matrix Classification - Example scene
mgarbade 8a14d3b
Matrix Classification.cs
mgarbade 0bf959d
refactor: rename FloatVectorFrame -> FloatVector
mgarbade 3a444c9
refactor: rename MatrixFrame -> Matrix
mgarbade ffd978e
move MatrixClassification example scene to Tutorials
mgarbade a723eec
GetArrayPtr() - change access to private
mgarbade 6622fcb
MatrixPacket: accept MatrixData as input
mgarbade 44202ab
add license
mgarbade 69caf3c
move native functions to Packet_Unsafe
mgarbade 3669c86
float_vector.cc -> faster vector allocation
mgarbade ebbdddc
float_vector.cc remove unused function - delete(...)
mgarbade 9328084
float_vector.h - remove unused headers
mgarbade b8384bf
refactor: float_vector.cc
mgarbade 3150340
removed unused headers
mgarbade 66f197b
refactor: float_vector.h
mgarbade 9c0d9ba
refactor: apply autoformatter on cc files
mgarbade f9ca7e1
refactor: mp__MakeMatrixFramePacket_At__PA_i_Rt -> mp__MakeMatrixPack…
mgarbade 6f3d1b7
FloatVectorPacketTest added
mgarbade 4e60769
fix: float_vector.cc
mgarbade 8a018c3
fix: MatrixPacket.cs
mgarbade 08133c7
fix: Test: FloatVectorPacketTest - Consume_ShouldThrowNotSupportedExc…
mgarbade 8edacb8
MatrixPacketTest - add
mgarbade e19cbad
fix: Make MatrixClassification.cs run on Android
mgarbade 81f7520
Update mediapipe_api/framework/formats/matrix_data.h
mgarbade ff378ed
Apply suggestions from code review
mgarbade 9fa813e
float_vector - return vector size (+2 squashed commit)
mgarbade 14dba01
fix: matrix_data.cc - wrong func name (+3 squashed commit)
mgarbade a8ec2a3
FloatVectorPacket - replace list by array
mgarbade 8e7771d
Add license headers
mgarbade 829a365
Remove Tutorial Scene: MatrixClassification
mgarbade 861752f
fix: MatrixPacket tests
mgarbade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
86 changes: 86 additions & 0 deletions
86
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs
This file contains hidden or 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,86 @@ | ||
| // 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; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace Mediapipe | ||
| { | ||
| public class FloatVectorPacket : Packet<float[]> | ||
| { | ||
| /// <summary> | ||
| /// Creates an empty <see cref="FloatVectorPacket" /> instance. | ||
| /// </summary> | ||
| /// | ||
|
|
||
| private int _vectorLength = -1; | ||
|
|
||
|
|
||
| public FloatVectorPacket() : base(true) { } | ||
|
|
||
| [UnityEngine.Scripting.Preserve] | ||
| public FloatVectorPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { } | ||
|
|
||
| public FloatVectorPacket(float[] value) : base() | ||
| { | ||
| UnsafeNativeMethods.mp__MakeFloatVectorPacket__PA_i(value, value.Length, out var ptr).Assert(); | ||
| this.ptr = ptr; | ||
| _vectorLength = value.Length; | ||
| } | ||
|
|
||
| public FloatVectorPacket(float[] value, Timestamp timestamp) : base() | ||
| { | ||
| UnsafeNativeMethods.mp__MakeFloatVectorPacket_At__PA_i_Rt(value, value.Length, timestamp.mpPtr, out var ptr).Assert(); | ||
| GC.KeepAlive(timestamp); | ||
| this.ptr = ptr; | ||
| } | ||
|
|
||
| public FloatVectorPacket At(Timestamp timestamp) | ||
| { | ||
| var packet = At<FloatVectorPacket>(timestamp); | ||
| packet._vectorLength = _vectorLength; | ||
| return packet; | ||
| } | ||
|
|
||
| public override float[] Get() | ||
| { | ||
| UnsafeNativeMethods.mp_Packet__GetFloatVector(mpPtr, out var floatFrameVector, out var size).Assert(); | ||
| GC.KeepAlive(this); | ||
| if (size < 0) | ||
| { | ||
| throw new InvalidOperationException("The array's length is unknown, set Length first"); | ||
| } | ||
|
|
||
| var result = new float[size]; | ||
|
|
||
| unsafe | ||
| { | ||
| var src = (float*)floatFrameVector; | ||
|
|
||
| for (var i = 0; i < result.Length; i++) | ||
| { | ||
| result[i] = *src++; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public override StatusOr<float[]> Consume() | ||
| { | ||
| throw new NotSupportedException(); | ||
| } | ||
|
|
||
| public override Status ValidateAsType() | ||
| { | ||
| UnsafeNativeMethods.mp_Packet__ValidateAsFloatVector(mpPtr, out var statusPtr).Assert(); | ||
|
|
||
| GC.KeepAlive(this); | ||
| return new Status(statusPtr); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
...s/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs
This file contains hidden or 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,88 @@ | ||
| // 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. | ||
|
|
||
mgarbade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using Google.Protobuf; | ||
| using System; | ||
|
|
||
| namespace Mediapipe | ||
| { | ||
| public class MatrixPacket : Packet<MatrixData> | ||
| { | ||
| private int _length = -1; | ||
|
|
||
| public int length | ||
| { | ||
| get => _length; | ||
| set | ||
| { | ||
| if (_length >= 0) | ||
| { | ||
| throw new InvalidOperationException("Length is already set and cannot be changed"); | ||
| } | ||
|
|
||
| _length = value; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates an empty <see cref="MatrixPacket | ||
| /// " /> instance. | ||
| /// </summary> | ||
| public MatrixPacket() : base(true) { } | ||
|
|
||
| [UnityEngine.Scripting.Preserve] | ||
| public MatrixPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { } | ||
|
|
||
| public MatrixPacket(MatrixData matrixData) : base() | ||
| { | ||
| var value = matrixData.ToByteArray(); | ||
| UnsafeNativeMethods.mp__MakeMatrixPacket__PKc_i(value, value.Length, out var ptr).Assert(); | ||
| this.ptr = ptr; | ||
| length = value.Length; | ||
| } | ||
|
|
||
| public MatrixPacket(MatrixData matrixData, Timestamp timestamp) : base() | ||
| { | ||
| var value = matrixData.ToByteArray(); | ||
| UnsafeNativeMethods.mp__MakeMatrixPacket_At__PKc_i_Rt(value, value.Length, timestamp.mpPtr, out var ptr).Assert(); | ||
| GC.KeepAlive(timestamp); | ||
| this.ptr = ptr; | ||
| length = value.Length; | ||
| } | ||
|
|
||
| public MatrixPacket At(Timestamp timestamp) | ||
| { | ||
| var packet = At<MatrixPacket>(timestamp); | ||
| packet.length = length; | ||
| return packet; | ||
| } | ||
|
|
||
| public override MatrixData Get() | ||
| { | ||
| UnsafeNativeMethods.mp_Packet__GetMatrix(mpPtr, out var serializedMatrixData).Assert(); | ||
| GC.KeepAlive(this); | ||
|
|
||
| var matrixData = serializedMatrixData.Deserialize(MatrixData.Parser); | ||
| serializedMatrixData.Dispose(); | ||
|
|
||
| return matrixData; | ||
| } | ||
|
|
||
| public override StatusOr<MatrixData> Consume() | ||
| { | ||
| throw new NotSupportedException(); | ||
| } | ||
|
|
||
| public override Status ValidateAsType() | ||
| { | ||
| UnsafeNativeMethods.mp_Packet__ValidateAsMatrix(mpPtr, out var statusPtr).Assert(); | ||
|
|
||
| GC.KeepAlive(this); | ||
| return new Status(statusPtr); | ||
| } | ||
|
|
||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs
This file contains hidden or 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,29 @@ | ||
| // 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; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Mediapipe | ||
| { | ||
| internal static partial class UnsafeNativeMethods | ||
| { | ||
| #region Packet | ||
| [DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
| public static extern MpReturnCode mp__MakeMatrixPacket__PKc_i(byte[] serializedMatrixData, int size, out IntPtr packet_out); | ||
|
|
||
| [DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
| public static extern MpReturnCode mp__MakeMatrixPacket_At__PKc_i_Rt(byte[] serializedMatrixData, int size, IntPtr timestamp, out IntPtr packet_out); | ||
|
|
||
| [DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
| public static extern MpReturnCode mp_Packet__ValidateAsMatrix(IntPtr packet, out IntPtr status); | ||
|
|
||
| [DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
| public static extern MpReturnCode mp_Packet__GetMatrix(IntPtr packet, out SerializedProto serializedProto); | ||
|
|
||
| #endregion | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...er.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
124 changes: 124 additions & 0 deletions
124
...ges/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatVectorPacketTest.cs
This file contains hidden or 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,124 @@ | ||
| // 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.Collections.Generic; | ||
| using NUnit.Framework; | ||
| using System; | ||
|
|
||
| namespace Mediapipe.Tests | ||
| { | ||
| public class FloatVectorPacketTest | ||
| { | ||
| #region Constructor | ||
| // [Test, SignalAbort] | ||
| // public void Ctor_ShouldInstantiatePacket_When_CalledWithNoArguments() | ||
| // { | ||
| // using (var packet = new FloatPacket()) | ||
| // { | ||
| //#pragma warning disable IDE0058 | ||
| // Assert.AreEqual(Status.StatusCode.Internal, packet.ValidateAsType().Code()); | ||
| // Assert.Throws<MediaPipeException>(() => { packet.Get(); }); | ||
| // Assert.AreEqual(Timestamp.Unset(), packet.Timestamp()); | ||
| //#pragma warning restore IDE0058 | ||
| // } | ||
| // } | ||
|
|
||
| [Test] | ||
| public void Ctor_ShouldInstantiatePacket_When_CalledWithValue() | ||
| { | ||
| var floatVector = new float[6] { 10, 11, 12, 13, 14, 15 }; | ||
| using (var packet = new FloatVectorPacket(floatVector)) | ||
| { | ||
| Assert.True(packet.ValidateAsType().Ok()); | ||
| Assert.AreEqual(floatVector, packet.Get()); | ||
| Assert.AreEqual(Timestamp.Unset(), packet.Timestamp()); | ||
| } | ||
| } | ||
|
|
||
| //[Test] | ||
| //public void Ctor_ShouldInstantiatePacket_When_CalledWithValueAndTimestamp() | ||
| //{ | ||
| // using (var timestamp = new Timestamp(1)) | ||
| // { | ||
| // var floatArray = new float[6] { 10, 11, 12, 13, 14, 15 }; | ||
| // using (var packet = new FloatPacket(floatArray, timestamp)) | ||
| // { | ||
| // Assert.True(packet.ValidateAsType().Ok()); | ||
| // Assert.AreEqual(0.01f, packet.Get()); | ||
| // Assert.AreEqual(timestamp, packet.Timestamp()); | ||
| // } | ||
| // } | ||
| //} | ||
| #endregion | ||
|
|
||
| #region #isDisposed | ||
| [Test] | ||
| public void IsDisposed_ShouldReturnFalse_When_NotDisposedYet() | ||
| { | ||
| using (var packet = new FloatVectorPacket()) | ||
| { | ||
| Assert.False(packet.isDisposed); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void IsDisposed_ShouldReturnTrue_When_AlreadyDisposed() | ||
| { | ||
| var packet = new FloatVectorPacket(); | ||
| packet.Dispose(); | ||
|
|
||
| Assert.True(packet.isDisposed); | ||
| } | ||
| #endregion | ||
|
|
||
| #region #At | ||
| [Test] | ||
| public void At_ShouldReturnNewPacketWithTimestamp() | ||
| { | ||
| using (var timestamp = new Timestamp(1)) | ||
| { | ||
| var floatVector = new float[6] { 10, 11, 12, 13, 14, 15 }; | ||
| var packet = new FloatVectorPacket(floatVector).At(timestamp); | ||
| Assert.AreEqual(floatVector, packet.Get()); | ||
| Assert.AreEqual(timestamp, packet.Timestamp()); | ||
|
|
||
| using (var newTimestamp = new Timestamp(2)) | ||
| { | ||
| var newPacket = packet.At(newTimestamp); | ||
| Assert.AreEqual(floatVector, newPacket.Get()); | ||
| Assert.AreEqual(newTimestamp, newPacket.Timestamp()); | ||
| } | ||
|
|
||
| Assert.AreEqual(timestamp, packet.Timestamp()); | ||
| } | ||
| } | ||
| #endregion | ||
|
|
||
| #region #Consume | ||
| [Test] | ||
| public void Consume_ShouldThrowNotSupportedException() | ||
| { | ||
| using (var packet = new FloatVectorPacket()) | ||
| { | ||
| #pragma warning disable IDE0058 | ||
| Assert.Throws<NotSupportedException>(() => { packet.Consume(); }); | ||
| #pragma warning restore IDE0058 | ||
| } | ||
| } | ||
| #endregion | ||
|
|
||
| // #region #DebugTypeName | ||
| // [Test] | ||
| // public void DebugTypeName_ShouldReturnFloat_When_ValueIsSet() | ||
| // { | ||
| // using (var packet = new FloatPacket(0.01f)) | ||
| // { | ||
| // Assert.AreEqual("float", packet.DebugTypeName()); | ||
| // } | ||
| // } | ||
| // #endregion | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.