FaceRecognizer.cs 9.3 KB

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