TextRecognitionModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 TextRecognitionModel
  10. /**
  11. * This class represents high-level API for text recognition networks.
  12. *
  13. * TextRecognitionModel allows to set params for preprocessing input image.
  14. * TextRecognitionModel creates net from file with trained weights and config,
  15. * sets preprocessing input, runs forward pass and return recognition result.
  16. * For TextRecognitionModel, CRNN-CTC is supported.
  17. */
  18. public class TextRecognitionModel : 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_TextRecognitionModel_delete(nativeObj);
  31. nativeObj = IntPtr.Zero;
  32. }
  33. }
  34. finally
  35. {
  36. base.Dispose(disposing);
  37. }
  38. }
  39. protected internal TextRecognitionModel(IntPtr addr) : base(addr) { }
  40. // internal usage only
  41. public static new TextRecognitionModel __fromPtr__(IntPtr addr) { return new TextRecognitionModel(addr); }
  42. //
  43. // C++: cv::dnn::TextRecognitionModel::TextRecognitionModel(Net network)
  44. //
  45. /**
  46. * Create Text Recognition model from deep learning network
  47. * Call setDecodeType() and setVocabulary() after constructor to initialize the decoding method
  48. * param network Net object
  49. */
  50. public TextRecognitionModel(Net network) :
  51. base(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_TextRecognitionModel_10(network.nativeObj)))
  52. {
  53. }
  54. //
  55. // C++: cv::dnn::TextRecognitionModel::TextRecognitionModel(string model, string config = "")
  56. //
  57. /**
  58. * Create text recognition model from network represented in one of the supported formats
  59. * Call setDecodeType() and setVocabulary() after constructor to initialize the decoding method
  60. * param model Binary file contains trained weights
  61. * param config Text file contains network configuration
  62. */
  63. public TextRecognitionModel(string model, string config) :
  64. base(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_TextRecognitionModel_11(model, config)))
  65. {
  66. }
  67. /**
  68. * Create text recognition model from network represented in one of the supported formats
  69. * Call setDecodeType() and setVocabulary() after constructor to initialize the decoding method
  70. * param model Binary file contains trained weights
  71. */
  72. public TextRecognitionModel(string model) :
  73. base(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_TextRecognitionModel_12(model)))
  74. {
  75. }
  76. //
  77. // C++: TextRecognitionModel cv::dnn::TextRecognitionModel::setDecodeType(string decodeType)
  78. //
  79. /**
  80. * Set the decoding method of translating the network output into string
  81. * param decodeType The decoding method of translating the network output into string, currently supported type:
  82. * - {code "CTC-greedy"} greedy decoding for the output of CTC-based methods
  83. * - {code "CTC-prefix-beam-search"} Prefix beam search decoding for the output of CTC-based methods
  84. * return automatically generated
  85. */
  86. public TextRecognitionModel setDecodeType(string decodeType)
  87. {
  88. ThrowIfDisposed();
  89. return new TextRecognitionModel(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_setDecodeType_10(nativeObj, decodeType)));
  90. }
  91. //
  92. // C++: string cv::dnn::TextRecognitionModel::getDecodeType()
  93. //
  94. /**
  95. * Get the decoding method
  96. * return the decoding method
  97. */
  98. public string getDecodeType()
  99. {
  100. ThrowIfDisposed();
  101. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_getDecodeType_10(nativeObj)));
  102. return retVal;
  103. }
  104. //
  105. // C++: TextRecognitionModel cv::dnn::TextRecognitionModel::setDecodeOptsCTCPrefixBeamSearch(int beamSize, int vocPruneSize = 0)
  106. //
  107. /**
  108. * Set the decoding method options for {code "CTC-prefix-beam-search"} decode usage
  109. * param beamSize Beam size for search
  110. * param vocPruneSize Parameter to optimize big vocabulary search,
  111. * only take top {code vocPruneSize} tokens in each search step, {code vocPruneSize} <= 0 stands for disable this prune.
  112. * return automatically generated
  113. */
  114. public TextRecognitionModel setDecodeOptsCTCPrefixBeamSearch(int beamSize, int vocPruneSize)
  115. {
  116. ThrowIfDisposed();
  117. return new TextRecognitionModel(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_setDecodeOptsCTCPrefixBeamSearch_10(nativeObj, beamSize, vocPruneSize)));
  118. }
  119. /**
  120. * Set the decoding method options for {code "CTC-prefix-beam-search"} decode usage
  121. * param beamSize Beam size for search
  122. * only take top {code vocPruneSize} tokens in each search step, {code vocPruneSize} <= 0 stands for disable this prune.
  123. * return automatically generated
  124. */
  125. public TextRecognitionModel setDecodeOptsCTCPrefixBeamSearch(int beamSize)
  126. {
  127. ThrowIfDisposed();
  128. return new TextRecognitionModel(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_setDecodeOptsCTCPrefixBeamSearch_11(nativeObj, beamSize)));
  129. }
  130. //
  131. // C++: TextRecognitionModel cv::dnn::TextRecognitionModel::setVocabulary(vector_string vocabulary)
  132. //
  133. /**
  134. * Set the vocabulary for recognition.
  135. * param vocabulary the associated vocabulary of the network.
  136. * return automatically generated
  137. */
  138. public TextRecognitionModel setVocabulary(List<string> vocabulary)
  139. {
  140. ThrowIfDisposed();
  141. Mat vocabulary_mat = Converters.vector_string_to_Mat(vocabulary);
  142. return new TextRecognitionModel(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_setVocabulary_10(nativeObj, vocabulary_mat.nativeObj)));
  143. }
  144. //
  145. // C++: vector_string cv::dnn::TextRecognitionModel::getVocabulary()
  146. //
  147. /**
  148. * Get the vocabulary for recognition.
  149. * return vocabulary the associated vocabulary
  150. */
  151. public List<string> getVocabulary()
  152. {
  153. ThrowIfDisposed();
  154. List<string> retVal = new List<string>();
  155. Mat retValMat = new Mat(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_getVocabulary_10(nativeObj)));
  156. Converters.Mat_to_vector_string(retValMat, retVal);
  157. return retVal;
  158. }
  159. //
  160. // C++: string cv::dnn::TextRecognitionModel::recognize(Mat frame)
  161. //
  162. /**
  163. * Given the {code input} frame, create input blob, run net and return recognition result
  164. * param frame The input image
  165. * return The text recognition result
  166. */
  167. public string recognize(Mat frame)
  168. {
  169. ThrowIfDisposed();
  170. if (frame != null) frame.ThrowIfDisposed();
  171. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(dnn_TextRecognitionModel_recognize_10(nativeObj, frame.nativeObj)));
  172. return retVal;
  173. }
  174. //
  175. // C++: void cv::dnn::TextRecognitionModel::recognize(Mat frame, vector_Mat roiRects, vector_string& results)
  176. //
  177. /**
  178. * Given the {code input} frame, create input blob, run net and return recognition result
  179. * param frame The input image
  180. * param roiRects List of text detection regions of interest (cv::Rect, CV_32SC4). ROIs is be cropped as the network inputs
  181. * param results A set of text recognition results.
  182. */
  183. public void recognize(Mat frame, List<Mat> roiRects, List<string> results)
  184. {
  185. ThrowIfDisposed();
  186. if (frame != null) frame.ThrowIfDisposed();
  187. Mat roiRects_mat = Converters.vector_Mat_to_Mat(roiRects);
  188. Mat results_mat = new Mat();
  189. dnn_TextRecognitionModel_recognize_11(nativeObj, frame.nativeObj, roiRects_mat.nativeObj, results_mat.nativeObj);
  190. Converters.Mat_to_vector_string(results_mat, results);
  191. results_mat.release();
  192. }
  193. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  194. const string LIBNAME = "__Internal";
  195. #else
  196. const string LIBNAME = "opencvforunity";
  197. #endif
  198. // C++: cv::dnn::TextRecognitionModel::TextRecognitionModel(Net network)
  199. [DllImport(LIBNAME)]
  200. private static extern IntPtr dnn_TextRecognitionModel_TextRecognitionModel_10(IntPtr network_nativeObj);
  201. // C++: cv::dnn::TextRecognitionModel::TextRecognitionModel(string model, string config = "")
  202. [DllImport(LIBNAME)]
  203. private static extern IntPtr dnn_TextRecognitionModel_TextRecognitionModel_11(string model, string config);
  204. [DllImport(LIBNAME)]
  205. private static extern IntPtr dnn_TextRecognitionModel_TextRecognitionModel_12(string model);
  206. // C++: TextRecognitionModel cv::dnn::TextRecognitionModel::setDecodeType(string decodeType)
  207. [DllImport(LIBNAME)]
  208. private static extern IntPtr dnn_TextRecognitionModel_setDecodeType_10(IntPtr nativeObj, string decodeType);
  209. // C++: string cv::dnn::TextRecognitionModel::getDecodeType()
  210. [DllImport(LIBNAME)]
  211. private static extern IntPtr dnn_TextRecognitionModel_getDecodeType_10(IntPtr nativeObj);
  212. // C++: TextRecognitionModel cv::dnn::TextRecognitionModel::setDecodeOptsCTCPrefixBeamSearch(int beamSize, int vocPruneSize = 0)
  213. [DllImport(LIBNAME)]
  214. private static extern IntPtr dnn_TextRecognitionModel_setDecodeOptsCTCPrefixBeamSearch_10(IntPtr nativeObj, int beamSize, int vocPruneSize);
  215. [DllImport(LIBNAME)]
  216. private static extern IntPtr dnn_TextRecognitionModel_setDecodeOptsCTCPrefixBeamSearch_11(IntPtr nativeObj, int beamSize);
  217. // C++: TextRecognitionModel cv::dnn::TextRecognitionModel::setVocabulary(vector_string vocabulary)
  218. [DllImport(LIBNAME)]
  219. private static extern IntPtr dnn_TextRecognitionModel_setVocabulary_10(IntPtr nativeObj, IntPtr vocabulary_mat_nativeObj);
  220. // C++: vector_string cv::dnn::TextRecognitionModel::getVocabulary()
  221. [DllImport(LIBNAME)]
  222. private static extern IntPtr dnn_TextRecognitionModel_getVocabulary_10(IntPtr nativeObj);
  223. // C++: string cv::dnn::TextRecognitionModel::recognize(Mat frame)
  224. [DllImport(LIBNAME)]
  225. private static extern IntPtr dnn_TextRecognitionModel_recognize_10(IntPtr nativeObj, IntPtr frame_nativeObj);
  226. // C++: void cv::dnn::TextRecognitionModel::recognize(Mat frame, vector_Mat roiRects, vector_string& results)
  227. [DllImport(LIBNAME)]
  228. private static extern void dnn_TextRecognitionModel_recognize_11(IntPtr nativeObj, IntPtr frame_nativeObj, IntPtr roiRects_mat_nativeObj, IntPtr results_mat_nativeObj);
  229. // native support for java finalize()
  230. [DllImport(LIBNAME)]
  231. private static extern void dnn_TextRecognitionModel_delete(IntPtr nativeObj);
  232. }
  233. }
  234. #endif