StatModel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.MlModule {
  8. // C++: class StatModel
  9. //javadoc: StatModel
  10. public class StatModel : 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. ml_StatModel_delete(nativeObj);
  19. nativeObj = IntPtr.Zero;
  20. }
  21. } finally {
  22. base.Dispose(disposing);
  23. }
  24. #else
  25. return;
  26. #endif
  27. }
  28. protected internal StatModel(IntPtr addr)
  29. : base(addr) {
  30. }
  31. // internal usage only
  32. public static new StatModel __fromPtr__(IntPtr addr) {
  33. return new StatModel(addr);
  34. }
  35. // C++: enum Flags
  36. public const int UPDATE_MODEL = 1;
  37. public const int RAW_OUTPUT = 1;
  38. public const int COMPRESSED_INPUT = 2;
  39. public const int PREPROCESSED_INPUT = 4;
  40. //
  41. // C++: bool cv::ml::StatModel::empty()
  42. //
  43. //javadoc: StatModel::empty()
  44. public override bool empty() {
  45. ThrowIfDisposed();
  46. #if (UNITY_ANDROID && !UNITY_EDITOR)
  47. bool retVal = ml_StatModel_empty_10(nativeObj);
  48. return retVal;
  49. #else
  50. return false;
  51. #endif
  52. }
  53. //
  54. // C++: bool cv::ml::StatModel::isClassifier()
  55. //
  56. //javadoc: StatModel::isClassifier()
  57. public bool isClassifier() {
  58. ThrowIfDisposed();
  59. #if (UNITY_ANDROID && !UNITY_EDITOR)
  60. bool retVal = ml_StatModel_isClassifier_10(nativeObj);
  61. return retVal;
  62. #else
  63. return false;
  64. #endif
  65. }
  66. //
  67. // C++: bool cv::ml::StatModel::isTrained()
  68. //
  69. //javadoc: StatModel::isTrained()
  70. public bool isTrained() {
  71. ThrowIfDisposed();
  72. #if (UNITY_ANDROID && !UNITY_EDITOR)
  73. bool retVal = ml_StatModel_isTrained_10(nativeObj);
  74. return retVal;
  75. #else
  76. return false;
  77. #endif
  78. }
  79. //
  80. // C++: bool cv::ml::StatModel::train(Mat samples, int layout, Mat responses)
  81. //
  82. //javadoc: StatModel::train(samples, layout, responses)
  83. public bool train(Mat samples, int layout, Mat responses) {
  84. ThrowIfDisposed();
  85. if (samples != null)
  86. samples.ThrowIfDisposed();
  87. if (responses != null)
  88. responses.ThrowIfDisposed();
  89. #if (UNITY_ANDROID && !UNITY_EDITOR)
  90. bool retVal = ml_StatModel_train_10(nativeObj, samples.nativeObj, layout, responses.nativeObj);
  91. return retVal;
  92. #else
  93. return false;
  94. #endif
  95. }
  96. //
  97. // C++: bool cv::ml::StatModel::train(Ptr_TrainData trainData, int flags = 0)
  98. //
  99. //javadoc: StatModel::train(trainData, flags)
  100. public bool train(TrainData trainData, int flags) {
  101. ThrowIfDisposed();
  102. if (trainData != null)
  103. trainData.ThrowIfDisposed();
  104. #if (UNITY_ANDROID && !UNITY_EDITOR)
  105. bool retVal = ml_StatModel_train_11(nativeObj, trainData.getNativeObjAddr(), flags);
  106. return retVal;
  107. #else
  108. return false;
  109. #endif
  110. }
  111. //javadoc: StatModel::train(trainData)
  112. public bool train(TrainData trainData) {
  113. ThrowIfDisposed();
  114. if (trainData != null)
  115. trainData.ThrowIfDisposed();
  116. #if (UNITY_ANDROID && !UNITY_EDITOR)
  117. bool retVal = ml_StatModel_train_12(nativeObj, trainData.getNativeObjAddr());
  118. return retVal;
  119. #else
  120. return false;
  121. #endif
  122. }
  123. //
  124. // C++: float cv::ml::StatModel::calcError(Ptr_TrainData data, bool test, Mat& resp)
  125. //
  126. //javadoc: StatModel::calcError(data, test, resp)
  127. public float calcError(TrainData data, bool test, Mat resp) {
  128. ThrowIfDisposed();
  129. if (data != null)
  130. data.ThrowIfDisposed();
  131. if (resp != null)
  132. resp.ThrowIfDisposed();
  133. #if (UNITY_ANDROID && !UNITY_EDITOR)
  134. float retVal = ml_StatModel_calcError_10(nativeObj, data.getNativeObjAddr(), test, resp.nativeObj);
  135. return retVal;
  136. #else
  137. return -1;
  138. #endif
  139. }
  140. //
  141. // C++: float cv::ml::StatModel::predict(Mat samples, Mat& results = Mat(), int flags = 0)
  142. //
  143. //javadoc: StatModel::predict(samples, results, flags)
  144. public virtual float predict(Mat samples, Mat results, int flags) {
  145. ThrowIfDisposed();
  146. if (samples != null)
  147. samples.ThrowIfDisposed();
  148. if (results != null)
  149. results.ThrowIfDisposed();
  150. #if (UNITY_ANDROID && !UNITY_EDITOR)
  151. float retVal = ml_StatModel_predict_10(nativeObj, samples.nativeObj, results.nativeObj, flags);
  152. return retVal;
  153. #else
  154. return -1;
  155. #endif
  156. }
  157. //javadoc: StatModel::predict(samples, results)
  158. public virtual float predict(Mat samples, Mat results) {
  159. ThrowIfDisposed();
  160. if (samples != null)
  161. samples.ThrowIfDisposed();
  162. if (results != null)
  163. results.ThrowIfDisposed();
  164. #if (UNITY_ANDROID && !UNITY_EDITOR)
  165. float retVal = ml_StatModel_predict_11(nativeObj, samples.nativeObj, results.nativeObj);
  166. return retVal;
  167. #else
  168. return -1;
  169. #endif
  170. }
  171. //javadoc: StatModel::predict(samples)
  172. public virtual float predict(Mat samples) {
  173. ThrowIfDisposed();
  174. if (samples != null)
  175. samples.ThrowIfDisposed();
  176. #if (UNITY_ANDROID && !UNITY_EDITOR)
  177. float retVal = ml_StatModel_predict_12(nativeObj, samples.nativeObj);
  178. return retVal;
  179. #else
  180. return -1;
  181. #endif
  182. }
  183. //
  184. // C++: int cv::ml::StatModel::getVarCount()
  185. //
  186. //javadoc: StatModel::getVarCount()
  187. public int getVarCount() {
  188. ThrowIfDisposed();
  189. #if (UNITY_ANDROID && !UNITY_EDITOR)
  190. int retVal = ml_StatModel_getVarCount_10(nativeObj);
  191. return retVal;
  192. #else
  193. return -1;
  194. #endif
  195. }
  196. #if (UNITY_ANDROID && !UNITY_EDITOR)
  197. const string LIBNAME = "opencvforunity";
  198. // C++: bool cv::ml::StatModel::empty()
  199. [DllImport(LIBNAME)]
  200. private static extern bool ml_StatModel_empty_10(IntPtr nativeObj);
  201. // C++: bool cv::ml::StatModel::isClassifier()
  202. [DllImport(LIBNAME)]
  203. private static extern bool ml_StatModel_isClassifier_10(IntPtr nativeObj);
  204. // C++: bool cv::ml::StatModel::isTrained()
  205. [DllImport(LIBNAME)]
  206. private static extern bool ml_StatModel_isTrained_10(IntPtr nativeObj);
  207. // C++: bool cv::ml::StatModel::train(Mat samples, int layout, Mat responses)
  208. [DllImport(LIBNAME)]
  209. private static extern bool ml_StatModel_train_10(IntPtr nativeObj, IntPtr samples_nativeObj, int layout, IntPtr responses_nativeObj);
  210. // C++: bool cv::ml::StatModel::train(Ptr_TrainData trainData, int flags = 0)
  211. [DllImport(LIBNAME)]
  212. private static extern bool ml_StatModel_train_11(IntPtr nativeObj, IntPtr trainData_nativeObj, int flags);
  213. [DllImport(LIBNAME)]
  214. private static extern bool ml_StatModel_train_12(IntPtr nativeObj, IntPtr trainData_nativeObj);
  215. // C++: float cv::ml::StatModel::calcError(Ptr_TrainData data, bool test, Mat& resp)
  216. [DllImport(LIBNAME)]
  217. private static extern float ml_StatModel_calcError_10(IntPtr nativeObj, IntPtr data_nativeObj, bool test, IntPtr resp_nativeObj);
  218. // C++: float cv::ml::StatModel::predict(Mat samples, Mat& results = Mat(), int flags = 0)
  219. [DllImport(LIBNAME)]
  220. private static extern float ml_StatModel_predict_10(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj, int flags);
  221. [DllImport(LIBNAME)]
  222. private static extern float ml_StatModel_predict_11(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj);
  223. [DllImport(LIBNAME)]
  224. private static extern float ml_StatModel_predict_12(IntPtr nativeObj, IntPtr samples_nativeObj);
  225. // C++: int cv::ml::StatModel::getVarCount()
  226. [DllImport(LIBNAME)]
  227. private static extern int ml_StatModel_getVarCount_10(IntPtr nativeObj);
  228. // native support for java finalize()
  229. [DllImport(LIBNAME)]
  230. private static extern void ml_StatModel_delete(IntPtr nativeObj);
  231. #endif
  232. }
  233. }