FaceDetectorYN.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.ObjdetectModule
  7. {
  8. // C++: class FaceDetectorYN
  9. /**
  10. * DNN-based face detector
  11. *
  12. * model download link: https://github.com/opencv/opencv_zoo/tree/master/models/face_detection_yunet
  13. */
  14. public class FaceDetectorYN : DisposableOpenCVObject
  15. {
  16. protected override void Dispose(bool disposing)
  17. {
  18. try
  19. {
  20. if (disposing)
  21. {
  22. }
  23. if (IsEnabledDispose)
  24. {
  25. if (nativeObj != IntPtr.Zero)
  26. objdetect_FaceDetectorYN_delete(nativeObj);
  27. nativeObj = IntPtr.Zero;
  28. }
  29. }
  30. finally
  31. {
  32. base.Dispose(disposing);
  33. }
  34. }
  35. protected internal FaceDetectorYN(IntPtr addr) : base(addr) { }
  36. public IntPtr getNativeObjAddr() { return nativeObj; }
  37. // internal usage only
  38. public static FaceDetectorYN __fromPtr__(IntPtr addr) { return new FaceDetectorYN(addr); }
  39. //
  40. // C++: void cv::FaceDetectorYN::setInputSize(Size input_size)
  41. //
  42. /**
  43. * Set the size for the network input, which overwrites the input size of creating model. Call this method when the size of input image does not match the input size when creating model
  44. *
  45. * param input_size the size of the input image
  46. */
  47. public void setInputSize(Size input_size)
  48. {
  49. ThrowIfDisposed();
  50. objdetect_FaceDetectorYN_setInputSize_10(nativeObj, input_size.width, input_size.height);
  51. }
  52. //
  53. // C++: Size cv::FaceDetectorYN::getInputSize()
  54. //
  55. public Size getInputSize()
  56. {
  57. ThrowIfDisposed();
  58. double[] tmpArray = new double[2];
  59. objdetect_FaceDetectorYN_getInputSize_10(nativeObj, tmpArray);
  60. Size retVal = new Size(tmpArray);
  61. return retVal;
  62. }
  63. //
  64. // C++: void cv::FaceDetectorYN::setScoreThreshold(float score_threshold)
  65. //
  66. /**
  67. * Set the score threshold to filter out bounding boxes of score less than the given value
  68. *
  69. * param score_threshold threshold for filtering out bounding boxes
  70. */
  71. public void setScoreThreshold(float score_threshold)
  72. {
  73. ThrowIfDisposed();
  74. objdetect_FaceDetectorYN_setScoreThreshold_10(nativeObj, score_threshold);
  75. }
  76. //
  77. // C++: float cv::FaceDetectorYN::getScoreThreshold()
  78. //
  79. public float getScoreThreshold()
  80. {
  81. ThrowIfDisposed();
  82. return objdetect_FaceDetectorYN_getScoreThreshold_10(nativeObj);
  83. }
  84. //
  85. // C++: void cv::FaceDetectorYN::setNMSThreshold(float nms_threshold)
  86. //
  87. /**
  88. * Set the Non-maximum-suppression threshold to suppress bounding boxes that have IoU greater than the given value
  89. *
  90. * param nms_threshold threshold for NMS operation
  91. */
  92. public void setNMSThreshold(float nms_threshold)
  93. {
  94. ThrowIfDisposed();
  95. objdetect_FaceDetectorYN_setNMSThreshold_10(nativeObj, nms_threshold);
  96. }
  97. //
  98. // C++: float cv::FaceDetectorYN::getNMSThreshold()
  99. //
  100. public float getNMSThreshold()
  101. {
  102. ThrowIfDisposed();
  103. return objdetect_FaceDetectorYN_getNMSThreshold_10(nativeObj);
  104. }
  105. //
  106. // C++: void cv::FaceDetectorYN::setTopK(int top_k)
  107. //
  108. /**
  109. * Set the number of bounding boxes preserved before NMS
  110. *
  111. * param top_k the number of bounding boxes to preserve from top rank based on score
  112. */
  113. public void setTopK(int top_k)
  114. {
  115. ThrowIfDisposed();
  116. objdetect_FaceDetectorYN_setTopK_10(nativeObj, top_k);
  117. }
  118. //
  119. // C++: int cv::FaceDetectorYN::getTopK()
  120. //
  121. public int getTopK()
  122. {
  123. ThrowIfDisposed();
  124. return objdetect_FaceDetectorYN_getTopK_10(nativeObj);
  125. }
  126. //
  127. // C++: int cv::FaceDetectorYN::detect(Mat image, Mat& faces)
  128. //
  129. /**
  130. * Detects faces in the input image. Following is an example output.
  131. *
  132. * ![image](pics/lena-face-detection.jpg)
  133. *
  134. * param image an image to detect
  135. * param faces detection results stored in a 2D cv::Mat of shape [num_faces, 15]
  136. * - 0-1: x, y of bbox top left corner
  137. * - 2-3: width, height of bbox
  138. * - 4-5: x, y of right eye (blue point in the example image)
  139. * - 6-7: x, y of left eye (red point in the example image)
  140. * - 8-9: x, y of nose tip (green point in the example image)
  141. * - 10-11: x, y of right corner of mouth (pink point in the example image)
  142. * - 12-13: x, y of left corner of mouth (yellow point in the example image)
  143. * - 14: face score
  144. * return automatically generated
  145. */
  146. public int detect(Mat image, Mat faces)
  147. {
  148. ThrowIfDisposed();
  149. if (image != null) image.ThrowIfDisposed();
  150. if (faces != null) faces.ThrowIfDisposed();
  151. return objdetect_FaceDetectorYN_detect_10(nativeObj, image.nativeObj, faces.nativeObj);
  152. }
  153. //
  154. // C++: static Ptr_FaceDetectorYN cv::FaceDetectorYN::create(String model, String config, Size input_size, float score_threshold = 0.9f, float nms_threshold = 0.3f, int top_k = 5000, int backend_id = 0, int target_id = 0)
  155. //
  156. /**
  157. * Creates an instance of this class with given parameters
  158. *
  159. * param model the path to the requested model
  160. * param config the path to the config file for compability, which is not requested for ONNX models
  161. * param input_size the size of the input image
  162. * param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
  163. * param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
  164. * param top_k keep top K bboxes before NMS
  165. * param backend_id the id of backend
  166. * param target_id the id of target device
  167. * return automatically generated
  168. */
  169. public static FaceDetectorYN create(string model, string config, Size input_size, float score_threshold, float nms_threshold, int top_k, int backend_id, int target_id)
  170. {
  171. return FaceDetectorYN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceDetectorYN_create_10(model, config, input_size.width, input_size.height, score_threshold, nms_threshold, top_k, backend_id, target_id)));
  172. }
  173. /**
  174. * Creates an instance of this class with given parameters
  175. *
  176. * param model the path to the requested model
  177. * param config the path to the config file for compability, which is not requested for ONNX models
  178. * param input_size the size of the input image
  179. * param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
  180. * param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
  181. * param top_k keep top K bboxes before NMS
  182. * param backend_id the id of backend
  183. * return automatically generated
  184. */
  185. public static FaceDetectorYN create(string model, string config, Size input_size, float score_threshold, float nms_threshold, int top_k, int backend_id)
  186. {
  187. return FaceDetectorYN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceDetectorYN_create_11(model, config, input_size.width, input_size.height, score_threshold, nms_threshold, top_k, backend_id)));
  188. }
  189. /**
  190. * Creates an instance of this class with given parameters
  191. *
  192. * param model the path to the requested model
  193. * param config the path to the config file for compability, which is not requested for ONNX models
  194. * param input_size the size of the input image
  195. * param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
  196. * param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
  197. * param top_k keep top K bboxes before NMS
  198. * return automatically generated
  199. */
  200. public static FaceDetectorYN create(string model, string config, Size input_size, float score_threshold, float nms_threshold, int top_k)
  201. {
  202. return FaceDetectorYN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceDetectorYN_create_12(model, config, input_size.width, input_size.height, score_threshold, nms_threshold, top_k)));
  203. }
  204. /**
  205. * Creates an instance of this class with given parameters
  206. *
  207. * param model the path to the requested model
  208. * param config the path to the config file for compability, which is not requested for ONNX models
  209. * param input_size the size of the input image
  210. * param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
  211. * param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
  212. * return automatically generated
  213. */
  214. public static FaceDetectorYN create(string model, string config, Size input_size, float score_threshold, float nms_threshold)
  215. {
  216. return FaceDetectorYN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceDetectorYN_create_13(model, config, input_size.width, input_size.height, score_threshold, nms_threshold)));
  217. }
  218. /**
  219. * Creates an instance of this class with given parameters
  220. *
  221. * param model the path to the requested model
  222. * param config the path to the config file for compability, which is not requested for ONNX models
  223. * param input_size the size of the input image
  224. * param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
  225. * return automatically generated
  226. */
  227. public static FaceDetectorYN create(string model, string config, Size input_size, float score_threshold)
  228. {
  229. return FaceDetectorYN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceDetectorYN_create_14(model, config, input_size.width, input_size.height, score_threshold)));
  230. }
  231. /**
  232. * Creates an instance of this class with given parameters
  233. *
  234. * param model the path to the requested model
  235. * param config the path to the config file for compability, which is not requested for ONNX models
  236. * param input_size the size of the input image
  237. * return automatically generated
  238. */
  239. public static FaceDetectorYN create(string model, string config, Size input_size)
  240. {
  241. return FaceDetectorYN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceDetectorYN_create_15(model, config, input_size.width, input_size.height)));
  242. }
  243. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  244. const string LIBNAME = "__Internal";
  245. #else
  246. const string LIBNAME = "opencvforunity";
  247. #endif
  248. // C++: void cv::FaceDetectorYN::setInputSize(Size input_size)
  249. [DllImport(LIBNAME)]
  250. private static extern void objdetect_FaceDetectorYN_setInputSize_10(IntPtr nativeObj, double input_size_width, double input_size_height);
  251. // C++: Size cv::FaceDetectorYN::getInputSize()
  252. [DllImport(LIBNAME)]
  253. private static extern void objdetect_FaceDetectorYN_getInputSize_10(IntPtr nativeObj, double[] retVal);
  254. // C++: void cv::FaceDetectorYN::setScoreThreshold(float score_threshold)
  255. [DllImport(LIBNAME)]
  256. private static extern void objdetect_FaceDetectorYN_setScoreThreshold_10(IntPtr nativeObj, float score_threshold);
  257. // C++: float cv::FaceDetectorYN::getScoreThreshold()
  258. [DllImport(LIBNAME)]
  259. private static extern float objdetect_FaceDetectorYN_getScoreThreshold_10(IntPtr nativeObj);
  260. // C++: void cv::FaceDetectorYN::setNMSThreshold(float nms_threshold)
  261. [DllImport(LIBNAME)]
  262. private static extern void objdetect_FaceDetectorYN_setNMSThreshold_10(IntPtr nativeObj, float nms_threshold);
  263. // C++: float cv::FaceDetectorYN::getNMSThreshold()
  264. [DllImport(LIBNAME)]
  265. private static extern float objdetect_FaceDetectorYN_getNMSThreshold_10(IntPtr nativeObj);
  266. // C++: void cv::FaceDetectorYN::setTopK(int top_k)
  267. [DllImport(LIBNAME)]
  268. private static extern void objdetect_FaceDetectorYN_setTopK_10(IntPtr nativeObj, int top_k);
  269. // C++: int cv::FaceDetectorYN::getTopK()
  270. [DllImport(LIBNAME)]
  271. private static extern int objdetect_FaceDetectorYN_getTopK_10(IntPtr nativeObj);
  272. // C++: int cv::FaceDetectorYN::detect(Mat image, Mat& faces)
  273. [DllImport(LIBNAME)]
  274. private static extern int objdetect_FaceDetectorYN_detect_10(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr faces_nativeObj);
  275. // C++: static Ptr_FaceDetectorYN cv::FaceDetectorYN::create(String model, String config, Size input_size, float score_threshold = 0.9f, float nms_threshold = 0.3f, int top_k = 5000, int backend_id = 0, int target_id = 0)
  276. [DllImport(LIBNAME)]
  277. private static extern IntPtr objdetect_FaceDetectorYN_create_10(string model, string config, double input_size_width, double input_size_height, float score_threshold, float nms_threshold, int top_k, int backend_id, int target_id);
  278. [DllImport(LIBNAME)]
  279. private static extern IntPtr objdetect_FaceDetectorYN_create_11(string model, string config, double input_size_width, double input_size_height, float score_threshold, float nms_threshold, int top_k, int backend_id);
  280. [DllImport(LIBNAME)]
  281. private static extern IntPtr objdetect_FaceDetectorYN_create_12(string model, string config, double input_size_width, double input_size_height, float score_threshold, float nms_threshold, int top_k);
  282. [DllImport(LIBNAME)]
  283. private static extern IntPtr objdetect_FaceDetectorYN_create_13(string model, string config, double input_size_width, double input_size_height, float score_threshold, float nms_threshold);
  284. [DllImport(LIBNAME)]
  285. private static extern IntPtr objdetect_FaceDetectorYN_create_14(string model, string config, double input_size_width, double input_size_height, float score_threshold);
  286. [DllImport(LIBNAME)]
  287. private static extern IntPtr objdetect_FaceDetectorYN_create_15(string model, string config, double input_size_width, double input_size_height);
  288. // native support for java finalize()
  289. [DllImport(LIBNAME)]
  290. private static extern void objdetect_FaceDetectorYN_delete(IntPtr nativeObj);
  291. }
  292. }