ocr.hpp 30 KB

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