Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;

#pragma warning disable 1591
#pragma warning disable CA1401 // P/Invokes should not be visible
#pragma warning disable IDE1006 // Naming style
// ReSharper disable InconsistentNaming

namespace OpenCvSharp.Internal;

static partial class NativeMethods
{
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus objdetect_FaceDetectorYN_create(
IntPtr model,
IntPtr config,
ref Size inputSize,
float scoreThreshold,
float nmsThreshold,
int topK,
int backendId,
int targetId,
out IntPtr returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus objdetect_Ptr_FaceDetectorYN_delete(IntPtr ptr);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus objdetect_Ptr_FaceDetectorYN_get(IntPtr ptr, out IntPtr returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus objdetect_FaceDetectorYN_detect(
IntPtr obj, IntPtr image, IntPtr faces, out int returnValue);
}
65 changes: 44 additions & 21 deletions src/OpenCvSharp/Modules/objdetect/FaceDetectorYN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ namespace OpenCvSharp;
/// </summary>
public class FaceDetectorYN : DisposableCvObject
{
private Ptr? detectorPtr;

/// <summary>
/// A pointer to the shared pointer to the unmanaged object
/// Creates instance by raw pointer cv::FaceDetectorYN*
/// </summary>
private IntPtr _sharedPtr;
protected FaceDetectorYN(IntPtr p)
{
detectorPtr = new Ptr(p);
ptr = detectorPtr.Get();
}

/// <summary>
/// Creates an instance of this class with given parameters.
Expand All @@ -24,7 +30,7 @@ public class FaceDetectorYN : DisposableCvObject
/// <param name="topK">Keep top K bboxes before NMS</param>
/// <param name="backendId">The id of backend</param>
/// <param name="targetId">The id of target device</param>
public FaceDetectorYN(
public static FaceDetectorYN Create(
string model,
string config,
Size inputSize,
Expand All @@ -37,17 +43,19 @@ public FaceDetectorYN(
using StdString csModel = new(model);
using StdString csConfig = new(config);

ptr = NativeMethods.cveFaceDetectorYNCreate(
csModel.CvPtr,
csConfig.CvPtr,
ref inputSize,
scoreThreshold,
nmsThreshold,
topK,
backendId,
targetId,
ref _sharedPtr
);
NativeMethods.HandleException(
NativeMethods.objdetect_FaceDetectorYN_create(
csModel.CvPtr,
csConfig.CvPtr,
ref inputSize,
scoreThreshold,
nmsThreshold,
topK,
(int)backendId,
(int)targetId,
out var p));

return new FaceDetectorYN(p);
}

/// <summary>
Expand All @@ -61,22 +69,37 @@ public int Detect(Mat image, Mat faces)
ThrowIfDisposed();
using InputArray iaImage = new(image);
using OutputArray oaFaces = new(faces);
int result = NativeMethods.cveFaceDetectorYNDetect(ptr, iaImage.CvPtr, oaFaces.CvPtr);
NativeMethods.HandleException(
NativeMethods.objdetect_FaceDetectorYN_detect(ptr, iaImage.CvPtr, oaFaces.CvPtr, out var result));
GC.KeepAlive(this);
return result;
}

/// <summary>
/// Release the unmanaged memory associated with this FaceDetectorYN
/// Releases managed resources
/// </summary>
protected override void DisposeUnmanaged()
protected override void DisposeManaged()
{
if (!IntPtr.Zero.Equals(_sharedPtr))
detectorPtr?.Dispose();
detectorPtr = null;
base.DisposeManaged();
}

internal sealed class Ptr(IntPtr ptr) : OpenCvSharp.Ptr(ptr)
{
public override IntPtr Get()
{
NativeMethods.cveFaceDetectorYNRelease(ref _sharedPtr);
_sharedPtr = IntPtr.Zero;
NativeMethods.HandleException(
NativeMethods.objdetect_Ptr_FaceDetectorYN_get(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}

base.DisposeUnmanaged();
protected override void DisposeUnmanaged()
{
NativeMethods.HandleException(
NativeMethods.objdetect_Ptr_FaceDetectorYN_delete(ptr));
base.DisposeUnmanaged();
}
}
}
3 changes: 1 addition & 2 deletions src/OpenCvSharpExtern/OpenCvSharpExtern.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ copy "$(SolutionDir)opencv_files\opencv_win_x64\x64\vc17\bin\opencv_videoio_ffmp
<ClCompile Include="dnn.cpp" />
<ClCompile Include="dnn_superres.cpp" />
<ClCompile Include="face.cpp" />
<ClCompile Include="face_detector_yn.cpp" />
<ClCompile Include="highgui.cpp" />
<ClCompile Include="imgcodecs.cpp" />
<ClCompile Include="imgproc.cpp" />
Expand Down Expand Up @@ -283,7 +282,6 @@ copy "$(SolutionDir)opencv_files\opencv_win_x64\x64\vc17\bin\opencv_videoio_ffmp
<ClInclude Include="dnn.h" />
<ClInclude Include="dnn_Net.h" />
<ClInclude Include="dnn_superres.h" />
<ClInclude Include="face_detector_yn.h" />
<ClInclude Include="face_Facemark.h" />
<ClInclude Include="face_FaceRecognizer.h" />
<ClInclude Include="cuda.h" />
Expand Down Expand Up @@ -314,6 +312,7 @@ copy "$(SolutionDir)opencv_files\opencv_win_x64\x64\vc17\bin\opencv_videoio_ffmp
<ClInclude Include="ml_NormalBayesClassifier.h" />
<ClInclude Include="ml_StatModel.h" />
<ClInclude Include="ml_SVM.h" />
<ClInclude Include="objdetect_FaceDetectorYN.h" />
<ClInclude Include="objdetect_QRCodeDetector.h" />
<ClInclude Include="optflow.h" />
<ClInclude Include="optflow_motempl.h" />
Expand Down
54 changes: 0 additions & 54 deletions src/OpenCvSharpExtern/face_detector_yn.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions src/OpenCvSharpExtern/face_detector_yn.h

This file was deleted.

1 change: 1 addition & 0 deletions src/OpenCvSharpExtern/objdetect.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable CppUnusedIncludeDirective
#include "objdetect.h"
#include "objdetect_FaceDetectorYN.h"
#include "objdetect_HOGDescriptor.h"
#include "objdetect_QRCodeDetector.h"
62 changes: 62 additions & 0 deletions src/OpenCvSharpExtern/objdetect_FaceDetectorYN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#ifndef NO_OBJDETECT

// ReSharper disable IdentifierTypo
// ReSharper disable CppInconsistentNaming
// ReSharper disable CppNonInlineFunctionDefinitionInHeaderFile

#include "include_opencv.h"

#ifndef _WINRT_DLL

#pragma region FaceDetectorYN

CVAPI(ExceptionStatus) objdetect_FaceDetectorYN_create(
cv::String* model,
cv::String* config,
CvSize* inputSize,
float scoreThreshold,
float nmsThreshold,
int topK,
int backendId,
int targetId,
cv::Ptr<cv::FaceDetectorYN>** returnValue)
{
BEGIN_WRAP
const auto p = cv::FaceDetectorYN::create(
*model, *config, *inputSize,
scoreThreshold, nmsThreshold, topK,
backendId, targetId);
*returnValue = clone(p);
END_WRAP
}

CVAPI(ExceptionStatus) objdetect_Ptr_FaceDetectorYN_delete(cv::Ptr<cv::FaceDetectorYN>* ptr)
{
BEGIN_WRAP
delete ptr;
END_WRAP
}

CVAPI(ExceptionStatus) objdetect_Ptr_FaceDetectorYN_get(
cv::Ptr<cv::FaceDetectorYN>* ptr, cv::FaceDetectorYN** returnValue)
{
BEGIN_WRAP
*returnValue = ptr->get();
END_WRAP
}

CVAPI(ExceptionStatus) objdetect_FaceDetectorYN_detect(
cv::FaceDetectorYN* obj, cv::_InputArray* image, cv::_OutputArray* faces, int* returnValue)
{
BEGIN_WRAP
*returnValue = obj->detect(*image, *faces);
END_WRAP
}

#pragma endregion

#endif

#endif // NO_OBJDETECT
Loading
Loading