ocr.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*M//////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef __OPENCV_TEXT_OCR_HPP__
  44. #define __OPENCV_TEXT_OCR_HPP__
  45. #include <vector>
  46. #include <string>
  47. namespace cv
  48. {
  49. namespace text
  50. {
  51. //! @addtogroup text_recognize
  52. //! @{
  53. enum
  54. {
  55. OCR_LEVEL_WORD,
  56. OCR_LEVEL_TEXTLINE
  57. };
  58. //! Tesseract.PageSegMode Enumeration
  59. enum page_seg_mode
  60. {
  61. PSM_OSD_ONLY,
  62. PSM_AUTO_OSD,
  63. PSM_AUTO_ONLY,
  64. PSM_AUTO,
  65. PSM_SINGLE_COLUMN,
  66. PSM_SINGLE_BLOCK_VERT_TEXT,
  67. PSM_SINGLE_BLOCK,
  68. PSM_SINGLE_LINE,
  69. PSM_SINGLE_WORD,
  70. PSM_CIRCLE_WORD,
  71. PSM_SINGLE_CHAR
  72. };
  73. //! Tesseract.OcrEngineMode Enumeration
  74. enum ocr_engine_mode
  75. {
  76. OEM_TESSERACT_ONLY,
  77. OEM_CUBE_ONLY,
  78. OEM_TESSERACT_CUBE_COMBINED,
  79. OEM_DEFAULT
  80. };
  81. //base class BaseOCR declares a common API that would be used in a typical text recognition scenario
  82. class CV_EXPORTS_W BaseOCR
  83. {
  84. public:
  85. virtual ~BaseOCR() {};
  86. virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  87. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  88. int component_level=0) = 0;
  89. virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  90. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  91. int component_level=0) = 0;
  92. };
  93. /** @brief OCRTesseract class provides an interface with the tesseract-ocr API (v3.02.02) in C++.
  94. Notice that it is compiled only when tesseract-ocr is correctly installed.
  95. @note
  96. - (C++) An example of OCRTesseract recognition combined with scene text detection can be found
  97. at the end_to_end_recognition demo:
  98. <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/end_to_end_recognition.cpp>
  99. - (C++) Another example of OCRTesseract recognition combined with scene text detection can be
  100. found at the webcam_demo:
  101. <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp>
  102. */
  103. class CV_EXPORTS_W OCRTesseract : public BaseOCR
  104. {
  105. public:
  106. /** @brief Recognize text using the tesseract-ocr API.
  107. Takes image on input and returns recognized text in the output_text parameter. Optionally
  108. provides also the Rects for individual text elements found (e.g. words), and the list of those
  109. text elements with their confidence values.
  110. @param image Input image CV_8UC1 or CV_8UC3
  111. @param output_text Output text of the tesseract-ocr.
  112. @param component_rects If provided the method will output a list of Rects for the individual
  113. text elements found (e.g. words or text lines).
  114. @param component_texts If provided the method will output a list of text strings for the
  115. recognition of individual text elements found (e.g. words or text lines).
  116. @param component_confidences If provided the method will output a list of confidence values
  117. for the recognition of individual text elements found (e.g. words or text lines).
  118. @param component_level OCR_LEVEL_WORD (by default), or OCR_LEVEL_TEXTLINE.
  119. */
  120. virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  121. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  122. int component_level=0);
  123. virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  124. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  125. int component_level=0);
  126. // aliases for scripting
  127. CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
  128. CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
  129. CV_WRAP virtual void setWhiteList(const String& char_whitelist) = 0;
  130. /** @brief Creates an instance of the OCRTesseract class. Initializes Tesseract.
  131. @param datapath the name of the parent directory of tessdata ended with "/", or NULL to use the
  132. system's default directory.
  133. @param language an ISO 639-3 code or NULL will default to "eng".
  134. @param char_whitelist specifies the list of characters used for recognition. NULL defaults to
  135. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
  136. @param oem tesseract-ocr offers different OCR Engine Modes (OEM), by default
  137. tesseract::OEM_DEFAULT is used. See the tesseract-ocr API documentation for other possible
  138. values.
  139. @param psmode tesseract-ocr offers different Page Segmentation Modes (PSM) tesseract::PSM_AUTO
  140. (fully automatic layout analysis) is used. See the tesseract-ocr API documentation for other
  141. possible values.
  142. */
  143. CV_WRAP static Ptr<OCRTesseract> create(const char* datapath=NULL, const char* language=NULL,
  144. const char* char_whitelist=NULL, int oem=OEM_DEFAULT, int psmode=PSM_AUTO);
  145. };
  146. /* OCR HMM Decoder */
  147. enum decoder_mode
  148. {
  149. OCR_DECODER_VITERBI = 0 // Other algorithms may be added
  150. };
  151. /* OCR classifier type*/
  152. enum classifier_type
  153. {
  154. OCR_KNN_CLASSIFIER = 0,
  155. OCR_CNN_CLASSIFIER = 1
  156. };
  157. /** @brief OCRHMMDecoder class provides an interface for OCR using Hidden Markov Models.
  158. @note
  159. - (C++) An example on using OCRHMMDecoder recognition combined with scene text detection can
  160. be found at the webcam_demo sample:
  161. <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/webcam_demo.cpp>
  162. */
  163. class CV_EXPORTS_W OCRHMMDecoder : public BaseOCR
  164. {
  165. public:
  166. /** @brief Callback with the character classifier is made a class.
  167. This way it hides the feature extractor and the classifier itself, so developers can write
  168. their own OCR code.
  169. The default character classifier and feature extractor can be loaded using the utility function
  170. loadOCRHMMClassifierNM and KNN model provided in
  171. <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/OCRHMM_knn_model_data.xml.gz>.
  172. */
  173. class CV_EXPORTS_W ClassifierCallback
  174. {
  175. public:
  176. virtual ~ClassifierCallback() { }
  177. /** @brief The character classifier must return a (ranked list of) class(es) id('s)
  178. @param image Input image CV_8UC1 or CV_8UC3 with a single letter.
  179. @param out_class The classifier returns the character class categorical label, or list of
  180. class labels, to which the input image corresponds.
  181. @param out_confidence The classifier returns the probability of the input image
  182. corresponding to each classes in out_class.
  183. */
  184. virtual void eval( InputArray image, std::vector<int>& out_class, std::vector<double>& out_confidence);
  185. };
  186. public:
  187. /** @brief Recognize text using HMM.
  188. Takes binary image on input and returns recognized text in the output_text parameter. Optionally
  189. provides also the Rects for individual text elements found (e.g. words), and the list of those
  190. text elements with their confidence values.
  191. @param image Input binary image CV_8UC1 with a single text line (or word).
  192. @param output_text Output text. Most likely character sequence found by the HMM decoder.
  193. @param component_rects If provided the method will output a list of Rects for the individual
  194. text elements found (e.g. words).
  195. @param component_texts If provided the method will output a list of text strings for the
  196. recognition of individual text elements found (e.g. words).
  197. @param component_confidences If provided the method will output a list of confidence values
  198. for the recognition of individual text elements found (e.g. words).
  199. @param component_level Only OCR_LEVEL_WORD is supported.
  200. */
  201. virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  202. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  203. int component_level=0);
  204. /** @brief Recognize text using HMM.
  205. Takes an image and a mask (where each connected component corresponds to a segmented character)
  206. on input and returns recognized text in the output_text parameter. Optionally
  207. provides also the Rects for individual text elements found (e.g. words), and the list of those
  208. text elements with their confidence values.
  209. @param image Input image CV_8UC1 or CV_8UC3 with a single text line (or word).
  210. @param mask Input binary image CV_8UC1 same size as input image. Each connected component in mask corresponds to a segmented character in the input image.
  211. @param output_text Output text. Most likely character sequence found by the HMM decoder.
  212. @param component_rects If provided the method will output a list of Rects for the individual
  213. text elements found (e.g. words).
  214. @param component_texts If provided the method will output a list of text strings for the
  215. recognition of individual text elements found (e.g. words).
  216. @param component_confidences If provided the method will output a list of confidence values
  217. for the recognition of individual text elements found (e.g. words).
  218. @param component_level Only OCR_LEVEL_WORD is supported.
  219. */
  220. virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  221. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  222. int component_level=0);
  223. // aliases for scripting
  224. CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
  225. CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
  226. /** @brief Creates an instance of the OCRHMMDecoder class. Initializes HMMDecoder.
  227. @param classifier The character classifier with built in feature extractor.
  228. @param vocabulary The language vocabulary (chars when ascii english text). vocabulary.size()
  229. must be equal to the number of classes of the classifier.
  230. @param transition_probabilities_table Table with transition probabilities between character
  231. pairs. cols == rows == vocabulary.size().
  232. @param emission_probabilities_table Table with observation emission probabilities. cols ==
  233. rows == vocabulary.size().
  234. @param mode HMM Decoding algorithm. Only OCR_DECODER_VITERBI is available for the moment
  235. (<http://en.wikipedia.org/wiki/Viterbi_algorithm>).
  236. */
  237. static Ptr<OCRHMMDecoder> create(const Ptr<OCRHMMDecoder::ClassifierCallback> classifier,// The character classifier with built in feature extractor
  238. const std::string& vocabulary, // The language vocabulary (chars when ASCII English text)
  239. // size() must be equal to the number of classes
  240. InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
  241. // cols == rows == vocabulary.size()
  242. InputArray emission_probabilities_table, // Table with observation emission probabilities
  243. // cols == rows == vocabulary.size()
  244. decoder_mode mode = OCR_DECODER_VITERBI); // HMM Decoding algorithm (only Viterbi for the moment)
  245. CV_WRAP static Ptr<OCRHMMDecoder> create(const Ptr<OCRHMMDecoder::ClassifierCallback> classifier,// The character classifier with built in feature extractor
  246. const String& vocabulary, // The language vocabulary (chars when ASCII English text)
  247. // size() must be equal to the number of classes
  248. InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
  249. // cols == rows == vocabulary.size()
  250. InputArray emission_probabilities_table, // Table with observation emission probabilities
  251. // cols == rows == vocabulary.size()
  252. int mode = OCR_DECODER_VITERBI); // HMM Decoding algorithm (only Viterbi for the moment)
  253. /** @brief Creates an instance of the OCRHMMDecoder class. Loads and initializes HMMDecoder from the specified path
  254. @overload
  255. */
  256. CV_WRAP static Ptr<OCRHMMDecoder> create(const String& filename,
  257. const String& vocabulary, // The language vocabulary (chars when ASCII English text)
  258. // size() must be equal to the number of classes
  259. InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
  260. // cols == rows == vocabulary.size()
  261. InputArray emission_probabilities_table, // Table with observation emission probabilities
  262. // cols == rows == vocabulary.size()
  263. int mode = OCR_DECODER_VITERBI, // HMM Decoding algorithm (only Viterbi for the moment)
  264. int classifier = OCR_KNN_CLASSIFIER); // The character classifier type
  265. protected:
  266. Ptr<OCRHMMDecoder::ClassifierCallback> classifier;
  267. std::string vocabulary;
  268. Mat transition_p;
  269. Mat emission_p;
  270. decoder_mode mode;
  271. };
  272. /** @brief Allow to implicitly load the default character classifier when creating an OCRHMMDecoder object.
  273. @param filename The XML or YAML file with the classifier model (e.g. OCRHMM_knn_model_data.xml)
  274. The KNN default classifier is based in the scene text recognition method proposed by Lukás Neumann &
  275. Jiri Matas in [Neumann11b]. Basically, the region (contour) in the input image is normalized to a
  276. fixed size, while retaining the centroid and aspect ratio, in order to extract a feature vector
  277. based on gradient orientations along the chain-code of its perimeter. Then, the region is classified
  278. using a KNN model trained with synthetic data of rendered characters with different standard font
  279. types.
  280. @deprecated loadOCRHMMClassifier instead
  281. */
  282. CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifierNM(const String& filename);
  283. /** @brief Allow to implicitly load the default character classifier when creating an OCRHMMDecoder object.
  284. @param filename The XML or YAML file with the classifier model (e.g. OCRBeamSearch_CNN_model_data.xml.gz)
  285. The CNN default classifier is based in the scene text recognition method proposed by Adam Coates &
  286. Andrew NG in [Coates11a]. The character classifier consists in a Single Layer Convolutional Neural Network and
  287. a linear classifier. It is applied to the input image in a sliding window fashion, providing a set of recognitions
  288. at each window location.
  289. @deprecated use loadOCRHMMClassifier instead
  290. */
  291. CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifierCNN(const String& filename);
  292. /** @brief Allow to implicitly load the default character classifier when creating an OCRHMMDecoder object.
  293. @param filename The XML or YAML file with the classifier model (e.g. OCRBeamSearch_CNN_model_data.xml.gz)
  294. @param classifier Can be one of classifier_type enum values.
  295. */
  296. CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifier(const String& filename, int classifier);
  297. //! @}
  298. /** @brief Utility function to create a tailored language model transitions table from a given list of words (lexicon).
  299. *
  300. * @param vocabulary The language vocabulary (chars when ASCII English text).
  301. *
  302. * @param lexicon The list of words that are expected to be found in a particular image.
  303. *
  304. * @param transition_probabilities_table Output table with transition probabilities between character pairs. cols == rows == vocabulary.size().
  305. *
  306. * The function calculate frequency statistics of character pairs from the given lexicon and fills the output transition_probabilities_table with them. The transition_probabilities_table can be used as input in the OCRHMMDecoder::create() and OCRBeamSearchDecoder::create() methods.
  307. * @note
  308. * - (C++) An alternative would be to load the default generic language transition table provided in the text module samples folder (created from ispell 42869 english words list) :
  309. * <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/OCRHMM_transitions_table.xml>
  310. **/
  311. CV_EXPORTS void createOCRHMMTransitionsTable(std::string& vocabulary, std::vector<std::string>& lexicon, OutputArray transition_probabilities_table);
  312. CV_EXPORTS_W Mat createOCRHMMTransitionsTable(const String& vocabulary, std::vector<cv::String>& lexicon);
  313. /* OCR BeamSearch Decoder */
  314. /** @brief OCRBeamSearchDecoder class provides an interface for OCR using Beam Search algorithm.
  315. @note
  316. - (C++) An example on using OCRBeamSearchDecoder recognition combined with scene text detection can
  317. be found at the demo sample:
  318. <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/word_recognition.cpp>
  319. */
  320. class CV_EXPORTS_W OCRBeamSearchDecoder : public BaseOCR
  321. {
  322. public:
  323. /** @brief Callback with the character classifier is made a class.
  324. This way it hides the feature extractor and the classifier itself, so developers can write
  325. their own OCR code.
  326. The default character classifier and feature extractor can be loaded using the utility funtion
  327. loadOCRBeamSearchClassifierCNN with all its parameters provided in
  328. <https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/OCRBeamSearch_CNN_model_data.xml.gz>.
  329. */
  330. class CV_EXPORTS_W ClassifierCallback
  331. {
  332. public:
  333. virtual ~ClassifierCallback() { }
  334. /** @brief The character classifier must return a (ranked list of) class(es) id('s)
  335. @param image Input image CV_8UC1 or CV_8UC3 with a single letter.
  336. @param recognition_probabilities For each of the N characters found the classifier returns a list with
  337. class probabilities for each class.
  338. @param oversegmentation The classifier returns a list of N+1 character locations' x-coordinates,
  339. including 0 as start-sequence location.
  340. */
  341. virtual void eval( InputArray image, std::vector< std::vector<double> >& recognition_probabilities, std::vector<int>& oversegmentation );
  342. int getWindowSize() {return 0;}
  343. int getStepSize() {return 0;}
  344. };
  345. public:
  346. /** @brief Recognize text using Beam Search.
  347. Takes image on input and returns recognized text in the output_text parameter. Optionally
  348. provides also the Rects for individual text elements found (e.g. words), and the list of those
  349. text elements with their confidence values.
  350. @param image Input binary image CV_8UC1 with a single text line (or word).
  351. @param output_text Output text. Most likely character sequence found by the HMM decoder.
  352. @param component_rects If provided the method will output a list of Rects for the individual
  353. text elements found (e.g. words).
  354. @param component_texts If provided the method will output a list of text strings for the
  355. recognition of individual text elements found (e.g. words).
  356. @param component_confidences If provided the method will output a list of confidence values
  357. for the recognition of individual text elements found (e.g. words).
  358. @param component_level Only OCR_LEVEL_WORD is supported.
  359. */
  360. virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  361. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  362. int component_level=0);
  363. virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
  364. std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
  365. int component_level=0);
  366. // aliases for scripting
  367. CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
  368. CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
  369. /** @brief Creates an instance of the OCRBeamSearchDecoder class. Initializes HMMDecoder.
  370. @param classifier The character classifier with built in feature extractor.
  371. @param vocabulary The language vocabulary (chars when ASCII English text). vocabulary.size()
  372. must be equal to the number of classes of the classifier.
  373. @param transition_probabilities_table Table with transition probabilities between character
  374. pairs. cols == rows == vocabulary.size().
  375. @param emission_probabilities_table Table with observation emission probabilities. cols ==
  376. rows == vocabulary.size().
  377. @param mode HMM Decoding algorithm. Only OCR_DECODER_VITERBI is available for the moment
  378. (<http://en.wikipedia.org/wiki/Viterbi_algorithm>).
  379. @param beam_size Size of the beam in Beam Search algorithm.
  380. */
  381. static Ptr<OCRBeamSearchDecoder> create(const Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier,// The character classifier with built in feature extractor
  382. const std::string& vocabulary, // The language vocabulary (chars when ASCII English text)
  383. // size() must be equal to the number of classes
  384. InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
  385. // cols == rows == vocabulary.size()
  386. InputArray emission_probabilities_table, // Table with observation emission probabilities
  387. // cols == rows == vocabulary.size()
  388. decoder_mode mode = OCR_DECODER_VITERBI, // HMM Decoding algorithm (only Viterbi for the moment)
  389. int beam_size = 500); // Size of the beam in Beam Search algorithm
  390. CV_WRAP static Ptr<OCRBeamSearchDecoder> create(const Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier, // The character classifier with built in feature extractor
  391. const String& vocabulary, // The language vocabulary (chars when ASCII English text)
  392. // size() must be equal to the number of classes
  393. InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
  394. // cols == rows == vocabulary.size()
  395. InputArray emission_probabilities_table, // Table with observation emission probabilities
  396. // cols == rows == vocabulary.size()
  397. int mode = OCR_DECODER_VITERBI, // HMM Decoding algorithm (only Viterbi for the moment)
  398. int beam_size = 500); // Size of the beam in Beam Search algorithm
  399. /** @brief Creates an instance of the OCRBeamSearchDecoder class. Initializes HMMDecoder from the specified path.
  400. @overload
  401. */
  402. CV_WRAP static Ptr<OCRBeamSearchDecoder> create(const String& filename, // The character classifier file
  403. const String& vocabulary, // The language vocabulary (chars when ASCII English text)
  404. // size() must be equal to the number of classes
  405. InputArray transition_probabilities_table, // Table with transition probabilities between character pairs
  406. // cols == rows == vocabulary.size()
  407. InputArray emission_probabilities_table, // Table with observation emission probabilities
  408. // cols == rows == vocabulary.size()
  409. int mode = OCR_DECODER_VITERBI, // HMM Decoding algorithm (only Viterbi for the moment)
  410. int beam_size = 500);
  411. protected:
  412. Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier;
  413. std::string vocabulary;
  414. Mat transition_p;
  415. Mat emission_p;
  416. decoder_mode mode;
  417. int beam_size;
  418. };
  419. /** @brief Allow to implicitly load the default character classifier when creating an OCRBeamSearchDecoder object.
  420. @param filename The XML or YAML file with the classifier model (e.g. OCRBeamSearch_CNN_model_data.xml.gz)
  421. The CNN default classifier is based in the scene text recognition method proposed by Adam Coates &
  422. Andrew NG in [Coates11a]. The character classifier consists in a Single Layer Convolutional Neural Network and
  423. a linear classifier. It is applied to the input image in a sliding window fashion, providing a set of recognitions
  424. at each window location.
  425. */
  426. CV_EXPORTS_W Ptr<OCRBeamSearchDecoder::ClassifierCallback> loadOCRBeamSearchClassifierCNN(const String& filename);
  427. /** @brief OCRHolisticWordRecognizer class provides the functionallity of segmented wordspotting.
  428. * Given a predefined vocabulary , a DictNet is employed to select the most probable
  429. * word given an input image.
  430. *
  431. * DictNet is described in detail in:
  432. * Max Jaderberg et al.: Reading Text in the Wild with Convolutional Neural Networks, IJCV 2015
  433. * http://arxiv.org/abs/1412.1842
  434. */
  435. class CV_EXPORTS OCRHolisticWordRecognizer : public BaseOCR
  436. {
  437. public:
  438. virtual void run(Mat& image,
  439. std::string& output_text,
  440. std::vector<Rect>* component_rects = NULL,
  441. std::vector<std::string>* component_texts = NULL,
  442. std::vector<float>* component_confidences = NULL,
  443. int component_level = OCR_LEVEL_WORD) = 0;
  444. /** @brief Recognize text using a segmentation based word-spotting/classifier cnn.
  445. Takes image on input and returns recognized text in the output_text parameter. Optionally
  446. provides also the Rects for individual text elements found (e.g. words), and the list of those
  447. text elements with their confidence values.
  448. @param image Input image CV_8UC1 or CV_8UC3
  449. @param mask is totally ignored and is only available for compatibillity reasons
  450. @param output_text Output text of the the word spoting, always one that exists in the dictionary.
  451. @param component_rects Not applicable for word spotting can be be NULL if not, a single elemnt will
  452. be put in the vector.
  453. @param component_texts Not applicable for word spotting can be be NULL if not, a single elemnt will
  454. be put in the vector.
  455. @param component_confidences Not applicable for word spotting can be be NULL if not, a single elemnt will
  456. be put in the vector.
  457. @param component_level must be OCR_LEVEL_WORD.
  458. */
  459. virtual void run(Mat& image,
  460. Mat& mask,
  461. std::string& output_text,
  462. std::vector<Rect>* component_rects = NULL,
  463. std::vector<std::string>* component_texts = NULL,
  464. std::vector<float>* component_confidences = NULL,
  465. int component_level = OCR_LEVEL_WORD) = 0;
  466. /** @brief Creates an instance of the OCRHolisticWordRecognizer class.
  467. */
  468. static Ptr<OCRHolisticWordRecognizer> create(const std::string &archFilename,
  469. const std::string &weightsFilename,
  470. const std::string &wordsFilename);
  471. };
  472. //! @}
  473. }} // cv::text::
  474. #endif // _OPENCV_TEXT_OCR_HPP_