tesseract  3.05.02
baseapi.h
Go to the documentation of this file.
1 // File: baseapi.h
3 // Description: Simple API for calling tesseract.
4 // Author: Ray Smith
5 // Created: Fri Oct 06 15:35:01 PDT 2006
6 //
7 // (C) Copyright 2006, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #ifndef TESSERACT_API_BASEAPI_H__
21 #define TESSERACT_API_BASEAPI_H__
22 
23 #define TESSERACT_VERSION_STR "3.05.02"
24 #define TESSERACT_VERSION 0x030502
25 #define MAKE_VERSION(major, minor, patch) (((major) << 16) | ((minor) << 8) | \
26  (patch))
27 
28 #include <stdio.h>
29 // To avoid collision with other typenames include the ABSOLUTE MINIMUM
30 // complexity of includes here. Use forward declarations wherever possible
31 // and hide includes of complex types in baseapi.cpp.
32 #include "platform.h"
33 #include "apitypes.h"
34 #include "thresholder.h"
35 #include "unichar.h"
36 #include "tesscallback.h"
37 #include "publictypes.h"
38 #include "pageiterator.h"
39 #include "resultiterator.h"
40 
41 template <typename T> class GenericVector;
42 class PAGE_RES;
43 class PAGE_RES_IT;
44 class ParagraphModel;
45 struct BlamerBundle;
46 class BLOCK_LIST;
47 class DENORM;
48 class MATRIX;
49 class ROW;
50 class STRING;
51 class WERD;
52 struct Pix;
53 struct Box;
54 struct Pixa;
55 struct Boxa;
56 class ETEXT_DESC;
57 struct OSResults;
58 class TBOX;
59 class UNICHARSET;
60 class WERD_CHOICE_LIST;
61 
64 struct TBLOB;
65 
66 namespace tesseract {
67 
68 #ifndef NO_CUBE_BUILD
69 class CubeRecoContext;
70 #endif // NO_CUBE_BUILD
71 class Dawg;
72 class Dict;
73 class EquationDetect;
74 class PageIterator;
75 class LTRResultIterator;
76 class ResultIterator;
77 class MutableIterator;
78 class TessResultRenderer;
79 class Tesseract;
80 class Trie;
81 class Wordrec;
82 
83 typedef int (Dict::*DictFunc)(void* void_dawg_args,
84  UNICHAR_ID unichar_id, bool word_end) const;
85 typedef double (Dict::*ProbabilityInContextFunc)(const char* lang,
86  const char* context,
87  int context_bytes,
88  const char* character,
89  int character_bytes);
90 typedef float (Dict::*ParamsModelClassifyFunc)(
91  const char *lang, void *path);
92 typedef void (Wordrec::*FillLatticeFunc)(const MATRIX &ratings,
93  const WERD_CHOICE_LIST &best_choices,
94  const UNICHARSET &unicharset,
95  BlamerBundle *blamer_bundle);
98 
108  public:
109  TessBaseAPI();
110  virtual ~TessBaseAPI();
111 
115  static const char* Version();
116 
124  static size_t getOpenCLDevice(void **device);
125 
130  static void CatchSignals();
131 
136  void SetInputName(const char* name);
144  const char* GetInputName();
145  // Takes ownership of the input pix.
146  void SetInputImage(Pix *pix);
147  Pix* GetInputImage();
148  int GetSourceYResolution();
149  const char* GetDatapath();
150 
152  void SetOutputName(const char* name);
153 
167  bool SetVariable(const char* name, const char* value);
168  bool SetDebugVariable(const char* name, const char* value);
169 
174  bool GetIntVariable(const char *name, int *value) const;
175  bool GetBoolVariable(const char *name, bool *value) const;
176  bool GetDoubleVariable(const char *name, double *value) const;
177 
182  const char *GetStringVariable(const char *name) const;
183 
187  void PrintVariables(FILE *fp) const;
188 
192  bool GetVariableAsString(const char *name, STRING *val);
193 
232  int Init(const char* datapath, const char* language, OcrEngineMode mode,
233  char **configs, int configs_size,
234  const GenericVector<STRING> *vars_vec,
235  const GenericVector<STRING> *vars_values,
236  bool set_only_non_debug_params);
237  int Init(const char* datapath, const char* language, OcrEngineMode oem) {
238  return Init(datapath, language, oem, NULL, 0, NULL, NULL, false);
239  }
240  int Init(const char* datapath, const char* language) {
241  return Init(datapath, language, OEM_DEFAULT, NULL, 0, NULL, NULL, false);
242  }
243 
252  const char* GetInitLanguagesAsString() const;
253 
259  void GetLoadedLanguagesAsVector(GenericVector<STRING>* langs) const;
260 
264  void GetAvailableLanguagesAsVector(GenericVector<STRING>* langs) const;
265 
272  int InitLangMod(const char* datapath, const char* language);
273 
278  void InitForAnalysePage();
279 
286  void ReadConfigFile(const char* filename);
288  void ReadDebugConfigFile(const char* filename);
289 
295  void SetPageSegMode(PageSegMode mode);
296 
298  PageSegMode GetPageSegMode() const;
299 
317  char* TesseractRect(const unsigned char* imagedata,
318  int bytes_per_pixel, int bytes_per_line,
319  int left, int top, int width, int height);
320 
325  void ClearAdaptiveClassifier();
326 
333  /* @{ */
334 
342  void SetImage(const unsigned char* imagedata, int width, int height,
343  int bytes_per_pixel, int bytes_per_line);
344 
353  void SetImage(Pix* pix);
354 
359  void SetSourceResolution(int ppi);
360 
366  void SetRectangle(int left, int top, int width, int height);
367 
375  void SetThresholder(ImageThresholder* thresholder) {
376  delete thresholder_;
377  thresholder_ = thresholder;
378  ClearResults();
379  }
380 
386  Pix* GetThresholdedImage();
387 
393  Boxa* GetRegions(Pixa** pixa);
394 
406  Boxa* GetTextlines(const bool raw_image, const int raw_padding,
407  Pixa** pixa, int** blockids, int** paraids);
408  /*
409  Helper method to extract from the thresholded image. (most common usage)
410  */
411  Boxa* GetTextlines(Pixa** pixa, int** blockids) {
412  return GetTextlines(false, 0, pixa, blockids, NULL);
413  }
414 
423  Boxa* GetStrips(Pixa** pixa, int** blockids);
424 
430  Boxa* GetWords(Pixa** pixa);
431 
440  Boxa* GetConnectedComponents(Pixa** cc);
441 
455  Boxa* GetComponentImages(const PageIteratorLevel level,
456  const bool text_only, const bool raw_image,
457  const int raw_padding,
458  Pixa** pixa, int** blockids, int** paraids);
459  // Helper function to get binary images with no padding (most common usage).
461  const bool text_only,
462  Pixa** pixa, int** blockids) {
463  return GetComponentImages(level, text_only, false, 0, pixa, blockids, NULL);
464  }
465 
472  int GetThresholdedImageScaleFactor() const;
473 
479  void DumpPGM(const char* filename);
480 
496  PageIterator* AnalyseLayout();
497  PageIterator* AnalyseLayout(bool merge_similar_words);
498 
505  int Recognize(ETEXT_DESC* monitor);
506 
513  int RecognizeForChopTest(ETEXT_DESC* monitor);
514 
537  bool ProcessPages(const char* filename, const char* retry_config,
538  int timeout_millisec, TessResultRenderer* renderer);
539  // Does the real work of ProcessPages.
540  bool ProcessPagesInternal(const char* filename, const char* retry_config,
541  int timeout_millisec, TessResultRenderer* renderer);
542 
552  bool ProcessPage(Pix* pix, int page_index, const char* filename,
553  const char* retry_config, int timeout_millisec,
554  TessResultRenderer* renderer);
555 
564  ResultIterator* GetIterator();
565 
574  MutableIterator* GetMutableIterator();
575 
580  char* GetUTF8Text();
581 
590  char* GetHOCRText(ETEXT_DESC* monitor, int page_number);
591 
597  char* GetHOCRText(int page_number);
598 
603  char* GetTSVText(int page_number);
604 
612  char* GetBoxText(int page_number);
613 
619  char* GetUNLVText();
620 
629  bool DetectOrientationScript(int* orient_deg, float* orient_conf, const char** script_name, float* script_conf);
630 
636  char* GetOsdText(int page_number);
637 
639  int MeanTextConf();
646  int* AllWordConfidences();
647 
658  bool AdaptToWordStr(PageSegMode mode, const char* wordstr);
659 
666  void Clear();
667 
674  void End();
675 
683  static void ClearPersistentCache();
684 
691  int IsValidWord(const char *word);
692  // Returns true if utf8_character is defined in the UniCharset.
693  bool IsValidCharacter(const char *utf8_character);
694 
695 
696  bool GetTextDirection(int* out_offset, float* out_slope);
697 
699  void SetDictFunc(DictFunc f);
700 
704  void SetProbabilityInContextFunc(ProbabilityInContextFunc f);
705 
707  void SetFillLatticeFunc(FillLatticeFunc f);
708 
713  bool DetectOS(OSResults*);
714 
716  void GetFeaturesForBlob(TBLOB* blob, INT_FEATURE_STRUCT* int_features,
717  int* num_features, int* feature_outline_index);
718 
723  static ROW* FindRowForBox(BLOCK_LIST* blocks, int left, int top,
724  int right, int bottom);
725 
730  void RunAdaptiveClassifier(TBLOB* blob,
731  int num_max_matches,
732  int* unichar_ids,
733  float* ratings,
734  int* num_matches_returned);
735 
737  const char* GetUnichar(int unichar_id);
738 
740  const Dawg *GetDawg(int i) const;
741 
743  int NumDawgs() const;
744 
746  static ROW *MakeTessOCRRow(float baseline, float xheight,
747  float descender, float ascender);
748 
750  static TBLOB *MakeTBLOB(Pix *pix);
751 
757  static void NormalizeTBLOB(TBLOB *tblob, ROW *row, bool numeric_mode);
758 
759  Tesseract* tesseract() const { return tesseract_; }
760 
761  OcrEngineMode oem() const { return last_oem_requested_; }
762 
763  void InitTruthCallback(TruthCallback *cb) { truth_cb_ = cb; }
764 
765 #ifndef NO_CUBE_BUILD
766 
767  CubeRecoContext *GetCubeRecoContext() const;
768 #endif // NO_CUBE_BUILD
769 
770  void set_min_orientation_margin(double margin);
771 
776  void GetBlockTextOrientations(int** block_orientation,
777  bool** vertical_writing);
778 
780  BLOCK_LIST* FindLinesCreateBlockList();
781 
787  static void DeleteBlockList(BLOCK_LIST* block_list);
788  /* @} */
789 
790  protected:
791 
793  TESS_LOCAL bool InternalSetImage();
794 
799  TESS_LOCAL virtual void Threshold(Pix** pix);
800 
805  TESS_LOCAL int FindLines();
806 
808  void ClearResults();
809 
815  TESS_LOCAL LTRResultIterator* GetLTRIterator();
816 
823  TESS_LOCAL int TextLength(int* blob_count);
824 
826  /* @{ */
827 
832  TESS_LOCAL void AdaptToCharacter(const char *unichar_repr,
833  int length,
834  float baseline,
835  float xheight,
836  float descender,
837  float ascender);
838 
840  TESS_LOCAL PAGE_RES* RecognitionPass1(BLOCK_LIST* block_list);
841  TESS_LOCAL PAGE_RES* RecognitionPass2(BLOCK_LIST* block_list,
842  PAGE_RES* pass1_result);
843 
845  TESS_LOCAL void DetectParagraphs(bool after_text_recognition);
846 
851  TESS_LOCAL static int TesseractExtractResult(char** text,
852  int** lengths,
853  float** costs,
854  int** x0,
855  int** y0,
856  int** x1,
857  int** y1,
858  PAGE_RES* page_res);
859 
860  TESS_LOCAL const PAGE_RES* GetPageRes() const { return page_res_; }
861  /* @} */
862 
863 
864  protected:
870  BLOCK_LIST* block_list_;
879 
884  /* @{ */
891  /* @} */
892 
893  private:
894  // A list of image filenames gets special consideration
895  bool ProcessPagesFileList(FILE *fp,
896  STRING *buf,
897  const char* retry_config, int timeout_millisec,
898  TessResultRenderer* renderer,
899  int tessedit_page_number);
900  // TIFF supports multipage so gets special consideration.
901  bool ProcessPagesMultipageTiff(const unsigned char *data,
902  size_t size,
903  const char* filename,
904  const char* retry_config,
905  int timeout_millisec,
906  TessResultRenderer* renderer,
907  int tessedit_page_number);
908  // There's currently no way to pass a document title from the
909  // Tesseract command line, and we have multiple places that choose
910  // to set the title to an empty string. Using a single named
911  // variable will hopefully reduce confusion if the situation changes
912  // in the future.
913  const char *unknown_title_;
914 }; // class TessBaseAPI.
915 
917 STRING HOcrEscape(const char* text);
918 } // namespace tesseract.
919 
920 #endif // TESSERACT_API_BASEAPI_H__
void SetThresholder(ImageThresholder *thresholder)
Definition: baseapi.h:375
Boxa * GetTextlines(Pixa **pixa, int **blockids)
Definition: baseapi.h:411
void DetectParagraphs(int debug_level, GenericVector< RowInfo > *row_infos, GenericVector< PARA *> *row_owners, PARA_LIST *paragraphs, GenericVector< ParagraphModel *> *models)
STRING * output_file_
Name used by debug code.
Definition: baseapi.h:873
struct TessBaseAPI TessBaseAPI
Definition: capi.h:86
#define TESS_API
Definition: platform.h:81
TESS_LOCAL const PAGE_RES * GetPageRes() const
Definition: baseapi.h:860
int Init(const char *datapath, const char *language, OcrEngineMode oem)
Definition: baseapi.h:237
TessCallback4< const UNICHARSET &, int, PageIterator *, Pix * > TruthCallback
Definition: baseapi.h:97
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:871
Definition: matrix.h:572
void InitTruthCallback(TruthCallback *cb)
Definition: baseapi.h:763
INT_FEATURE_STRUCT * INT_FEATURE
Definition: baseapi.h:62
OcrEngineMode oem() const
Definition: baseapi.h:761
Tesseract * osd_tesseract_
For orientation & script detection.
Definition: baseapi.h:866
float(Dict::* ParamsModelClassifyFunc)(const char *lang, void *path)
Definition: baseapi.h:90
int(Dict::* DictFunc)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
Definition: baseapi.h:83
Definition: werd.h:60
double(Dict::* ProbabilityInContextFunc)(const char *lang, const char *context, int context_bytes, const char *character, int character_bytes)
Definition: baseapi.h:85
int Init(const char *datapath, const char *language)
Definition: baseapi.h:240
GenericVector< ParagraphModel * > * paragraph_models_
Definition: baseapi.h:869
CMD_EVENTS mode
Definition: pgedit.cpp:116
EquationDetect * equ_detect_
The equation detector.
Definition: baseapi.h:867
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:877
void(Wordrec::* FillLatticeFunc)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
Definition: baseapi.h:92
#define TESS_LOCAL
Definition: platform.h:82
TruthCallback * truth_cb_
Definition: baseapi.h:878
Definition: ocrrow.h:32
Definition: strngs.h:44
STRING * language_
Last initialized language.
Definition: baseapi.h:875
Definition: blobs.h:261
STRING HOcrEscape(const char *text)
Definition: baseapi.cpp:2841
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:868
struct TessResultRenderer TessResultRenderer
Definition: capi.h:80
Tesseract * tesseract() const
Definition: baseapi.h:759
Definition: rect.h:30
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
OcrEngineMode last_oem_requested_
Last ocr language mode requested.
Definition: baseapi.h:876
STRING * input_file_
Name used by training code.
Definition: baseapi.h:872
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, Pixa **pixa, int **blockids)
Definition: baseapi.h:460
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:870
STRING * datapath_
Current location of tessdata.
Definition: baseapi.h:874
int UNICHAR_ID
Definition: unichar.h:33