OCRBeamSearchDecoder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.TextModule
  8. {
  9. // C++: class OCRBeamSearchDecoder
  10. /**
  11. * OCRBeamSearchDecoder class provides an interface for OCR using Beam Search algorithm.
  12. *
  13. * <b>Note:</b>
  14. * <ul>
  15. * <li>
  16. * (C++) An example on using OCRBeamSearchDecoder recognition combined with scene text detection can
  17. * be found at the demo sample:
  18. * &lt;https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/word_recognition.cpp&gt;
  19. * </li>
  20. * </ul>
  21. */
  22. public class OCRBeamSearchDecoder : BaseOCR
  23. {
  24. protected override void Dispose(bool disposing)
  25. {
  26. try
  27. {
  28. if (disposing)
  29. {
  30. }
  31. if (IsEnabledDispose)
  32. {
  33. if (nativeObj != IntPtr.Zero)
  34. text_OCRBeamSearchDecoder_delete(nativeObj);
  35. nativeObj = IntPtr.Zero;
  36. }
  37. }
  38. finally
  39. {
  40. base.Dispose(disposing);
  41. }
  42. }
  43. protected internal OCRBeamSearchDecoder(IntPtr addr) : base(addr) { }
  44. // internal usage only
  45. public static new OCRBeamSearchDecoder __fromPtr__(IntPtr addr) { return new OCRBeamSearchDecoder(addr); }
  46. //
  47. // C++: String cv::text::OCRBeamSearchDecoder::run(Mat image, int min_confidence, int component_level = 0)
  48. //
  49. /**
  50. * Recognize text using Beam Search.
  51. *
  52. * Takes image on input and returns recognized text in the output_text parameter. Optionally
  53. * provides also the Rects for individual text elements found (e.g. words), and the list of those
  54. * text elements with their confidence values.
  55. *
  56. * param image Input binary image CV_8UC1 with a single text line (or word).
  57. *
  58. *
  59. * text elements found (e.g. words).
  60. *
  61. * recognition of individual text elements found (e.g. words).
  62. *
  63. * for the recognition of individual text elements found (e.g. words).
  64. *
  65. * param component_level Only OCR_LEVEL_WORD is supported.
  66. * param min_confidence automatically generated
  67. * return automatically generated
  68. */
  69. public string run(Mat image, int min_confidence, int component_level)
  70. {
  71. ThrowIfDisposed();
  72. if (image != null) image.ThrowIfDisposed();
  73. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRBeamSearchDecoder_run_10(nativeObj, image.nativeObj, min_confidence, component_level)));
  74. return retVal;
  75. }
  76. /**
  77. * Recognize text using Beam Search.
  78. *
  79. * Takes image on input and returns recognized text in the output_text parameter. Optionally
  80. * provides also the Rects for individual text elements found (e.g. words), and the list of those
  81. * text elements with their confidence values.
  82. *
  83. * param image Input binary image CV_8UC1 with a single text line (or word).
  84. *
  85. *
  86. * text elements found (e.g. words).
  87. *
  88. * recognition of individual text elements found (e.g. words).
  89. *
  90. * for the recognition of individual text elements found (e.g. words).
  91. *
  92. * param min_confidence automatically generated
  93. * return automatically generated
  94. */
  95. public string run(Mat image, int min_confidence)
  96. {
  97. ThrowIfDisposed();
  98. if (image != null) image.ThrowIfDisposed();
  99. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRBeamSearchDecoder_run_11(nativeObj, image.nativeObj, min_confidence)));
  100. return retVal;
  101. }
  102. //
  103. // C++: String cv::text::OCRBeamSearchDecoder::run(Mat image, Mat mask, int min_confidence, int component_level = 0)
  104. //
  105. public string run(Mat image, Mat mask, int min_confidence, int component_level)
  106. {
  107. ThrowIfDisposed();
  108. if (image != null) image.ThrowIfDisposed();
  109. if (mask != null) mask.ThrowIfDisposed();
  110. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRBeamSearchDecoder_run_12(nativeObj, image.nativeObj, mask.nativeObj, min_confidence, component_level)));
  111. return retVal;
  112. }
  113. public string run(Mat image, Mat mask, int min_confidence)
  114. {
  115. ThrowIfDisposed();
  116. if (image != null) image.ThrowIfDisposed();
  117. if (mask != null) mask.ThrowIfDisposed();
  118. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRBeamSearchDecoder_run_13(nativeObj, image.nativeObj, mask.nativeObj, min_confidence)));
  119. return retVal;
  120. }
  121. //
  122. // C++: static Ptr_OCRBeamSearchDecoder cv::text::OCRBeamSearchDecoder::create(Ptr_OCRBeamSearchDecoder_ClassifierCallback classifier, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, text_decoder_mode mode = OCR_DECODER_VITERBI, int beam_size = 500)
  123. //
  124. /**
  125. * Creates an instance of the OCRBeamSearchDecoder class. Initializes HMMDecoder.
  126. *
  127. * param classifier The character classifier with built in feature extractor.
  128. *
  129. * param vocabulary The language vocabulary (chars when ASCII English text). vocabulary.size()
  130. * must be equal to the number of classes of the classifier.
  131. *
  132. * param transition_probabilities_table Table with transition probabilities between character
  133. * pairs. cols == rows == vocabulary.size().
  134. *
  135. * param emission_probabilities_table Table with observation emission probabilities. cols ==
  136. * rows == vocabulary.size().
  137. *
  138. * param mode HMM Decoding algorithm. Only OCR_DECODER_VITERBI is available for the moment
  139. * (&lt;http://en.wikipedia.org/wiki/Viterbi_algorithm&gt;).
  140. *
  141. * param beam_size Size of the beam in Beam Search algorithm.
  142. * return automatically generated
  143. */
  144. public static OCRBeamSearchDecoder create(OCRBeamSearchDecoder_ClassifierCallback classifier, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode, int beam_size)
  145. {
  146. if (classifier != null) classifier.ThrowIfDisposed();
  147. if (transition_probabilities_table != null) transition_probabilities_table.ThrowIfDisposed();
  148. if (emission_probabilities_table != null) emission_probabilities_table.ThrowIfDisposed();
  149. return OCRBeamSearchDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRBeamSearchDecoder_create_10(classifier.getNativeObjAddr(), vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj, mode, beam_size)));
  150. }
  151. /**
  152. * Creates an instance of the OCRBeamSearchDecoder class. Initializes HMMDecoder.
  153. *
  154. * param classifier The character classifier with built in feature extractor.
  155. *
  156. * param vocabulary The language vocabulary (chars when ASCII English text). vocabulary.size()
  157. * must be equal to the number of classes of the classifier.
  158. *
  159. * param transition_probabilities_table Table with transition probabilities between character
  160. * pairs. cols == rows == vocabulary.size().
  161. *
  162. * param emission_probabilities_table Table with observation emission probabilities. cols ==
  163. * rows == vocabulary.size().
  164. *
  165. * param mode HMM Decoding algorithm. Only OCR_DECODER_VITERBI is available for the moment
  166. * (&lt;http://en.wikipedia.org/wiki/Viterbi_algorithm&gt;).
  167. *
  168. * return automatically generated
  169. */
  170. public static OCRBeamSearchDecoder create(OCRBeamSearchDecoder_ClassifierCallback classifier, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode)
  171. {
  172. if (classifier != null) classifier.ThrowIfDisposed();
  173. if (transition_probabilities_table != null) transition_probabilities_table.ThrowIfDisposed();
  174. if (emission_probabilities_table != null) emission_probabilities_table.ThrowIfDisposed();
  175. return OCRBeamSearchDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRBeamSearchDecoder_create_11(classifier.getNativeObjAddr(), vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj, mode)));
  176. }
  177. /**
  178. * Creates an instance of the OCRBeamSearchDecoder class. Initializes HMMDecoder.
  179. *
  180. * param classifier The character classifier with built in feature extractor.
  181. *
  182. * param vocabulary The language vocabulary (chars when ASCII English text). vocabulary.size()
  183. * must be equal to the number of classes of the classifier.
  184. *
  185. * param transition_probabilities_table Table with transition probabilities between character
  186. * pairs. cols == rows == vocabulary.size().
  187. *
  188. * param emission_probabilities_table Table with observation emission probabilities. cols ==
  189. * rows == vocabulary.size().
  190. *
  191. * (&lt;http://en.wikipedia.org/wiki/Viterbi_algorithm&gt;).
  192. *
  193. * return automatically generated
  194. */
  195. public static OCRBeamSearchDecoder create(OCRBeamSearchDecoder_ClassifierCallback classifier, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table)
  196. {
  197. if (classifier != null) classifier.ThrowIfDisposed();
  198. if (transition_probabilities_table != null) transition_probabilities_table.ThrowIfDisposed();
  199. if (emission_probabilities_table != null) emission_probabilities_table.ThrowIfDisposed();
  200. return OCRBeamSearchDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRBeamSearchDecoder_create_12(classifier.getNativeObjAddr(), vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj)));
  201. }
  202. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  203. const string LIBNAME = "__Internal";
  204. #else
  205. const string LIBNAME = "opencvforunity";
  206. #endif
  207. // C++: String cv::text::OCRBeamSearchDecoder::run(Mat image, int min_confidence, int component_level = 0)
  208. [DllImport(LIBNAME)]
  209. private static extern IntPtr text_OCRBeamSearchDecoder_run_10(IntPtr nativeObj, IntPtr image_nativeObj, int min_confidence, int component_level);
  210. [DllImport(LIBNAME)]
  211. private static extern IntPtr text_OCRBeamSearchDecoder_run_11(IntPtr nativeObj, IntPtr image_nativeObj, int min_confidence);
  212. // C++: String cv::text::OCRBeamSearchDecoder::run(Mat image, Mat mask, int min_confidence, int component_level = 0)
  213. [DllImport(LIBNAME)]
  214. private static extern IntPtr text_OCRBeamSearchDecoder_run_12(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr mask_nativeObj, int min_confidence, int component_level);
  215. [DllImport(LIBNAME)]
  216. private static extern IntPtr text_OCRBeamSearchDecoder_run_13(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr mask_nativeObj, int min_confidence);
  217. // C++: static Ptr_OCRBeamSearchDecoder cv::text::OCRBeamSearchDecoder::create(Ptr_OCRBeamSearchDecoder_ClassifierCallback classifier, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, text_decoder_mode mode = OCR_DECODER_VITERBI, int beam_size = 500)
  218. [DllImport(LIBNAME)]
  219. private static extern IntPtr text_OCRBeamSearchDecoder_create_10(IntPtr classifier_nativeObj, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj, int mode, int beam_size);
  220. [DllImport(LIBNAME)]
  221. private static extern IntPtr text_OCRBeamSearchDecoder_create_11(IntPtr classifier_nativeObj, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj, int mode);
  222. [DllImport(LIBNAME)]
  223. private static extern IntPtr text_OCRBeamSearchDecoder_create_12(IntPtr classifier_nativeObj, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj);
  224. // native support for java finalize()
  225. [DllImport(LIBNAME)]
  226. private static extern void text_OCRBeamSearchDecoder_delete(IntPtr nativeObj);
  227. }
  228. }
  229. #endif