OCRHMMDecoder.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 OCRHMMDecoder
  10. /**
  11. * OCRHMMDecoder class provides an interface for OCR using Hidden Markov Models.
  12. *
  13. * <b>Note:</b>
  14. * <ul>
  15. * <li>
  16. * (C++) An example on using OCRHMMDecoder recognition combined with scene text detection can
  17. * be found at the webcam_demo sample:
  18. * &lt;https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp&gt;
  19. * </li>
  20. * </ul>
  21. */
  22. public class OCRHMMDecoder : 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_OCRHMMDecoder_delete(nativeObj);
  35. nativeObj = IntPtr.Zero;
  36. }
  37. }
  38. finally
  39. {
  40. base.Dispose(disposing);
  41. }
  42. }
  43. protected internal OCRHMMDecoder(IntPtr addr) : base(addr) { }
  44. // internal usage only
  45. public static new OCRHMMDecoder __fromPtr__(IntPtr addr) { return new OCRHMMDecoder(addr); }
  46. //
  47. // C++: String cv::text::OCRHMMDecoder::run(Mat image, int min_confidence, int component_level = 0)
  48. //
  49. /**
  50. * Recognize text using HMM.
  51. *
  52. * Takes an image and a mask (where each connected component corresponds to a segmented character)
  53. * on input and returns recognized text in the output_text parameter. Optionally
  54. * provides also the Rects for individual text elements found (e.g. words), and the list of those
  55. * text elements with their confidence values.
  56. *
  57. * param image Input image CV_8UC1 or CV_8UC3 with a single text line (or word).
  58. *
  59. *
  60. * text elements found (e.g. words).
  61. *
  62. * recognition of individual text elements found (e.g. words).
  63. *
  64. * for the recognition of individual text elements found (e.g. words).
  65. *
  66. * param component_level Only OCR_LEVEL_WORD is supported.
  67. * param min_confidence automatically generated
  68. * return automatically generated
  69. */
  70. public string run(Mat image, int min_confidence, int component_level)
  71. {
  72. ThrowIfDisposed();
  73. if (image != null) image.ThrowIfDisposed();
  74. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_run_10(nativeObj, image.nativeObj, min_confidence, component_level)));
  75. return retVal;
  76. }
  77. /**
  78. * Recognize text using HMM.
  79. *
  80. * Takes an image and a mask (where each connected component corresponds to a segmented character)
  81. * on input and returns recognized text in the output_text parameter. Optionally
  82. * provides also the Rects for individual text elements found (e.g. words), and the list of those
  83. * text elements with their confidence values.
  84. *
  85. * param image Input image CV_8UC1 or CV_8UC3 with a single text line (or word).
  86. *
  87. *
  88. * text elements found (e.g. words).
  89. *
  90. * recognition of individual text elements found (e.g. words).
  91. *
  92. * for the recognition of individual text elements found (e.g. words).
  93. *
  94. * param min_confidence automatically generated
  95. * return automatically generated
  96. */
  97. public string run(Mat image, int min_confidence)
  98. {
  99. ThrowIfDisposed();
  100. if (image != null) image.ThrowIfDisposed();
  101. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_run_11(nativeObj, image.nativeObj, min_confidence)));
  102. return retVal;
  103. }
  104. //
  105. // C++: String cv::text::OCRHMMDecoder::run(Mat image, Mat mask, int min_confidence, int component_level = 0)
  106. //
  107. public string run(Mat image, Mat mask, int min_confidence, int component_level)
  108. {
  109. ThrowIfDisposed();
  110. if (image != null) image.ThrowIfDisposed();
  111. if (mask != null) mask.ThrowIfDisposed();
  112. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_run_12(nativeObj, image.nativeObj, mask.nativeObj, min_confidence, component_level)));
  113. return retVal;
  114. }
  115. public string run(Mat image, Mat mask, int min_confidence)
  116. {
  117. ThrowIfDisposed();
  118. if (image != null) image.ThrowIfDisposed();
  119. if (mask != null) mask.ThrowIfDisposed();
  120. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_run_13(nativeObj, image.nativeObj, mask.nativeObj, min_confidence)));
  121. return retVal;
  122. }
  123. //
  124. // C++: static Ptr_OCRHMMDecoder cv::text::OCRHMMDecoder::create(Ptr_OCRHMMDecoder_ClassifierCallback classifier, String vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode = OCR_DECODER_VITERBI)
  125. //
  126. /**
  127. * Creates an instance of the OCRHMMDecoder class. Initializes HMMDecoder.
  128. *
  129. * param classifier The character classifier with built in feature extractor.
  130. *
  131. * param vocabulary The language vocabulary (chars when ascii english text). vocabulary.size()
  132. * must be equal to the number of classes of the classifier.
  133. *
  134. * param transition_probabilities_table Table with transition probabilities between character
  135. * pairs. cols == rows == vocabulary.size().
  136. *
  137. * param emission_probabilities_table Table with observation emission probabilities. cols ==
  138. * rows == vocabulary.size().
  139. *
  140. * param mode HMM Decoding algorithm. Only OCR_DECODER_VITERBI is available for the moment
  141. * (&lt;http://en.wikipedia.org/wiki/Viterbi_algorithm&gt;).
  142. * return automatically generated
  143. */
  144. public static OCRHMMDecoder create(OCRHMMDecoder_ClassifierCallback classifier, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode)
  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 OCRHMMDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_create_10(classifier.getNativeObjAddr(), vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj, mode)));
  150. }
  151. /**
  152. * Creates an instance of the OCRHMMDecoder 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. * (&lt;http://en.wikipedia.org/wiki/Viterbi_algorithm&gt;).
  166. * return automatically generated
  167. */
  168. public static OCRHMMDecoder create(OCRHMMDecoder_ClassifierCallback classifier, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table)
  169. {
  170. if (classifier != null) classifier.ThrowIfDisposed();
  171. if (transition_probabilities_table != null) transition_probabilities_table.ThrowIfDisposed();
  172. if (emission_probabilities_table != null) emission_probabilities_table.ThrowIfDisposed();
  173. return OCRHMMDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_create_11(classifier.getNativeObjAddr(), vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj)));
  174. }
  175. //
  176. // C++: static Ptr_OCRHMMDecoder cv::text::OCRHMMDecoder::create(String filename, String vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode = OCR_DECODER_VITERBI, int classifier = OCR_KNN_CLASSIFIER)
  177. //
  178. /**
  179. * Creates an instance of the OCRHMMDecoder class. Loads and initializes HMMDecoder from the specified path
  180. *
  181. *
  182. * param filename automatically generated
  183. * param vocabulary automatically generated
  184. * param transition_probabilities_table automatically generated
  185. * param emission_probabilities_table automatically generated
  186. * param mode automatically generated
  187. * param classifier automatically generated
  188. * return automatically generated
  189. */
  190. public static OCRHMMDecoder create(string filename, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode, int classifier)
  191. {
  192. if (transition_probabilities_table != null) transition_probabilities_table.ThrowIfDisposed();
  193. if (emission_probabilities_table != null) emission_probabilities_table.ThrowIfDisposed();
  194. return OCRHMMDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_create_12(filename, vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj, mode, classifier)));
  195. }
  196. /**
  197. * Creates an instance of the OCRHMMDecoder class. Loads and initializes HMMDecoder from the specified path
  198. *
  199. *
  200. * param filename automatically generated
  201. * param vocabulary automatically generated
  202. * param transition_probabilities_table automatically generated
  203. * param emission_probabilities_table automatically generated
  204. * param mode automatically generated
  205. * return automatically generated
  206. */
  207. public static OCRHMMDecoder create(string filename, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode)
  208. {
  209. if (transition_probabilities_table != null) transition_probabilities_table.ThrowIfDisposed();
  210. if (emission_probabilities_table != null) emission_probabilities_table.ThrowIfDisposed();
  211. return OCRHMMDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_create_13(filename, vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj, mode)));
  212. }
  213. /**
  214. * Creates an instance of the OCRHMMDecoder class. Loads and initializes HMMDecoder from the specified path
  215. *
  216. *
  217. * param filename automatically generated
  218. * param vocabulary automatically generated
  219. * param transition_probabilities_table automatically generated
  220. * param emission_probabilities_table automatically generated
  221. * return automatically generated
  222. */
  223. public static OCRHMMDecoder create(string filename, string vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table)
  224. {
  225. if (transition_probabilities_table != null) transition_probabilities_table.ThrowIfDisposed();
  226. if (emission_probabilities_table != null) emission_probabilities_table.ThrowIfDisposed();
  227. return OCRHMMDecoder.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_OCRHMMDecoder_create_14(filename, vocabulary, transition_probabilities_table.nativeObj, emission_probabilities_table.nativeObj)));
  228. }
  229. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  230. const string LIBNAME = "__Internal";
  231. #else
  232. const string LIBNAME = "opencvforunity";
  233. #endif
  234. // C++: String cv::text::OCRHMMDecoder::run(Mat image, int min_confidence, int component_level = 0)
  235. [DllImport(LIBNAME)]
  236. private static extern IntPtr text_OCRHMMDecoder_run_10(IntPtr nativeObj, IntPtr image_nativeObj, int min_confidence, int component_level);
  237. [DllImport(LIBNAME)]
  238. private static extern IntPtr text_OCRHMMDecoder_run_11(IntPtr nativeObj, IntPtr image_nativeObj, int min_confidence);
  239. // C++: String cv::text::OCRHMMDecoder::run(Mat image, Mat mask, int min_confidence, int component_level = 0)
  240. [DllImport(LIBNAME)]
  241. private static extern IntPtr text_OCRHMMDecoder_run_12(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr mask_nativeObj, int min_confidence, int component_level);
  242. [DllImport(LIBNAME)]
  243. private static extern IntPtr text_OCRHMMDecoder_run_13(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr mask_nativeObj, int min_confidence);
  244. // C++: static Ptr_OCRHMMDecoder cv::text::OCRHMMDecoder::create(Ptr_OCRHMMDecoder_ClassifierCallback classifier, String vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode = OCR_DECODER_VITERBI)
  245. [DllImport(LIBNAME)]
  246. private static extern IntPtr text_OCRHMMDecoder_create_10(IntPtr classifier_nativeObj, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj, int mode);
  247. [DllImport(LIBNAME)]
  248. private static extern IntPtr text_OCRHMMDecoder_create_11(IntPtr classifier_nativeObj, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj);
  249. // C++: static Ptr_OCRHMMDecoder cv::text::OCRHMMDecoder::create(String filename, String vocabulary, Mat transition_probabilities_table, Mat emission_probabilities_table, int mode = OCR_DECODER_VITERBI, int classifier = OCR_KNN_CLASSIFIER)
  250. [DllImport(LIBNAME)]
  251. private static extern IntPtr text_OCRHMMDecoder_create_12(string filename, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj, int mode, int classifier);
  252. [DllImport(LIBNAME)]
  253. private static extern IntPtr text_OCRHMMDecoder_create_13(string filename, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj, int mode);
  254. [DllImport(LIBNAME)]
  255. private static extern IntPtr text_OCRHMMDecoder_create_14(string filename, string vocabulary, IntPtr transition_probabilities_table_nativeObj, IntPtr emission_probabilities_table_nativeObj);
  256. // native support for java finalize()
  257. [DllImport(LIBNAME)]
  258. private static extern void text_OCRHMMDecoder_delete(IntPtr nativeObj);
  259. }
  260. }
  261. #endif