|
1 | 1 | using OpenCvSharp.Dnn; |
2 | 2 | using OpenCvSharp.Internal; |
| 3 | +using OpenCvSharp.Internal.Vectors; |
3 | 4 |
|
4 | 5 | namespace OpenCvSharp; |
5 | 6 |
|
@@ -56,6 +57,152 @@ public static FaceDetectorYN Create( |
56 | 57 | return new FaceDetectorYN(smartPtr, rawPtr); |
57 | 58 | } |
58 | 59 |
|
| 60 | + /// <summary> |
| 61 | + /// Creates an instance of this class from buffers containing the model weights and configuration. |
| 62 | + /// </summary> |
| 63 | + /// <param name="framework">Name of the framework.</param> |
| 64 | + /// <param name="bufferModel">A buffer containing the binary model weights.</param> |
| 65 | + /// <param name="bufferConfig">A buffer containing the network configuration.</param> |
| 66 | + /// <param name="inputSize">The size of the input image.</param> |
| 67 | + /// <param name="scoreThreshold">The threshold to filter out bounding boxes of score smaller than the given value.</param> |
| 68 | + /// <param name="nmsThreshold">The threshold to suppress bounding boxes of IoU bigger than the given value.</param> |
| 69 | + /// <param name="topK">Keep top K bounding boxes before NMS.</param> |
| 70 | + /// <param name="backendId">The id of backend.</param> |
| 71 | + /// <param name="targetId">The id of target device.</param> |
| 72 | + public static FaceDetectorYN Create( |
| 73 | + string framework, |
| 74 | + byte[] bufferModel, |
| 75 | + byte[] bufferConfig, |
| 76 | + Size inputSize, |
| 77 | + float scoreThreshold = 0.9f, |
| 78 | + float nmsThreshold = 0.3f, |
| 79 | + int topK = 5000, |
| 80 | + Backend backendId = Backend.DEFAULT, |
| 81 | + Target targetId = Target.CPU) |
| 82 | + { |
| 83 | + ArgumentNullException.ThrowIfNull(bufferModel); |
| 84 | + ArgumentNullException.ThrowIfNull(bufferConfig); |
| 85 | + |
| 86 | + using StdString csFramework = new(framework); |
| 87 | + using var bufferModelVec = new StdVector<byte>(bufferModel); |
| 88 | + using var bufferConfigVec = new StdVector<byte>(bufferConfig); |
| 89 | + |
| 90 | + NativeMethods.HandleException( |
| 91 | + NativeMethods.objdetect_FaceDetectorYN_create_buffer( |
| 92 | + csFramework.CvPtr, |
| 93 | + bufferModelVec.CvPtr, |
| 94 | + bufferConfigVec.CvPtr, |
| 95 | + inputSize, |
| 96 | + scoreThreshold, |
| 97 | + nmsThreshold, |
| 98 | + topK, |
| 99 | + (int)backendId, |
| 100 | + (int)targetId, |
| 101 | + out var smartPtr)); |
| 102 | + NativeMethods.HandleException( |
| 103 | + NativeMethods.objdetect_Ptr_FaceDetectorYN_get(smartPtr, out var rawPtr)); |
| 104 | + return new FaceDetectorYN(smartPtr, rawPtr); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Sets the network input size, overwriting the size specified when the detector was created. |
| 109 | + /// </summary> |
| 110 | + /// <param name="inputSize">The size of the input image.</param> |
| 111 | + public void SetInputSize(Size inputSize) |
| 112 | + { |
| 113 | + ThrowIfDisposed(); |
| 114 | + NativeMethods.HandleException( |
| 115 | + NativeMethods.objdetect_FaceDetectorYN_setInputSize(Handle, inputSize)); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Sets the network input size, overwriting the size specified when the detector was created. |
| 120 | + /// </summary> |
| 121 | + /// <param name="width">The input image width.</param> |
| 122 | + /// <param name="height">The input image height.</param> |
| 123 | + public void SetInputSize(int width, int height) => SetInputSize(new Size(width, height)); |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Gets the network input size. |
| 127 | + /// </summary> |
| 128 | + /// <returns>The size of the input image.</returns> |
| 129 | + public Size GetInputSize() |
| 130 | + { |
| 131 | + ThrowIfDisposed(); |
| 132 | + NativeMethods.HandleException( |
| 133 | + NativeMethods.objdetect_FaceDetectorYN_getInputSize(Handle, out var result)); |
| 134 | + return result; |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Sets the score threshold used to filter bounding boxes. |
| 139 | + /// </summary> |
| 140 | + /// <param name="scoreThreshold">The threshold for filtering bounding boxes.</param> |
| 141 | + public void SetScoreThreshold(float scoreThreshold) |
| 142 | + { |
| 143 | + ThrowIfDisposed(); |
| 144 | + NativeMethods.HandleException( |
| 145 | + NativeMethods.objdetect_FaceDetectorYN_setScoreThreshold(Handle, scoreThreshold)); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Gets the score threshold used to filter bounding boxes. |
| 150 | + /// </summary> |
| 151 | + /// <returns>The score threshold.</returns> |
| 152 | + public float GetScoreThreshold() |
| 153 | + { |
| 154 | + ThrowIfDisposed(); |
| 155 | + NativeMethods.HandleException( |
| 156 | + NativeMethods.objdetect_FaceDetectorYN_getScoreThreshold(Handle, out var result)); |
| 157 | + return result; |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Sets the non-maximum-suppression threshold. |
| 162 | + /// </summary> |
| 163 | + /// <param name="nmsThreshold">The threshold for NMS.</param> |
| 164 | + public void SetNMSThreshold(float nmsThreshold) |
| 165 | + { |
| 166 | + ThrowIfDisposed(); |
| 167 | + NativeMethods.HandleException( |
| 168 | + NativeMethods.objdetect_FaceDetectorYN_setNMSThreshold(Handle, nmsThreshold)); |
| 169 | + } |
| 170 | + |
| 171 | + /// <summary> |
| 172 | + /// Gets the non-maximum-suppression threshold. |
| 173 | + /// </summary> |
| 174 | + /// <returns>The threshold for NMS.</returns> |
| 175 | + public float GetNMSThreshold() |
| 176 | + { |
| 177 | + ThrowIfDisposed(); |
| 178 | + NativeMethods.HandleException( |
| 179 | + NativeMethods.objdetect_FaceDetectorYN_getNMSThreshold(Handle, out var result)); |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// Sets the number of bounding boxes preserved before NMS. |
| 185 | + /// </summary> |
| 186 | + /// <param name="topK">The number of bounding boxes to preserve.</param> |
| 187 | + public void SetTopK(int topK) |
| 188 | + { |
| 189 | + ThrowIfDisposed(); |
| 190 | + NativeMethods.HandleException( |
| 191 | + NativeMethods.objdetect_FaceDetectorYN_setTopK(Handle, topK)); |
| 192 | + } |
| 193 | + |
| 194 | + /// <summary> |
| 195 | + /// Gets the number of bounding boxes preserved before NMS. |
| 196 | + /// </summary> |
| 197 | + /// <returns>The number of bounding boxes to preserve.</returns> |
| 198 | + public int GetTopK() |
| 199 | + { |
| 200 | + ThrowIfDisposed(); |
| 201 | + NativeMethods.HandleException( |
| 202 | + NativeMethods.objdetect_FaceDetectorYN_getTopK(Handle, out var result)); |
| 203 | + return result; |
| 204 | + } |
| 205 | + |
59 | 206 | /// <summary> |
60 | 207 | /// A simple interface to detect face from given image. |
61 | 208 | /// </summary> |
|
0 commit comments