FaceRecognizer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. 
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.UtilsModule;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace OpenCVForUnity.FaceModule {
  8. // C++: class FaceRecognizer
  9. //javadoc: FaceRecognizer
  10. public class FaceRecognizer : Algorithm {
  11. protected override void Dispose(bool disposing) {
  12. #if (UNITY_ANDROID && !UNITY_EDITOR)
  13. try {
  14. if (disposing) {
  15. }
  16. if (IsEnabledDispose) {
  17. if (nativeObj != IntPtr.Zero)
  18. face_FaceRecognizer_delete(nativeObj);
  19. nativeObj = IntPtr.Zero;
  20. }
  21. } finally {
  22. base.Dispose(disposing);
  23. }
  24. #else
  25. return;
  26. #endif
  27. }
  28. protected internal FaceRecognizer(IntPtr addr) : base(addr) { }
  29. // internal usage only
  30. public static new FaceRecognizer __fromPtr__(IntPtr addr) { return new FaceRecognizer(addr); }
  31. //
  32. // C++: String cv::face::FaceRecognizer::getLabelInfo(int label)
  33. //
  34. //javadoc: FaceRecognizer::getLabelInfo(label)
  35. public string getLabelInfo(int label) {
  36. ThrowIfDisposed();
  37. #if (UNITY_ANDROID && !UNITY_EDITOR)
  38. string retVal = Marshal.PtrToStringAnsi(face_FaceRecognizer_getLabelInfo_10(nativeObj, label));
  39. return retVal;
  40. #else
  41. return null;
  42. #endif
  43. }
  44. //
  45. // C++: int cv::face::FaceRecognizer::predict(Mat src)
  46. //
  47. //javadoc: FaceRecognizer::predict_label(src)
  48. public int predict_label(Mat src) {
  49. ThrowIfDisposed();
  50. if (src != null) src.ThrowIfDisposed();
  51. #if (UNITY_ANDROID && !UNITY_EDITOR)
  52. int retVal = face_FaceRecognizer_predict_1label_10(nativeObj, src.nativeObj);
  53. return retVal;
  54. #else
  55. return -1;
  56. #endif
  57. }
  58. //
  59. // C++: vector_int cv::face::FaceRecognizer::getLabelsByString(String str)
  60. //
  61. //javadoc: FaceRecognizer::getLabelsByString(str)
  62. public MatOfInt getLabelsByString(string str) {
  63. ThrowIfDisposed();
  64. #if (UNITY_ANDROID && !UNITY_EDITOR)
  65. MatOfInt retVal = MatOfInt.fromNativeAddr(face_FaceRecognizer_getLabelsByString_10(nativeObj, str));
  66. return retVal;
  67. #else
  68. return null;
  69. #endif
  70. }
  71. //
  72. // C++: void cv::face::FaceRecognizer::predict(Mat src, Ptr_PredictCollector collector)
  73. //
  74. //javadoc: FaceRecognizer::predict_collect(src, collector)
  75. public void predict_collect(Mat src, PredictCollector collector) {
  76. ThrowIfDisposed();
  77. if (src != null) src.ThrowIfDisposed();
  78. if (collector != null) collector.ThrowIfDisposed();
  79. #if (UNITY_ANDROID && !UNITY_EDITOR)
  80. face_FaceRecognizer_predict_1collect_10(nativeObj, src.nativeObj, collector.getNativeObjAddr());
  81. return;
  82. #else
  83. return;
  84. #endif
  85. }
  86. //
  87. // C++: void cv::face::FaceRecognizer::predict(Mat src, int& label, double& confidence)
  88. //
  89. //javadoc: FaceRecognizer::predict(src, label, confidence)
  90. public void predict(Mat src, int[] label, double[] confidence) {
  91. ThrowIfDisposed();
  92. if (src != null) src.ThrowIfDisposed();
  93. #if (UNITY_ANDROID && !UNITY_EDITOR)
  94. double[] label_out = new double[1];
  95. double[] confidence_out = new double[1];
  96. face_FaceRecognizer_predict_10(nativeObj, src.nativeObj, label_out, confidence_out);
  97. if (label != null) label[0] = (int)label_out[0];
  98. if (confidence != null) confidence[0] = (double)confidence_out[0];
  99. return;
  100. #else
  101. return;
  102. #endif
  103. }
  104. //
  105. // C++: void cv::face::FaceRecognizer::read(String filename)
  106. //
  107. //javadoc: FaceRecognizer::read(filename)
  108. public void read(string filename) {
  109. ThrowIfDisposed();
  110. #if (UNITY_ANDROID && !UNITY_EDITOR)
  111. face_FaceRecognizer_read_10(nativeObj, filename);
  112. return;
  113. #else
  114. return;
  115. #endif
  116. }
  117. //
  118. // C++: void cv::face::FaceRecognizer::setLabelInfo(int label, String strInfo)
  119. //
  120. //javadoc: FaceRecognizer::setLabelInfo(label, strInfo)
  121. public void setLabelInfo(int label, string strInfo) {
  122. ThrowIfDisposed();
  123. #if (UNITY_ANDROID && !UNITY_EDITOR)
  124. face_FaceRecognizer_setLabelInfo_10(nativeObj, label, strInfo);
  125. return;
  126. #else
  127. return;
  128. #endif
  129. }
  130. //
  131. // C++: void cv::face::FaceRecognizer::train(vector_Mat src, Mat labels)
  132. //
  133. //javadoc: FaceRecognizer::train(src, labels)
  134. public void train(List<Mat> src, Mat labels) {
  135. ThrowIfDisposed();
  136. if (labels != null) labels.ThrowIfDisposed();
  137. #if (UNITY_ANDROID && !UNITY_EDITOR)
  138. Mat src_mat = Converters.vector_Mat_to_Mat(src);
  139. face_FaceRecognizer_train_10(nativeObj, src_mat.nativeObj, labels.nativeObj);
  140. return;
  141. #else
  142. return;
  143. #endif
  144. }
  145. //
  146. // C++: void cv::face::FaceRecognizer::update(vector_Mat src, Mat labels)
  147. //
  148. //javadoc: FaceRecognizer::update(src, labels)
  149. public void update(List<Mat> src, Mat labels) {
  150. ThrowIfDisposed();
  151. if (labels != null) labels.ThrowIfDisposed();
  152. #if (UNITY_ANDROID && !UNITY_EDITOR)
  153. Mat src_mat = Converters.vector_Mat_to_Mat(src);
  154. face_FaceRecognizer_update_10(nativeObj, src_mat.nativeObj, labels.nativeObj);
  155. return;
  156. #else
  157. return;
  158. #endif
  159. }
  160. //
  161. // C++: void cv::face::FaceRecognizer::write(String filename)
  162. //
  163. //javadoc: FaceRecognizer::write(filename)
  164. public void write(string filename) {
  165. ThrowIfDisposed();
  166. #if (UNITY_ANDROID && !UNITY_EDITOR)
  167. face_FaceRecognizer_write_10(nativeObj, filename);
  168. return;
  169. #else
  170. return;
  171. #endif
  172. }
  173. #if (UNITY_ANDROID && !UNITY_EDITOR)
  174. const string LIBNAME = "opencvforunity";
  175. // C++: String cv::face::FaceRecognizer::getLabelInfo(int label)
  176. [DllImport(LIBNAME)]
  177. private static extern IntPtr face_FaceRecognizer_getLabelInfo_10(IntPtr nativeObj, int label);
  178. // C++: int cv::face::FaceRecognizer::predict(Mat src)
  179. [DllImport(LIBNAME)]
  180. private static extern int face_FaceRecognizer_predict_1label_10(IntPtr nativeObj, IntPtr src_nativeObj);
  181. // C++: vector_int cv::face::FaceRecognizer::getLabelsByString(String str)
  182. [DllImport(LIBNAME)]
  183. private static extern IntPtr face_FaceRecognizer_getLabelsByString_10(IntPtr nativeObj, string str);
  184. // C++: void cv::face::FaceRecognizer::predict(Mat src, Ptr_PredictCollector collector)
  185. [DllImport(LIBNAME)]
  186. private static extern void face_FaceRecognizer_predict_1collect_10(IntPtr nativeObj, IntPtr src_nativeObj, IntPtr collector_nativeObj);
  187. // C++: void cv::face::FaceRecognizer::predict(Mat src, int& label, double& confidence)
  188. [DllImport(LIBNAME)]
  189. private static extern void face_FaceRecognizer_predict_10(IntPtr nativeObj, IntPtr src_nativeObj, double[] label_out, double[] confidence_out);
  190. // C++: void cv::face::FaceRecognizer::read(String filename)
  191. [DllImport(LIBNAME)]
  192. private static extern void face_FaceRecognizer_read_10(IntPtr nativeObj, string filename);
  193. // C++: void cv::face::FaceRecognizer::setLabelInfo(int label, String strInfo)
  194. [DllImport(LIBNAME)]
  195. private static extern void face_FaceRecognizer_setLabelInfo_10(IntPtr nativeObj, int label, string strInfo);
  196. // C++: void cv::face::FaceRecognizer::train(vector_Mat src, Mat labels)
  197. [DllImport(LIBNAME)]
  198. private static extern void face_FaceRecognizer_train_10(IntPtr nativeObj, IntPtr src_mat_nativeObj, IntPtr labels_nativeObj);
  199. // C++: void cv::face::FaceRecognizer::update(vector_Mat src, Mat labels)
  200. [DllImport(LIBNAME)]
  201. private static extern void face_FaceRecognizer_update_10(IntPtr nativeObj, IntPtr src_mat_nativeObj, IntPtr labels_nativeObj);
  202. // C++: void cv::face::FaceRecognizer::write(String filename)
  203. [DllImport(LIBNAME)]
  204. private static extern void face_FaceRecognizer_write_10(IntPtr nativeObj, string filename);
  205. // native support for java finalize()
  206. [DllImport(LIBNAME)]
  207. private static extern void face_FaceRecognizer_delete(IntPtr nativeObj);
  208. #endif
  209. }
  210. }