Skip to content
Merged
Show file tree
Hide file tree
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 Jul 15, 2022
537b83f
cc: float_vector_frame as input to graph
mgarbade Jul 15, 2022
9e1334b
MatrixFramePacket - c# helper functions
mgarbade Jul 15, 2022
69f705f
FloatVectorFramePacket - c# helper functions
mgarbade Jul 15, 2022
4d43489
Unity: Matrix Classification - Example scene
mgarbade Jul 15, 2022
8a14d3b
Matrix Classification.cs
mgarbade Jul 15, 2022
0bf959d
refactor: rename FloatVectorFrame -> FloatVector
mgarbade Jul 29, 2022
3a444c9
refactor: rename MatrixFrame -> Matrix
mgarbade Jul 29, 2022
ffd978e
move MatrixClassification example scene to Tutorials
mgarbade Jul 29, 2022
a723eec
GetArrayPtr() - change access to private
mgarbade Jul 29, 2022
6622fcb
MatrixPacket: accept MatrixData as input
mgarbade Jul 29, 2022
44202ab
add license
mgarbade Jul 29, 2022
69caf3c
move native functions to Packet_Unsafe
mgarbade Jul 29, 2022
3669c86
float_vector.cc -> faster vector allocation
mgarbade Jul 29, 2022
ebbdddc
float_vector.cc remove unused function - delete(...)
mgarbade Jul 29, 2022
9328084
float_vector.h - remove unused headers
mgarbade Jul 29, 2022
b8384bf
refactor: float_vector.cc
mgarbade Jul 29, 2022
3150340
removed unused headers
mgarbade Jul 29, 2022
66f197b
refactor: float_vector.h
mgarbade Jul 29, 2022
9c0d9ba
refactor: apply autoformatter on cc files
mgarbade Aug 1, 2022
f9ca7e1
refactor: mp__MakeMatrixFramePacket_At__PA_i_Rt -> mp__MakeMatrixPack…
mgarbade Aug 10, 2022
6f3d1b7
FloatVectorPacketTest added
mgarbade Aug 10, 2022
4e60769
fix: float_vector.cc
mgarbade Aug 10, 2022
8a018c3
fix: MatrixPacket.cs
mgarbade Aug 10, 2022
08133c7
fix: Test: FloatVectorPacketTest - Consume_ShouldThrowNotSupportedExc…
mgarbade Aug 10, 2022
8edacb8
MatrixPacketTest - add
mgarbade Aug 10, 2022
e19cbad
fix: Make MatrixClassification.cs run on Android
mgarbade Aug 11, 2022
81f7520
Update mediapipe_api/framework/formats/matrix_data.h
mgarbade Sep 22, 2022
ff378ed
Apply suggestions from code review
mgarbade Sep 23, 2022
9fa813e
float_vector - return vector size (+2 squashed commit)
mgarbade Sep 26, 2022
14dba01
fix: matrix_data.cc - wrong func name (+3 squashed commit)
mgarbade Sep 26, 2022
a8ec2a3
FloatVectorPacket - replace list by array
mgarbade Sep 27, 2022
8e7771d
Add license headers
mgarbade Sep 27, 2022
829a365
Remove Tutorial Scene: MatrixClassification
mgarbade Sep 27, 2022
861752f
fix: MatrixPacket tests
mgarbade Oct 12, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override float[] Get()
return result;
}

public IntPtr GetArrayPtr()
private IntPtr GetArrayPtr()
{
UnsafeNativeMethods.mp_Packet__GetFloatArray(mpPtr, out var value).Assert();
GC.KeepAlive(this);
Expand Down
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);
}
}
}

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,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.

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);
}

}
}

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,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
}
}

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
Expand Up @@ -65,6 +65,20 @@ internal static partial class UnsafeNativeMethods
public static extern MpReturnCode mp_Packet__ValidateAsFloat(IntPtr packet, out IntPtr status);
#endregion

#region FloatVector
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeFloatVectorPacket__PA_i(float[] value, int size, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeFloatVectorPacket_At__PA_i_Rt(float[] value, int size, IntPtr timestamp, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetFloatVector(IntPtr packet, out IntPtr value, out int size);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__ValidateAsFloatVector(IntPtr packet, out IntPtr status);
#endregion

#region Int
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeIntPacket__i(int value, out IntPtr packet);
Expand Down
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
}
}
Loading