diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatArrayPacket.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatArrayPacket.cs index 1b7f4bd80..f9afa6521 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatArrayPacket.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatArrayPacket.cs @@ -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); diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs new file mode 100644 index 000000000..26bbdae5f --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs @@ -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 + { + /// + /// Creates an empty instance. + /// + /// + + 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(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 Consume() + { + throw new NotSupportedException(); + } + + public override Status ValidateAsType() + { + UnsafeNativeMethods.mp_Packet__ValidateAsFloatVector(mpPtr, out var statusPtr).Assert(); + + GC.KeepAlive(this); + return new Status(statusPtr); + } + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs.meta new file mode 100644 index 000000000..23702bbfb --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorPacket.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8a9b77e746fd38d46a09d847f7370446 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs new file mode 100644 index 000000000..04fce4cdc --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs @@ -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 + { + 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; + } + } + + /// + /// Creates an empty instance. + /// + 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(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 Consume() + { + throw new NotSupportedException(); + } + + public override Status ValidateAsType() + { + UnsafeNativeMethods.mp_Packet__ValidateAsMatrix(mpPtr, out var statusPtr).Assert(); + + GC.KeepAlive(this); + return new Status(statusPtr); + } + + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs.meta new file mode 100644 index 000000000..2e04b5783 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ef898503888a0ee42af28aef1fbbe794 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs new file mode 100644 index 000000000..bfc16aec5 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs @@ -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 + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs.meta new file mode 100644 index 000000000..67688a598 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Matrix_Unsafe.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fc5345171757d2049a546634b03b8c5e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Packet_Unsafe.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Packet_Unsafe.cs index 0e5d03392..db505c43b 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Packet_Unsafe.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Packet_Unsafe.cs @@ -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); diff --git a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatVectorPacketTest.cs b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatVectorPacketTest.cs new file mode 100644 index 000000000..dcce600bf --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatVectorPacketTest.cs @@ -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(() => { 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(() => { 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 + } +} diff --git a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatVectorPacketTest.cs.meta b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatVectorPacketTest.cs.meta new file mode 100644 index 000000000..15a7a320d --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatVectorPacketTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8a567519c65348c47ae2c5784431179e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs new file mode 100644 index 000000000..1e10b29c9 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs @@ -0,0 +1,101 @@ +// 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 MatrixPacketTest + { + #region Constructor + [Test] + public void Ctor_ShouldInstantiatePacket_When_CalledWithValue() + { + var matrix = CreateMatrixInputData(); + using (var packet = new MatrixPacket(matrix)) + { + Assert.True(packet.ValidateAsType().Ok()); + Assert.AreEqual(matrix, packet.Get()); + Assert.AreEqual(Timestamp.Unset(), packet.Timestamp()); + } + } + #endregion + + #region #isDisposed + [Test] + public void IsDisposed_ShouldReturnFalse_When_NotDisposedYet() + { + using (var packet = new MatrixPacket()) + { + Assert.False(packet.isDisposed); + } + } + + [Test] + public void IsDisposed_ShouldReturnTrue_When_AlreadyDisposed() + { + var packet = new MatrixPacket(); + packet.Dispose(); + + Assert.True(packet.isDisposed); + } + #endregion + + #region #At + [Test] + public void At_ShouldReturnNewPacketWithTimestamp() + { + using (var timestamp = new Timestamp(1)) + { + var matrix = CreateMatrixInputData(); + var packet = new MatrixPacket(matrix).At(timestamp); + Assert.AreEqual(matrix, packet.Get()); + Assert.AreEqual(timestamp, packet.Timestamp()); + + using (var newTimestamp = new Timestamp(2)) + { + var newPacket = packet.At(newTimestamp); + Assert.AreEqual(matrix, newPacket.Get()); + Assert.AreEqual(newTimestamp, newPacket.Timestamp()); + } + + Assert.AreEqual(timestamp, packet.Timestamp()); + } + } + #endregion + + #region #Consume + [Test] + public void Consume_ShouldThrowNotSupportedException() + { + using (var packet = new MatrixPacket()) + { +#pragma warning disable IDE0058 + Assert.Throws(() => { packet.Consume(); }); +#pragma warning restore IDE0058 + } + } + #endregion + + private static MatrixData CreateMatrixInputData() + { + var matrix = new MatrixData(); + matrix.PackedData.Add(0); + matrix.PackedData.Add(1); + matrix.PackedData.Add(2); + matrix.PackedData.Add(3); + matrix.PackedData.Add(4); + matrix.PackedData.Add(5); + + matrix.Rows = 2; + matrix.Cols = 3; + return matrix; + } + + } +} diff --git a/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs.meta b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs.meta new file mode 100644 index 000000000..b349e3721 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a3a4963f37dcdcc43a8292c2493c19bc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/mediapipe_api/BUILD b/mediapipe_api/BUILD index cc4ba455f..5e313382f 100644 --- a/mediapipe_api/BUILD +++ b/mediapipe_api/BUILD @@ -195,6 +195,8 @@ cc_library( "//mediapipe_api/framework/formats:detection", "//mediapipe_api/framework/formats:image_frame", "//mediapipe_api/framework/formats:landmark", + "//mediapipe_api/framework/formats:matrix_data", + "//mediapipe_api/framework/formats:float_vector", "//mediapipe_api/framework/formats:rect", "//mediapipe_api/framework/port:logging", "//mediapipe_api/graphs/object_detection_3d/calculators:model_matrix", diff --git a/mediapipe_api/framework/formats/BUILD b/mediapipe_api/framework/formats/BUILD index 6bd9f8966..bc98f3f85 100644 --- a/mediapipe_api/framework/formats/BUILD +++ b/mediapipe_api/framework/formats/BUILD @@ -64,6 +64,34 @@ cc_library( alwayslink = True, ) +cc_library( + name = "matrix_data", + srcs = ["matrix_data.cc"], + hdrs = ["matrix_data.h"], + deps = [ + "//mediapipe_api:common", + "//mediapipe_api/external/absl:status", + "//mediapipe_api/external/absl:statusor", + "//mediapipe_api/framework:packet", + "@com_google_mediapipe//mediapipe/framework/formats:matrix", + "@com_google_mediapipe//mediapipe/framework/formats:matrix_data_cc_proto", + ], + alwayslink = True, +) + +cc_library( + name = "float_vector", + srcs = ["float_vector.cc"], + hdrs = ["float_vector.h"], + deps = [ + "//mediapipe_api:common", + "//mediapipe_api/external/absl:status", + "//mediapipe_api/external/absl:statusor", + "//mediapipe_api/framework:packet", + ], + alwayslink = True, +) + cc_library( name = "rect", srcs = ["rect.cc"], diff --git a/mediapipe_api/framework/formats/float_vector.cc b/mediapipe_api/framework/formats/float_vector.cc new file mode 100644 index 000000000..d4e6f3328 --- /dev/null +++ b/mediapipe_api/framework/formats/float_vector.cc @@ -0,0 +1,43 @@ +// 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. + +#include "mediapipe_api/framework/formats/float_vector.h" + +#include + +MpReturnCode mp__MakeFloatVectorPacket__PA_i(const float* value, int size, mediapipe::Packet** packet_out) { + TRY + std::vector vector(value, value + size); + *packet_out = new mediapipe::Packet{mediapipe::MakePacket>(vector)}; + RETURN_CODE(MpReturnCode::Success); + CATCH_EXCEPTION +} + +MpReturnCode mp__MakeFloatVectorPacket_At__PA_i_Rt(const float* value, int size, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out) { + TRY + std::vector vector(value, value + size); + *packet_out = new mediapipe::Packet{mediapipe::MakePacket>(vector).At(*timestamp)}; + RETURN_CODE(MpReturnCode::Success); + CATCH_EXCEPTION +} + +MpReturnCode mp_Packet__GetFloatVector(mediapipe::Packet* packet, const float** value_out, int* size_out) { + TRY_ALL + auto& vector_float = packet->Get>(); + auto length = vector_float.size(); + + *value_out = vector_float.data(); + *size_out = length; + RETURN_CODE(MpReturnCode::Success); + CATCH_ALL +} + +MP_CAPI(MpReturnCode) mp_Packet__ValidateAsFloatVector(mediapipe::Packet* packet, absl::Status** status_out) { + TRY + *status_out = new absl::Status{packet->ValidateAsType>()}; + RETURN_CODE(MpReturnCode::Success); + CATCH_EXCEPTION +} diff --git a/mediapipe_api/framework/formats/float_vector.h b/mediapipe_api/framework/formats/float_vector.h new file mode 100644 index 000000000..8adb1c513 --- /dev/null +++ b/mediapipe_api/framework/formats/float_vector.h @@ -0,0 +1,21 @@ +// 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. + +#ifndef MEDIAPIPE_API_FRAMEWORK_FORMATS_FLOAT_VECTOR_H_ +#define MEDIAPIPE_API_FRAMEWORK_FORMATS_FLOAT_VECTOR_H_ + +#include "mediapipe_api/common.h" +#include "mediapipe_api/framework/packet.h" + +extern "C" { +MP_CAPI(MpReturnCode) mp__MakeFloatVectorPacket__PA_i(const float* value, int size, mediapipe::Packet** packet_out); +MP_CAPI(MpReturnCode) mp__MakeFloatVectorPacket_At__PA_i_Rt(const float* value, int size, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out); +MP_CAPI(MpReturnCode) mp_Packet__GetFloatVector(mediapipe::Packet* packet, const float** value_out, int* size_out); +MP_CAPI(MpReturnCode) mp_Packet__ValidateAsFloatVector(mediapipe::Packet* packet, absl::Status** status_out); + +} // extern "C" + +#endif // MEDIAPIPE_API_FRAMEWORK_FORMATS_FLOAT_VECTOR_H_ diff --git a/mediapipe_api/framework/formats/matrix_data.cc b/mediapipe_api/framework/formats/matrix_data.cc new file mode 100644 index 000000000..441bf6345 --- /dev/null +++ b/mediapipe_api/framework/formats/matrix_data.cc @@ -0,0 +1,65 @@ +// 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. + +#include "mediapipe_api/framework/formats/matrix_data.h" + +MpReturnCode mp__MakeMatrixPacket__PKc_i(const char* matrix_data_serialized, int size, mediapipe::Packet** packet_out) { + TRY + mediapipe::Matrix matrix; + + // convert matrix data from char into mediapipe::MatrixData + std::string content; + mediapipe::MatrixData matrix_data; + CHECK(matrix_data.ParseFromString(std::string(matrix_data_serialized, size))); + + // fill matrix with data from matrix_data_serialized + mediapipe::MatrixFromMatrixDataProto(matrix_data, &matrix); + + *packet_out = new mediapipe::Packet{mediapipe::MakePacket(matrix)}; + RETURN_CODE(MpReturnCode::Success); + CATCH_EXCEPTION +} + +MpReturnCode mp__MakeMatrixPacket_At__PKc_i_Rt(const char* matrix_data_serialized, int size, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out) { + TRY + mediapipe::Matrix matrix; + + // convert matrix data from char into mediapipe::MatrixData + std::string content; + mediapipe::MatrixData matrix_data; + CHECK(matrix_data.ParseFromString(std::string(matrix_data_serialized, size))); + + // fill matrix with data from matrix_data_serialized + mediapipe::MatrixFromMatrixDataProto(matrix_data, &matrix); + + *packet_out = new mediapipe::Packet{mediapipe::MakePacket(matrix).At(*timestamp)}; + RETURN_CODE(MpReturnCode::Success); + CATCH_EXCEPTION +} + +MP_CAPI(MpReturnCode) mp_Packet__ValidateAsMatrix(mediapipe::Packet* packet, absl::Status** status_out) { + TRY + *status_out = new absl::Status{packet->ValidateAsType()}; + RETURN_CODE(MpReturnCode::Success); + CATCH_EXCEPTION +} + +MP_CAPI(MpReturnCode) mp_Packet__GetMatrix(mediapipe::Packet* packet, mp_api::SerializedProto* value_out) { + TRY + // Get Eigen::Matrix from packet + mediapipe::Matrix matrix; + matrix = packet->Get(); + + // Convert to format that can be send to Unity + mediapipe::MatrixData matrix_data; + mediapipe::MatrixDataProtoFromMatrix(matrix, &matrix_data); + + // auto matrix_data_serialized = new mp_api::SerializedProto(); + SerializeProto(matrix_data, value_out); + + RETURN_CODE(MpReturnCode::Success); + CATCH_EXCEPTION +} diff --git a/mediapipe_api/framework/formats/matrix_data.h b/mediapipe_api/framework/formats/matrix_data.h new file mode 100644 index 000000000..e56e953be --- /dev/null +++ b/mediapipe_api/framework/formats/matrix_data.h @@ -0,0 +1,25 @@ +// 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. + +#ifndef MEDIAPIPE_API_FRAMEWORK_FORMATS_MATRIX_DATA_H_ +#define MEDIAPIPE_API_FRAMEWORK_FORMATS_MATRIX_DATA_H_ + +#include "mediapipe/framework/formats/matrix.h" +#include "mediapipe_api/common.h" +#include "mediapipe_api/external/protobuf.h" +#include "mediapipe_api/framework/packet.h" + +extern "C" { + +MP_CAPI(MpReturnCode) mp__MakeMatrixPacket__PKc_i(const char* matrix_data_serialized, int size, mediapipe::Packet** packet_out); +MP_CAPI(MpReturnCode) mp__MakeMatrixPacket_At__PKc_i_Rt(const char* matrix_data_serialized, int size, mediapipe::Timestamp* timestamp, + mediapipe::Packet** packet_out); +MP_CAPI(MpReturnCode) mp_Packet__ValidateAsMatrix(mediapipe::Packet* packet, absl::Status** status_out); +MP_CAPI(MpReturnCode) mp_Packet__GetMatrix(mediapipe::Packet* packet, mp_api::SerializedProto* value_out); + +} // extern "C" + +#endif // MEDIAPIPE_API_FRAMEWORK_FORMATS_MATRIX_DATA_H_