DetectionModel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 DetectionModel
  10. /**
  11. * This class represents high-level API for object detection networks.
  12. *
  13. * DetectionModel allows to set params for preprocessing input image.
  14. * DetectionModel creates net from file with trained weights and config,
  15. * sets preprocessing input, runs forward pass and return result detections.
  16. * For DetectionModel SSD, Faster R-CNN, YOLO topologies are supported.
  17. */
  18. public class DetectionModel : Model
  19. {
  20. protected override void Dispose(bool disposing)
  21. {
  22. try
  23. {
  24. if (disposing)
  25. {
  26. }
  27. if (IsEnabledDispose)
  28. {
  29. if (nativeObj != IntPtr.Zero)
  30. dnn_DetectionModel_delete(nativeObj);
  31. nativeObj = IntPtr.Zero;
  32. }
  33. }
  34. finally
  35. {
  36. base.Dispose(disposing);
  37. }
  38. }
  39. protected internal DetectionModel(IntPtr addr) : base(addr) { }
  40. // internal usage only
  41. public static new DetectionModel __fromPtr__(IntPtr addr) { return new DetectionModel(addr); }
  42. //
  43. // C++: cv::dnn::DetectionModel::DetectionModel(String model, String config = "")
  44. //
  45. /**
  46. * Create detection model from 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 DetectionModel(string model, string config) :
  52. base(DisposableObject.ThrowIfNullIntPtr(dnn_DetectionModel_DetectionModel_10(model, config)))
  53. {
  54. }
  55. /**
  56. * Create detection model from 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 DetectionModel(string model) :
  61. base(DisposableObject.ThrowIfNullIntPtr(dnn_DetectionModel_DetectionModel_11(model)))
  62. {
  63. }
  64. //
  65. // C++: cv::dnn::DetectionModel::DetectionModel(Net network)
  66. //
  67. /**
  68. * Create model from deep learning network.
  69. * param network Net object.
  70. */
  71. public DetectionModel(Net network) :
  72. base(DisposableObject.ThrowIfNullIntPtr(dnn_DetectionModel_DetectionModel_12(network.nativeObj)))
  73. {
  74. }
  75. //
  76. // C++: DetectionModel cv::dnn::DetectionModel::setNmsAcrossClasses(bool value)
  77. //
  78. /**
  79. * nmsAcrossClasses defaults to false,
  80. * such that when non max suppression is used during the detect() function, it will do so per-class.
  81. * This function allows you to toggle this behaviour.
  82. * param value The new value for nmsAcrossClasses
  83. * return automatically generated
  84. */
  85. public DetectionModel setNmsAcrossClasses(bool value)
  86. {
  87. ThrowIfDisposed();
  88. return new DetectionModel(DisposableObject.ThrowIfNullIntPtr(dnn_DetectionModel_setNmsAcrossClasses_10(nativeObj, value)));
  89. }
  90. //
  91. // C++: bool cv::dnn::DetectionModel::getNmsAcrossClasses()
  92. //
  93. /**
  94. * Getter for nmsAcrossClasses. This variable defaults to false,
  95. * such that when non max suppression is used during the detect() function, it will do so only per-class
  96. * return automatically generated
  97. */
  98. public bool getNmsAcrossClasses()
  99. {
  100. ThrowIfDisposed();
  101. return dnn_DetectionModel_getNmsAcrossClasses_10(nativeObj);
  102. }
  103. //
  104. // C++: void cv::dnn::DetectionModel::detect(Mat frame, vector_int& classIds, vector_float& confidences, vector_Rect& boxes, float confThreshold = 0.5f, float nmsThreshold = 0.0f)
  105. //
  106. /**
  107. * Given the {code input} frame, create input blob, run net and return result detections.
  108. * param classIds Class indexes in result detection.
  109. * param confidences A set of corresponding confidences.
  110. * param boxes A set of bounding boxes.
  111. * param confThreshold A threshold used to filter boxes by confidences.
  112. * param nmsThreshold A threshold used in non maximum suppression.
  113. * param frame automatically generated
  114. */
  115. public void detect(Mat frame, MatOfInt classIds, MatOfFloat confidences, MatOfRect boxes, float confThreshold, float nmsThreshold)
  116. {
  117. ThrowIfDisposed();
  118. if (frame != null) frame.ThrowIfDisposed();
  119. if (classIds != null) classIds.ThrowIfDisposed();
  120. if (confidences != null) confidences.ThrowIfDisposed();
  121. if (boxes != null) boxes.ThrowIfDisposed();
  122. Mat classIds_mat = classIds;
  123. Mat confidences_mat = confidences;
  124. Mat boxes_mat = boxes;
  125. dnn_DetectionModel_detect_10(nativeObj, frame.nativeObj, classIds_mat.nativeObj, confidences_mat.nativeObj, boxes_mat.nativeObj, confThreshold, nmsThreshold);
  126. }
  127. /**
  128. * Given the {code input} frame, create input blob, run net and return result detections.
  129. * param classIds Class indexes in result detection.
  130. * param confidences A set of corresponding confidences.
  131. * param boxes A set of bounding boxes.
  132. * param confThreshold A threshold used to filter boxes by confidences.
  133. * param frame automatically generated
  134. */
  135. public void detect(Mat frame, MatOfInt classIds, MatOfFloat confidences, MatOfRect boxes, float confThreshold)
  136. {
  137. ThrowIfDisposed();
  138. if (frame != null) frame.ThrowIfDisposed();
  139. if (classIds != null) classIds.ThrowIfDisposed();
  140. if (confidences != null) confidences.ThrowIfDisposed();
  141. if (boxes != null) boxes.ThrowIfDisposed();
  142. Mat classIds_mat = classIds;
  143. Mat confidences_mat = confidences;
  144. Mat boxes_mat = boxes;
  145. dnn_DetectionModel_detect_11(nativeObj, frame.nativeObj, classIds_mat.nativeObj, confidences_mat.nativeObj, boxes_mat.nativeObj, confThreshold);
  146. }
  147. /**
  148. * Given the {code input} frame, create input blob, run net and return result detections.
  149. * param classIds Class indexes in result detection.
  150. * param confidences A set of corresponding confidences.
  151. * param boxes A set of bounding boxes.
  152. * param frame automatically generated
  153. */
  154. public void detect(Mat frame, MatOfInt classIds, MatOfFloat confidences, MatOfRect boxes)
  155. {
  156. ThrowIfDisposed();
  157. if (frame != null) frame.ThrowIfDisposed();
  158. if (classIds != null) classIds.ThrowIfDisposed();
  159. if (confidences != null) confidences.ThrowIfDisposed();
  160. if (boxes != null) boxes.ThrowIfDisposed();
  161. Mat classIds_mat = classIds;
  162. Mat confidences_mat = confidences;
  163. Mat boxes_mat = boxes;
  164. dnn_DetectionModel_detect_12(nativeObj, frame.nativeObj, classIds_mat.nativeObj, confidences_mat.nativeObj, boxes_mat.nativeObj);
  165. }
  166. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  167. const string LIBNAME = "__Internal";
  168. #else
  169. const string LIBNAME = "opencvforunity";
  170. #endif
  171. // C++: cv::dnn::DetectionModel::DetectionModel(String model, String config = "")
  172. [DllImport(LIBNAME)]
  173. private static extern IntPtr dnn_DetectionModel_DetectionModel_10(string model, string config);
  174. [DllImport(LIBNAME)]
  175. private static extern IntPtr dnn_DetectionModel_DetectionModel_11(string model);
  176. // C++: cv::dnn::DetectionModel::DetectionModel(Net network)
  177. [DllImport(LIBNAME)]
  178. private static extern IntPtr dnn_DetectionModel_DetectionModel_12(IntPtr network_nativeObj);
  179. // C++: DetectionModel cv::dnn::DetectionModel::setNmsAcrossClasses(bool value)
  180. [DllImport(LIBNAME)]
  181. private static extern IntPtr dnn_DetectionModel_setNmsAcrossClasses_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool value);
  182. // C++: bool cv::dnn::DetectionModel::getNmsAcrossClasses()
  183. [DllImport(LIBNAME)]
  184. [return: MarshalAs(UnmanagedType.U1)]
  185. private static extern bool dnn_DetectionModel_getNmsAcrossClasses_10(IntPtr nativeObj);
  186. // C++: void cv::dnn::DetectionModel::detect(Mat frame, vector_int& classIds, vector_float& confidences, vector_Rect& boxes, float confThreshold = 0.5f, float nmsThreshold = 0.0f)
  187. [DllImport(LIBNAME)]
  188. private static extern void dnn_DetectionModel_detect_10(IntPtr nativeObj, IntPtr frame_nativeObj, IntPtr classIds_mat_nativeObj, IntPtr confidences_mat_nativeObj, IntPtr boxes_mat_nativeObj, float confThreshold, float nmsThreshold);
  189. [DllImport(LIBNAME)]
  190. private static extern void dnn_DetectionModel_detect_11(IntPtr nativeObj, IntPtr frame_nativeObj, IntPtr classIds_mat_nativeObj, IntPtr confidences_mat_nativeObj, IntPtr boxes_mat_nativeObj, float confThreshold);
  191. [DllImport(LIBNAME)]
  192. private static extern void dnn_DetectionModel_detect_12(IntPtr nativeObj, IntPtr frame_nativeObj, IntPtr classIds_mat_nativeObj, IntPtr confidences_mat_nativeObj, IntPtr boxes_mat_nativeObj);
  193. // native support for java finalize()
  194. [DllImport(LIBNAME)]
  195. private static extern void dnn_DetectionModel_delete(IntPtr nativeObj);
  196. }
  197. }
  198. #endif