|
| 1 | +using OpenCvSharp.Dnn; |
| 2 | +using OpenCvSharp.Internal; |
| 3 | + |
| 4 | +namespace OpenCvSharp; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// DNN-based face detector |
| 8 | +/// </summary> |
| 9 | +public class FaceDetectorYN : DisposableCvObject |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A pointer to the shared pointer to the unmanaged object |
| 13 | + /// </summary> |
| 14 | + private IntPtr _sharedPtr; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Creates an instance of this class with given parameters. |
| 18 | + /// </summary> |
| 19 | + /// <param name="model">The path to the requested model</param> |
| 20 | + /// <param name="config">The path to the config file for compability, which is not requested for ONNX models</param> |
| 21 | + /// <param name="inputSize">The size of the input image</param> |
| 22 | + /// <param name="scoreThreshold">The threshold to filter out bounding boxes of score smaller than the given value</param> |
| 23 | + /// <param name="nmsThreshold">The threshold to suppress bounding boxes of IoU bigger than the given value</param> |
| 24 | + /// <param name="topK">Keep top K bboxes before NMS</param> |
| 25 | + /// <param name="backendId">The id of backend</param> |
| 26 | + /// <param name="targetId">The id of target device</param> |
| 27 | + public FaceDetectorYN( |
| 28 | + string model, |
| 29 | + string config, |
| 30 | + Size inputSize, |
| 31 | + float scoreThreshold = 0.9f, |
| 32 | + float nmsThreshold = 0.3f, |
| 33 | + int topK = 5000, |
| 34 | + Backend backendId = Backend.DEFAULT, |
| 35 | + Target targetId = Target.CPU) |
| 36 | + { |
| 37 | + using StdString csModel = new(model); |
| 38 | + using StdString csConfig = new(config); |
| 39 | + |
| 40 | + ptr = NativeMethods.cveFaceDetectorYNCreate( |
| 41 | + csModel.CvPtr, |
| 42 | + csConfig.CvPtr, |
| 43 | + ref inputSize, |
| 44 | + scoreThreshold, |
| 45 | + nmsThreshold, |
| 46 | + topK, |
| 47 | + backendId, |
| 48 | + targetId, |
| 49 | + ref _sharedPtr |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// A simple interface to detect face from given image. |
| 55 | + /// </summary> |
| 56 | + /// <param name="image">An image to detect</param> |
| 57 | + /// <param name="faces">Detection results stored in a Mat</param> |
| 58 | + /// <returns>1 if detection is successful, 0 otherwise.</returns> |
| 59 | + public int Detect(Mat image, Mat faces) |
| 60 | + { |
| 61 | + using InputArray iaImage = new(image); |
| 62 | + using OutputArray oaFaces = new(faces); |
| 63 | + return NativeMethods.cveFaceDetectorYNDetect(ptr, iaImage.CvPtr, oaFaces.CvPtr); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Release the unmanaged memory associated with this FaceDetectorYN |
| 68 | + /// </summary> |
| 69 | + protected override void DisposeUnmanaged() |
| 70 | + { |
| 71 | + if (!IntPtr.Zero.Equals(_sharedPtr)) |
| 72 | + { |
| 73 | + NativeMethods.cveFaceDetectorYNRelease(ref _sharedPtr); |
| 74 | + _sharedPtr = IntPtr.Zero; |
| 75 | + } |
| 76 | + |
| 77 | + base.DisposeUnmanaged(); |
| 78 | + } |
| 79 | +} |
0 commit comments