-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
FaceDetectorYN implementation: Added FaceDetectorYN class and associated native methods for face detection. #1827
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
32 changes: 32 additions & 0 deletions
32
src/OpenCvSharp/Internal/PInvoke/NativeMethods/objdetect/NativeMethods_FaceDetectorYN.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,32 @@ | ||
| using System.Diagnostics.Contracts; | ||
| using System.Runtime.InteropServices; | ||
| using OpenCvSharp.Dnn; | ||
|
|
||
| #pragma warning disable 1591 | ||
| #pragma warning disable CA1401 // P/Invokes should not be visible | ||
| #pragma warning disable CA2101 // Specify marshalling for P-Invoke string arguments (code analysis) | ||
| #pragma warning disable IDE1006 // Naming style | ||
|
|
||
| namespace OpenCvSharp.Internal; | ||
|
|
||
| public static partial class NativeMethods | ||
| { | ||
| [Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] | ||
| internal static extern IntPtr cveFaceDetectorYNCreate( | ||
| IntPtr model, | ||
| IntPtr config, | ||
| ref Size inputSize, | ||
| float scoreThreshold, | ||
| float nmsThreshold, | ||
| int topK, | ||
| Backend backendId, | ||
| Target targetId, | ||
| ref IntPtr sharedPtr | ||
| ); | ||
|
|
||
| [Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] | ||
| internal static extern int cveFaceDetectorYNDetect(IntPtr faceDetector, IntPtr image, IntPtr faces); | ||
|
|
||
| [Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] | ||
| internal static extern void cveFaceDetectorYNRelease(ref IntPtr faceDetector); | ||
| } |
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,82 @@ | ||
| using OpenCvSharp.Dnn; | ||
| using OpenCvSharp.Internal; | ||
|
|
||
| namespace OpenCvSharp; | ||
|
|
||
| /// <summary> | ||
| /// DNN-based face detector | ||
| /// </summary> | ||
| public class FaceDetectorYN : DisposableCvObject | ||
| { | ||
| /// <summary> | ||
| /// A pointer to the shared pointer to the unmanaged object | ||
| /// </summary> | ||
| private IntPtr _sharedPtr; | ||
|
|
||
| /// <summary> | ||
| /// Creates an instance of this class with given parameters. | ||
| /// </summary> | ||
| /// <param name="model">The path to the requested model</param> | ||
| /// <param name="config">The path to the config file for compatibility, which is not requested for ONNX models</param> | ||
| /// <param name="inputSize">The size of the input image</param> | ||
| /// <param name="scoreThreshold">The threshold to filter out bounding boxes of score smaller than the given value</param> | ||
| /// <param name="nmsThreshold">The threshold to suppress bounding boxes of IoU bigger than the given value</param> | ||
| /// <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( | ||
| string model, | ||
| string config, | ||
| Size inputSize, | ||
| float scoreThreshold = 0.9f, | ||
| float nmsThreshold = 0.3f, | ||
| int topK = 5000, | ||
| Backend backendId = Backend.DEFAULT, | ||
| Target targetId = Target.CPU) | ||
| { | ||
| 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 | ||
| ); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A simple interface to detect face from given image. | ||
| /// </summary> | ||
| /// <param name="image">An image to detect</param> | ||
| /// <param name="faces">Detection results stored in a Mat</param> | ||
| /// <returns>1 if detection is successful, 0 otherwise.</returns> | ||
| 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); | ||
| GC.KeepAlive(this); | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Release the unmanaged memory associated with this FaceDetectorYN | ||
| /// </summary> | ||
| protected override void DisposeUnmanaged() | ||
| { | ||
| if (!IntPtr.Zero.Equals(_sharedPtr)) | ||
| { | ||
| NativeMethods.cveFaceDetectorYNRelease(ref _sharedPtr); | ||
| _sharedPtr = IntPtr.Zero; | ||
| } | ||
|
|
||
| base.DisposeUnmanaged(); | ||
| } | ||
| } | ||
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
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
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,54 @@ | ||
| #include "face_detector_yn.h" | ||
| #include <opencv2/core/cvstd.hpp> | ||
| #include <opencv2/core/cvstd_wrapper.hpp> | ||
| #include <opencv2/core/mat.hpp> | ||
| #include <opencv2/core/types_c.h> | ||
| #include <opencv2/objdetect/face.hpp> | ||
| #include <opencv2/opencv_modules.hpp> | ||
|
|
||
| cv::FaceDetectorYN* cveFaceDetectorYNCreate( | ||
| cv::String* model, | ||
| cv::String* config, | ||
| CvSize* inputSize, | ||
| float scoreThreshold, | ||
| float nmsThreshold, | ||
| int topK, | ||
| int backendId, | ||
| int targetId, | ||
| cv::Ptr<cv::FaceDetectorYN>** sharedPtr) | ||
| { | ||
| #ifdef HAVE_OPENCV_OBJDETECT | ||
| cv::Ptr<cv::FaceDetectorYN> ptr = cv::FaceDetectorYN::create( | ||
| *model, | ||
| *config, | ||
| *inputSize, | ||
| scoreThreshold, | ||
| nmsThreshold, | ||
| topK, | ||
| backendId, | ||
| targetId); | ||
| *sharedPtr = new cv::Ptr<cv::FaceDetectorYN>(ptr); | ||
| return (*sharedPtr)->get(); | ||
| #else | ||
| throw_no_objdetect(); | ||
| #endif | ||
| } | ||
|
|
||
| void cveFaceDetectorYNRelease(cv::Ptr<cv::FaceDetectorYN>** faceDetector) | ||
| { | ||
| #ifdef HAVE_OPENCV_OBJDETECT | ||
| delete* faceDetector; | ||
| *faceDetector = 0; | ||
| #else | ||
| throw_no_objdetect(); | ||
| #endif | ||
| } | ||
|
|
||
| int cveFaceDetectorYNDetect(cv::FaceDetectorYN* faceDetector, cv::_InputArray* image, cv::_OutputArray* faces) | ||
| { | ||
| #ifdef HAVE_OPENCV_OBJDETECT | ||
| return faceDetector->detect(*image, *faces); | ||
| #else | ||
| throw_no_objdetect(); | ||
| #endif | ||
| } |
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,34 @@ | ||
| #pragma once | ||
|
|
||
| #ifndef NO_OBJDETECT | ||
|
|
||
| // ReSharper disable IdentifierTypo | ||
| // ReSharper disable CppInconsistentNaming | ||
| // ReSharper disable CppNonInlineFunctionDefinitionInHeaderFile | ||
|
|
||
| #include "my_functions.h" | ||
| #include <opencv2/core/cvstd.hpp> | ||
| #include <opencv2/core/cvstd_wrapper.hpp> | ||
| #include <opencv2/core/types_c.h> | ||
| #include <opencv2/objdetect/face.hpp> | ||
|
|
||
| #ifndef _WINRT_DLL | ||
|
|
||
| CVAPI(cv::FaceDetectorYN*) cveFaceDetectorYNCreate( | ||
| cv::String* model, | ||
| cv::String* config, | ||
| CvSize* inputSize, | ||
| float scoreThreshold, | ||
| float nmsThreshold, | ||
| int topK, | ||
| int backendId, | ||
| int targetId, | ||
| cv::Ptr<cv::FaceDetectorYN>** sharedPtr); | ||
|
|
||
| CVAPI(void) cveFaceDetectorYNRelease(cv::Ptr<cv::FaceDetectorYN>** faceDetector); | ||
|
|
||
| CVAPI(int) cveFaceDetectorYNDetect(cv::FaceDetectorYN* faceDetector, cv::_InputArray* image, cv::_OutputArray* faces); | ||
|
|
||
| #endif | ||
|
|
||
| #endif // NO_OBJDETECT |
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.