Wes xx mediapipe integration

This commit is contained in:
Jelle De Geest
2023-03-12 20:34:16 +00:00
parent 8349b5f149
commit b11eeb465c
975 changed files with 192230 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
// 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.
/// based on [OpenCvSharp](https://github.com/shimat/opencvsharp/blob/9a5f9828a74cfa3995562a06716e177705cde038/src/OpenCvSharp/Fundamentals/DisposableObject.cs)
using System;
using System.Threading;
namespace Mediapipe
{
public abstract class DisposableObject : IDisposable
{
private volatile int _disposeSignaled = 0;
public bool isDisposed { get; protected set; }
protected bool isOwner { get; private set; }
protected DisposableObject() : this(true) { }
protected DisposableObject(bool isOwner)
{
isDisposed = false;
this.isOwner = isOwner;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (Interlocked.Exchange(ref _disposeSignaled, 1) != 0)
{
return;
}
isDisposed = true;
if (disposing)
{
DisposeManaged();
}
DisposeUnmanaged();
}
~DisposableObject()
{
Dispose(false);
}
protected virtual void DisposeManaged() { }
protected virtual void DisposeUnmanaged() { }
public void TransferOwnership()
{
isOwner = false;
}
public void ThrowIfDisposed()
{
if (isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e9bf4260db1fcf38d9ab2123b9b848aa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
// 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;
namespace Mediapipe
{
public interface IMpResourceHandle : IDisposable
{
IntPtr mpPtr { get; }
/// <summary>
/// Relinquish the ownership, and release the resource it owns if necessary.
/// This method should be called only if the underlying native api moves the pointer.
/// </summary>
/// <remarks>If the object itself is no longer used, call <see cref="Dispose" /> instead.</remarks>
void ReleaseMpResource();
/// <summary>Relinquish the ownership</summary>
void TransferOwnership();
bool OwnsResource();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e5d6d5b8194184fe7bf9b284a3d1d76c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
// 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;
namespace Mediapipe
{
public class InternalException : Exception
{
public InternalException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8c01fc4de4ae037a79dab98092851538
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
// 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;
namespace Mediapipe
{
public class MediaPipeException : Exception
{
public MediaPipeException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 09e5bf77632ac85288dacb5b5445e9ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
// 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;
namespace Mediapipe
{
public class MediaPipePluginException : Exception
{
public MediaPipePluginException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e17a1d893e9d74c4eb7e85860d115c02
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,102 @@
// 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
{
public abstract class MpResourceHandle : DisposableObject, IMpResourceHandle
{
private IntPtr _ptr = IntPtr.Zero;
protected IntPtr ptr
{
get => _ptr;
set
{
if (value != IntPtr.Zero && OwnsResource())
{
throw new InvalidOperationException($"This object owns another resource");
}
_ptr = value;
}
}
protected MpResourceHandle(bool isOwner = true) : this(IntPtr.Zero, isOwner) { }
protected MpResourceHandle(IntPtr ptr, bool isOwner = true) : base(isOwner)
{
this.ptr = ptr;
}
#region IMpResourceHandle
public IntPtr mpPtr
{
get
{
ThrowIfDisposed();
return ptr;
}
}
public void ReleaseMpResource()
{
if (OwnsResource())
{
DeleteMpPtr();
}
ReleaseMpPtr();
TransferOwnership();
}
public bool OwnsResource()
{
return isOwner && IsResourcePresent();
}
#endregion
protected override void DisposeUnmanaged()
{
if (OwnsResource())
{
DeleteMpPtr();
}
ReleaseMpPtr();
base.DisposeUnmanaged();
}
/// <summary>
/// Forgets the pointer address.
/// After calling this method, <see ref="OwnsResource" /> will return false.
/// </summary>
protected void ReleaseMpPtr()
{
ptr = IntPtr.Zero;
}
/// <summary>
/// Release the memory (call `delete` or `delete[]`) whether or not it owns it.
/// </summary>
/// <remarks>In most cases, this method should not be called directly</remarks>
protected abstract void DeleteMpPtr();
protected delegate MpReturnCode StringOutFunc(IntPtr ptr, out IntPtr strPtr);
protected string MarshalStringFromNative(StringOutFunc f)
{
f(mpPtr, out var strPtr).Assert();
GC.KeepAlive(this);
var str = Marshal.PtrToStringAnsi(strPtr);
UnsafeNativeMethods.delete_array__PKc(strPtr);
return str;
}
protected bool IsResourcePresent()
{
return ptr != IntPtr.Zero;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 63faa26aefbc969038939ad114363133
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
// 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;
namespace Mediapipe
{
public abstract class SharedPtrHandle : MpResourceHandle
{
protected SharedPtrHandle(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }
/// <returns>The owning pointer</returns>
public abstract IntPtr Get();
/// <summary>Release the owning pointer</summary>
public abstract void Reset();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 90b1644d510da0d46a0db08fd07d3c49
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
// 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;
namespace Mediapipe
{
public abstract class UniquePtrHandle : MpResourceHandle
{
protected UniquePtrHandle(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }
/// <returns>The owning pointer</returns>
public abstract IntPtr Get();
/// <summary>Release the owning pointer</summary>
public abstract IntPtr Release();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5d0d5e05b9407fab79fe7814143e1fe6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: