Model.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #if !UNITY_WSA_10_0
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.UtilsModule;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace OpenCVForUnity.DnnModule
  8. {
  9. // C++: class Model
  10. /**
  11. * This class is presented high-level API for neural networks.
  12. *
  13. * Model allows to set params for preprocessing input image.
  14. * Model creates net from file with trained weights and config,
  15. * sets preprocessing input and runs forward pass.
  16. */
  17. public class Model : DisposableOpenCVObject
  18. {
  19. protected override void Dispose(bool disposing)
  20. {
  21. try
  22. {
  23. if (disposing)
  24. {
  25. }
  26. if (IsEnabledDispose)
  27. {
  28. if (nativeObj != IntPtr.Zero)
  29. dnn_Model_delete(nativeObj);
  30. nativeObj = IntPtr.Zero;
  31. }
  32. }
  33. finally
  34. {
  35. base.Dispose(disposing);
  36. }
  37. }
  38. protected internal Model(IntPtr addr) : base(addr) { }
  39. public IntPtr getNativeObjAddr() { return nativeObj; }
  40. // internal usage only
  41. public static Model __fromPtr__(IntPtr addr) { return new Model(addr); }
  42. //
  43. // C++: cv::dnn::Model::Model(String model, String config = "")
  44. //
  45. /**
  46. * Create model from deep learning network represented in one of the supported formats.
  47. * An order of {code model} and {code config} arguments does not matter.
  48. * param model Binary file contains trained weights.
  49. * param config Text file contains network configuration.
  50. */
  51. public Model(string model, string config)
  52. {
  53. nativeObj = DisposableObject.ThrowIfNullIntPtr(dnn_Model_Model_10(model, config));
  54. }
  55. /**
  56. * Create model from deep learning network represented in one of the supported formats.
  57. * An order of {code model} and {code config} arguments does not matter.
  58. * param model Binary file contains trained weights.
  59. */
  60. public Model(string model)
  61. {
  62. nativeObj = DisposableObject.ThrowIfNullIntPtr(dnn_Model_Model_11(model));
  63. }
  64. //
  65. // C++: cv::dnn::Model::Model(Net network)
  66. //
  67. /**
  68. * Create model from deep learning network.
  69. * param network Net object.
  70. */
  71. public Model(Net network)
  72. {
  73. if (network != null) network.ThrowIfDisposed();
  74. nativeObj = DisposableObject.ThrowIfNullIntPtr(dnn_Model_Model_12(network.nativeObj));
  75. }
  76. //
  77. // C++: Model cv::dnn::Model::setInputSize(Size size)
  78. //
  79. /**
  80. * Set input size for frame.
  81. * param size New input size.
  82. * <b>Note:</b> If shape of the new blob less than 0, then frame size not change.
  83. * return automatically generated
  84. */
  85. public Model setInputSize(Size size)
  86. {
  87. ThrowIfDisposed();
  88. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setInputSize_10(nativeObj, size.width, size.height)));
  89. }
  90. //
  91. // C++: Model cv::dnn::Model::setInputSize(int width, int height)
  92. //
  93. /**
  94. *
  95. * param width New input width.
  96. * param height New input height.
  97. * return automatically generated
  98. */
  99. public Model setInputSize(int width, int height)
  100. {
  101. ThrowIfDisposed();
  102. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setInputSize_11(nativeObj, width, height)));
  103. }
  104. //
  105. // C++: Model cv::dnn::Model::setInputMean(Scalar mean)
  106. //
  107. /**
  108. * Set mean value for frame.
  109. * param mean Scalar with mean values which are subtracted from channels.
  110. * return automatically generated
  111. */
  112. public Model setInputMean(Scalar mean)
  113. {
  114. ThrowIfDisposed();
  115. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setInputMean_10(nativeObj, mean.val[0], mean.val[1], mean.val[2], mean.val[3])));
  116. }
  117. //
  118. // C++: Model cv::dnn::Model::setInputScale(Scalar scale)
  119. //
  120. /**
  121. * Set scalefactor value for frame.
  122. * param scale Multiplier for frame values.
  123. * return automatically generated
  124. */
  125. public Model setInputScale(Scalar scale)
  126. {
  127. ThrowIfDisposed();
  128. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setInputScale_10(nativeObj, scale.val[0], scale.val[1], scale.val[2], scale.val[3])));
  129. }
  130. //
  131. // C++: Model cv::dnn::Model::setInputCrop(bool crop)
  132. //
  133. /**
  134. * Set flag crop for frame.
  135. * param crop Flag which indicates whether image will be cropped after resize or not.
  136. * return automatically generated
  137. */
  138. public Model setInputCrop(bool crop)
  139. {
  140. ThrowIfDisposed();
  141. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setInputCrop_10(nativeObj, crop)));
  142. }
  143. //
  144. // C++: Model cv::dnn::Model::setInputSwapRB(bool swapRB)
  145. //
  146. /**
  147. * Set flag swapRB for frame.
  148. * param swapRB Flag which indicates that swap first and last channels.
  149. * return automatically generated
  150. */
  151. public Model setInputSwapRB(bool swapRB)
  152. {
  153. ThrowIfDisposed();
  154. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setInputSwapRB_10(nativeObj, swapRB)));
  155. }
  156. //
  157. // C++: void cv::dnn::Model::setInputParams(double scale = 1.0, Size size = Size(), Scalar mean = Scalar(), bool swapRB = false, bool crop = false)
  158. //
  159. /**
  160. * Set preprocessing parameters for frame.
  161. * param size New input size.
  162. * param mean Scalar with mean values which are subtracted from channels.
  163. * param scale Multiplier for frame values.
  164. * param swapRB Flag which indicates that swap first and last channels.
  165. * param crop Flag which indicates whether image will be cropped after resize or not.
  166. * blob(n, c, y, x) = scale * resize( frame(y, x, c) ) - mean(c) )
  167. */
  168. public void setInputParams(double scale, Size size, Scalar mean, bool swapRB, bool crop)
  169. {
  170. ThrowIfDisposed();
  171. dnn_Model_setInputParams_10(nativeObj, scale, size.width, size.height, mean.val[0], mean.val[1], mean.val[2], mean.val[3], swapRB, crop);
  172. }
  173. /**
  174. * Set preprocessing parameters for frame.
  175. * param size New input size.
  176. * param mean Scalar with mean values which are subtracted from channels.
  177. * param scale Multiplier for frame values.
  178. * param swapRB Flag which indicates that swap first and last channels.
  179. * blob(n, c, y, x) = scale * resize( frame(y, x, c) ) - mean(c) )
  180. */
  181. public void setInputParams(double scale, Size size, Scalar mean, bool swapRB)
  182. {
  183. ThrowIfDisposed();
  184. dnn_Model_setInputParams_11(nativeObj, scale, size.width, size.height, mean.val[0], mean.val[1], mean.val[2], mean.val[3], swapRB);
  185. }
  186. /**
  187. * Set preprocessing parameters for frame.
  188. * param size New input size.
  189. * param mean Scalar with mean values which are subtracted from channels.
  190. * param scale Multiplier for frame values.
  191. * blob(n, c, y, x) = scale * resize( frame(y, x, c) ) - mean(c) )
  192. */
  193. public void setInputParams(double scale, Size size, Scalar mean)
  194. {
  195. ThrowIfDisposed();
  196. dnn_Model_setInputParams_12(nativeObj, scale, size.width, size.height, mean.val[0], mean.val[1], mean.val[2], mean.val[3]);
  197. }
  198. /**
  199. * Set preprocessing parameters for frame.
  200. * param size New input size.
  201. * param scale Multiplier for frame values.
  202. * blob(n, c, y, x) = scale * resize( frame(y, x, c) ) - mean(c) )
  203. */
  204. public void setInputParams(double scale, Size size)
  205. {
  206. ThrowIfDisposed();
  207. dnn_Model_setInputParams_13(nativeObj, scale, size.width, size.height);
  208. }
  209. /**
  210. * Set preprocessing parameters for frame.
  211. * param scale Multiplier for frame values.
  212. * blob(n, c, y, x) = scale * resize( frame(y, x, c) ) - mean(c) )
  213. */
  214. public void setInputParams(double scale)
  215. {
  216. ThrowIfDisposed();
  217. dnn_Model_setInputParams_14(nativeObj, scale);
  218. }
  219. /**
  220. * Set preprocessing parameters for frame.
  221. * blob(n, c, y, x) = scale * resize( frame(y, x, c) ) - mean(c) )
  222. */
  223. public void setInputParams()
  224. {
  225. ThrowIfDisposed();
  226. dnn_Model_setInputParams_15(nativeObj);
  227. }
  228. //
  229. // C++: void cv::dnn::Model::predict(Mat frame, vector_Mat& outs)
  230. //
  231. /**
  232. * Given the {code input} frame, create input blob, run net and return the output {code blobs}.
  233. * param outs Allocated output blobs, which will store results of the computation.
  234. * param frame automatically generated
  235. */
  236. public void predict(Mat frame, List<Mat> outs)
  237. {
  238. ThrowIfDisposed();
  239. if (frame != null) frame.ThrowIfDisposed();
  240. Mat outs_mat = new Mat();
  241. dnn_Model_predict_10(nativeObj, frame.nativeObj, outs_mat.nativeObj);
  242. Converters.Mat_to_vector_Mat(outs_mat, outs);
  243. outs_mat.release();
  244. }
  245. //
  246. // C++: Model cv::dnn::Model::setPreferableBackend(dnn_Backend backendId)
  247. //
  248. public Model setPreferableBackend(int backendId)
  249. {
  250. ThrowIfDisposed();
  251. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setPreferableBackend_10(nativeObj, backendId)));
  252. }
  253. //
  254. // C++: Model cv::dnn::Model::setPreferableTarget(dnn_Target targetId)
  255. //
  256. public Model setPreferableTarget(int targetId)
  257. {
  258. ThrowIfDisposed();
  259. return new Model(DisposableObject.ThrowIfNullIntPtr(dnn_Model_setPreferableTarget_10(nativeObj, targetId)));
  260. }
  261. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  262. const string LIBNAME = "__Internal";
  263. #else
  264. const string LIBNAME = "opencvforunity";
  265. #endif
  266. // C++: cv::dnn::Model::Model(String model, String config = "")
  267. [DllImport(LIBNAME)]
  268. private static extern IntPtr dnn_Model_Model_10(string model, string config);
  269. [DllImport(LIBNAME)]
  270. private static extern IntPtr dnn_Model_Model_11(string model);
  271. // C++: cv::dnn::Model::Model(Net network)
  272. [DllImport(LIBNAME)]
  273. private static extern IntPtr dnn_Model_Model_12(IntPtr network_nativeObj);
  274. // C++: Model cv::dnn::Model::setInputSize(Size size)
  275. [DllImport(LIBNAME)]
  276. private static extern IntPtr dnn_Model_setInputSize_10(IntPtr nativeObj, double size_width, double size_height);
  277. // C++: Model cv::dnn::Model::setInputSize(int width, int height)
  278. [DllImport(LIBNAME)]
  279. private static extern IntPtr dnn_Model_setInputSize_11(IntPtr nativeObj, int width, int height);
  280. // C++: Model cv::dnn::Model::setInputMean(Scalar mean)
  281. [DllImport(LIBNAME)]
  282. private static extern IntPtr dnn_Model_setInputMean_10(IntPtr nativeObj, double mean_val0, double mean_val1, double mean_val2, double mean_val3);
  283. // C++: Model cv::dnn::Model::setInputScale(Scalar scale)
  284. [DllImport(LIBNAME)]
  285. private static extern IntPtr dnn_Model_setInputScale_10(IntPtr nativeObj, double scale_val0, double scale_val1, double scale_val2, double scale_val3);
  286. // C++: Model cv::dnn::Model::setInputCrop(bool crop)
  287. [DllImport(LIBNAME)]
  288. private static extern IntPtr dnn_Model_setInputCrop_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool crop);
  289. // C++: Model cv::dnn::Model::setInputSwapRB(bool swapRB)
  290. [DllImport(LIBNAME)]
  291. private static extern IntPtr dnn_Model_setInputSwapRB_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool swapRB);
  292. // C++: void cv::dnn::Model::setInputParams(double scale = 1.0, Size size = Size(), Scalar mean = Scalar(), bool swapRB = false, bool crop = false)
  293. [DllImport(LIBNAME)]
  294. private static extern void dnn_Model_setInputParams_10(IntPtr nativeObj, double scale, double size_width, double size_height, double mean_val0, double mean_val1, double mean_val2, double mean_val3, [MarshalAs(UnmanagedType.U1)] bool swapRB, [MarshalAs(UnmanagedType.U1)] bool crop);
  295. [DllImport(LIBNAME)]
  296. private static extern void dnn_Model_setInputParams_11(IntPtr nativeObj, double scale, double size_width, double size_height, double mean_val0, double mean_val1, double mean_val2, double mean_val3, [MarshalAs(UnmanagedType.U1)] bool swapRB);
  297. [DllImport(LIBNAME)]
  298. private static extern void dnn_Model_setInputParams_12(IntPtr nativeObj, double scale, double size_width, double size_height, double mean_val0, double mean_val1, double mean_val2, double mean_val3);
  299. [DllImport(LIBNAME)]
  300. private static extern void dnn_Model_setInputParams_13(IntPtr nativeObj, double scale, double size_width, double size_height);
  301. [DllImport(LIBNAME)]
  302. private static extern void dnn_Model_setInputParams_14(IntPtr nativeObj, double scale);
  303. [DllImport(LIBNAME)]
  304. private static extern void dnn_Model_setInputParams_15(IntPtr nativeObj);
  305. // C++: void cv::dnn::Model::predict(Mat frame, vector_Mat& outs)
  306. [DllImport(LIBNAME)]
  307. private static extern void dnn_Model_predict_10(IntPtr nativeObj, IntPtr frame_nativeObj, IntPtr outs_mat_nativeObj);
  308. // C++: Model cv::dnn::Model::setPreferableBackend(dnn_Backend backendId)
  309. [DllImport(LIBNAME)]
  310. private static extern IntPtr dnn_Model_setPreferableBackend_10(IntPtr nativeObj, int backendId);
  311. // C++: Model cv::dnn::Model::setPreferableTarget(dnn_Target targetId)
  312. [DllImport(LIBNAME)]
  313. private static extern IntPtr dnn_Model_setPreferableTarget_10(IntPtr nativeObj, int targetId);
  314. // native support for java finalize()
  315. [DllImport(LIBNAME)]
  316. private static extern void dnn_Model_delete(IntPtr nativeObj);
  317. }
  318. }
  319. #endif