tesseract  3.04.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.04.01"
24 #define TESSERACT_VERSION 0x030401
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  void SetInputImage(Pix *pix);
146  Pix* GetInputImage();
147  int GetSourceYResolution();
148  const char* GetDatapath();
149 
151  void SetOutputName(const char* name);
152 
166  bool SetVariable(const char* name, const char* value);
167  bool SetDebugVariable(const char* name, const char* value);
168 
173  bool GetIntVariable(const char *name, int *value) const;
174  bool GetBoolVariable(const char *name, bool *value) const;
175  bool GetDoubleVariable(const char *name, double *value) const;
176 
181  const char *GetStringVariable(const char *name) const;
182 
186  void PrintVariables(FILE *fp) const;
187 
191  bool GetVariableAsString(const char *name, STRING *val);
192 
231  int Init(const char* datapath, const char* language, OcrEngineMode mode,
232  char **configs, int configs_size,
233  const GenericVector<STRING> *vars_vec,
234  const GenericVector<STRING> *vars_values,
235  bool set_only_non_debug_params);
236  int Init(const char* datapath, const char* language, OcrEngineMode oem) {
237  return Init(datapath, language, oem, NULL, 0, NULL, NULL, false);
238  }
239  int Init(const char* datapath, const char* language) {
240  return Init(datapath, language, OEM_DEFAULT, NULL, 0, NULL, NULL, false);
241  }
242 
251  const char* GetInitLanguagesAsString() const;
252 
258  void GetLoadedLanguagesAsVector(GenericVector<STRING>* langs) const;
259 
263  void GetAvailableLanguagesAsVector(GenericVector<STRING>* langs) const;
264 
271  int InitLangMod(const char* datapath, const char* language);
272 
277  void InitForAnalysePage();
278 
285  void ReadConfigFile(const char* filename);
287  void ReadDebugConfigFile(const char* filename);
288 
294  void SetPageSegMode(PageSegMode mode);
295 
297  PageSegMode GetPageSegMode() const;
298 
316  char* TesseractRect(const unsigned char* imagedata,
317  int bytes_per_pixel, int bytes_per_line,
318  int left, int top, int width, int height);
319 
324  void ClearAdaptiveClassifier();
325 
332  /* @{ */
333 
343  void SetImage(const unsigned char* imagedata, int width, int height,
344  int bytes_per_pixel, int bytes_per_line);
345 
356  void SetImage(Pix* pix);
357 
362  void SetSourceResolution(int ppi);
363 
369  void SetRectangle(int left, int top, int width, int height);
370 
378  void SetThresholder(ImageThresholder* thresholder) {
379  if (thresholder_ != NULL)
380  delete thresholder_;
381  thresholder_ = thresholder;
382  ClearResults();
383  }
384 
390  Pix* GetThresholdedImage();
391 
397  Boxa* GetRegions(Pixa** pixa);
398 
410  Boxa* GetTextlines(const bool raw_image, const int raw_padding,
411  Pixa** pixa, int** blockids, int** paraids);
412  /*
413  Helper method to extract from the thresholded image. (most common usage)
414  */
415  Boxa* GetTextlines(Pixa** pixa, int** blockids) {
416  return GetTextlines(false, 0, pixa, blockids, NULL);
417  }
418 
427  Boxa* GetStrips(Pixa** pixa, int** blockids);
428 
434  Boxa* GetWords(Pixa** pixa);
435 
444  Boxa* GetConnectedComponents(Pixa** cc);
445 
459  Boxa* GetComponentImages(const PageIteratorLevel level,
460  const bool text_only, const bool raw_image,
461  const int raw_padding,
462  Pixa** pixa, int** blockids, int** paraids);
463  // Helper function to get binary images with no padding (most common usage).
465  const bool text_only,
466  Pixa** pixa, int** blockids) {
467  return GetComponentImages(level, text_only, false, 0, pixa, blockids, NULL);
468  }
469 
476  int GetThresholdedImageScaleFactor() const;
477 
483  void DumpPGM(const char* filename);
484 
501  return AnalyseLayout(false);
502  }
503  PageIterator* AnalyseLayout(bool merge_similar_words);
504 
511  int Recognize(ETEXT_DESC* monitor);
512 
519  int RecognizeForChopTest(ETEXT_DESC* monitor);
520 
543  bool ProcessPages(const char* filename, const char* retry_config,
544  int timeout_millisec, TessResultRenderer* renderer);
545  // Does the real work of ProcessPages.
546  bool ProcessPagesInternal(const char* filename, const char* retry_config,
547  int timeout_millisec, TessResultRenderer* renderer);
548 
558  bool ProcessPage(Pix* pix, int page_index, const char* filename,
559  const char* retry_config, int timeout_millisec,
560  TessResultRenderer* renderer);
561 
570  ResultIterator* GetIterator();
571 
580  MutableIterator* GetMutableIterator();
581 
586  char* GetUTF8Text();
587 
593  char* GetHOCRText(int page_number);
594 
602  char* GetBoxText(int page_number);
603 
609  char* GetUNLVText();
610 
616  char* GetOsdText(int page_number);
617 
619  int MeanTextConf();
626  int* AllWordConfidences();
627 
638  bool AdaptToWordStr(PageSegMode mode, const char* wordstr);
639 
646  void Clear();
647 
654  void End();
655 
663  static void ClearPersistentCache();
664 
671  int IsValidWord(const char *word);
672  // Returns true if utf8_character is defined in the UniCharset.
673  bool IsValidCharacter(const char *utf8_character);
674 
675 
676  bool GetTextDirection(int* out_offset, float* out_slope);
677 
679  void SetDictFunc(DictFunc f);
680 
684  void SetProbabilityInContextFunc(ProbabilityInContextFunc f);
685 
687  void SetFillLatticeFunc(FillLatticeFunc f);
688 
693  bool DetectOS(OSResults*);
694 
696  void GetFeaturesForBlob(TBLOB* blob, INT_FEATURE_STRUCT* int_features,
697  int* num_features, int* feature_outline_index);
698 
703  static ROW* FindRowForBox(BLOCK_LIST* blocks, int left, int top,
704  int right, int bottom);
705 
710  void RunAdaptiveClassifier(TBLOB* blob,
711  int num_max_matches,
712  int* unichar_ids,
713  float* ratings,
714  int* num_matches_returned);
715 
717  const char* GetUnichar(int unichar_id);
718 
720  const Dawg *GetDawg(int i) const;
721 
723  int NumDawgs() const;
724 
726  static ROW *MakeTessOCRRow(float baseline, float xheight,
727  float descender, float ascender);
728 
730  static TBLOB *MakeTBLOB(Pix *pix);
731 
737  static void NormalizeTBLOB(TBLOB *tblob, ROW *row, bool numeric_mode);
738 
739  Tesseract* tesseract() const {
740  return tesseract_;
741  }
742 
743  OcrEngineMode oem() const {
744  return last_oem_requested_;
745  }
746 
747  void InitTruthCallback(TruthCallback *cb) { truth_cb_ = cb; }
748 
749 #ifndef NO_CUBE_BUILD
750 
751  CubeRecoContext *GetCubeRecoContext() const;
752 #endif // NO_CUBE_BUILD
753 
754  void set_min_orientation_margin(double margin);
755 
760  void GetBlockTextOrientations(int** block_orientation,
761  bool** vertical_writing);
762 
764  BLOCK_LIST* FindLinesCreateBlockList();
765 
771  static void DeleteBlockList(BLOCK_LIST* block_list);
772  /* @} */
773 
774  protected:
775 
777  TESS_LOCAL bool InternalSetImage();
778 
783  TESS_LOCAL virtual void Threshold(Pix** pix);
784 
789  TESS_LOCAL int FindLines();
790 
792  void ClearResults();
793 
799  TESS_LOCAL LTRResultIterator* GetLTRIterator();
800 
807  TESS_LOCAL int TextLength(int* blob_count);
808 
810  /* @{ */
811 
816  TESS_LOCAL void AdaptToCharacter(const char *unichar_repr,
817  int length,
818  float baseline,
819  float xheight,
820  float descender,
821  float ascender);
822 
824  TESS_LOCAL PAGE_RES* RecognitionPass1(BLOCK_LIST* block_list);
825  TESS_LOCAL PAGE_RES* RecognitionPass2(BLOCK_LIST* block_list,
826  PAGE_RES* pass1_result);
827 
829  TESS_LOCAL void DetectParagraphs(bool after_text_recognition);
830 
835  TESS_LOCAL static int TesseractExtractResult(char** text,
836  int** lengths,
837  float** costs,
838  int** x0,
839  int** y0,
840  int** x1,
841  int** y1,
842  PAGE_RES* page_res);
843 
844  TESS_LOCAL const PAGE_RES* GetPageRes() const {
845  return page_res_;
846  };
847  /* @} */
848 
849 
850  protected:
851  Tesseract* tesseract_;
856  BLOCK_LIST* block_list_;
866 
871  /* @{ */
878  /* @} */
879 
880  private:
881  // A list of image filenames gets special consideration
882  bool ProcessPagesFileList(FILE *fp,
883  STRING *buf,
884  const char* retry_config, int timeout_millisec,
885  TessResultRenderer* renderer,
886  int tessedit_page_number);
887  // TIFF supports multipage so gets special consideration
888  bool ProcessPagesMultipageTiff(const unsigned char *data,
889  size_t size,
890  const char* filename,
891  const char* retry_config,
892  int timeout_millisec,
893  TessResultRenderer* renderer,
894  int tessedit_page_number);
895 }; // class TessBaseAPI.
896 
898 STRING HOcrEscape(const char* text);
899 } // namespace tesseract.
900 
901 #endif // TESSERACT_API_BASEAPI_H__
Definition: blobs.h:261
#define TESS_API
Definition: platform.h:75
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, Pixa **pixa, int **blockids)
Definition: baseapi.h:464
EquationDetect * equ_detect_
The equation detector.
Definition: baseapi.h:853
int Init(const char *datapath, const char *language, OcrEngineMode oem)
Definition: baseapi.h:236
OcrEngineMode last_oem_requested_
Last ocr language mode requested.
Definition: baseapi.h:863
float(Dict::* ParamsModelClassifyFunc)(const char *lang, void *path)
Definition: baseapi.h:90
TruthCallback * truth_cb_
Definition: baseapi.h:865
PageIterator * AnalyseLayout()
Definition: baseapi.h:500
void InitTruthCallback(TruthCallback *cb)
Definition: baseapi.h:747
GenericVector< ParagraphModel * > * paragraph_models_
Definition: baseapi.h:855
Boxa * GetTextlines(Pixa **pixa, int **blockids)
Definition: baseapi.h:415
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:854
Pix * input_image_
Image used for searchable PDF.
Definition: baseapi.h:859
STRING * language_
Last initialized language.
Definition: baseapi.h:862
Definition: werd.h:60
OcrEngineMode oem() const
Definition: baseapi.h:743
Tesseract * tesseract() const
Definition: baseapi.h:739
Definition: ocrrow.h:32
TessCallback4< const UNICHARSET &, int, PageIterator *, Pix * > TruthCallback
Definition: baseapi.h:97
double(Dict::* ProbabilityInContextFunc)(const char *lang, const char *context, int context_bytes, const char *character, int character_bytes)
Definition: baseapi.h:85
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:860
INT_FEATURE_STRUCT * INT_FEATURE
Definition: baseapi.h:62
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:857
STRING * datapath_
Current location of tessdata.
Definition: baseapi.h:861
void SetThresholder(ImageThresholder *thresholder)
Definition: baseapi.h:378
#define TESS_LOCAL
Definition: platform.h:76
TESS_LOCAL const PAGE_RES * GetPageRes() const
Definition: baseapi.h:844
int(Dict::* DictFunc)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
Definition: baseapi.h:83
struct TessResultRenderer TessResultRenderer
Definition: capi.h:63
int Init(const char *datapath, const char *language)
Definition: baseapi.h:239
int UNICHAR_ID
Definition: unichar.h:33
CMD_EVENTS mode
Definition: pgedit.cpp:116
void(Wordrec::* FillLatticeFunc)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
Definition: baseapi.h:92
Tesseract * osd_tesseract_
For orientation & script detection.
Definition: baseapi.h:852
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:856
Definition: rect.h:30
STRING HOcrEscape(const char *text)
Definition: baseapi.cpp:2713
Definition: matrix.h:289
name_table name
Definition: strngs.h:44
STRING * input_file_
Name used by training code.
Definition: baseapi.h:858
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:864
struct TessBaseAPI TessBaseAPI
Definition: capi.h:69