Layer.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Layer
  10. /**
  11. * This interface class allows to build new Layers - are building blocks of networks.
  12. *
  13. * Each class, derived from Layer, must implement allocate() methods to declare own outputs and forward() to compute outputs.
  14. * Also before using the new layer into networks you must register your layer by using one of REF: dnnLayerFactory "LayerFactory" macros.
  15. */
  16. public class Layer : Algorithm
  17. {
  18. protected override void Dispose(bool disposing)
  19. {
  20. try
  21. {
  22. if (disposing)
  23. {
  24. }
  25. if (IsEnabledDispose)
  26. {
  27. if (nativeObj != IntPtr.Zero)
  28. dnn_Layer_delete(nativeObj);
  29. nativeObj = IntPtr.Zero;
  30. }
  31. }
  32. finally
  33. {
  34. base.Dispose(disposing);
  35. }
  36. }
  37. protected internal Layer(IntPtr addr) : base(addr) { }
  38. // internal usage only
  39. public static new Layer __fromPtr__(IntPtr addr) { return new Layer(addr); }
  40. //
  41. // C++: void cv::dnn::Layer::finalize(vector_Mat inputs, vector_Mat& outputs)
  42. //
  43. /**
  44. * Computes and sets internal parameters according to inputs, outputs and blobs.
  45. * param outputs vector of already allocated output blobs
  46. *
  47. * If this method is called after network has allocated all memory for input and output blobs
  48. * and before inferencing.
  49. * param inputs automatically generated
  50. */
  51. public void finalize(List<Mat> inputs, List<Mat> outputs)
  52. {
  53. ThrowIfDisposed();
  54. Mat inputs_mat = Converters.vector_Mat_to_Mat(inputs);
  55. Mat outputs_mat = new Mat();
  56. dnn_Layer_finalize_10(nativeObj, inputs_mat.nativeObj, outputs_mat.nativeObj);
  57. Converters.Mat_to_vector_Mat(outputs_mat, outputs);
  58. outputs_mat.release();
  59. }
  60. //
  61. // C++: void cv::dnn::Layer::run(vector_Mat inputs, vector_Mat& outputs, vector_Mat& internals)
  62. //
  63. /**
  64. * Allocates layer and computes output.
  65. * deprecated This method will be removed in the future release.
  66. * param inputs automatically generated
  67. * param outputs automatically generated
  68. * param internals automatically generated
  69. */
  70. [Obsolete("This method is deprecated.")]
  71. public void run(List<Mat> inputs, List<Mat> outputs, List<Mat> internals)
  72. {
  73. ThrowIfDisposed();
  74. Mat inputs_mat = Converters.vector_Mat_to_Mat(inputs);
  75. Mat outputs_mat = new Mat();
  76. Mat internals_mat = Converters.vector_Mat_to_Mat(internals);
  77. dnn_Layer_run_10(nativeObj, inputs_mat.nativeObj, outputs_mat.nativeObj, internals_mat.nativeObj);
  78. Converters.Mat_to_vector_Mat(outputs_mat, outputs);
  79. outputs_mat.release();
  80. Converters.Mat_to_vector_Mat(internals_mat, internals);
  81. internals_mat.release();
  82. }
  83. //
  84. // C++: int cv::dnn::Layer::outputNameToIndex(String outputName)
  85. //
  86. /**
  87. * Returns index of output blob in output array.
  88. * SEE: inputNameToIndex()
  89. * param outputName automatically generated
  90. * return automatically generated
  91. */
  92. public int outputNameToIndex(string outputName)
  93. {
  94. ThrowIfDisposed();
  95. return dnn_Layer_outputNameToIndex_10(nativeObj, outputName);
  96. }
  97. //
  98. // C++: vector_Mat Layer::blobs
  99. //
  100. public List<Mat> get_blobs()
  101. {
  102. ThrowIfDisposed();
  103. List<Mat> retVal = new List<Mat>();
  104. Mat retValMat = new Mat(DisposableObject.ThrowIfNullIntPtr(dnn_Layer_get_1blobs_10(nativeObj)));
  105. Converters.Mat_to_vector_Mat(retValMat, retVal);
  106. return retVal;
  107. }
  108. //
  109. // C++: void Layer::blobs
  110. //
  111. public void set_blobs(List<Mat> blobs)
  112. {
  113. ThrowIfDisposed();
  114. Mat blobs_mat = Converters.vector_Mat_to_Mat(blobs);
  115. dnn_Layer_set_1blobs_10(nativeObj, blobs_mat.nativeObj);
  116. }
  117. //
  118. // C++: String Layer::name
  119. //
  120. public string get_name()
  121. {
  122. ThrowIfDisposed();
  123. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(dnn_Layer_get_1name_10(nativeObj)));
  124. return retVal;
  125. }
  126. //
  127. // C++: String Layer::type
  128. //
  129. public string get_type()
  130. {
  131. ThrowIfDisposed();
  132. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(dnn_Layer_get_1type_10(nativeObj)));
  133. return retVal;
  134. }
  135. //
  136. // C++: int Layer::preferableTarget
  137. //
  138. public int get_preferableTarget()
  139. {
  140. ThrowIfDisposed();
  141. return dnn_Layer_get_1preferableTarget_10(nativeObj);
  142. }
  143. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  144. const string LIBNAME = "__Internal";
  145. #else
  146. const string LIBNAME = "opencvforunity";
  147. #endif
  148. // C++: void cv::dnn::Layer::finalize(vector_Mat inputs, vector_Mat& outputs)
  149. [DllImport(LIBNAME)]
  150. private static extern void dnn_Layer_finalize_10(IntPtr nativeObj, IntPtr inputs_mat_nativeObj, IntPtr outputs_mat_nativeObj);
  151. // C++: void cv::dnn::Layer::run(vector_Mat inputs, vector_Mat& outputs, vector_Mat& internals)
  152. [DllImport(LIBNAME)]
  153. private static extern void dnn_Layer_run_10(IntPtr nativeObj, IntPtr inputs_mat_nativeObj, IntPtr outputs_mat_nativeObj, IntPtr internals_mat_nativeObj);
  154. // C++: int cv::dnn::Layer::outputNameToIndex(String outputName)
  155. [DllImport(LIBNAME)]
  156. private static extern int dnn_Layer_outputNameToIndex_10(IntPtr nativeObj, string outputName);
  157. // C++: vector_Mat Layer::blobs
  158. [DllImport(LIBNAME)]
  159. private static extern IntPtr dnn_Layer_get_1blobs_10(IntPtr nativeObj);
  160. // C++: void Layer::blobs
  161. [DllImport(LIBNAME)]
  162. private static extern void dnn_Layer_set_1blobs_10(IntPtr nativeObj, IntPtr blobs_mat_nativeObj);
  163. // C++: String Layer::name
  164. [DllImport(LIBNAME)]
  165. private static extern IntPtr dnn_Layer_get_1name_10(IntPtr nativeObj);
  166. // C++: String Layer::type
  167. [DllImport(LIBNAME)]
  168. private static extern IntPtr dnn_Layer_get_1type_10(IntPtr nativeObj);
  169. // C++: int Layer::preferableTarget
  170. [DllImport(LIBNAME)]
  171. private static extern int dnn_Layer_get_1preferableTarget_10(IntPtr nativeObj);
  172. // native support for java finalize()
  173. [DllImport(LIBNAME)]
  174. private static extern void dnn_Layer_delete(IntPtr nativeObj);
  175. }
  176. }
  177. #endif