FaceRecognizerSF.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 FaceRecognizerSF
  9. /**
  10. * DNN-based face recognizer
  11. *
  12. * model download link: https://github.com/opencv/opencv_zoo/tree/master/models/face_recognition_sface
  13. */
  14. public class FaceRecognizerSF : 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_FaceRecognizerSF_delete(nativeObj);
  27. nativeObj = IntPtr.Zero;
  28. }
  29. }
  30. finally
  31. {
  32. base.Dispose(disposing);
  33. }
  34. }
  35. protected internal FaceRecognizerSF(IntPtr addr) : base(addr) { }
  36. public IntPtr getNativeObjAddr() { return nativeObj; }
  37. // internal usage only
  38. public static FaceRecognizerSF __fromPtr__(IntPtr addr) { return new FaceRecognizerSF(addr); }
  39. // C++: enum cv.FaceRecognizerSF.DisType
  40. public const int FR_COSINE = 0;
  41. public const int FR_NORM_L2 = 1;
  42. //
  43. // C++: void cv::FaceRecognizerSF::alignCrop(Mat src_img, Mat face_box, Mat& aligned_img)
  44. //
  45. /**
  46. * Aligning image to put face on the standard position
  47. * param src_img input image
  48. * param face_box the detection result used for indicate face in input image
  49. * param aligned_img output aligned image
  50. */
  51. public void alignCrop(Mat src_img, Mat face_box, Mat aligned_img)
  52. {
  53. ThrowIfDisposed();
  54. if (src_img != null) src_img.ThrowIfDisposed();
  55. if (face_box != null) face_box.ThrowIfDisposed();
  56. if (aligned_img != null) aligned_img.ThrowIfDisposed();
  57. objdetect_FaceRecognizerSF_alignCrop_10(nativeObj, src_img.nativeObj, face_box.nativeObj, aligned_img.nativeObj);
  58. }
  59. //
  60. // C++: void cv::FaceRecognizerSF::feature(Mat aligned_img, Mat& face_feature)
  61. //
  62. /**
  63. * Extracting face feature from aligned image
  64. * param aligned_img input aligned image
  65. * param face_feature output face feature
  66. */
  67. public void feature(Mat aligned_img, Mat face_feature)
  68. {
  69. ThrowIfDisposed();
  70. if (aligned_img != null) aligned_img.ThrowIfDisposed();
  71. if (face_feature != null) face_feature.ThrowIfDisposed();
  72. objdetect_FaceRecognizerSF_feature_10(nativeObj, aligned_img.nativeObj, face_feature.nativeObj);
  73. }
  74. //
  75. // C++: double cv::FaceRecognizerSF::match(Mat face_feature1, Mat face_feature2, int dis_type = FaceRecognizerSF::FR_COSINE)
  76. //
  77. /**
  78. * Calculating the distance between two face features
  79. * param face_feature1 the first input feature
  80. * param face_feature2 the second input feature of the same size and the same type as face_feature1
  81. * param dis_type defining the similarity with optional values "FR_OSINE" or "FR_NORM_L2"
  82. * return automatically generated
  83. */
  84. public double match(Mat face_feature1, Mat face_feature2, int dis_type)
  85. {
  86. ThrowIfDisposed();
  87. if (face_feature1 != null) face_feature1.ThrowIfDisposed();
  88. if (face_feature2 != null) face_feature2.ThrowIfDisposed();
  89. return objdetect_FaceRecognizerSF_match_10(nativeObj, face_feature1.nativeObj, face_feature2.nativeObj, dis_type);
  90. }
  91. /**
  92. * Calculating the distance between two face features
  93. * param face_feature1 the first input feature
  94. * param face_feature2 the second input feature of the same size and the same type as face_feature1
  95. * return automatically generated
  96. */
  97. public double match(Mat face_feature1, Mat face_feature2)
  98. {
  99. ThrowIfDisposed();
  100. if (face_feature1 != null) face_feature1.ThrowIfDisposed();
  101. if (face_feature2 != null) face_feature2.ThrowIfDisposed();
  102. return objdetect_FaceRecognizerSF_match_11(nativeObj, face_feature1.nativeObj, face_feature2.nativeObj);
  103. }
  104. //
  105. // C++: static Ptr_FaceRecognizerSF cv::FaceRecognizerSF::create(String model, String config, int backend_id = 0, int target_id = 0)
  106. //
  107. /**
  108. * Creates an instance of this class with given parameters
  109. * param model the path of the onnx model used for face recognition
  110. * param config the path to the config file for compability, which is not requested for ONNX models
  111. * param backend_id the id of backend
  112. * param target_id the id of target device
  113. * return automatically generated
  114. */
  115. public static FaceRecognizerSF create(string model, string config, int backend_id, int target_id)
  116. {
  117. return FaceRecognizerSF.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceRecognizerSF_create_10(model, config, backend_id, target_id)));
  118. }
  119. /**
  120. * Creates an instance of this class with given parameters
  121. * param model the path of the onnx model used for face recognition
  122. * param config the path to the config file for compability, which is not requested for ONNX models
  123. * param backend_id the id of backend
  124. * return automatically generated
  125. */
  126. public static FaceRecognizerSF create(string model, string config, int backend_id)
  127. {
  128. return FaceRecognizerSF.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceRecognizerSF_create_11(model, config, backend_id)));
  129. }
  130. /**
  131. * Creates an instance of this class with given parameters
  132. * param model the path of the onnx model used for face recognition
  133. * param config the path to the config file for compability, which is not requested for ONNX models
  134. * return automatically generated
  135. */
  136. public static FaceRecognizerSF create(string model, string config)
  137. {
  138. return FaceRecognizerSF.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(objdetect_FaceRecognizerSF_create_12(model, config)));
  139. }
  140. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  141. const string LIBNAME = "__Internal";
  142. #else
  143. const string LIBNAME = "opencvforunity";
  144. #endif
  145. // C++: void cv::FaceRecognizerSF::alignCrop(Mat src_img, Mat face_box, Mat& aligned_img)
  146. [DllImport(LIBNAME)]
  147. private static extern void objdetect_FaceRecognizerSF_alignCrop_10(IntPtr nativeObj, IntPtr src_img_nativeObj, IntPtr face_box_nativeObj, IntPtr aligned_img_nativeObj);
  148. // C++: void cv::FaceRecognizerSF::feature(Mat aligned_img, Mat& face_feature)
  149. [DllImport(LIBNAME)]
  150. private static extern void objdetect_FaceRecognizerSF_feature_10(IntPtr nativeObj, IntPtr aligned_img_nativeObj, IntPtr face_feature_nativeObj);
  151. // C++: double cv::FaceRecognizerSF::match(Mat face_feature1, Mat face_feature2, int dis_type = FaceRecognizerSF::FR_COSINE)
  152. [DllImport(LIBNAME)]
  153. private static extern double objdetect_FaceRecognizerSF_match_10(IntPtr nativeObj, IntPtr face_feature1_nativeObj, IntPtr face_feature2_nativeObj, int dis_type);
  154. [DllImport(LIBNAME)]
  155. private static extern double objdetect_FaceRecognizerSF_match_11(IntPtr nativeObj, IntPtr face_feature1_nativeObj, IntPtr face_feature2_nativeObj);
  156. // C++: static Ptr_FaceRecognizerSF cv::FaceRecognizerSF::create(String model, String config, int backend_id = 0, int target_id = 0)
  157. [DllImport(LIBNAME)]
  158. private static extern IntPtr objdetect_FaceRecognizerSF_create_10(string model, string config, int backend_id, int target_id);
  159. [DllImport(LIBNAME)]
  160. private static extern IntPtr objdetect_FaceRecognizerSF_create_11(string model, string config, int backend_id);
  161. [DllImport(LIBNAME)]
  162. private static extern IntPtr objdetect_FaceRecognizerSF_create_12(string model, string config);
  163. // native support for java finalize()
  164. [DllImport(LIBNAME)]
  165. private static extern void objdetect_FaceRecognizerSF_delete(IntPtr nativeObj);
  166. }
  167. }