tesseract  5.0.0-alpha-619-ge9db
tesseract::TessBaseAPI Class Reference

#include <baseapi.h>

Public Member Functions

 TessBaseAPI ()
 
virtual ~TessBaseAPI ()
 
 TessBaseAPI (TessBaseAPI const &)=delete
 
TessBaseAPIoperator= (TessBaseAPI const &)=delete
 
void SetInputName (const char *name)
 
const char * GetInputName ()
 
void SetInputImage (Pix *pix)
 
Pix * GetInputImage ()
 
int GetSourceYResolution ()
 
const char * GetDatapath ()
 
void SetOutputName (const char *name)
 
bool SetVariable (const char *name, const char *value)
 
bool SetDebugVariable (const char *name, const char *value)
 
bool GetIntVariable (const char *name, int *value) const
 
bool GetBoolVariable (const char *name, bool *value) const
 
bool GetDoubleVariable (const char *name, double *value) const
 
const char * GetStringVariable (const char *name) const
 
void PrintVariables (FILE *fp) const
 
bool GetVariableAsString (const char *name, STRING *val)
 
int Init (const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_non_debug_params)
 
int Init (const char *datapath, const char *language, OcrEngineMode oem)
 
int Init (const char *datapath, const char *language)
 
int Init (const char *data, int data_size, const char *language, OcrEngineMode mode, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_non_debug_params, FileReader reader)
 
const char * GetInitLanguagesAsString () const
 
void GetLoadedLanguagesAsVector (GenericVector< STRING > *langs) const
 
void GetAvailableLanguagesAsVector (GenericVector< STRING > *langs) const
 
int InitLangMod (const char *datapath, const char *language)
 
void InitForAnalysePage ()
 
void ReadConfigFile (const char *filename)
 
void ReadDebugConfigFile (const char *filename)
 
void SetPageSegMode (PageSegMode mode)
 
PageSegMode GetPageSegMode () const
 
char * TesseractRect (const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height)
 
void ClearAdaptiveClassifier ()
 
void SetImage (const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
 
void SetImage (Pix *pix)
 
void SetSourceResolution (int ppi)
 
void SetRectangle (int left, int top, int width, int height)
 
void SetThresholder (ImageThresholder *thresholder)
 
Pix * GetThresholdedImage ()
 
Boxa * GetRegions (Pixa **pixa)
 
Boxa * GetTextlines (bool raw_image, int raw_padding, Pixa **pixa, int **blockids, int **paraids)
 
Boxa * GetTextlines (Pixa **pixa, int **blockids)
 
Boxa * GetStrips (Pixa **pixa, int **blockids)
 
Boxa * GetWords (Pixa **pixa)
 
Boxa * GetConnectedComponents (Pixa **cc)
 
Boxa * GetComponentImages (PageIteratorLevel level, bool text_only, bool raw_image, int raw_padding, Pixa **pixa, int **blockids, int **paraids)
 
Boxa * GetComponentImages (const PageIteratorLevel level, const bool text_only, Pixa **pixa, int **blockids)
 
int GetThresholdedImageScaleFactor () const
 
PageIteratorAnalyseLayout ()
 
PageIteratorAnalyseLayout (bool merge_similar_words)
 
int Recognize (ETEXT_DESC *monitor)
 
int RecognizeForChopTest (ETEXT_DESC *monitor)
 
bool ProcessPages (const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
 
bool ProcessPagesInternal (const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
 
bool ProcessPage (Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
 
ResultIteratorGetIterator ()
 
MutableIteratorGetMutableIterator ()
 
char * GetUTF8Text ()
 
char * GetHOCRText (ETEXT_DESC *monitor, int page_number)
 
char * GetHOCRText (int page_number)
 
char * GetAltoText (ETEXT_DESC *monitor, int page_number)
 
char * GetAltoText (int page_number)
 
char * GetTSVText (int page_number)
 
char * GetLSTMBoxText (int page_number)
 
char * GetBoxText (int page_number)
 
char * GetWordStrBoxText (int page_number)
 
char * GetUNLVText ()
 
bool DetectOrientationScript (int *orient_deg, float *orient_conf, const char **script_name, float *script_conf)
 
char * GetOsdText (int page_number)
 
int MeanTextConf ()
 
int * AllWordConfidences ()
 
bool AdaptToWordStr (PageSegMode mode, const char *wordstr)
 
void Clear ()
 
void End ()
 
int IsValidWord (const char *word)
 
bool IsValidCharacter (const char *utf8_character)
 
bool GetTextDirection (int *out_offset, float *out_slope)
 
void SetDictFunc (DictFunc f)
 
void SetProbabilityInContextFunc (ProbabilityInContextFunc f)
 
bool DetectOS (OSResults *)
 
void GetBlockTextOrientations (int **block_orientation, bool **vertical_writing)
 
void SetFillLatticeFunc (FillLatticeFunc f)
 
BLOCK_LIST * FindLinesCreateBlockList ()
 
void GetFeaturesForBlob (TBLOB *blob, INT_FEATURE_STRUCT *int_features, int *num_features, int *feature_outline_index)
 
void RunAdaptiveClassifier (TBLOB *blob, int num_max_matches, int *unichar_ids, float *ratings, int *num_matches_returned)
 
const char * GetUnichar (int unichar_id)
 
const DawgGetDawg (int i) const
 
int NumDawgs () const
 
Tesseracttesseract () const
 
OcrEngineMode oem () const
 
void InitTruthCallback (TruthCallback cb)
 
void set_min_orientation_margin (double margin)
 

Static Public Member Functions

static const char * Version ()
 
static size_t getOpenCLDevice (void **device)
 
static void ClearPersistentCache ()
 
static void DeleteBlockList (BLOCK_LIST *block_list)
 
static ROWMakeTessOCRRow (float baseline, float xheight, float descender, float ascender)
 
static TBLOBMakeTBLOB (Pix *pix)
 
static void NormalizeTBLOB (TBLOB *tblob, ROW *row, bool numeric_mode)
 
static ROWFindRowForBox (BLOCK_LIST *blocks, int left, int top, int right, int bottom)
 

Protected Member Functions

TESS_LOCAL bool InternalSetImage ()
 
virtual TESS_LOCAL bool Threshold (Pix **pix)
 
TESS_LOCAL int FindLines ()
 
void ClearResults ()
 
TESS_LOCAL LTRResultIteratorGetLTRIterator ()
 
TESS_LOCAL int TextLength (int *blob_count)
 
TESS_LOCAL void DetectParagraphs (bool after_text_recognition)
 
TESS_LOCAL void AdaptToCharacter (const char *unichar_repr, int length, float baseline, float xheight, float descender, float ascender)
 
TESS_LOCAL PAGE_RESRecognitionPass1 (BLOCK_LIST *block_list)
 
TESS_LOCAL PAGE_RESRecognitionPass2 (BLOCK_LIST *block_list, PAGE_RES *pass1_result)
 
const TESS_LOCAL PAGE_RESGetPageRes () const
 

Static Protected Member Functions

static TESS_LOCAL int TesseractExtractResult (char **text, int **lengths, float **costs, int **x0, int **y0, int **x1, int **y1, PAGE_RES *page_res)
 

Protected Attributes

Tesseracttesseract_
 The underlying data object. More...
 
Tesseractosd_tesseract_
 For orientation & script detection. More...
 
EquationDetectequ_detect_
 The equation detector. More...
 
FileReader reader_
 Reads files from any filesystem. More...
 
ImageThresholderthresholder_
 Image thresholding module. More...
 
GenericVector< ParagraphModel * > * paragraph_models_
 
BLOCK_LIST * block_list_
 The page layout. More...
 
PAGE_RESpage_res_
 The page-level data. More...
 
STRINGinput_file_
 Name used by training code. More...
 
STRINGoutput_file_
 Name used by debug code. More...
 
STRINGdatapath_
 Current location of tessdata. More...
 
STRINGlanguage_
 Last initialized language. More...
 
OcrEngineMode last_oem_requested_
 Last ocr language mode requested. More...
 
bool recognition_done_
 page_res_ contains recognition data. More...
 
TruthCallback truth_cb_
 fxn for setting truth_* in WERD_RES More...
 
int rect_left_
 
int rect_top_
 
int rect_width_
 
int rect_height_
 
int image_width_
 
int image_height_
 

Detailed Description

Base class for all tesseract APIs. Specific classes can add ability to work on different inputs or produce different outputs. This class is mostly an interface layer on top of the Tesseract instance class to hide the data types so that users of this class don't have to include any other Tesseract headers.

Definition at line 98 of file baseapi.h.

Constructor & Destructor Documentation

◆ TessBaseAPI() [1/2]

tesseract::TessBaseAPI::TessBaseAPI ( )

Definition at line 189 of file baseapi.cpp.

190  : tesseract_(nullptr),
191  osd_tesseract_(nullptr),
192  equ_detect_(nullptr),
193  reader_(nullptr),
194  // Thresholder is initialized to nullptr here, but will be set before use by:
195  // A constructor of a derived API, SetThresholder(), or
196  // created implicitly when used in InternalSetImage.
197  thresholder_(nullptr),
198  paragraph_models_(nullptr),
199  block_list_(nullptr),
200  page_res_(nullptr),
201  input_file_(nullptr),
202  output_file_(nullptr),
203  datapath_(nullptr),
204  language_(nullptr),
206  recognition_done_(false),
207  truth_cb_(nullptr),
208  rect_left_(0),
209  rect_top_(0),
210  rect_width_(0),
211  rect_height_(0),
212  image_width_(0),
213  image_height_(0) {
214 #if defined(DEBUG)
215  // The Tesseract executables would use the "C" locale by default,
216  // but other software which is linked against the Tesseract library
217  // typically uses the locale from the user's environment.
218  // Here the default is overridden to allow debugging of potential
219  // problems caused by the locale settings.
220 
221  // Use the current locale if building debug code.
222  std::locale::global(std::locale(""));
223 #endif
224 }

◆ ~TessBaseAPI()

tesseract::TessBaseAPI::~TessBaseAPI ( )
virtual

Definition at line 226 of file baseapi.cpp.

226  {
227  End();
228 }

◆ TessBaseAPI() [2/2]

tesseract::TessBaseAPI::TessBaseAPI ( TessBaseAPI const &  )
delete

Member Function Documentation

◆ AdaptToCharacter()

void tesseract::TessBaseAPI::AdaptToCharacter ( const char *  unichar_repr,
int  length,
float  baseline,
float  xheight,
float  descender,
float  ascender 
)
protected

Adapt to recognize the current image as the given character. The image must be preloaded and be just an image of a single character.

Adapt to recognize the current image as the given character. The image must be preloaded into pix_binary_ and be just an image of a single character.

Definition at line 2426 of file baseapi.cpp.

2431  {
2432  UNICHAR_ID id = tesseract_->unicharset.unichar_to_id(unichar_repr, length);
2433  TBLOB *blob = make_tesseract_blob(baseline, xheight, descender, ascender,
2435  tesseract_->pix_binary());
2436  float threshold;
2437  float best_rating = -100;
2438 
2439 
2440  // Classify to get a raw choice.
2441  BLOB_CHOICE_LIST choices;
2442  tesseract_->AdaptiveClassifier(blob, &choices);
2443  BLOB_CHOICE_IT choice_it;
2444  choice_it.set_to_list(&choices);
2445  for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
2446  choice_it.forward()) {
2447  if (choice_it.data()->rating() > best_rating) {
2448  best_rating = choice_it.data()->rating();
2449  }
2450  }
2451 
2452  threshold = tesseract_->matcher_good_threshold;
2453 
2454  if (blob->outlines)
2455  tesseract_->AdaptToChar(blob, id, kUnknownFontinfoId, threshold,
2457  delete blob;
2458 }

◆ AdaptToWordStr()

bool tesseract::TessBaseAPI::AdaptToWordStr ( PageSegMode  mode,
const char *  wordstr 
)

Applies the given word to the adaptive classifier if possible. The word must be SPACE-DELIMITED UTF-8 - l i k e t h i s , so it can tell the boundaries of the graphemes. Assumes that SetImage/SetRectangle have been used to set the image to the given word. The mode arg should be PSM_SINGLE_WORD or PSM_CIRCLE_WORD, as that will be used to control layout analysis. The currently set PageSegMode is preserved. Returns false if adaption was not possible for some reason.

Definition at line 1796 of file baseapi.cpp.

1796  {
1797  int debug = 0;
1798  GetIntVariable("applybox_debug", &debug);
1799  bool success = true;
1800  PageSegMode current_psm = GetPageSegMode();
1801  SetPageSegMode(mode);
1802  SetVariable("classify_enable_learning", "0");
1803  const std::unique_ptr<const char[]> text(GetUTF8Text());
1804  if (debug) {
1805  tprintf("Trying to adapt \"%s\" to \"%s\"\n", text.get(), wordstr);
1806  }
1807  if (text != nullptr) {
1808  PAGE_RES_IT it(page_res_);
1809  WERD_RES* word_res = it.word();
1810  if (word_res != nullptr) {
1811  word_res->word->set_text(wordstr);
1812  // Check to see if text matches wordstr.
1813  int w = 0;
1814  int t;
1815  for (t = 0; text[t] != '\0'; ++t) {
1816  if (text[t] == '\n' || text[t] == ' ')
1817  continue;
1818  while (wordstr[w] == ' ') ++w;
1819  if (text[t] != wordstr[w])
1820  break;
1821  ++w;
1822  }
1823  if (text[t] != '\0' || wordstr[w] != '\0') {
1824  // No match.
1825  delete page_res_;
1826  GenericVector<TBOX> boxes;
1830  PAGE_RES_IT pr_it(page_res_);
1831  if (pr_it.word() == nullptr)
1832  success = false;
1833  else
1834  word_res = pr_it.word();
1835  } else {
1836  word_res->BestChoiceToCorrectText();
1837  }
1838  if (success) {
1839  tesseract_->EnableLearning = true;
1840  tesseract_->LearnWord(nullptr, word_res);
1841  }
1842  } else {
1843  success = false;
1844  }
1845  } else {
1846  success = false;
1847  }
1848  SetPageSegMode(current_psm);
1849  return success;
1850 }

◆ AllWordConfidences()

int * tesseract::TessBaseAPI::AllWordConfidences ( )

Returns all word confidences (between 0 and 100) in an array, terminated by -1. The calling function must delete [] after use. The number of confidences should correspond to the number of space- delimited words in GetUTF8Text.

Returns an array of all word confidences, terminated by -1.

Definition at line 1761 of file baseapi.cpp.

1761  {
1762  if (tesseract_ == nullptr ||
1763  (!recognition_done_ && Recognize(nullptr) < 0))
1764  return nullptr;
1765  int n_word = 0;
1766  PAGE_RES_IT res_it(page_res_);
1767  for (res_it.restart_page(); res_it.word() != nullptr; res_it.forward())
1768  n_word++;
1769 
1770  int* conf = new int[n_word+1];
1771  n_word = 0;
1772  for (res_it.restart_page(); res_it.word() != nullptr; res_it.forward()) {
1773  WERD_RES *word = res_it.word();
1774  WERD_CHOICE* choice = word->best_choice;
1775  int w_conf = static_cast<int>(100 + 5 * choice->certainty());
1776  // This is the eq for converting Tesseract confidence to 1..100
1777  if (w_conf < 0) w_conf = 0;
1778  if (w_conf > 100) w_conf = 100;
1779  conf[n_word++] = w_conf;
1780  }
1781  conf[n_word] = -1;
1782  return conf;
1783 }

◆ AnalyseLayout() [1/2]

PageIterator * tesseract::TessBaseAPI::AnalyseLayout ( )

Runs page layout analysis in the mode set by SetPageSegMode. May optionally be called prior to Recognize to get access to just the page layout results. Returns an iterator to the results. If merge_similar_words is true, words are combined where suitable for use with a line recognizer. Use if you want to use AnalyseLayout to find the textlines, and then want to process textline fragments with an external line recognizer. Returns nullptr on error or an empty page. The returned iterator must be deleted after use. WARNING! This class points to data held within the TessBaseAPI class, and therefore can only be used while the TessBaseAPI class still exists and has not been subjected to a call of Init, SetImage, Recognize, Clear, End DetectOS, or anything else that changes the internal PAGE_RES.

Definition at line 807 of file baseapi.cpp.

807 { return AnalyseLayout(false); }

◆ AnalyseLayout() [2/2]

PageIterator * tesseract::TessBaseAPI::AnalyseLayout ( bool  merge_similar_words)

Definition at line 809 of file baseapi.cpp.

809  {
810  if (FindLines() == 0) {
811  if (block_list_->empty())
812  return nullptr; // The page was empty.
813  page_res_ = new PAGE_RES(merge_similar_words, block_list_, nullptr);
814  DetectParagraphs(false);
815  return new PageIterator(
819  }
820  return nullptr;
821 }

◆ Clear()

void tesseract::TessBaseAPI::Clear ( )

Free up recognition results and any stored image data, without actually freeing any recognition data that would be time-consuming to reload. Afterwards, you must call SetImage or TesseractRect before doing any Recognize or Get* operation.

Definition at line 1859 of file baseapi.cpp.

1859  {
1860  if (thresholder_ != nullptr)
1861  thresholder_->Clear();
1862  ClearResults();
1863  if (tesseract_ != nullptr) SetInputImage(nullptr);
1864 }

◆ ClearAdaptiveClassifier()

void tesseract::TessBaseAPI::ClearAdaptiveClassifier ( )

Call between pages or documents etc to free up memory and forget adaptive data.

Definition at line 556 of file baseapi.cpp.

556  {
557  if (tesseract_ == nullptr)
558  return;
561 }

◆ ClearPersistentCache()

void tesseract::TessBaseAPI::ClearPersistentCache ( )
static

Clear any library-level memory caches. There are a variety of expensive-to-load constant data structures (mostly language dictionaries) that are cached globally – surviving the Init() and End() of individual TessBaseAPI's. This function allows the clearing of these caches.

Definition at line 1907 of file baseapi.cpp.

1907  {
1909 }

◆ ClearResults()

void tesseract::TessBaseAPI::ClearResults ( )
protected

Delete the pageres and block list ready for a new page.

Delete the pageres and clear the block list ready for a new page.

Definition at line 2141 of file baseapi.cpp.

2141  {
2142  if (tesseract_ != nullptr) {
2143  tesseract_->Clear();
2144  }
2145  delete page_res_;
2146  page_res_ = nullptr;
2147  recognition_done_ = false;
2148  if (block_list_ == nullptr)
2149  block_list_ = new BLOCK_LIST;
2150  else
2151  block_list_->clear();
2152  if (paragraph_models_ != nullptr) {
2154  delete paragraph_models_;
2155  paragraph_models_ = nullptr;
2156  }
2157 }

◆ DeleteBlockList()

void tesseract::TessBaseAPI::DeleteBlockList ( BLOCK_LIST *  block_list)
static

Delete a block list. This is to keep BLOCK_LIST pointer opaque and let go of including the other headers.

Definition at line 2343 of file baseapi.cpp.

2343  {
2344  delete block_list;
2345 }

◆ DetectOrientationScript()

bool tesseract::TessBaseAPI::DetectOrientationScript ( int *  orient_deg,
float *  orient_conf,
const char **  script_name,
float *  script_conf 
)

Detect the orientation of the input image and apparent script (alphabet). orient_deg is the detected clockwise rotation of the input image in degrees (0, 90, 180, 270) orient_conf is the confidence (15.0 is reasonably confident) script_name is an ASCII string, the name of the script, e.g. "Latin" script_conf is confidence level in the script Returns true on success and writes values to each parameter as an output

Definition at line 1683 of file baseapi.cpp.

1685  {
1686  OSResults osr;
1687 
1688  bool osd = DetectOS(&osr);
1689  if (!osd) {
1690  return false;
1691  }
1692 
1693  int orient_id = osr.best_result.orientation_id;
1694  int script_id = osr.get_best_script(orient_id);
1695  if (orient_conf) *orient_conf = osr.best_result.oconfidence;
1696  if (orient_deg) *orient_deg = orient_id * 90; // convert quadrant to degrees
1697 
1698  if (script_name) {
1699  const char* script = osr.unicharset->get_script_from_script_id(script_id);
1700 
1701  *script_name = script;
1702  }
1703 
1704  if (script_conf) *script_conf = osr.best_result.sconfidence;
1705 
1706  return true;
1707 }

◆ DetectOS()

bool tesseract::TessBaseAPI::DetectOS ( OSResults osr)

Estimates the Orientation And Script of the image.

Returns
true if the image was processed successfully.

Estimates the Orientation And Script of the image. Returns true if the image was processed successfully.

Definition at line 2197 of file baseapi.cpp.

2197  {
2198  if (tesseract_ == nullptr)
2199  return false;
2200  ClearResults();
2201  if (tesseract_->pix_binary() == nullptr &&
2203  return false;
2204  }
2205 
2206  if (input_file_ == nullptr)
2207  input_file_ = new STRING(kInputFile);
2209 }

◆ DetectParagraphs()

void tesseract::TessBaseAPI::DetectParagraphs ( bool  after_text_recognition)
protected

Definition at line 2275 of file baseapi.cpp.

2275  {
2276  int debug_level = 0;
2277  GetIntVariable("paragraph_debug_level", &debug_level);
2278  if (paragraph_models_ == nullptr)
2280  MutableIterator *result_it = GetMutableIterator();
2281  do { // Detect paragraphs for this block
2283  ::tesseract::DetectParagraphs(debug_level, after_text_recognition,
2284  result_it, &models);
2285  *paragraph_models_ += models;
2286  } while (result_it->Next(RIL_BLOCK));
2287  delete result_it;
2288 }

◆ End()

void tesseract::TessBaseAPI::End ( )

Close down tesseract and free up all memory. End() is equivalent to destructing and reconstructing your TessBaseAPI. Once End() has been used, none of the other API functions may be used other than Init and anything declared above it in the class definition.

Definition at line 1872 of file baseapi.cpp.

1872  {
1873  Clear();
1874  delete thresholder_;
1875  thresholder_ = nullptr;
1876  delete page_res_;
1877  page_res_ = nullptr;
1878  delete block_list_;
1879  block_list_ = nullptr;
1880  if (paragraph_models_ != nullptr) {
1882  delete paragraph_models_;
1883  paragraph_models_ = nullptr;
1884  }
1885  if (osd_tesseract_ == tesseract_) osd_tesseract_ = nullptr;
1886  delete tesseract_;
1887  tesseract_ = nullptr;
1888  delete osd_tesseract_;
1889  osd_tesseract_ = nullptr;
1890  delete equ_detect_;
1891  equ_detect_ = nullptr;
1892  delete input_file_;
1893  input_file_ = nullptr;
1894  delete output_file_;
1895  output_file_ = nullptr;
1896  delete datapath_;
1897  datapath_ = nullptr;
1898  delete language_;
1899  language_ = nullptr;
1900 }

◆ FindLines()

int tesseract::TessBaseAPI::FindLines ( )
protected

Find lines from the image making the BLOCK_LIST.

Returns
0 on success.

Find lines from the image making the BLOCK_LIST.

Definition at line 2065 of file baseapi.cpp.

2065  {
2066  if (thresholder_ == nullptr || thresholder_->IsEmpty()) {
2067  tprintf("Please call SetImage before attempting recognition.\n");
2068  return -1;
2069  }
2070  if (recognition_done_)
2071  ClearResults();
2072  if (!block_list_->empty()) {
2073  return 0;
2074  }
2075  if (tesseract_ == nullptr) {
2076  tesseract_ = new Tesseract;
2077  #ifndef DISABLED_LEGACY_ENGINE
2079  #endif
2080  }
2081  if (tesseract_->pix_binary() == nullptr &&
2083  return -1;
2084  }
2085 
2087 
2088 #ifndef DISABLED_LEGACY_ENGINE
2090  if (equ_detect_ == nullptr && datapath_ != nullptr) {
2091  equ_detect_ = new EquationDetect(datapath_->c_str(), nullptr);
2092  }
2093  if (equ_detect_ == nullptr) {
2094  tprintf("Warning: Could not set equation detector\n");
2095  } else {
2097  }
2098  }
2099 #endif // ndef DISABLED_LEGACY_ENGINE
2100 
2101  Tesseract* osd_tess = osd_tesseract_;
2102  OSResults osr;
2104  osd_tess == nullptr) {
2105  if (strcmp(language_->c_str(), "osd") == 0) {
2106  osd_tess = tesseract_;
2107  } else {
2108  osd_tesseract_ = new Tesseract;
2109  TessdataManager mgr(reader_);
2110  if (datapath_ == nullptr) {
2111  tprintf("Warning: Auto orientation and script detection requested,"
2112  " but data path is undefined\n");
2113  delete osd_tesseract_;
2114  osd_tesseract_ = nullptr;
2115  } else if (osd_tesseract_->init_tesseract(datapath_->c_str(), nullptr,
2116  "osd", OEM_TESSERACT_ONLY,
2117  nullptr, 0, nullptr, nullptr,
2118  false, &mgr) == 0) {
2119  osd_tess = osd_tesseract_;
2122  } else {
2123  tprintf("Warning: Auto orientation and script detection requested,"
2124  " but osd language failed to load\n");
2125  delete osd_tesseract_;
2126  osd_tesseract_ = nullptr;
2127  }
2128  }
2129  }
2130 
2131  if (tesseract_->SegmentPage(input_file_, block_list_, osd_tess, &osr) < 0)
2132  return -1;
2133 
2134  // If Devanagari is being recognized, we use different images for page seg
2135  // and for OCR.
2136  tesseract_->PrepareForTessOCR(block_list_, osd_tess, &osr);
2137  return 0;
2138 }

◆ FindLinesCreateBlockList()

BLOCK_LIST * tesseract::TessBaseAPI::FindLinesCreateBlockList ( )

Find lines from the image making the BLOCK_LIST.

Definition at line 2331 of file baseapi.cpp.

2331  {
2332  ASSERT_HOST(FindLines() == 0);
2333  BLOCK_LIST* result = block_list_;
2334  block_list_ = nullptr;
2335  return result;
2336 }

◆ FindRowForBox()

ROW * tesseract::TessBaseAPI::FindRowForBox ( BLOCK_LIST *  blocks,
int  left,
int  top,
int  right,
int  bottom 
)
static

This method returns the row to which a box of specified dimensions would belong. If no good match is found, it returns nullptr.

Definition at line 2632 of file baseapi.cpp.

2633  {
2634  TBOX box(left, bottom, right, top);
2635  BLOCK_IT b_it(blocks);
2636  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
2637  BLOCK* block = b_it.data();
2638  if (!box.major_overlap(block->pdblk.bounding_box()))
2639  continue;
2640  ROW_IT r_it(block->row_list());
2641  for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward()) {
2642  ROW* row = r_it.data();
2643  if (!box.major_overlap(row->bounding_box()))
2644  continue;
2645  WERD_IT w_it(row->word_list());
2646  for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
2647  WERD* word = w_it.data();
2648  if (box.major_overlap(word->bounding_box()))
2649  return row;
2650  }
2651  }
2652  }
2653  return nullptr;
2654 }

◆ GetAltoText() [1/2]

char * tesseract::TessBaseAPI::GetAltoText ( ETEXT_DESC monitor,
int  page_number 
)

Make an XML-formatted string with Alto markup from the internal data structures.

Make an XML-formatted string with ALTO markup from the internal data structures.

Definition at line 126 of file altorenderer.cpp.

126  {
127  if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(monitor) < 0))
128  return nullptr;
129 
130  int lcnt = 0, tcnt = 0, bcnt = 0, wcnt = 0;
131 
132  if (input_file_ == nullptr) SetInputName(nullptr);
133 
134 #ifdef _WIN32
135  // convert input name from ANSI encoding to utf-8
136  int str16_len =
137  MultiByteToWideChar(CP_ACP, 0, input_file_->c_str(), -1, nullptr, 0);
138  wchar_t* uni16_str = new WCHAR[str16_len];
139  str16_len = MultiByteToWideChar(CP_ACP, 0, input_file_->c_str(), -1,
140  uni16_str, str16_len);
141  int utf8_len = WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, nullptr,
142  0, nullptr, nullptr);
143  char* utf8_str = new char[utf8_len];
144  WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, utf8_str, utf8_len,
145  nullptr, nullptr);
146  *input_file_ = utf8_str;
147  delete[] uni16_str;
148  delete[] utf8_str;
149 #endif
150 
151  std::stringstream alto_str;
152  // Use "C" locale (needed for int values larger than 999).
153  alto_str.imbue(std::locale::classic());
154  alto_str
155  << "\t\t<Page WIDTH=\"" << rect_width_ << "\" HEIGHT=\""
156  << rect_height_
157  << "\" PHYSICAL_IMG_NR=\"" << page_number << "\""
158  << " ID=\"page_" << page_number << "\">\n"
159  << "\t\t\t<PrintSpace HPOS=\"0\" VPOS=\"0\""
160  << " WIDTH=\"" << rect_width_ << "\""
161  << " HEIGHT=\"" << rect_height_ << "\">\n";
162 
163  ResultIterator* res_it = GetIterator();
164  while (!res_it->Empty(RIL_BLOCK)) {
165  if (res_it->Empty(RIL_WORD)) {
166  res_it->Next(RIL_WORD);
167  continue;
168  }
169 
170  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
171  alto_str << "\t\t\t\t<ComposedBlock ID=\"cblock_" << bcnt << "\"";
172  AddBoxToAlto(res_it, RIL_BLOCK, alto_str);
173  alto_str << "\n";
174  }
175 
176  if (res_it->IsAtBeginningOf(RIL_PARA)) {
177  alto_str << "\t\t\t\t\t<TextBlock ID=\"block_" << tcnt << "\"";
178  AddBoxToAlto(res_it, RIL_PARA, alto_str);
179  alto_str << "\n";
180  }
181 
182  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
183  alto_str << "\t\t\t\t\t\t<TextLine ID=\"line_" << lcnt << "\"";
184  AddBoxToAlto(res_it, RIL_TEXTLINE, alto_str);
185  alto_str << "\n";
186  }
187 
188  alto_str << "\t\t\t\t\t\t\t<String ID=\"string_" << wcnt << "\"";
189  AddBoxToAlto(res_it, RIL_WORD, alto_str);
190  alto_str << " CONTENT=\"";
191 
192  bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
193  bool last_word_in_tblock = res_it->IsAtFinalElement(RIL_PARA, RIL_WORD);
194  bool last_word_in_cblock = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
195 
196 
197  int left, top, right, bottom;
198  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
199 
200  do {
201  const std::unique_ptr<const char[]> grapheme(
202  res_it->GetUTF8Text(RIL_SYMBOL));
203  if (grapheme && grapheme[0] != 0) {
204  alto_str << HOcrEscape(grapheme.get()).c_str();
205  }
206  res_it->Next(RIL_SYMBOL);
207  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
208 
209  alto_str << "\"/>";
210 
211  wcnt++;
212 
213  if (last_word_in_line) {
214  alto_str << "\n\t\t\t\t\t\t</TextLine>\n";
215  lcnt++;
216  } else {
217  int hpos = right;
218  int vpos = top;
219  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
220  int width = left - hpos;
221  alto_str << "<SP WIDTH=\"" << width << "\" VPOS=\"" << vpos
222  << "\" HPOS=\"" << hpos << "\"/>\n";
223  }
224 
225  if (last_word_in_tblock) {
226  alto_str << "\t\t\t\t\t</TextBlock>\n";
227  tcnt++;
228  }
229 
230  if (last_word_in_cblock) {
231  alto_str << "\t\t\t\t</ComposedBlock>\n";
232  bcnt++;
233  }
234  }
235 
236  alto_str << "\t\t\t</PrintSpace>\n"
237  << "\t\t</Page>\n";
238  const std::string& text = alto_str.str();
239 
240  char* result = new char[text.length() + 1];
241  strcpy(result, text.c_str());
242  delete res_it;
243  return result;
244 }

◆ GetAltoText() [2/2]

char * tesseract::TessBaseAPI::GetAltoText ( int  page_number)

Make an XML-formatted string with Alto markup from the internal data structures.

Make an XML-formatted string with ALTO markup from the internal data structures.

Definition at line 118 of file altorenderer.cpp.

118  {
119  return GetAltoText(nullptr, page_number);
120 }

◆ GetAvailableLanguagesAsVector()

void tesseract::TessBaseAPI::GetAvailableLanguagesAsVector ( GenericVector< STRING > *  langs) const

Returns the available languages in the sorted vector of STRINGs.

Definition at line 447 of file baseapi.cpp.

448  {
449  langs->clear();
450  if (tesseract_ != nullptr) {
451  addAvailableLanguages(tesseract_->datadir, "", langs);
452  langs->sort(CompareSTRING);
453  }
454 }

◆ GetBlockTextOrientations()

void tesseract::TessBaseAPI::GetBlockTextOrientations ( int **  block_orientation,
bool **  vertical_writing 
)

Return text orientation of each block as determined by an earlier run of layout analysis.

Return text orientation of each block as determined in an earlier page layout analysis operation. Orientation is returned as the number of ccw 90-degree rotations (in [0..3]) required to make the text in the block upright (readable). Note that this may not necessary be the block orientation preferred for recognition (such as the case of vertical CJK text).

Also returns whether the text in the block is believed to have vertical writing direction (when in an upright page orientation).

The returned array is of length equal to the number of text blocks, which may be less than the total number of blocks. The ordering is intended to be consistent with GetTextLines().

Definition at line 2230 of file baseapi.cpp.

2231  {
2232  delete[] *block_orientation;
2233  *block_orientation = nullptr;
2234  delete[] *vertical_writing;
2235  *vertical_writing = nullptr;
2236  BLOCK_IT block_it(block_list_);
2237 
2238  block_it.move_to_first();
2239  int num_blocks = 0;
2240  for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
2241  if (!block_it.data()->pdblk.poly_block()->IsText()) {
2242  continue;
2243  }
2244  ++num_blocks;
2245  }
2246  if (!num_blocks) {
2247  tprintf("WARNING: Found no blocks\n");
2248  return;
2249  }
2250  *block_orientation = new int[num_blocks];
2251  *vertical_writing = new bool[num_blocks];
2252  block_it.move_to_first();
2253  int i = 0;
2254  for (block_it.mark_cycle_pt(); !block_it.cycled_list();
2255  block_it.forward()) {
2256  if (!block_it.data()->pdblk.poly_block()->IsText()) {
2257  continue;
2258  }
2259  FCOORD re_rotation = block_it.data()->re_rotation();
2260  float re_theta = re_rotation.angle();
2261  FCOORD classify_rotation = block_it.data()->classify_rotation();
2262  float classify_theta = classify_rotation.angle();
2263  double rot_theta = - (re_theta - classify_theta) * 2.0 / M_PI;
2264  if (rot_theta < 0) rot_theta += 4;
2265  int num_rotations = static_cast<int>(rot_theta + 0.5);
2266  (*block_orientation)[i] = num_rotations;
2267  // The classify_rotation is non-zero only if the text has vertical
2268  // writing direction.
2269  (*vertical_writing)[i] = classify_rotation.y() != 0.0f;
2270  ++i;
2271  }
2272 }

◆ GetBoolVariable()

bool tesseract::TessBaseAPI::GetBoolVariable ( const char *  name,
bool *  value 
) const

Definition at line 297 of file baseapi.cpp.

297  {
298  auto *p = ParamUtils::FindParam<BoolParam>(
299  name, GlobalParams()->bool_params, tesseract_->params()->bool_params);
300  if (p == nullptr) return false;
301  *value = bool(*p);
302  return true;
303 }

◆ GetBoxText()

char * tesseract::TessBaseAPI::GetBoxText ( int  page_number)

The recognized text is returned as a char* which is coded in the same format as a box file used in training. Constructs coordinates in the original image - not just the rectangle. page_number is a 0-based page index that will appear in the box file. Returned string must be freed with the delete [] operator.

The recognized text is returned as a char* which is coded as a UTF8 box file. page_number is a 0-base page index that will appear in the box file. Returned string must be freed with the delete [] operator.

Definition at line 1517 of file baseapi.cpp.

1517  {
1518  if (tesseract_ == nullptr ||
1519  (!recognition_done_ && Recognize(nullptr) < 0))
1520  return nullptr;
1521  int blob_count;
1522  int utf8_length = TextLength(&blob_count);
1523  int total_length = blob_count * kBytesPerBoxFileLine + utf8_length +
1525  char* result = new char[total_length];
1526  result[0] = '\0';
1527  int output_length = 0;
1528  LTRResultIterator* it = GetLTRIterator();
1529  do {
1530  int left, top, right, bottom;
1531  if (it->BoundingBox(RIL_SYMBOL, &left, &top, &right, &bottom)) {
1532  const std::unique_ptr</*non-const*/ char[]> text(
1533  it->GetUTF8Text(RIL_SYMBOL));
1534  // Tesseract uses space for recognition failure. Fix to a reject
1535  // character, kTesseractReject so we don't create illegal box files.
1536  for (int i = 0; text[i] != '\0'; ++i) {
1537  if (text[i] == ' ')
1538  text[i] = kTesseractReject;
1539  }
1540  snprintf(result + output_length, total_length - output_length,
1541  "%s %d %d %d %d %d\n", text.get(), left, image_height_ - bottom,
1542  right, image_height_ - top, page_number);
1543  output_length += strlen(result + output_length);
1544  // Just in case...
1545  if (output_length + kMaxBytesPerLine > total_length)
1546  break;
1547  }
1548  } while (it->Next(RIL_SYMBOL));
1549  delete it;
1550  return result;
1551 }

◆ GetComponentImages() [1/2]

Boxa* tesseract::TessBaseAPI::GetComponentImages ( const PageIteratorLevel  level,
const bool  text_only,
Pixa **  pixa,
int **  blockids 
)
inline

Definition at line 453 of file baseapi.h.

454  {
455  return GetComponentImages(level, text_only, false, 0, pixa, blockids,
456  nullptr);
457  }

◆ GetComponentImages() [2/2]

Boxa * tesseract::TessBaseAPI::GetComponentImages ( PageIteratorLevel  level,
bool  text_only,
bool  raw_image,
int  raw_padding,
Pixa **  pixa,
int **  blockids,
int **  paraids 
)

Get the given level kind of components (block, textline, word etc.) as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize. If blockids is not nullptr, the block-id of each component is also returned as an array of one element per component. delete [] after use. If blockids is not nullptr, the paragraph-id of each component with its block is also returned as an array of one element per component. delete [] after use. If raw_image is true, then portions of the original image are extracted instead of the thresholded image and padded with raw_padding. If text_only is true, then only text components are returned.

Get the given level kind of components (block, textline, word etc.) as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize. If blockids is not nullptr, the block-id of each component is also returned as an array of one element per component. delete [] after use. If text_only is true, then only text components are returned.

Definition at line 697 of file baseapi.cpp.

701  {
702  PageIterator* page_it = GetIterator();
703  if (page_it == nullptr)
704  page_it = AnalyseLayout();
705  if (page_it == nullptr)
706  return nullptr; // Failed.
707 
708  // Count the components to get a size for the arrays.
709  int component_count = 0;
710  int left, top, right, bottom;
711 
712  if (raw_image) {
713  // Get bounding box in original raw image with padding.
714  do {
715  if (page_it->BoundingBox(level, raw_padding,
716  &left, &top, &right, &bottom) &&
717  (!text_only || PTIsTextType(page_it->BlockType())))
718  ++component_count;
719  } while (page_it->Next(level));
720  } else {
721  // Get bounding box from binarized imaged. Note that this could be
722  // differently scaled from the original image.
723  do {
724  if (page_it->BoundingBoxInternal(level, &left, &top, &right, &bottom) &&
725  (!text_only || PTIsTextType(page_it->BlockType())))
726  ++component_count;
727  } while (page_it->Next(level));
728  }
729 
730  Boxa* boxa = boxaCreate(component_count);
731  if (pixa != nullptr)
732  *pixa = pixaCreate(component_count);
733  if (blockids != nullptr)
734  *blockids = new int[component_count];
735  if (paraids != nullptr)
736  *paraids = new int[component_count];
737 
738  int blockid = 0;
739  int paraid = 0;
740  int component_index = 0;
741  page_it->Begin();
742  do {
743  bool got_bounding_box;
744  if (raw_image) {
745  got_bounding_box =
746  page_it->BoundingBox(level, raw_padding, &left, &top, &right, &bottom);
747  } else {
748  got_bounding_box =
749  page_it->BoundingBoxInternal(level, &left, &top, &right, &bottom);
750  }
751  if (got_bounding_box &&
752  (!text_only || PTIsTextType(page_it->BlockType()))) {
753  Box* lbox = boxCreate(left, top, right - left, bottom - top);
754  boxaAddBox(boxa, lbox, L_INSERT);
755  if (pixa != nullptr) {
756  Pix* pix = nullptr;
757  if (raw_image) {
758  pix = page_it->GetImage(level, raw_padding, GetInputImage(), &left,
759  &top);
760  } else {
761  pix = page_it->GetBinaryImage(level);
762  }
763  pixaAddPix(*pixa, pix, L_INSERT);
764  pixaAddBox(*pixa, lbox, L_CLONE);
765  }
766  if (paraids != nullptr) {
767  (*paraids)[component_index] = paraid;
768  if (page_it->IsAtFinalElement(RIL_PARA, level))
769  ++paraid;
770  }
771  if (blockids != nullptr) {
772  (*blockids)[component_index] = blockid;
773  if (page_it->IsAtFinalElement(RIL_BLOCK, level)) {
774  ++blockid;
775  paraid = 0;
776  }
777  }
778  ++component_index;
779  }
780  } while (page_it->Next(level));
781  delete page_it;
782  return boxa;
783 }

◆ GetConnectedComponents()

Boxa * tesseract::TessBaseAPI::GetConnectedComponents ( Pixa **  pixa)

Gets the individual connected (text) components (created after pages segmentation step, but before recognition) as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize. Note: the caller is responsible for calling boxaDestroy() on the returned Boxa array and pixaDestroy() on cc array.

Gets the individual connected (text) components (created after pages segmentation step, but before recognition) as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize.

Definition at line 685 of file baseapi.cpp.

685  {
686  return GetComponentImages(RIL_SYMBOL, true, pixa, nullptr);
687 }

◆ GetDatapath()

const char * tesseract::TessBaseAPI::GetDatapath ( )

Definition at line 963 of file baseapi.cpp.

963  {
964  return tesseract_->datadir.c_str();
965 }

◆ GetDawg()

const Dawg * tesseract::TessBaseAPI::GetDawg ( int  i) const

Return the pointer to the i-th dawg loaded into tesseract_ object.

Definition at line 2296 of file baseapi.cpp.

2296  {
2297  if (tesseract_ == nullptr || i >= NumDawgs()) return nullptr;
2298  return tesseract_->getDict().GetDawg(i);
2299 }

◆ GetDoubleVariable()

bool tesseract::TessBaseAPI::GetDoubleVariable ( const char *  name,
double *  value 
) const

Definition at line 311 of file baseapi.cpp.

311  {
312  auto *p = ParamUtils::FindParam<DoubleParam>(
313  name, GlobalParams()->double_params, tesseract_->params()->double_params);
314  if (p == nullptr) return false;
315  *value = (double)(*p);
316  return true;
317 }

◆ GetFeaturesForBlob()

void tesseract::TessBaseAPI::GetFeaturesForBlob ( TBLOB blob,
INT_FEATURE_STRUCT int_features,
int *  num_features,
int *  feature_outline_index 
)

This method returns the features associated with the input image.

This method returns the features associated with the input blob.

Definition at line 2604 of file baseapi.cpp.

2607  {
2608  GenericVector<int> outline_counts;
2611  INT_FX_RESULT_STRUCT fx_info;
2612  tesseract_->ExtractFeatures(*blob, false, &bl_features,
2613  &cn_features, &fx_info, &outline_counts);
2614  if (cn_features.empty() || cn_features.size() > MAX_NUM_INT_FEATURES) {
2615  *num_features = 0;
2616  return; // Feature extraction failed.
2617  }
2618  *num_features = cn_features.size();
2619  memcpy(int_features, &cn_features[0], *num_features * sizeof(cn_features[0]));
2620  // TODO(rays) Pass outline_counts back and simplify the calling code.
2621  if (feature_outline_index != nullptr) {
2622  int f = 0;
2623  for (int i = 0; i < outline_counts.size(); ++i) {
2624  while (f < outline_counts[i])
2625  feature_outline_index[f++] = i;
2626  }
2627  }
2628 }

◆ GetHOCRText() [1/2]

char * tesseract::TessBaseAPI::GetHOCRText ( ETEXT_DESC monitor,
int  page_number 
)

Make a HTML-formatted string with hOCR markup from the internal data structures. page_number is 0-based but will appear in the output as 1-based. monitor can be used to cancel the recognition receive progress callbacks Returned string must be freed with the delete [] operator.

Make a HTML-formatted string with hOCR markup from the internal data structures. page_number is 0-based but will appear in the output as 1-based. Image name/input_file_ can be set by SetInputName before calling GetHOCRText STL removed from original patch submission and refactored by rays. Returned string must be freed with the delete [] operator.

Definition at line 147 of file hocrrenderer.cpp.

173  {
174  hocr_str << HOcrEscape(input_file_->c_str()).c_str();
175  } else {
176  hocr_str << "unknown";
177  }
178  hocr_str << "\"; bbox " << rect_left_ << " " << rect_top_ << " "
179  << rect_width_ << " " << rect_height_ << "; ppageno " << page_number
180  << "'>\n";
181 
182  std::unique_ptr<ResultIterator> res_it(GetIterator());
183  while (!res_it->Empty(RIL_BLOCK)) {
184  if (res_it->Empty(RIL_WORD)) {
185  res_it->Next(RIL_WORD);
186  continue;
187  }
188 
189  // Open any new block/paragraph/textline.
190  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
191  para_is_ltr = true; // reset to default direction
192  hocr_str << " <div class='ocr_carea'"
193  << " id='"
194  << "block_" << page_id << "_" << bcnt << "'";
195  AddBoxTohOCR(res_it.get(), RIL_BLOCK, hocr_str);
196  }
197  if (res_it->IsAtBeginningOf(RIL_PARA)) {
198  hocr_str << "\n <p class='ocr_par'";
199  para_is_ltr = res_it->ParagraphIsLtr();
200  if (!para_is_ltr) {
201  hocr_str << " dir='rtl'";
202  }
203  hocr_str << " id='"
204  << "par_" << page_id << "_" << pcnt << "'";
205  paragraph_lang = res_it->WordRecognitionLanguage();
206  if (paragraph_lang) {
207  hocr_str << " lang='" << paragraph_lang << "'";
208  }
209  AddBoxTohOCR(res_it.get(), RIL_PARA, hocr_str);
210  }
211  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
212  hocr_str << "\n <span class='";
213  switch (res_it->BlockType()) {
214  case PT_HEADING_TEXT:
215  hocr_str << "ocr_header";
216  break;
217  case PT_PULLOUT_TEXT:
218  hocr_str << "ocr_textfloat";
219  break;
220  case PT_CAPTION_TEXT:
221  hocr_str << "ocr_caption";
222  break;
223  default:
224  hocr_str << "ocr_line";
225  }
226  hocr_str << "' id='"
227  << "line_" << page_id << "_" << lcnt << "'";
228  AddBoxTohOCR(res_it.get(), RIL_TEXTLINE, hocr_str);
229  }
230 
231  // Now, process the word...
232  int32_t lstm_choice_mode = tesseract_->lstm_choice_mode;
233  std::vector<std::vector<std::vector<std::pair<const char*, float>>>>* rawTimestepMap =
234  nullptr;
235  std::vector<std::vector<std::pair<const char*, float>>>* CTCMap =
236  nullptr;
237  if (lstm_choice_mode) {
238 
239  CTCMap = res_it->GetBestLSTMSymbolChoices();
240  rawTimestepMap = res_it->GetRawLSTMTimesteps();
241  }
242  hocr_str << "\n <span class='ocrx_word'"
243  << " id='"
244  << "word_" << page_id << "_" << wcnt << "'";
245  int left, top, right, bottom;
246  bool bold, italic, underlined, monospace, serif, smallcaps;
247  int pointsize, font_id;
248  const char* font_name;
249  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
250  font_name =
251  res_it->WordFontAttributes(&bold, &italic, &underlined, &monospace,
252  &serif, &smallcaps, &pointsize, &font_id);
253  hocr_str << " title='bbox " << left << " " << top << " " << right << " "
254  << bottom << "; x_wconf "
255  << static_cast<int>(res_it->Confidence(RIL_WORD));
256  if (font_info) {
257  if (font_name) {
258  hocr_str << "; x_font " << HOcrEscape(font_name).c_str();
259  }
260  hocr_str << "; x_fsize " << pointsize;
261  }
262  hocr_str << "'";
263  const char* lang = res_it->WordRecognitionLanguage();
264  if (lang && (!paragraph_lang || strcmp(lang, paragraph_lang))) {
265  hocr_str << " lang='" << lang << "'";
266  }
267  switch (res_it->WordDirection()) {
268  // Only emit direction if different from current paragraph direction
269  case DIR_LEFT_TO_RIGHT:
270  if (!para_is_ltr) hocr_str << " dir='ltr'";
271  break;
272  case DIR_RIGHT_TO_LEFT:
273  if (para_is_ltr) hocr_str << " dir='rtl'";
274  break;
275  case DIR_MIX:
276  case DIR_NEUTRAL:
277  default: // Do nothing.
278  break;
279  }
280  hocr_str << ">";
281  bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
282  bool last_word_in_para = res_it->IsAtFinalElement(RIL_PARA, RIL_WORD);
283  bool last_word_in_block = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
284  if (bold) hocr_str << "<strong>";
285  if (italic) hocr_str << "<em>";
286  do {
287  const std::unique_ptr<const char[]> grapheme(
288  res_it->GetUTF8Text(RIL_SYMBOL));
289  if (grapheme && grapheme[0] != 0) {
290  if (hocr_boxes) {
291  res_it->BoundingBox(RIL_SYMBOL, &left, &top, &right, &bottom);
292  hocr_str << "\n <span class='ocrx_cinfo' title='x_bboxes "
293  << left << " " << top << " " << right << " " << bottom
294  << "; x_conf " << res_it->Confidence(RIL_SYMBOL) << "'>";
295  }
296  hocr_str << HOcrEscape(grapheme.get()).c_str();
297  if (hocr_boxes) {
298  hocr_str << "</span>";
299  tesseract::ChoiceIterator ci(*res_it);
300  if (lstm_choice_mode == 1 && ci.Timesteps() != nullptr) {
301  std::vector<std::vector<std::pair<const char*, float>>>* symbol =
302  ci.Timesteps();
303  hocr_str << "\n <span class='ocr_symbol'"
304  << " id='"
305  << "symbol_" << page_id << "_" << wcnt << "_" << scnt
306  << "'>";
307  for (auto timestep : *symbol) {
308  hocr_str << "\n <span class='ocrx_cinfo'"
309  << " id='"
310  << "timestep" << page_id << "_" << wcnt << "_" << tcnt
311  << "'>";
312  for (auto conf : timestep) {
313  hocr_str << "\n <span class='ocrx_cinfo'"
314  << " id='"
315  << "choice_" << page_id << "_" << wcnt << "_" << ccnt
316  << "'"
317  << " title='x_confs " << int(conf.second * 100)
318  << "'>" << HOcrEscape(conf.first).c_str()
319  << "</span>";
320  ++ccnt;
321  }
322  hocr_str << "</span>";
323  ++tcnt;
324  }
325  hocr_str << "\n </span>";
326  ++scnt;
327  } else if (lstm_choice_mode == 2) {
328  tesseract::ChoiceIterator ci(*res_it);
329  hocr_str << "\n <span class='ocrx_cinfo'"
330  << " id='"
331  << "lstm_choices_" << page_id << "_" << wcnt << "_" << tcnt
332  << "'>";
333  do {
334  const char* choice = ci.GetUTF8Text();
335  float choiceconf = ci.Confidence();
336  if (choice != nullptr) {
337  hocr_str << "\n <span class='ocrx_cinfo'"
338  << " id='"
339  << "choice_" << page_id << "_" << wcnt << "_" << ccnt
340  << "'"
341  << " title='x_confs " << choiceconf << "'>"
342  << HOcrEscape(choice).c_str() << "</span>";
343  ccnt++;
344  }
345  } while (ci.Next());
346  hocr_str << "\n </span>";
347  tcnt++;
348  }
349  }
350  }
351  res_it->Next(RIL_SYMBOL);
352  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
353  if (italic) hocr_str << "</em>";
354  if (bold) hocr_str << "</strong>";
355  // If the lstm choice mode is required it is added here
356  if (lstm_choice_mode == 1 && !hocr_boxes && rawTimestepMap != nullptr) {
357  for (auto symbol : *rawTimestepMap) {
358  hocr_str << "\n <span class='ocr_symbol'"
359  << " id='"
360  << "symbol_" << page_id << "_" << wcnt << "_" << scnt << "'>";
361  for (auto timestep : symbol) {
362  hocr_str << "\n <span class='ocrx_cinfo'"
363  << " id='"
364  << "timestep" << page_id << "_" << wcnt << "_" << tcnt
365  << "'>";
366  for (auto conf : timestep) {
367  hocr_str << "\n <span class='ocrx_cinfo'"
368  << " id='"
369  << "choice_" << page_id << "_" << wcnt << "_" << ccnt
370  << "'"
371  << " title='x_confs " << int(conf.second * 100) << "'>"
372  << HOcrEscape(conf.first).c_str() << "</span>";
373  ++ccnt;
374  }
375  hocr_str << "</span>";
376  ++tcnt;
377  }
378  hocr_str << "</span>";
379  ++scnt;
380  }
381  } else if (lstm_choice_mode == 2 && !hocr_boxes && CTCMap != nullptr) {
382  for (auto timestep : *CTCMap) {
383  if (timestep.size() > 0) {
384  hocr_str << "\n <span class='ocrx_cinfo'"
385  << " id='"
386  << "lstm_choices_" << page_id << "_" << wcnt << "_" << tcnt
387  << "'>";
388  for (auto& j : timestep) {
389  float conf = 100 - tesseract_->lstm_rating_coefficient * j.second;
390  if (conf < 0.0f)
391  conf = 0.0f;
392  if (conf > 100.0f)
393  conf = 100.0f;
394  hocr_str << "\n <span class='ocrx_cinfo'"
395  << " id='"
396  << "choice_" << page_id << "_" << wcnt << "_" << ccnt
397  << "'"
398  << " title='x_confs " << conf << "'>"
399  << HOcrEscape(j.first).c_str() << "</span>";
400  ccnt++;
401  }
402  hocr_str << "</span>";
403  tcnt++;
404  }
405  }
406  }
407  // Close ocrx_word.
408  if (hocr_boxes || lstm_choice_mode > 0) {
409  hocr_str << "\n ";
410  }
411  hocr_str << "</span>";
412  tcnt = 1;
413  ccnt = 1;
414  wcnt++;
415  // Close any ending block/paragraph/textline.
416  if (last_word_in_line) {
417  hocr_str << "\n </span>";
418  lcnt++;
419  }
420  if (last_word_in_para) {
421  hocr_str << "\n </p>\n";
422  pcnt++;
423  para_is_ltr = true; // back to default direction
424  }
425  if (last_word_in_block) {
426  hocr_str << " </div>\n";
427  bcnt++;
428  }
429  }
430  hocr_str << " </div>\n";
431 
432  const std::string& text = hocr_str.str();
433  char* result = new char[text.length() + 1];
434  strcpy(result, text.c_str());
435  return result;
436 }
437 
438 /**********************************************************************
439  * HOcr Text Renderer interface implementation
440  **********************************************************************/
441 TessHOcrRenderer::TessHOcrRenderer(const char* outputbase)
442  : TessResultRenderer(outputbase, "hocr") {
443  font_info_ = false;
444 }
445 
446 TessHOcrRenderer::TessHOcrRenderer(const char* outputbase, bool font_info)
447  : TessResultRenderer(outputbase, "hocr") {
448  font_info_ = font_info;
449 }
450 
451 bool TessHOcrRenderer::BeginDocumentHandler() {

◆ GetHOCRText() [2/2]

char * tesseract::TessBaseAPI::GetHOCRText ( int  page_number)

Make a HTML-formatted string with hOCR markup from the internal data structures. page_number is 0-based but will appear in the output as 1-based. Returned string must be freed with the delete [] operator.

Make a HTML-formatted string with hOCR markup from the internal data structures. page_number is 0-based but will appear in the output as 1-based. Image name/input_file_ can be set by SetInputName before calling GetHOCRText STL removed from original patch submission and refactored by rays. Returned string must be freed with the delete [] operator.

Definition at line 134 of file hocrrenderer.cpp.

◆ GetInitLanguagesAsString()

const char * tesseract::TessBaseAPI::GetInitLanguagesAsString ( ) const

Returns the languages string used in the last valid initialization. If the last initialization specified "deu+hin" then that will be returned. If hin loaded eng automatically as well, then that will not be included in this list. To find the languages actually loaded use GetLoadedLanguagesAsVector. The returned string should NOT be deleted.

Definition at line 423 of file baseapi.cpp.

423  {
424  return (language_ == nullptr || language_->c_str() == nullptr) ?
425  "" : language_->c_str();
426 }

◆ GetInputImage()

Pix * tesseract::TessBaseAPI::GetInputImage ( )

Definition at line 955 of file baseapi.cpp.

955 { return tesseract_->pix_original(); }

◆ GetInputName()

const char * tesseract::TessBaseAPI::GetInputName ( )

These functions are required for searchable PDF output. We need our hands on the input file so that we can include it in the PDF without transcoding. If that is not possible, we need the original image. Finally, resolution metadata is stored in the PDF so we need that as well.

Definition at line 957 of file baseapi.cpp.

957  {
958  if (input_file_)
959  return input_file_->c_str();
960  return nullptr;
961 }

◆ GetIntVariable()

bool tesseract::TessBaseAPI::GetIntVariable ( const char *  name,
int *  value 
) const

Returns true if the parameter was found among Tesseract parameters. Fills in value with the value of the parameter.

Definition at line 289 of file baseapi.cpp.

289  {
290  auto *p = ParamUtils::FindParam<IntParam>(
291  name, GlobalParams()->int_params, tesseract_->params()->int_params);
292  if (p == nullptr) return false;
293  *value = (int32_t)(*p);
294  return true;
295 }

◆ GetIterator()

ResultIterator * tesseract::TessBaseAPI::GetIterator ( )

Get a reading-order iterator to the results of LayoutAnalysis and/or Recognize. The returned iterator must be deleted after use. WARNING! This class points to data held within the TessBaseAPI class, and therefore can only be used while the TessBaseAPI class still exists and has not been subjected to a call of Init, SetImage, Recognize, Clear, End DetectOS, or anything else that changes the internal PAGE_RES.

Definition at line 1321 of file baseapi.cpp.

1321  {
1322  if (tesseract_ == nullptr || page_res_ == nullptr)
1323  return nullptr;
1324  return ResultIterator::StartOfParagraph(LTRResultIterator(
1328 }

◆ GetLoadedLanguagesAsVector()

void tesseract::TessBaseAPI::GetLoadedLanguagesAsVector ( GenericVector< STRING > *  langs) const

Returns the loaded languages in the vector of STRINGs. Includes all languages loaded by the last Init, including those loaded as dependencies of other loaded languages.

Definition at line 433 of file baseapi.cpp.

434  {
435  langs->clear();
436  if (tesseract_ != nullptr) {
437  langs->push_back(tesseract_->lang);
438  int num_subs = tesseract_->num_sub_langs();
439  for (int i = 0; i < num_subs; ++i)
440  langs->push_back(tesseract_->get_sub_lang(i)->lang);
441  }
442 }

◆ GetLSTMBoxText()

char * tesseract::TessBaseAPI::GetLSTMBoxText ( int  page_number = 0)

Make a box file for LSTM training from the internal data structures. Constructs coordinates in the original image - not just the rectangle. page_number is a 0-based page index that will appear in the box file. Returned string must be freed with the delete [] operator.

Definition at line 52 of file lstmboxrenderer.cpp.

52  {
53  if (!(res_it->IsAtBeginningOf(RIL_TEXTLINE))) {
54  if (res_it->IsAtBeginningOf(RIL_WORD)) {
55  lstm_box_str.add_str_int(" ", left);
56  AddBoxToLSTM(right, bottom, top, image_height_, page_number,
57  &lstm_box_str);
58  lstm_box_str += "\n"; // end of row for word
59  } // word
60  } else {
61  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
62  lstm_box_str.add_str_int("\t ", left);
63  AddBoxToLSTM(right, bottom, top, image_height_, page_number,
64  &lstm_box_str);
65  lstm_box_str += "\n"; // end of row for line
66  } // line
67  }
68  } // not first word
69  first_word = false;
70  // Use bounding box for whole line for everything
71  res_it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom);
72  do {
73  lstm_box_str +=
74  std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_SYMBOL)).get();
75  res_it->Next(RIL_SYMBOL);
76  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_SYMBOL));
77  lstm_box_str.add_str_int(" ", left);
78  AddBoxToLSTM(right, bottom, top, image_height_, page_number, &lstm_box_str);
79  lstm_box_str += "\n"; // end of row for symbol
80  }
81  if (!first_word) { // if first_word is true => empty page
82  lstm_box_str.add_str_int("\t ", left);
83  AddBoxToLSTM(right, bottom, top, image_height_, page_number, &lstm_box_str);
84  lstm_box_str += "\n"; // end of PAGE
85  }
86  char* ret = new char[lstm_box_str.length() + 1];
87  strcpy(ret, lstm_box_str.c_str());
88  delete res_it;
89  return ret;
90 }
91 
92 /**********************************************************************
93  * LSTMBox Renderer interface implementation
94  **********************************************************************/
95 TessLSTMBoxRenderer::TessLSTMBoxRenderer(const char* outputbase)
96  : TessResultRenderer(outputbase, "box") {}
97 
98 bool TessLSTMBoxRenderer::AddImageHandler(TessBaseAPI* api) {
99  const std::unique_ptr<const char[]> lstmbox(api->GetLSTMBoxText(imagenum()));
100  if (lstmbox == nullptr) return false;
101 
102  AppendString(lstmbox.get());
103 
104  return true;

◆ GetLTRIterator()

LTRResultIterator * tesseract::TessBaseAPI::GetLTRIterator ( )
protected

Return an LTR Result Iterator – used only for training, as we really want to ignore all BiDi smarts at that point. delete once you're done with it.

Get a left-to-right iterator to the results of LayoutAnalysis and/or Recognize. The returned iterator must be deleted after use.

Definition at line 1304 of file baseapi.cpp.

1304  {
1305  if (tesseract_ == nullptr || page_res_ == nullptr)
1306  return nullptr;
1307  return new LTRResultIterator(
1311 }

◆ GetMutableIterator()

MutableIterator * tesseract::TessBaseAPI::GetMutableIterator ( )

Get a mutable iterator to the results of LayoutAnalysis and/or Recognize. The returned iterator must be deleted after use. WARNING! This class points to data held within the TessBaseAPI class, and therefore can only be used while the TessBaseAPI class still exists and has not been subjected to a call of Init, SetImage, Recognize, Clear, End DetectOS, or anything else that changes the internal PAGE_RES.

Definition at line 1338 of file baseapi.cpp.

1338  {
1339  if (tesseract_ == nullptr || page_res_ == nullptr)
1340  return nullptr;
1341  return new MutableIterator(page_res_, tesseract_,
1345 }

◆ getOpenCLDevice()

size_t tesseract::TessBaseAPI::getOpenCLDevice ( void **  data)
static

If compiled with OpenCL AND an available OpenCL device is deemed faster than serial code, then "device" is populated with the cl_device_id and returns sizeof(cl_device_id) otherwise *device=nullptr and returns 0.

Definition at line 244 of file baseapi.cpp.

244  {
245 #ifdef USE_OPENCL
246  ds_device device = OpenclDevice::getDeviceSelection();
247  if (device.type == DS_DEVICE_OPENCL_DEVICE) {
248  *data = new cl_device_id;
249  memcpy(*data, &device.oclDeviceID, sizeof(cl_device_id));
250  return sizeof(cl_device_id);
251  }
252 #endif
253 
254  *data = nullptr;
255  return 0;
256 }

◆ GetOsdText()

char * tesseract::TessBaseAPI::GetOsdText ( int  page_number)

The recognized text is returned as a char* which is coded as UTF8 and must be freed with the delete [] operator. page_number is a 0-based page index that will appear in the osd file.

Definition at line 1714 of file baseapi.cpp.

1714  {
1715  int orient_deg;
1716  float orient_conf;
1717  const char* script_name;
1718  float script_conf;
1719 
1720  if (!DetectOrientationScript(&orient_deg, &orient_conf, &script_name,
1721  &script_conf))
1722  return nullptr;
1723 
1724  // clockwise rotation needed to make the page upright
1725  int rotate = OrientationIdToValue(orient_deg / 90);
1726 
1727  std::stringstream stream;
1728  // Use "C" locale (needed for float values orient_conf and script_conf).
1729  stream.imbue(std::locale::classic());
1730  // Use fixed notation with 2 digits after the decimal point for float values.
1731  stream.precision(2);
1732  stream
1733  << std::fixed
1734  << "Page number: " << page_number << "\n"
1735  << "Orientation in degrees: " << orient_deg << "\n"
1736  << "Rotate: " << rotate << "\n"
1737  << "Orientation confidence: " << orient_conf << "\n"
1738  << "Script: " << script_name << "\n"
1739  << "Script confidence: " << script_conf << "\n";
1740  const std::string& text = stream.str();
1741  char* result = new char[text.length() + 1];
1742  strcpy(result, text.c_str());
1743  return result;
1744 }

◆ GetPageRes()

const TESS_LOCAL PAGE_RES* tesseract::TessBaseAPI::GetPageRes ( ) const
inlineprotected

Definition at line 879 of file baseapi.h.

879  {
880  return page_res_;
881  }

◆ GetPageSegMode()

PageSegMode tesseract::TessBaseAPI::GetPageSegMode ( ) const

Return the current page segmentation mode.

Definition at line 513 of file baseapi.cpp.

513  {
514  if (tesseract_ == nullptr)
515  return PSM_SINGLE_BLOCK;
516  return static_cast<PageSegMode>(
517  static_cast<int>(tesseract_->tessedit_pageseg_mode));
518 }

◆ GetRegions()

Boxa * tesseract::TessBaseAPI::GetRegions ( Pixa **  pixa)

Get the result of page layout analysis as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize.

Definition at line 640 of file baseapi.cpp.

640  {
641  return GetComponentImages(RIL_BLOCK, false, pixa, nullptr);
642 }

◆ GetSourceYResolution()

int tesseract::TessBaseAPI::GetSourceYResolution ( )

Definition at line 967 of file baseapi.cpp.

967  {
969 }

◆ GetStringVariable()

const char * tesseract::TessBaseAPI::GetStringVariable ( const char *  name) const

Returns the pointer to the string that represents the value of the parameter if it was found among Tesseract parameters.

Definition at line 305 of file baseapi.cpp.

305  {
306  auto *p = ParamUtils::FindParam<StringParam>(
307  name, GlobalParams()->string_params, tesseract_->params()->string_params);
308  return (p != nullptr) ? p->c_str() : nullptr;
309 }

◆ GetStrips()

Boxa * tesseract::TessBaseAPI::GetStrips ( Pixa **  pixa,
int **  blockids 
)

Get textlines and strips of image regions as a leptonica-style Boxa, Pixa pair, in reading order. Enables downstream handling of non-rectangular regions. Can be called before or after Recognize. If blockids is not nullptr, the block-id of each line is also returned as an array of one element per line. delete [] after use.

Definition at line 666 of file baseapi.cpp.

666  {
667  return GetComponentImages(RIL_TEXTLINE, false, pixa, blockids);
668 }

◆ GetTextDirection()

bool tesseract::TessBaseAPI::GetTextDirection ( int *  out_offset,
float *  out_slope 
)

Definition at line 1926 of file baseapi.cpp.

1926  {
1927  PageIterator* it = AnalyseLayout();
1928  if (it == nullptr) {
1929  return false;
1930  }
1931  int x1, x2, y1, y2;
1932  it->Baseline(RIL_TEXTLINE, &x1, &y1, &x2, &y2);
1933  // Calculate offset and slope (NOTE: Kind of ugly)
1934  if (x2 <= x1) x2 = x1 + 1;
1935  // Convert the point pair to slope/offset of the baseline (in image coords.)
1936  *out_slope = static_cast<float>(y2 - y1) / (x2 - x1);
1937  *out_offset = static_cast<int>(y1 - *out_slope * x1);
1938  // Get the y-coord of the baseline at the left and right edges of the
1939  // textline's bounding box.
1940  int left, top, right, bottom;
1941  if (!it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom)) {
1942  delete it;
1943  return false;
1944  }
1945  int left_y = IntCastRounded(*out_slope * left + *out_offset);
1946  int right_y = IntCastRounded(*out_slope * right + *out_offset);
1947  // Shift the baseline down so it passes through the nearest bottom-corner
1948  // of the textline's bounding box. This is the difference between the y
1949  // at the lowest (max) edge of the box and the actual box bottom.
1950  *out_offset += bottom - std::max(left_y, right_y);
1951  // Switch back to bottom-up tesseract coordinates. Requires negation of
1952  // the slope and height - offset for the offset.
1953  *out_slope = -*out_slope;
1954  *out_offset = rect_height_ - *out_offset;
1955  delete it;
1956 
1957  return true;
1958 }

◆ GetTextlines() [1/2]

Boxa * tesseract::TessBaseAPI::GetTextlines ( bool  raw_image,
int  raw_padding,
Pixa **  pixa,
int **  blockids,
int **  paraids 
)

Get the textlines as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize. If raw_image is true, then extract from the original image instead of the thresholded image and pad by raw_padding pixels. If blockids is not nullptr, the block-id of each line is also returned as an array of one element per line. delete [] after use. If paraids is not nullptr, the paragraph-id of each line within its block is also returned as an array of one element per line. delete [] after use.

Get the textlines as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize. If blockids is not nullptr, the block-id of each line is also returned as an array of one element per line. delete [] after use. If paraids is not nullptr, the paragraph-id of each line within its block is also returned as an array of one element per line. delete [] after use.

Definition at line 652 of file baseapi.cpp.

653  {
654  return GetComponentImages(RIL_TEXTLINE, true, raw_image, raw_padding,
655  pixa, blockids, paraids);
656 }

◆ GetTextlines() [2/2]

Boxa* tesseract::TessBaseAPI::GetTextlines ( Pixa **  pixa,
int **  blockids 
)
inline

Definition at line 406 of file baseapi.h.

406  {
407  return GetTextlines(false, 0, pixa, blockids, nullptr);
408  }

◆ GetThresholdedImage()

Pix * tesseract::TessBaseAPI::GetThresholdedImage ( )

Get a copy of the internal thresholded image from Tesseract. Caller takes ownership of the Pix and must pixDestroy it. May be called any time after SetImage, or after TesseractRect.

ONLY available after SetImage if you have Leptonica installed. Get a copy of the internal thresholded image from Tesseract.

Definition at line 626 of file baseapi.cpp.

626  {
627  if (tesseract_ == nullptr || thresholder_ == nullptr) return nullptr;
628  if (tesseract_->pix_binary() == nullptr &&
630  return nullptr;
631  }
632  return pixClone(tesseract_->pix_binary());
633 }

◆ GetThresholdedImageScaleFactor()

int tesseract::TessBaseAPI::GetThresholdedImageScaleFactor ( ) const

Returns the scale factor of the thresholded image that would be returned by GetThresholdedImage() and the various GetX() methods that call GetComponentImages(). Returns 0 if no thresholder has been set.

Definition at line 785 of file baseapi.cpp.

785  {
786  if (thresholder_ == nullptr) {
787  return 0;
788  }
789  return thresholder_->GetScaleFactor();
790 }

◆ GetTSVText()

char * tesseract::TessBaseAPI::GetTSVText ( int  page_number)

Make a TSV-formatted string from the internal data structures. page_number is 0-based but will appear in the output as 1-based. Returned string must be freed with the delete [] operator.

Definition at line 1380 of file baseapi.cpp.

1380  {
1381  if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0))
1382  return nullptr;
1383 
1384  int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
1385  int page_id = page_number + 1; // we use 1-based page numbers.
1386 
1387  STRING tsv_str("");
1388 
1389  int page_num = page_id;
1390  int block_num = 0;
1391  int par_num = 0;
1392  int line_num = 0;
1393  int word_num = 0;
1394 
1395  tsv_str.add_str_int("1\t", page_num); // level 1 - page
1396  tsv_str.add_str_int("\t", block_num);
1397  tsv_str.add_str_int("\t", par_num);
1398  tsv_str.add_str_int("\t", line_num);
1399  tsv_str.add_str_int("\t", word_num);
1400  tsv_str.add_str_int("\t", rect_left_);
1401  tsv_str.add_str_int("\t", rect_top_);
1402  tsv_str.add_str_int("\t", rect_width_);
1403  tsv_str.add_str_int("\t", rect_height_);
1404  tsv_str += "\t-1\t\n";
1405 
1406  ResultIterator* res_it = GetIterator();
1407  while (!res_it->Empty(RIL_BLOCK)) {
1408  if (res_it->Empty(RIL_WORD)) {
1409  res_it->Next(RIL_WORD);
1410  continue;
1411  }
1412 
1413  // Add rows for any new block/paragraph/textline.
1414  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
1415  block_num++;
1416  par_num = 0;
1417  line_num = 0;
1418  word_num = 0;
1419  tsv_str.add_str_int("2\t", page_num); // level 2 - block
1420  tsv_str.add_str_int("\t", block_num);
1421  tsv_str.add_str_int("\t", par_num);
1422  tsv_str.add_str_int("\t", line_num);
1423  tsv_str.add_str_int("\t", word_num);
1424  AddBoxToTSV(res_it, RIL_BLOCK, &tsv_str);
1425  tsv_str += "\t-1\t\n"; // end of row for block
1426  }
1427  if (res_it->IsAtBeginningOf(RIL_PARA)) {
1428  par_num++;
1429  line_num = 0;
1430  word_num = 0;
1431  tsv_str.add_str_int("3\t", page_num); // level 3 - paragraph
1432  tsv_str.add_str_int("\t", block_num);
1433  tsv_str.add_str_int("\t", par_num);
1434  tsv_str.add_str_int("\t", line_num);
1435  tsv_str.add_str_int("\t", word_num);
1436  AddBoxToTSV(res_it, RIL_PARA, &tsv_str);
1437  tsv_str += "\t-1\t\n"; // end of row for para
1438  }
1439  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
1440  line_num++;
1441  word_num = 0;
1442  tsv_str.add_str_int("4\t", page_num); // level 4 - line
1443  tsv_str.add_str_int("\t", block_num);
1444  tsv_str.add_str_int("\t", par_num);
1445  tsv_str.add_str_int("\t", line_num);
1446  tsv_str.add_str_int("\t", word_num);
1447  AddBoxToTSV(res_it, RIL_TEXTLINE, &tsv_str);
1448  tsv_str += "\t-1\t\n"; // end of row for line
1449  }
1450 
1451  // Now, process the word...
1452  int left, top, right, bottom;
1453  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
1454  word_num++;
1455  tsv_str.add_str_int("5\t", page_num); // level 5 - word
1456  tsv_str.add_str_int("\t", block_num);
1457  tsv_str.add_str_int("\t", par_num);
1458  tsv_str.add_str_int("\t", line_num);
1459  tsv_str.add_str_int("\t", word_num);
1460  tsv_str.add_str_int("\t", left);
1461  tsv_str.add_str_int("\t", top);
1462  tsv_str.add_str_int("\t", right - left);
1463  tsv_str.add_str_int("\t", bottom - top);
1464  tsv_str.add_str_int("\t", res_it->Confidence(RIL_WORD));
1465  tsv_str += "\t";
1466 
1467  // Increment counts if at end of block/paragraph/textline.
1468  if (res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD)) lcnt++;
1469  if (res_it->IsAtFinalElement(RIL_PARA, RIL_WORD)) pcnt++;
1470  if (res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD)) bcnt++;
1471 
1472  do {
1473  tsv_str +=
1474  std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_SYMBOL)).get();
1475  res_it->Next(RIL_SYMBOL);
1476  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
1477  tsv_str += "\n"; // end of row
1478  wcnt++;
1479  }
1480 
1481  char* ret = new char[tsv_str.length() + 1];
1482  strcpy(ret, tsv_str.c_str());
1483  delete res_it;
1484  return ret;
1485 }

◆ GetUnichar()

const char * tesseract::TessBaseAPI::GetUnichar ( int  unichar_id)

This method returns the string form of the specified unichar.

Definition at line 2291 of file baseapi.cpp.

2291  {
2292  return tesseract_->unicharset.id_to_unichar(unichar_id);
2293 }

◆ GetUNLVText()

char * tesseract::TessBaseAPI::GetUNLVText ( )

The recognized text is returned as a char* which is coded as UNLV format Latin-1 with specific reject and suspect codes. Returned string must be freed with the delete [] operator.

Definition at line 1571 of file baseapi.cpp.

1571  {
1572  if (tesseract_ == nullptr ||
1573  (!recognition_done_ && Recognize(nullptr) < 0))
1574  return nullptr;
1575  bool tilde_crunch_written = false;
1576  bool last_char_was_newline = true;
1577  bool last_char_was_tilde = false;
1578 
1579  int total_length = TextLength(nullptr);
1580  PAGE_RES_IT page_res_it(page_res_);
1581  char* result = new char[total_length];
1582  char* ptr = result;
1583  for (page_res_it.restart_page(); page_res_it.word () != nullptr;
1584  page_res_it.forward()) {
1585  WERD_RES *word = page_res_it.word();
1586  // Process the current word.
1587  if (word->unlv_crunch_mode != CR_NONE) {
1588  if (word->unlv_crunch_mode != CR_DELETE &&
1589  (!tilde_crunch_written ||
1590  (word->unlv_crunch_mode == CR_KEEP_SPACE &&
1591  word->word->space() > 0 &&
1592  !word->word->flag(W_FUZZY_NON) &&
1593  !word->word->flag(W_FUZZY_SP)))) {
1594  if (!word->word->flag(W_BOL) &&
1595  word->word->space() > 0 &&
1596  !word->word->flag(W_FUZZY_NON) &&
1597  !word->word->flag(W_FUZZY_SP)) {
1598  /* Write a space to separate from preceding good text */
1599  *ptr++ = ' ';
1600  last_char_was_tilde = false;
1601  }
1602  if (!last_char_was_tilde) {
1603  // Write a reject char.
1604  last_char_was_tilde = true;
1605  *ptr++ = kUNLVReject;
1606  tilde_crunch_written = true;
1607  last_char_was_newline = false;
1608  }
1609  }
1610  } else {
1611  // NORMAL PROCESSING of non tilde crunched words.
1612  tilde_crunch_written = false;
1614  const char* wordstr = word->best_choice->unichar_string().c_str();
1615  const STRING& lengths = word->best_choice->unichar_lengths();
1616  int length = lengths.length();
1617  int i = 0;
1618  int offset = 0;
1619 
1620  if (last_char_was_tilde &&
1621  word->word->space() == 0 && wordstr[offset] == ' ') {
1622  // Prevent adjacent tilde across words - we know that adjacent tildes
1623  // within words have been removed.
1624  // Skip the first character.
1625  offset = lengths[i++];
1626  }
1627  if (i < length && wordstr[offset] != 0) {
1628  if (!last_char_was_newline)
1629  *ptr++ = ' ';
1630  else
1631  last_char_was_newline = false;
1632  for (; i < length; offset += lengths[i++]) {
1633  if (wordstr[offset] == ' ' ||
1634  wordstr[offset] == kTesseractReject) {
1635  *ptr++ = kUNLVReject;
1636  last_char_was_tilde = true;
1637  } else {
1638  if (word->reject_map[i].rejected())
1639  *ptr++ = kUNLVSuspect;
1640  UNICHAR ch(wordstr + offset, lengths[i]);
1641  int uni_ch = ch.first_uni();
1642  for (int j = 0; kUniChs[j] != 0; ++j) {
1643  if (kUniChs[j] == uni_ch) {
1644  uni_ch = kLatinChs[j];
1645  break;
1646  }
1647  }
1648  if (uni_ch <= 0xff) {
1649  *ptr++ = static_cast<char>(uni_ch);
1650  last_char_was_tilde = false;
1651  } else {
1652  *ptr++ = kUNLVReject;
1653  last_char_was_tilde = true;
1654  }
1655  }
1656  }
1657  }
1658  }
1659  if (word->word->flag(W_EOL) && !last_char_was_newline) {
1660  /* Add a new line output */
1661  *ptr++ = '\n';
1662  tilde_crunch_written = false;
1663  last_char_was_newline = true;
1664  last_char_was_tilde = false;
1665  }
1666  }
1667  *ptr++ = '\n';
1668  *ptr = '\0';
1669  return result;
1670 }

◆ GetUTF8Text()

char * tesseract::TessBaseAPI::GetUTF8Text ( )

The recognized text is returned as a char* which is coded as UTF8 and must be freed with the delete [] operator.

Make a text string from the internal data structures.

Definition at line 1348 of file baseapi.cpp.

1348  {
1349  if (tesseract_ == nullptr ||
1350  (!recognition_done_ && Recognize(nullptr) < 0))
1351  return nullptr;
1352  STRING text("");
1353  ResultIterator *it = GetIterator();
1354  do {
1355  if (it->Empty(RIL_PARA)) continue;
1356  const std::unique_ptr<const char[]> para_text(it->GetUTF8Text(RIL_PARA));
1357  text += para_text.get();
1358  } while (it->Next(RIL_PARA));
1359  char* result = new char[text.length() + 1];
1360  strncpy(result, text.c_str(), text.length() + 1);
1361  delete it;
1362  return result;
1363 }

◆ GetVariableAsString()

bool tesseract::TessBaseAPI::GetVariableAsString ( const char *  name,
STRING val 
)

Get value of named variable as a string, if it exists.

Definition at line 320 of file baseapi.cpp.

320  {
321  return ParamUtils::GetParamAsString(name, tesseract_->params(), val);
322 }

◆ GetWords()

Boxa * tesseract::TessBaseAPI::GetWords ( Pixa **  pixa)

Get the words as a leptonica-style Boxa, Pixa pair, in reading order. Can be called before or after Recognize.

Definition at line 675 of file baseapi.cpp.

675  {
676  return GetComponentImages(RIL_WORD, true, pixa, nullptr);
677 }

◆ GetWordStrBoxText()

char * tesseract::TessBaseAPI::GetWordStrBoxText ( int  page_number = 0)

The recognized text is returned as a char* which is coded in the same format as a WordStr box file used in training. page_number is a 0-based page index that will appear in the box file. Returned string must be freed with the delete [] operator.

Create a UTF8 box file with WordStr strings from the internal data structures. page_number is a 0-base page index that will appear in the box file. Returned string must be freed with the delete [] operator.

Definition at line 45 of file wordstrboxrenderer.cpp.

47  {
48  if (!first_line) {
49  wordstr_box_str.add_str_int("\n\t ", right + 1);
50  wordstr_box_str.add_str_int(" ", image_height_ - bottom);
51  wordstr_box_str.add_str_int(" ", right + 5);
52  wordstr_box_str.add_str_int(" ", image_height_ - top);
53  wordstr_box_str.add_str_int(" ", page_number); // row for tab for EOL
54  wordstr_box_str += "\n";
55  } else {
56  first_line = false;
57  }
58  // Use bounding box for whole line for WordStr
59  res_it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom);
60  wordstr_box_str.add_str_int("WordStr ", left);
61  wordstr_box_str.add_str_int(" ", image_height_ - bottom);
62  wordstr_box_str.add_str_int(" ", right);
63  wordstr_box_str.add_str_int(" ", image_height_ - top);
64  wordstr_box_str.add_str_int(" ", page_number); // word
65  wordstr_box_str += " #";
66  }
67  do {
68  wordstr_box_str +=
69  std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_WORD)).get();
70  wordstr_box_str += " ";
71  res_it->Next(RIL_WORD);
72  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
73  }
74 
75  if (left != 0 && top != 0 && right != 0 && bottom != 0) {
76  wordstr_box_str.add_str_int("\n\t ", right + 1);
77  wordstr_box_str.add_str_int(" ", image_height_ - bottom);
78  wordstr_box_str.add_str_int(" ", right + 5);
79  wordstr_box_str.add_str_int(" ", image_height_ - top);
80  wordstr_box_str.add_str_int(" ", page_number); // row for tab for EOL
81  wordstr_box_str += "\n";
82  }
83  char* ret = new char[wordstr_box_str.length() + 1];
84  strcpy(ret, wordstr_box_str.c_str());
85  delete res_it;
86  return ret;
87 }
88 
89 /**********************************************************************
90  * WordStrBox Renderer interface implementation
91  **********************************************************************/
93  : TessResultRenderer(outputbase, "box") {}
94 
95 bool TessWordStrBoxRenderer::AddImageHandler(TessBaseAPI* api) {
96  const std::unique_ptr<const char[]> wordstrbox(
97  api->GetWordStrBoxText(imagenum()));
98  if (wordstrbox == nullptr) return false;
99 
100  AppendString(wordstrbox.get());
101 

◆ Init() [1/4]

int tesseract::TessBaseAPI::Init ( const char *  data,
int  data_size,
const char *  language,
OcrEngineMode  mode,
char **  configs,
int  configs_size,
const GenericVector< STRING > *  vars_vec,
const GenericVector< STRING > *  vars_values,
bool  set_only_non_debug_params,
FileReader  reader 
)

Definition at line 349 of file baseapi.cpp.

353  {
354  // Default language is "eng".
355  if (language == nullptr) language = "eng";
356  STRING datapath = data_size == 0 ? data : language;
357  // If the datapath, OcrEngineMode or the language have changed - start again.
358  // Note that the language_ field stores the last requested language that was
359  // initialized successfully, while tesseract_->lang stores the language
360  // actually used. They differ only if the requested language was nullptr, in
361  // which case tesseract_->lang is set to the Tesseract default ("eng").
362  if (tesseract_ != nullptr &&
363  (datapath_ == nullptr || language_ == nullptr || *datapath_ != datapath ||
365  (*language_ != language && tesseract_->lang != language))) {
366  delete tesseract_;
367  tesseract_ = nullptr;
368  }
369 #ifdef USE_OPENCL
370  OpenclDevice od;
371  od.InitEnv();
372 #endif
373  bool reset_classifier = true;
374  if (tesseract_ == nullptr) {
375  reset_classifier = false;
376  tesseract_ = new Tesseract;
377  if (reader != nullptr) reader_ = reader;
378  TessdataManager mgr(reader_);
379  if (data_size != 0) {
380  mgr.LoadMemBuffer(language, data, data_size);
381  }
383  datapath.c_str(),
384  output_file_ != nullptr ? output_file_->c_str() : nullptr,
385  language, oem, configs, configs_size, vars_vec, vars_values,
386  set_only_non_debug_params, &mgr) != 0) {
387  return -1;
388  }
389  }
390 
391  // Update datapath and language requested for the last valid initialization.
392  if (datapath_ == nullptr)
393  datapath_ = new STRING(datapath);
394  else
395  *datapath_ = datapath;
396  if ((strcmp(datapath_->c_str(), "") == 0) &&
397  (strcmp(tesseract_->datadir.c_str(), "") != 0))
399 
400  if (language_ == nullptr)
401  language_ = new STRING(language);
402  else
403  *language_ = language;
405 
406 #ifndef DISABLED_LEGACY_ENGINE
407  // For same language and datapath, just reset the adaptive classifier.
408  if (reset_classifier) {
410  }
411 #endif // ndef DISABLED_LEGACY_ENGINE
412  return 0;
413 }

◆ Init() [2/4]

int tesseract::TessBaseAPI::Init ( const char *  datapath,
const char *  language 
)
inline

Definition at line 227 of file baseapi.h.

227  {
228  return Init(datapath, language, OEM_DEFAULT, nullptr, 0, nullptr, nullptr,
229  false);
230  }

◆ Init() [3/4]

int tesseract::TessBaseAPI::Init ( const char *  datapath,
const char *  language,
OcrEngineMode  oem,
char **  configs,
int  configs_size,
const GenericVector< STRING > *  vars_vec,
const GenericVector< STRING > *  vars_values,
bool  set_only_non_debug_params 
)

Instances are now mostly thread-safe and totally independent, but some global parameters remain. Basically it is safe to use multiple TessBaseAPIs in different threads in parallel, UNLESS: you use SetVariable on some of the Params in classify and textord. If you do, then the effect will be to change it for all your instances.

Start tesseract. Returns zero on success and -1 on failure. NOTE that the only members that may be called before Init are those listed above here in the class definition.

The datapath must be the name of the tessdata directory. The language is (usually) an ISO 639-3 string or nullptr will default to eng. It is entirely safe (and eventually will be efficient too) to call Init multiple times on the same instance to change language, or just to reset the classifier. The language may be a string of the form [~]<lang>[+[~]<lang>]* indicating that multiple languages are to be loaded. Eg hin+eng will load Hindi and English. Languages may specify internally that they want to be loaded with one or more other languages, so the ~ sign is available to override that. Eg if hin were set to load eng by default, then hin+~eng would force loading only hin. The number of loaded languages is limited only by memory, with the caveat that loading additional languages will impact both speed and accuracy, as there is more work to do to decide on the applicable language, and there is more chance of hallucinating incorrect words. WARNING: On changing languages, all Tesseract parameters are reset back to their default values. (Which may vary between languages.) If you have a rare need to set a Variable that controls initialization for a second call to Init you should explicitly call End() and then use SetVariable before Init. This is only a very rare use case, since there are very few uses that require any parameters to be set before Init.

If set_only_non_debug_params is true, only params that do not contain "debug" in the name will be set.

The datapath must be the name of the data directory or some other file in which the data directory resides (for instance argv[0].) The language is (usually) an ISO 639-3 string or nullptr will default to eng. If numeric_mode is true, then only digits and Roman numerals will be returned.

Returns
: 0 on success and -1 on initialization failure.

Definition at line 337 of file baseapi.cpp.

341  {
342  return Init(datapath, 0, language, oem, configs, configs_size, vars_vec,
343  vars_values, set_only_non_debug_params, nullptr);
344 }

◆ Init() [4/4]

int tesseract::TessBaseAPI::Init ( const char *  datapath,
const char *  language,
OcrEngineMode  oem 
)
inline

Definition at line 224 of file baseapi.h.

224  {
225  return Init(datapath, language, oem, nullptr, 0, nullptr, nullptr, false);
226  }

◆ InitForAnalysePage()

void tesseract::TessBaseAPI::InitForAnalysePage ( )

Init only for page layout analysis. Use only for calls to SetImage and AnalysePage. Calls that attempt recognition will generate an error.

Definition at line 478 of file baseapi.cpp.

478  {
479  if (tesseract_ == nullptr) {
480  tesseract_ = new Tesseract;
481  #ifndef DISABLED_LEGACY_ENGINE
483  #endif
484  }
485 }

◆ InitLangMod()

int tesseract::TessBaseAPI::InitLangMod ( const char *  datapath,
const char *  language 
)

Init only the lang model component of Tesseract. The only functions that work after this init are SetVariable and IsValidWord. WARNING: temporary! This function will be removed from here and placed in a separate API at some future time.

Definition at line 464 of file baseapi.cpp.

464  {
465  if (tesseract_ == nullptr)
466  tesseract_ = new Tesseract;
467  else
469  TessdataManager mgr;
470  return tesseract_->init_tesseract_lm(datapath, nullptr, language, &mgr);
471 }

◆ InitTruthCallback()

void tesseract::TessBaseAPI::InitTruthCallback ( TruthCallback  cb)
inline

Definition at line 806 of file baseapi.h.

806  {
807  truth_cb_ = cb;
808  }

◆ InternalSetImage()

bool tesseract::TessBaseAPI::InternalSetImage ( )
protected

Common code for setting the image. Returns true if Init has been called.

Common code for setting the image.

Definition at line 1994 of file baseapi.cpp.

1994  {
1995  if (tesseract_ == nullptr) {
1996  tprintf("Please call Init before attempting to set an image.\n");
1997  return false;
1998  }
1999  if (thresholder_ == nullptr)
2000  thresholder_ = new ImageThresholder;
2001  ClearResults();
2002  return true;
2003 }

◆ IsValidCharacter()

bool tesseract::TessBaseAPI::IsValidCharacter ( const char *  utf8_character)

Definition at line 1919 of file baseapi.cpp.

1919  {
1920  return tesseract_->unicharset.contains_unichar(utf8_character);
1921 }

◆ IsValidWord()

int tesseract::TessBaseAPI::IsValidWord ( const char *  word)

Check whether a word is valid according to Tesseract's language model

Returns
0 if the word is invalid, non-zero if valid.
Warning
temporary! This function will be removed from here and placed in a separate API at some future time.

Check whether a word is valid according to Tesseract's language model returns 0 if the word is invalid, non-zero if valid

Definition at line 1915 of file baseapi.cpp.

1915  {
1916  return tesseract_->getDict().valid_word(word);
1917 }

◆ MakeTBLOB()

TBLOB * tesseract::TessBaseAPI::MakeTBLOB ( Pix *  pix)
static

Returns a TBLOB corresponding to the entire input image.

Creates a TBLOB* from the whole pix.

Definition at line 2365 of file baseapi.cpp.

2365  {
2366  int width = pixGetWidth(pix);
2367  int height = pixGetHeight(pix);
2368  BLOCK block("a character", true, 0, 0, 0, 0, width, height);
2369 
2370  // Create C_BLOBs from the page
2371  extract_edges(pix, &block);
2372 
2373  // Merge all C_BLOBs
2374  C_BLOB_LIST *list = block.blob_list();
2375  C_BLOB_IT c_blob_it(list);
2376  if (c_blob_it.empty())
2377  return nullptr;
2378  // Move all the outlines to the first blob.
2379  C_OUTLINE_IT ol_it(c_blob_it.data()->out_list());
2380  for (c_blob_it.forward();
2381  !c_blob_it.at_first();
2382  c_blob_it.forward()) {
2383  C_BLOB *c_blob = c_blob_it.data();
2384  ol_it.add_list_after(c_blob->out_list());
2385  }
2386  // Convert the first blob to the output TBLOB.
2387  return TBLOB::PolygonalCopy(false, c_blob_it.data());
2388 }

◆ MakeTessOCRRow()

ROW * tesseract::TessBaseAPI::MakeTessOCRRow ( float  baseline,
float  xheight,
float  descender,
float  ascender 
)
static

Returns a ROW object created from the input row specification.

Definition at line 2348 of file baseapi.cpp.

2351  {
2352  int32_t xstarts[] = {-32000};
2353  double quad_coeffs[] = {0, 0, baseline};
2354  return new ROW(1,
2355  xstarts,
2356  quad_coeffs,
2357  xheight,
2358  ascender - (baseline + xheight),
2359  descender - baseline,
2360  0,
2361  0);
2362 }

◆ MeanTextConf()

int tesseract::TessBaseAPI::MeanTextConf ( )

Returns the (average) confidence value between 0 and 100.

Returns the average word confidence for Tesseract page result.

Definition at line 1749 of file baseapi.cpp.

1749  {
1750  int* conf = AllWordConfidences();
1751  if (!conf) return 0;
1752  int sum = 0;
1753  int *pt = conf;
1754  while (*pt >= 0) sum += *pt++;
1755  if (pt != conf) sum /= pt - conf;
1756  delete [] conf;
1757  return sum;
1758 }

◆ NormalizeTBLOB()

void tesseract::TessBaseAPI::NormalizeTBLOB ( TBLOB tblob,
ROW row,
bool  numeric_mode 
)
static

This method baseline normalizes a TBLOB in-place. The input row is used for normalization. The denorm is an optional parameter in which the normalization-antidote is returned.

Definition at line 2395 of file baseapi.cpp.

2395  {
2396  TBOX box = tblob->bounding_box();
2397  float x_center = (box.left() + box.right()) / 2.0f;
2398  float baseline = row->base_line(x_center);
2399  float scale = kBlnXHeight / row->x_height();
2400  tblob->Normalize(nullptr, nullptr, nullptr, x_center, baseline, scale, scale,
2401  0.0f, static_cast<float>(kBlnBaselineOffset), false, nullptr);
2402 }

◆ NumDawgs()

int tesseract::TessBaseAPI::NumDawgs ( ) const

Return the number of dawgs loaded into tesseract_ object.

Definition at line 2302 of file baseapi.cpp.

2302  {
2303  return tesseract_ == nullptr ? 0 : tesseract_->getDict().NumDawgs();
2304 }

◆ oem()

OcrEngineMode tesseract::TessBaseAPI::oem ( ) const
inline

Definition at line 802 of file baseapi.h.

802  {
803  return last_oem_requested_;
804  }

◆ operator=()

TessBaseAPI& tesseract::TessBaseAPI::operator= ( TessBaseAPI const &  )
delete

◆ PrintVariables()

void tesseract::TessBaseAPI::PrintVariables ( FILE *  fp) const

Print Tesseract parameters to the given file.

Definition at line 325 of file baseapi.cpp.

325  {
327 }

◆ ProcessPage()

bool tesseract::TessBaseAPI::ProcessPage ( Pix *  pix,
int  page_index,
const char *  filename,
const char *  retry_config,
int  timeout_millisec,
TessResultRenderer renderer 
)

Turn a single image into symbolic text.

The pix is the image processed. filename and page_index are metadata used by side-effect processes, such as reading a box file or formatting as hOCR.

See ProcessPages for desciptions of other parameters.

Definition at line 1237 of file baseapi.cpp.

1239  {
1240  SetInputName(filename);
1241  SetImage(pix);
1242  bool failed = false;
1243 
1245  // Disabled character recognition
1246  PageIterator* it = AnalyseLayout();
1247 
1248  if (it == nullptr) {
1249  failed = true;
1250  } else {
1251  delete it;
1252  }
1254  failed = FindLines() != 0;
1255  } else if (timeout_millisec > 0) {
1256  // Running with a timeout.
1257  ETEXT_DESC monitor;
1258  monitor.cancel = nullptr;
1259  monitor.cancel_this = nullptr;
1260  monitor.set_deadline_msecs(timeout_millisec);
1261 
1262  // Now run the main recognition.
1263  failed = Recognize(&monitor) < 0;
1264  } else {
1265  // Normal layout and character recognition with no timeout.
1266  failed = Recognize(nullptr) < 0;
1267  }
1268 
1270 #ifndef ANDROID_BUILD
1271  Pix* page_pix = GetThresholdedImage();
1272  pixWrite("tessinput.tif", page_pix, IFF_TIFF_G4);
1273 #endif // ANDROID_BUILD
1274  }
1275 
1276  if (failed && retry_config != nullptr && retry_config[0] != '\0') {
1277  // Save current config variables before switching modes.
1278  FILE* fp = fopen(kOldVarsFile, "wb");
1279  if (fp == nullptr) {
1280  tprintf("Error, failed to open file \"%s\"\n", kOldVarsFile);
1281  } else {
1282  PrintVariables(fp);
1283  fclose(fp);
1284  }
1285  // Switch to alternate mode for retry.
1286  ReadConfigFile(retry_config);
1287  SetImage(pix);
1288  Recognize(nullptr);
1289  // Restore saved config variables.
1290  ReadConfigFile(kOldVarsFile);
1291  }
1292 
1293  if (renderer && !failed) {
1294  failed = !renderer->AddImage(this);
1295  }
1296 
1297  return !failed;
1298 }

◆ ProcessPages()

bool tesseract::TessBaseAPI::ProcessPages ( const char *  filename,
const char *  retry_config,
int  timeout_millisec,
TessResultRenderer renderer 
)

Turns images into symbolic text.

filename can point to a single image, a multi-page TIFF, or a plain text list of image filenames.

retry_config is useful for debugging. If not nullptr, you can fall back to an alternate configuration if a page fails for some reason.

timeout_millisec terminates processing if any single page takes too long. Set to 0 for unlimited time.

renderer is responible for creating the output. For example, use the TessTextRenderer if you want plaintext output, or the TessPDFRender to produce searchable PDF.

If tessedit_page_number is non-negative, will only process that single page. Works for multi-page tiff file, or filelist.

Returns true if successful, false on error.

Definition at line 1073 of file baseapi.cpp.

1075  {
1076  bool result =
1077  ProcessPagesInternal(filename, retry_config, timeout_millisec, renderer);
1078  #ifndef DISABLED_LEGACY_ENGINE
1079  if (result) {
1082  tprintf("Write of TR file failed: %s\n", output_file_->c_str());
1083  return false;
1084  }
1085  }
1086  #endif // ndef DISABLED_LEGACY_ENGINE
1087  return result;
1088 }

◆ ProcessPagesInternal()

bool tesseract::TessBaseAPI::ProcessPagesInternal ( const char *  filename,
const char *  retry_config,
int  timeout_millisec,
TessResultRenderer renderer 
)

Definition at line 1110 of file baseapi.cpp.

1113  {
1114  bool stdInput = !strcmp(filename, "stdin") || !strcmp(filename, "-");
1115  if (stdInput) {
1116 #ifdef WIN32
1117  if (_setmode(_fileno(stdin), _O_BINARY) == -1)
1118  tprintf("ERROR: cin to binary: %s", strerror(errno));
1119 #endif // WIN32
1120  }
1121 
1122  if (stream_filelist) {
1123  return ProcessPagesFileList(stdin, nullptr, retry_config,
1124  timeout_millisec, renderer,
1126  }
1127 
1128  // At this point we are officially in autodection territory.
1129  // That means any data in stdin must be buffered, to make it
1130  // seekable.
1131  std::string buf;
1132  const l_uint8 *data = nullptr;
1133  if (stdInput) {
1134  buf.assign((std::istreambuf_iterator<char>(std::cin)),
1135  (std::istreambuf_iterator<char>()));
1136  data = reinterpret_cast<const l_uint8 *>(buf.data());
1137  } else if (strncmp(filename, "http:", 5) == 0 ||
1138  strncmp(filename, "https:", 6) == 0 ) {
1139  // Get image or image list by URL.
1140 #ifdef HAVE_LIBCURL
1141  CURL* curl = curl_easy_init();
1142  if (curl == nullptr) {
1143  fprintf(stderr, "Error, curl_easy_init failed\n");
1144  return false;
1145  } else {
1146  CURLcode curlcode;
1147  curlcode = curl_easy_setopt(curl, CURLOPT_URL, filename);
1148  ASSERT_HOST(curlcode == CURLE_OK);
1149  curlcode = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
1150  ASSERT_HOST(curlcode == CURLE_OK);
1151  curlcode = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
1152  ASSERT_HOST(curlcode == CURLE_OK);
1153  curlcode = curl_easy_perform(curl);
1154  ASSERT_HOST(curlcode == CURLE_OK);
1155  curl_easy_cleanup(curl);
1156  data = reinterpret_cast<const l_uint8 *>(buf.data());
1157  }
1158 #else
1159  fprintf(stderr, "Error, this tesseract has no URL support\n");
1160  return false;
1161 #endif
1162  } else {
1163  // Check whether the input file can be read.
1164  if (FILE* file = fopen(filename, "rb")) {
1165  fclose(file);
1166  } else {
1167  fprintf(stderr, "Error, cannot read input file %s: %s\n",
1168  filename, strerror(errno));
1169  return false;
1170  }
1171  }
1172 
1173  // Here is our autodetection
1174  int format;
1175  int r = (data != nullptr) ?
1176  findFileFormatBuffer(data, &format) :
1177  findFileFormat(filename, &format);
1178 
1179  // Maybe we have a filelist
1180  if (r != 0 || format == IFF_UNKNOWN) {
1181  STRING s;
1182  if (data != nullptr) {
1183  s = buf.c_str();
1184  } else {
1185  std::ifstream t(filename);
1186  std::string u((std::istreambuf_iterator<char>(t)),
1187  std::istreambuf_iterator<char>());
1188  s = u.c_str();
1189  }
1190  return ProcessPagesFileList(nullptr, &s, retry_config,
1191  timeout_millisec, renderer,
1193  }
1194 
1195  // Maybe we have a TIFF which is potentially multipage
1196  bool tiff = (format == IFF_TIFF || format == IFF_TIFF_PACKBITS ||
1197  format == IFF_TIFF_RLE || format == IFF_TIFF_G3 ||
1198  format == IFF_TIFF_G4 || format == IFF_TIFF_LZW ||
1199 #if LIBLEPT_MAJOR_VERSION > 1 || LIBLEPT_MINOR_VERSION > 76
1200  format == IFF_TIFF_JPEG ||
1201 #endif
1202  format == IFF_TIFF_ZIP);
1203 
1204  // Fail early if we can, before producing any output
1205  Pix *pix = nullptr;
1206  if (!tiff) {
1207  pix = (data != nullptr) ? pixReadMem(data, buf.size()) : pixRead(filename);
1208  if (pix == nullptr) {
1209  return false;
1210  }
1211  }
1212 
1213  // Begin the output
1214  if (renderer && !renderer->BeginDocument(document_title.c_str())) {
1215  pixDestroy(&pix);
1216  return false;
1217  }
1218 
1219  // Produce output
1220  r = (tiff) ?
1221  ProcessPagesMultipageTiff(data, buf.size(), filename, retry_config,
1222  timeout_millisec, renderer,
1224  ProcessPage(pix, 0, filename, retry_config,
1225  timeout_millisec, renderer);
1226 
1227  // Clean up memory as needed
1228  pixDestroy(&pix);
1229 
1230  // End the output
1231  if (!r || (renderer && !renderer->EndDocument())) {
1232  return false;
1233  }
1234  return true;
1235 }

◆ ReadConfigFile()

void tesseract::TessBaseAPI::ReadConfigFile ( const char *  filename)

Read a "config" file containing a set of param, value pairs. Searches the standard places: tessdata/configs, tessdata/tessconfigs and also accepts a relative or absolute path name. Note: only non-init params will be set (init params are set by Init()).

Read a "config" file containing a set of parameter name, value pairs. Searches the standard places: tessdata/configs, tessdata/tessconfigs and also accepts a relative or absolute path name.

Definition at line 492 of file baseapi.cpp.

◆ ReadDebugConfigFile()

void tesseract::TessBaseAPI::ReadDebugConfigFile ( const char *  filename)

Same as above, but only set debug params from the given config file.

Definition at line 497 of file baseapi.cpp.

497  {
499 }

◆ RecognitionPass1()

PAGE_RES * tesseract::TessBaseAPI::RecognitionPass1 ( BLOCK_LIST *  block_list)
protected

Recognize text doing one pass only, using settings for a given pass.

Definition at line 2461 of file baseapi.cpp.

2461  {
2462  auto *page_res = new PAGE_RES(false, block_list,
2464  tesseract_->recog_all_words(page_res, nullptr, nullptr, nullptr, 1);
2465  return page_res;
2466 }

◆ RecognitionPass2()

PAGE_RES * tesseract::TessBaseAPI::RecognitionPass2 ( BLOCK_LIST *  block_list,
PAGE_RES pass1_result 
)
protected

Definition at line 2468 of file baseapi.cpp.

2469  {
2470  if (!pass1_result)
2471  pass1_result = new PAGE_RES(false, block_list,
2473  tesseract_->recog_all_words(pass1_result, nullptr, nullptr, nullptr, 2);
2474  return pass1_result;
2475 }

◆ Recognize()

int tesseract::TessBaseAPI::Recognize ( ETEXT_DESC monitor)

Recognize the image from SetAndThresholdImage, generating Tesseract internal structures. Returns 0 on success. Optional. The Get*Text functions below will call Recognize if needed. After Recognize, the output is kept internally until the next SetImage.

Recognize the tesseract global image and return the result as Tesseract internal structures.

Definition at line 827 of file baseapi.cpp.

827  {
828  if (tesseract_ == nullptr)
829  return -1;
830  if (FindLines() != 0)
831  return -1;
832  delete page_res_;
833  if (block_list_->empty()) {
834  page_res_ = new PAGE_RES(false, block_list_,
836  return 0; // Empty page.
837  }
838 
840  recognition_done_ = true;
841 #ifndef DISABLED_LEGACY_ENGINE
846  } else
847 #endif // ndef DISABLED_LEGACY_ENGINE
848  {
851  }
852 
853  if (page_res_ == nullptr) {
854  return -1;
855  }
856 
859  return -1;
860  }
862  return 0;
863  }
864 #ifndef DISABLED_LEGACY_ENGINE
867  return 0;
868  }
869 #endif // ndef DISABLED_LEGACY_ENGINE
870 
871  if (truth_cb_ != nullptr) {
872  tesseract_->wordrec_run_blamer.set_value(true);
873  auto *page_it = new PageIterator(
878  image_height_, page_it, this->tesseract()->pix_grey());
879  delete page_it;
880  }
881 
882  int result = 0;
884  #ifndef GRAPHICS_DISABLED
886  #endif // GRAPHICS_DISABLED
887  // The page_res is invalid after an interactive session, so cleanup
888  // in a way that lets us continue to the next page without crashing.
889  delete page_res_;
890  page_res_ = nullptr;
891  return -1;
892  #ifndef DISABLED_LEGACY_ENGINE
894  STRING fontname;
895  ExtractFontName(*output_file_, &fontname);
897  } else if (tesseract_->tessedit_ambigs_training) {
898  FILE *training_output_file = tesseract_->init_recog_training(*input_file_);
899  // OCR the page segmented into words by tesseract.
901  *input_file_, page_res_, monitor, training_output_file);
902  fclose(training_output_file);
903  #endif // ndef DISABLED_LEGACY_ENGINE
904  } else {
905  // Now run the main recognition.
906  bool wait_for_text = true;
907  GetBoolVariable("paragraph_text_based", &wait_for_text);
908  if (!wait_for_text) DetectParagraphs(false);
909  if (tesseract_->recog_all_words(page_res_, monitor, nullptr, nullptr, 0)) {
910  if (wait_for_text) DetectParagraphs(true);
911  } else {
912  result = -1;
913  }
914  }
915  return result;
916 }

◆ RecognizeForChopTest()

int tesseract::TessBaseAPI::RecognizeForChopTest ( ETEXT_DESC monitor)

Methods to retrieve information after SetAndThresholdImage(), Recognize() or TesseractRect(). (Recognize is called implicitly if needed.) Variant on Recognize used for testing chopper.

Tests the chopper by exhaustively running chop_one_blob.

Definition at line 920 of file baseapi.cpp.

920  {
921  if (tesseract_ == nullptr)
922  return -1;
923  if (thresholder_ == nullptr || thresholder_->IsEmpty()) {
924  tprintf("Please call SetImage before attempting recognition.\n");
925  return -1;
926  }
927  if (page_res_ != nullptr)
928  ClearResults();
929  if (FindLines() != 0)
930  return -1;
931  // Additional conditions under which chopper test cannot be run
932  if (tesseract_->interactive_display_mode) return -1;
933 
934  recognition_done_ = true;
935 
936  page_res_ = new PAGE_RES(false, block_list_,
938 
939  PAGE_RES_IT page_res_it(page_res_);
940 
941  while (page_res_it.word() != nullptr) {
942  WERD_RES *word_res = page_res_it.word();
943  GenericVector<TBOX> boxes;
944  tesseract_->MaximallyChopWord(boxes, page_res_it.block()->block,
945  page_res_it.row()->row, word_res);
946  page_res_it.forward();
947  }
948  return 0;
949 }

◆ RunAdaptiveClassifier()

void tesseract::TessBaseAPI::RunAdaptiveClassifier ( TBLOB blob,
int  num_max_matches,
int *  unichar_ids,
float *  ratings,
int *  num_matches_returned 
)

Method to run adaptive classifier on a blob. It returns at max num_max_matches results.

Method to run adaptive classifier on a blob.

Definition at line 2657 of file baseapi.cpp.

2661  {
2662  auto* choices = new BLOB_CHOICE_LIST;
2663  tesseract_->AdaptiveClassifier(blob, choices);
2664  BLOB_CHOICE_IT choices_it(choices);
2665  int& index = *num_matches_returned;
2666  index = 0;
2667  for (choices_it.mark_cycle_pt();
2668  !choices_it.cycled_list() && index < num_max_matches;
2669  choices_it.forward()) {
2670  BLOB_CHOICE* choice = choices_it.data();
2671  unichar_ids[index] = choice->unichar_id();
2672  ratings[index] = choice->rating();
2673  ++index;
2674  }
2675  *num_matches_returned = index;
2676  delete choices;
2677 }

◆ set_min_orientation_margin()

void tesseract::TessBaseAPI::set_min_orientation_margin ( double  margin)

Definition at line 2212 of file baseapi.cpp.

2212  {
2213  tesseract_->min_orientation_margin.set_value(margin);
2214 }

◆ SetDebugVariable()

bool tesseract::TessBaseAPI::SetDebugVariable ( const char *  name,
const char *  value 
)

Definition at line 283 of file baseapi.cpp.

283  {
284  if (tesseract_ == nullptr) tesseract_ = new Tesseract;
286  tesseract_->params());
287 }

◆ SetDictFunc()

void tesseract::TessBaseAPI::SetDictFunc ( DictFunc  f)

Sets Dict::letter_is_okay_ function to point to the given function.

Definition at line 1961 of file baseapi.cpp.

1961  {
1962  if (tesseract_ != nullptr) {
1964  }
1965 }

◆ SetFillLatticeFunc()

void tesseract::TessBaseAPI::SetFillLatticeFunc ( FillLatticeFunc  f)

Sets Wordrec::fill_lattice_ function to point to the given function.

Definition at line 1988 of file baseapi.cpp.

1988  {
1989  if (tesseract_ != nullptr) tesseract_->fill_lattice_ = f;
1990 }

◆ SetImage() [1/2]

void tesseract::TessBaseAPI::SetImage ( const unsigned char *  imagedata,
int  width,
int  height,
int  bytes_per_pixel,
int  bytes_per_line 
)

Provide an image for Tesseract to recognize. Format is as TesseractRect above. Copies the image buffer and converts to Pix. SetImage clears all recognition results, and sets the rectangle to the full image, so it may be followed immediately by a GetUTF8Text, and it will automatically perform recognition.

Definition at line 571 of file baseapi.cpp.

573  {
574  if (InternalSetImage()) {
575  thresholder_->SetImage(imagedata, width, height,
576  bytes_per_pixel, bytes_per_line);
578  }
579 }

◆ SetImage() [2/2]

void tesseract::TessBaseAPI::SetImage ( Pix *  pix)

Provide an image for Tesseract to recognize. As with SetImage above, Tesseract takes its own copy of the image, so it need not persist until after Recognize. Pix vs raw, which to use? Use Pix where possible. Tesseract uses Pix as its internal representation and it is therefore more efficient to provide a Pix directly.

Definition at line 596 of file baseapi.cpp.

596  {
597  if (InternalSetImage()) {
598  if (pixGetSpp(pix) == 4 && pixGetInputFormat(pix) == IFF_PNG) {
599  // remove alpha channel from png
600  Pix* p1 = pixRemoveAlpha(pix);
601  pixSetSpp(p1, 3);
602  (void)pixCopy(pix, p1);
603  pixDestroy(&p1);
604  }
605  thresholder_->SetImage(pix);
607  }
608 }

◆ SetInputImage()

void tesseract::TessBaseAPI::SetInputImage ( Pix *  pix)

Definition at line 953 of file baseapi.cpp.

953 { tesseract_->set_pix_original(pix); }

◆ SetInputName()

void tesseract::TessBaseAPI::SetInputName ( const char *  name)

Set the name of the input file. Needed for training and reading a UNLV zone file, and for searchable PDF output.

Set the name of the input file. Needed only for training and loading a UNLV zone file.

Definition at line 262 of file baseapi.cpp.

262  {
263  if (input_file_ == nullptr)
264  input_file_ = new STRING(name);
265  else
266  *input_file_ = name;
267 }

◆ SetOutputName()

void tesseract::TessBaseAPI::SetOutputName ( const char *  name)

Set the name of the bonus output files. Needed only for debugging.

Set the name of the output files. Needed only for debugging.

Definition at line 270 of file baseapi.cpp.

270  {
271  if (output_file_ == nullptr)
272  output_file_ = new STRING(name);
273  else
274  *output_file_ = name;
275 }

◆ SetPageSegMode()

void tesseract::TessBaseAPI::SetPageSegMode ( PageSegMode  mode)

Set the current page segmentation mode. Defaults to PSM_SINGLE_BLOCK. The mode is stored as an IntParam so it can also be modified by ReadConfigFile or SetVariable("tessedit_pageseg_mode", mode as string).

Set the current page segmentation mode. Defaults to PSM_AUTO. The mode is stored as an IntParam so it can also be modified by ReadConfigFile or SetVariable("tessedit_pageseg_mode", mode as string).

Definition at line 506 of file baseapi.cpp.

506  {
507  if (tesseract_ == nullptr)
508  tesseract_ = new Tesseract;
509  tesseract_->tessedit_pageseg_mode.set_value(mode);
510 }

◆ SetProbabilityInContextFunc()

void tesseract::TessBaseAPI::SetProbabilityInContextFunc ( ProbabilityInContextFunc  f)

Sets Dict::probability_in_context_ function to point to the given function.

Sets Dict::probability_in_context_ function to point to the given function.

Parameters
fA single function that returns the probability of the current "character" (in general a utf-8 string), given the context of a previous utf-8 string.

Definition at line 1975 of file baseapi.cpp.

1975  {
1976  if (tesseract_ != nullptr) {
1978  // Set it for the sublangs too.
1979  int num_subs = tesseract_->num_sub_langs();
1980  for (int i = 0; i < num_subs; ++i) {
1982  }
1983  }
1984 }

◆ SetRectangle()

void tesseract::TessBaseAPI::SetRectangle ( int  left,
int  top,
int  width,
int  height 
)

Restrict recognition to a sub-rectangle of the image. Call after SetImage. Each SetRectangle clears the recogntion results so multiple rectangles can be recognized with the same image.

Definition at line 615 of file baseapi.cpp.

615  {
616  if (thresholder_ == nullptr)
617  return;
618  thresholder_->SetRectangle(left, top, width, height);
619  ClearResults();
620 }

◆ SetSourceResolution()

void tesseract::TessBaseAPI::SetSourceResolution ( int  ppi)

Set the resolution of the source image in pixels per inch so font size information can be calculated in results. Call this after SetImage().

Definition at line 581 of file baseapi.cpp.

581  {
582  if (thresholder_)
584  else
585  tprintf("Please call SetImage before SetSourceResolution.\n");
586 }

◆ SetThresholder()

void tesseract::TessBaseAPI::SetThresholder ( ImageThresholder thresholder)
inline

In extreme cases only, usually with a subclass of Thresholder, it is possible to provide a different Thresholder. The Thresholder may be preloaded with an image, settings etc, or they may be set after. Note that Tesseract takes ownership of the Thresholder and will delete it when it it is replaced or the API is destructed.

Definition at line 370 of file baseapi.h.

370  {
371  delete thresholder_;
372  thresholder_ = thresholder;
373  ClearResults();
374  }

◆ SetVariable()

bool tesseract::TessBaseAPI::SetVariable ( const char *  name,
const char *  value 
)

Set the value of an internal "parameter." Supply the name of the parameter and the value as a string, just as you would in a config file. Returns false if the name lookup failed. Eg SetVariable("tessedit_char_blacklist", "xyz"); to ignore x, y and z. Or SetVariable("classify_bln_numeric_mode", "1"); to set numeric-only mode. SetVariable may be used before Init, but settings will revert to defaults on End().

Note: Must be called after Init(). Only works for non-init variables (init variables should be passed to Init()).

Definition at line 277 of file baseapi.cpp.

277  {
278  if (tesseract_ == nullptr) tesseract_ = new Tesseract;
280  tesseract_->params());
281 }

◆ tesseract()

Tesseract* tesseract::TessBaseAPI::tesseract ( ) const
inline

Definition at line 798 of file baseapi.h.

798  {
799  return tesseract_;
800  }

◆ TesseractExtractResult()

int tesseract::TessBaseAPI::TesseractExtractResult ( char **  text,
int **  lengths,
float **  costs,
int **  x0,
int **  y0,
int **  x1,
int **  y1,
PAGE_RES page_res 
)
staticprotected

Extract the OCR results, costs (penalty points for uncertainty), and the bounding boxes of the characters.

Definition at line 2552 of file baseapi.cpp.

2559  {
2560  TESS_CHAR_LIST tess_chars;
2561  TESS_CHAR_IT tess_chars_it(&tess_chars);
2562  extract_result(&tess_chars_it, page_res);
2563  tess_chars_it.move_to_first();
2564  int n = tess_chars.length();
2565  int text_len = 0;
2566  *lengths = new int[n];
2567  *costs = new float[n];
2568  *x0 = new int[n];
2569  *y0 = new int[n];
2570  *x1 = new int[n];
2571  *y1 = new int[n];
2572  int i = 0;
2573  for (tess_chars_it.mark_cycle_pt();
2574  !tess_chars_it.cycled_list();
2575  tess_chars_it.forward(), i++) {
2576  TESS_CHAR *tc = tess_chars_it.data();
2577  text_len += (*lengths)[i] = tc->length;
2578  (*costs)[i] = tc->cost;
2579  (*x0)[i] = tc->box.left();
2580  (*y0)[i] = tc->box.bottom();
2581  (*x1)[i] = tc->box.right();
2582  (*y1)[i] = tc->box.top();
2583  }
2584  char *p = *text = new char[text_len];
2585 
2586  tess_chars_it.move_to_first();
2587  for (tess_chars_it.mark_cycle_pt();
2588  !tess_chars_it.cycled_list();
2589  tess_chars_it.forward()) {
2590  TESS_CHAR *tc = tess_chars_it.data();
2591  strncpy(p, tc->unicode_repr, tc->length);
2592  p += tc->length;
2593  }
2594  return n;
2595 }

◆ TesseractRect()

char * tesseract::TessBaseAPI::TesseractRect ( const unsigned char *  imagedata,
int  bytes_per_pixel,
int  bytes_per_line,
int  left,
int  top,
int  width,
int  height 
)

Recognize a rectangle from an image and return the result as a string. May be called many times for a single Init. Currently has no error checking. Greyscale of 8 and color of 24 or 32 bits per pixel may be given. Palette color images will not work properly and must be converted to 24 bit. Binary images of 1 bit per pixel may also be given but they must be byte packed with the MSB of the first byte being the first pixel, and a 1 represents WHITE. For binary images set bytes_per_pixel=0. The recognized text is returned as a char* which is coded as UTF8 and must be freed with the delete [] operator.

Note that TesseractRect is the simplified convenience interface. For advanced uses, use SetImage, (optionally) SetRectangle, Recognize, and one or more of the Get*Text functions below.

Recognize a rectangle from an image and return the result as a string. May be called many times for a single Init. Currently has no error checking. Greyscale of 8 and color of 24 or 32 bits per pixel may be given. Palette color images will not work properly and must be converted to 24 bit. Binary images of 1 bit per pixel may also be given but they must be byte packed with the MSB of the first byte being the first pixel, and a one pixel is WHITE. For binary images set bytes_per_pixel=0. The recognized text is returned as a char* which is coded as UTF8 and must be freed with the delete [] operator.

Definition at line 533 of file baseapi.cpp.

537  {
538  if (tesseract_ == nullptr || width < kMinRectSize || height < kMinRectSize)
539  return nullptr; // Nothing worth doing.
540 
541  // Since this original api didn't give the exact size of the image,
542  // we have to invent a reasonable value.
543  int bits_per_pixel = bytes_per_pixel == 0 ? 1 : bytes_per_pixel * 8;
544  SetImage(imagedata, bytes_per_line * 8 / bits_per_pixel, height + top,
545  bytes_per_pixel, bytes_per_line);
546  SetRectangle(left, top, width, height);
547 
548  return GetUTF8Text();
549 }

◆ TextLength()

int tesseract::TessBaseAPI::TextLength ( int *  blob_count)
protected

Return the length of the output text string, as UTF8, assuming one newline per line and one per block, with a terminator, and assuming a single character reject marker for each rejected character. Also return the number of recognized blobs in blob_count.

Return the length of the output text string, as UTF8, assuming liberally two spacing marks after each word (as paragraphs end with two newlines), and assuming a single character reject marker for each rejected character. Also return the number of recognized blobs in blob_count.

Definition at line 2166 of file baseapi.cpp.

2166  {
2167  if (tesseract_ == nullptr || page_res_ == nullptr)
2168  return 0;
2169 
2170  PAGE_RES_IT page_res_it(page_res_);
2171  int total_length = 2;
2172  int total_blobs = 0;
2173  // Iterate over the data structures to extract the recognition result.
2174  for (page_res_it.restart_page(); page_res_it.word () != nullptr;
2175  page_res_it.forward()) {
2176  WERD_RES *word = page_res_it.word();
2177  WERD_CHOICE* choice = word->best_choice;
2178  if (choice != nullptr) {
2179  total_blobs += choice->length() + 2;
2180  total_length += choice->unichar_string().length() + 2;
2181  for (int i = 0; i < word->reject_map.length(); ++i) {
2182  if (word->reject_map[i].rejected())
2183  ++total_length;
2184  }
2185  }
2186  }
2187  if (blob_count != nullptr)
2188  *blob_count = total_blobs;
2189  return total_length;
2190 }

◆ Threshold()

bool tesseract::TessBaseAPI::Threshold ( Pix **  pix)
protectedvirtual

Run the thresholder to make the thresholded image. If pix is not nullptr, the source is thresholded to pix instead of the internal IMAGE.

Run the thresholder to make the thresholded image, returned in pix, which must not be nullptr. *pix must be initialized to nullptr, or point to an existing pixDestroyable Pix. The usual argument to Threshold is Tesseract::mutable_pix_binary().

Definition at line 2011 of file baseapi.cpp.

2011  {
2012  ASSERT_HOST(pix != nullptr);
2013  if (*pix != nullptr)
2014  pixDestroy(pix);
2015  // Zero resolution messes up the algorithms, so make sure it is credible.
2016  int user_dpi = 0;
2017  GetIntVariable("user_defined_dpi", &user_dpi);
2018  int y_res = thresholder_->GetScaledYResolution();
2019  if (user_dpi && (user_dpi < kMinCredibleResolution ||
2020  user_dpi > kMaxCredibleResolution)) {
2021  tprintf("Warning: User defined image dpi is outside of expected range "
2022  "(%d - %d)!\n",
2024  }
2025  // Always use user defined dpi
2026  if (user_dpi) {
2028  } else if (y_res < kMinCredibleResolution ||
2029  y_res > kMaxCredibleResolution) {
2030  tprintf("Warning: Invalid resolution %d dpi. Using %d instead.\n",
2031  y_res, kMinCredibleResolution);
2033  }
2034  auto pageseg_mode =
2035  static_cast<PageSegMode>(
2036  static_cast<int>(tesseract_->tessedit_pageseg_mode));
2037  if (!thresholder_->ThresholdToPix(pageseg_mode, pix)) return false;
2041  if (!thresholder_->IsBinary()) {
2044  } else {
2045  tesseract_->set_pix_thresholds(nullptr);
2046  tesseract_->set_pix_grey(nullptr);
2047  }
2048  // Set the internal resolution that is used for layout parameters from the
2049  // estimated resolution, rather than the image resolution, which may be
2050  // fabricated, but we will use the image resolution, if there is one, to
2051  // report output point sizes.
2052  int estimated_res = ClipToRange(thresholder_->GetScaledEstimatedResolution(),
2055  if (estimated_res != thresholder_->GetScaledEstimatedResolution()) {
2056  tprintf("Estimated internal resolution %d out of range! "
2057  "Corrected to %d.\n",
2058  thresholder_->GetScaledEstimatedResolution(), estimated_res);
2059  }
2060  tesseract_->set_source_resolution(estimated_res);
2061  return true;
2062 }

◆ Version()

const char * tesseract::TessBaseAPI::Version ( )
static

Returns the version identifier as a static string. Do not delete.

Definition at line 233 of file baseapi.cpp.

233  {
234  return PACKAGE_VERSION;
235 }

Member Data Documentation

◆ block_list_

BLOCK_LIST* tesseract::TessBaseAPI::block_list_
protected

The page layout.

Definition at line 892 of file baseapi.h.

◆ datapath_

STRING* tesseract::TessBaseAPI::datapath_
protected

Current location of tessdata.

Definition at line 896 of file baseapi.h.

◆ equ_detect_

EquationDetect* tesseract::TessBaseAPI::equ_detect_
protected

The equation detector.

Definition at line 888 of file baseapi.h.

◆ image_height_

int tesseract::TessBaseAPI::image_height_
protected

Definition at line 912 of file baseapi.h.

◆ image_width_

int tesseract::TessBaseAPI::image_width_
protected

Definition at line 911 of file baseapi.h.

◆ input_file_

STRING* tesseract::TessBaseAPI::input_file_
protected

Name used by training code.

Definition at line 894 of file baseapi.h.

◆ language_

STRING* tesseract::TessBaseAPI::language_
protected

Last initialized language.

Definition at line 897 of file baseapi.h.

◆ last_oem_requested_

OcrEngineMode tesseract::TessBaseAPI::last_oem_requested_
protected

Last ocr language mode requested.

Definition at line 898 of file baseapi.h.

◆ osd_tesseract_

Tesseract* tesseract::TessBaseAPI::osd_tesseract_
protected

For orientation & script detection.

Definition at line 887 of file baseapi.h.

◆ output_file_

STRING* tesseract::TessBaseAPI::output_file_
protected

Name used by debug code.

Definition at line 895 of file baseapi.h.

◆ page_res_

PAGE_RES* tesseract::TessBaseAPI::page_res_
protected

The page-level data.

Definition at line 893 of file baseapi.h.

◆ paragraph_models_

GenericVector<ParagraphModel*>* tesseract::TessBaseAPI::paragraph_models_
protected

Definition at line 891 of file baseapi.h.

◆ reader_

FileReader tesseract::TessBaseAPI::reader_
protected

Reads files from any filesystem.

Definition at line 889 of file baseapi.h.

◆ recognition_done_

bool tesseract::TessBaseAPI::recognition_done_
protected

page_res_ contains recognition data.

Definition at line 899 of file baseapi.h.

◆ rect_height_

int tesseract::TessBaseAPI::rect_height_
protected

Definition at line 910 of file baseapi.h.

◆ rect_left_

int tesseract::TessBaseAPI::rect_left_
protected

Definition at line 907 of file baseapi.h.

◆ rect_top_

int tesseract::TessBaseAPI::rect_top_
protected

Definition at line 908 of file baseapi.h.

◆ rect_width_

int tesseract::TessBaseAPI::rect_width_
protected

Definition at line 909 of file baseapi.h.

◆ tesseract_

Tesseract* tesseract::TessBaseAPI::tesseract_
protected

The underlying data object.

Definition at line 886 of file baseapi.h.

◆ thresholder_

ImageThresholder* tesseract::TessBaseAPI::thresholder_
protected

Image thresholding module.

Definition at line 890 of file baseapi.h.

◆ truth_cb_

TruthCallback tesseract::TessBaseAPI::truth_cb_
protected

fxn for setting truth_* in WERD_RES

Definition at line 900 of file baseapi.h.


The documentation for this class was generated from the following files:
tesseract::TessBaseAPI::DetectOrientationScript
bool DetectOrientationScript(int *orient_deg, float *orient_conf, const char **script_name, float *script_conf)
Definition: baseapi.cpp:1683
string
std::string string
Definition: equationdetect_test.cc:21
WERD_CHOICE::unichar_string
const STRING & unichar_string() const
Definition: ratngs.h:529
GenericVector::delete_data_pointers
void delete_data_pointers()
Definition: genericvector.h:872
tesseract::ImageThresholder::GetImageSizes
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
Definition: thresholder.cpp:134
ClipToRange
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:106
tesseract::TessBaseAPI::GetMutableIterator
MutableIterator * GetMutableIterator()
Definition: baseapi.cpp:1338
tesseract::Dict::NumDawgs
int NumDawgs() const
Return the number of dawgs in the dawgs_ vector.
Definition: dict.h:430
tesseract::Tesseract::init_tesseract
int init_tesseract(const char *arg0, const char *textbase, const char *language, OcrEngineMode oem, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_init_params, TessdataManager *mgr)
Definition: tessedit.cpp:302
tesseract::CCUtil::datadir
STRING datadir
Definition: ccutil.h:53
tesseract::Tesseract::PrepareForTessOCR
void PrepareForTessOCR(BLOCK_LIST *block_list, Tesseract *osd_tess, OSResults *osr)
Definition: tesseractclass.cpp:672
tesseract::PSM_OSD_ENABLED
bool PSM_OSD_ENABLED(int pageseg_mode)
Definition: publictypes.h:188
OSResults::best_result
OSBestResult best_result
Definition: osdetect.h:81
tesseract::RIL_WORD
Definition: publictypes.h:220
FCOORD::angle
float angle() const
find angle
Definition: points.h:246
tesseract::Tesseract::tessedit_train_from_boxes
bool tessedit_train_from_boxes
Definition: tesseractclass.h:787
tesseract::Tesseract::pix_grey
Pix * pix_grey() const
Definition: tesseractclass.h:203
tesseract::TessBaseAPI::Threshold
virtual TESS_LOCAL bool Threshold(Pix **pix)
Definition: baseapi.cpp:2011
tesseract::Tesseract::ResetDocumentDictionary
void ResetDocumentDictionary()
Definition: tesseractclass.cpp:607
CR_DELETE
Definition: pageres.h:156
ROW::base_line
float base_line(float xpos) const
Definition: ocrrow.h:58
tesseract::TessBaseAPI::page_res_
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:893
tesseract::TessBaseAPI::last_oem_requested_
OcrEngineMode last_oem_requested_
Last ocr language mode requested.
Definition: baseapi.h:898
WERD::flag
bool flag(WERD_FLAGS mask) const
Definition: werd.h:116
C_BLOB::out_list
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:69
OrientationIdToValue
TESS_API int OrientationIdToValue(const int &id)
Definition: osdetect.cpp:566
kBlnXHeight
const int kBlnXHeight
Definition: normalis.h:23
PDBLK::bounding_box
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:58
WERD_RES::BestChoiceToCorrectText
void BestChoiceToCorrectText()
Definition: pageres.cpp:920
tesseract::Dict::probability_in_context_
double(Dict::* probability_in_context_)(const char *lang, const char *context, int context_bytes, const char *character, int character_bytes)
Probability in context function used by the ngram permuter.
Definition: dict.h:384
tesseract::SET_PARAM_CONSTRAINT_NON_INIT_ONLY
Definition: params.h:53
tesseract::Tesseract::lstm_choice_mode
int lstm_choice_mode
Definition: tesseractclass.h:1086
tesseract::TessBaseAPI::image_height_
int image_height_
Definition: baseapi.h:912
tesseract::ImageThresholder::IsBinary
bool IsBinary() const
Returns true if the source image is binary.
Definition: thresholder.h:74
WERD_CHOICE
Definition: ratngs.h:261
tesseract::Tesseract::recog_training_segmented
void recog_training_segmented(const STRING &fname, PAGE_RES *page_res, volatile ETEXT_DESC *monitor, FILE *output_file)
Definition: recogtraining.cpp:84
tesseract::kUniChs
const int kUniChs[]
Definition: baseapi.cpp:1558
tesseract::TessBaseAPI::TextLength
TESS_LOCAL int TextLength(int *blob_count)
Definition: baseapi.cpp:2166
tesseract::TessWordStrBoxRenderer::TessWordStrBoxRenderer
TessWordStrBoxRenderer(const char *outputbase)
Definition: wordstrboxrenderer.cpp:105
tesseract::ImageThresholder::GetPixRectThresholds
virtual Pix * GetPixRectThresholds()
Definition: thresholder.cpp:209
tesseract::Tesseract::lstm_rating_coefficient
double lstm_rating_coefficient
Definition: tesseractclass.h:1095
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::TessBaseAPI::paragraph_models_
GenericVector< ParagraphModel * > * paragraph_models_
Definition: baseapi.h:891
tesseract::TessBaseAPI::Clear
void Clear()
Definition: baseapi.cpp:1859
tesseract::TessBaseAPI::NumDawgs
int NumDawgs() const
Definition: baseapi.cpp:2302
WERD::bounding_box
TBOX bounding_box() const
Definition: werd.cpp:147
TBLOB::outlines
TESSLINE * outlines
Definition: blobs.h:398
BLOCK::row_list
ROW_LIST * row_list()
get rows
Definition: ocrblock.h:115
tesseract::TessBaseAPI::AnalyseLayout
PageIterator * AnalyseLayout()
Definition: baseapi.cpp:807
baseline
Definition: mfoutline.h:62
INT_FX_RESULT_STRUCT
Definition: intfx.h:34
tesseract::Classify::EnableLearning
bool EnableLearning
Definition: classify.h:577
tesseract::RIL_BLOCK
Definition: publictypes.h:217
tesseract::Tesseract::SetEquationDetect
void SetEquationDetect(EquationDetect *detector)
Definition: tesseractclass.cpp:591
ETEXT_DESC::set_deadline_msecs
void set_deadline_msecs(int32_t deadline_msecs)
Definition: ocrclass.h:121
tesseract::ChoiceIterator
Definition: ltrresultiterator.h:186
FCOORD::y
float y() const
Definition: points.h:209
WERD_RES::unlv_crunch_mode
CRUNCH_MODE unlv_crunch_mode
Definition: pageres.h:309
kMaxCredibleResolution
constexpr int kMaxCredibleResolution
Definition: publictypes.h:39
tesseract::Tesseract::min_orientation_margin
double min_orientation_margin
Definition: tesseractclass.h:1057
tesseract::TessBaseAPI::DetectOS
bool DetectOS(OSResults *)
Definition: baseapi.cpp:2197
CR_NONE
Definition: pageres.h:153
tesseract::kBytesPerBoxFileLine
const int kBytesPerBoxFileLine
Definition: baseapi.cpp:1499
tesseract::kUNLVReject
const char kUNLVReject
Definition: baseapi.cpp:108
tesseract::TessBaseAPI::GetComponentImages
Boxa * GetComponentImages(PageIteratorLevel level, bool text_only, bool raw_image, int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:697
tesseract::Classify::matcher_good_threshold
double matcher_good_threshold
Definition: classify.h:456
tesseract::TessBaseAPI::GetTextlines
Boxa * GetTextlines(bool raw_image, int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:652
WERD_CHOICE::certainty
float certainty() const
Definition: ratngs.h:318
OSResults
Definition: osdetect.h:50
tesseract::Tesseract::set_unlv_suspects
void set_unlv_suspects(WERD_RES *word)
Definition: output.cpp:272
tesseract::ImageThresholder::SetSourceYResolution
void SetSourceYResolution(int ppi)
Definition: thresholder.h:85
tesseract::TessBaseAPI::block_list_
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:892
STRING
Definition: strngs.h:45
UNICHARSET::get_script_from_script_id
const char * get_script_from_script_id(int id) const
Definition: unicharset.h:844
tesseract::Classify::ExtractFeatures
static void ExtractFeatures(const TBLOB &blob, bool nonlinear_norm, GenericVector< INT_FEATURE_STRUCT > *bl_features, GenericVector< INT_FEATURE_STRUCT > *cn_features, INT_FX_RESULT_STRUCT *results, GenericVector< int > *outline_cn_counts)
Definition: intfx.cpp:440
tesseract::ParamsVectors::int_params
GenericVector< IntParam * > int_params
Definition: params.h:57
tesseract::TessBaseAPI::GetIntVariable
bool GetIntVariable(const char *name, int *value) const
Definition: baseapi.cpp:289
tesseract::TessBaseAPI::rect_top_
int rect_top_
Definition: baseapi.h:908
tesseract::Wordrec::prev_word_best_choice_
WERD_CHOICE * prev_word_best_choice_
Definition: wordrec.h:476
WERD_RES
Definition: pageres.h:160
tesseract::ParamUtils::PrintParams
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:168
tesseract::Tesseract::tessedit_train_line_recognizer
bool tessedit_train_line_recognizer
Definition: tesseractclass.h:791
tesseract::Tesseract::read_config_file
void read_config_file(const char *filename, SetParamConstraint constraint)
Definition: tessedit.cpp:64
tesseract::PSM_SINGLE_BLOCK
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:168
tesseract::Classify::LearnWord
void LearnWord(const char *fontname, WERD_RES *word)
Definition: adaptmatch.cpp:250
IntCastRounded
int IntCastRounded(double x)
Definition: helpers.h:173
tesseract::DawgCache::DeleteUnusedDawgs
void DeleteUnusedDawgs()
Definition: dawg_cache.h:43
PT_CAPTION_TEXT
Definition: capi.h:116
tesseract::Tesseract::tessedit_pageseg_mode
int tessedit_pageseg_mode
Definition: tesseractclass.h:799
OSResults::unicharset
UNICHARSET * unicharset
Definition: osdetect.h:80
tesseract::Tesseract::SegmentPage
int SegmentPage(const STRING *input_file, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr)
Definition: pagesegmain.cpp:113
OSResults::get_best_script
TESS_API int get_best_script(int orientation_id) const
Definition: osdetect.cpp:112
tesseract::Dict::GlobalDawgCache
static TESS_API DawgCache * GlobalDawgCache()
Definition: dict.cpp:184
tesseract::TessBaseAPI::rect_height_
int rect_height_
Definition: baseapi.h:910
ETEXT_DESC
Definition: ocrclass.h:95
FCOORD
Definition: points.h:187
BLOB_CHOICE::unichar_id
UNICHAR_ID unichar_id() const
Definition: ratngs.h:75
tesseract::TessBaseAPI::AllWordConfidences
int * AllWordConfidences()
Definition: baseapi.cpp:1761
tesseract::TessBaseAPI::Init
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:337
tesseract::Tesseract::get_sub_lang
Tesseract * get_sub_lang(int index) const
Definition: tesseractclass.h:279
tesseract::ImageThresholder::GetSourceYResolution
int GetSourceYResolution() const
Definition: thresholder.h:89
C_BLOB
Definition: stepblob.h:36
tesseract::CCUtil::unicharset
UNICHARSET unicharset
Definition: ccutil.h:57
tesseract::SET_PARAM_CONSTRAINT_DEBUG_ONLY
Definition: params.h:51
tesseract::TessBaseAPI::End
void End()
Definition: baseapi.cpp:1872
tesseract::ParamsVectors::double_params
GenericVector< DoubleParam * > double_params
Definition: params.h:60
tesseract::Tesseract::pgeditor_main
void pgeditor_main(int width, int height, PAGE_RES *page_res)
Definition: pgedit.cpp:378
DIR_LEFT_TO_RIGHT
Definition: unichar.h:45
tesseract::ExtractFontName
void ExtractFontName(const STRING &filename, STRING *fontname)
Definition: blobclass.cpp:45
tesseract::RIL_SYMBOL
Definition: publictypes.h:221
tesseract::TessBaseAPI::GetThresholdedImage
Pix * GetThresholdedImage()
Definition: baseapi.cpp:626
tesseract::Tesseract::init_tesseract_lm
int init_tesseract_lm(const char *arg0, const char *textbase, const char *language, TessdataManager *mgr)
Definition: tessedit.cpp:469
tesseract::kLatinChs
const int kLatinChs[]
Definition: baseapi.cpp:1562
tesseract::Tesseract::tessedit_resegment_from_line_boxes
bool tessedit_resegment_from_line_boxes
Definition: tesseractclass.h:785
tesseract::Tesseract::tessedit_make_boxes_from_boxes
bool tessedit_make_boxes_from_boxes
Definition: tesseractclass.h:789
tesseract::Tesseract::ApplyBoxTraining
void ApplyBoxTraining(const STRING &fontname, PAGE_RES *page_res)
OSBestResult::orientation_id
int orientation_id
Definition: osdetect.h:44
tesseract::ImageThresholder::Clear
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
Definition: thresholder.cpp:48
tesseract::TessHOcrRenderer::TessHOcrRenderer
TessHOcrRenderer(const char *outputbase, bool font_info)
Definition: hocrrenderer.cpp:460
tesseract::Tesseract::set_pix_grey
void set_pix_grey(Pix *grey_pix)
Definition: tesseractclass.h:206
extract_edges
void extract_edges(Pix *pix, BLOCK *block)
Definition: edgblob.cpp:329
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
BLOCK
Definition: ocrblock.h:28
tesseract::TessBaseAPI::truth_cb_
TruthCallback truth_cb_
fxn for setting truth_* in WERD_RES
Definition: baseapi.h:900
BLOCK::pdblk
PDBLK pdblk
Page Description Block.
Definition: ocrblock.h:189
tesseract::TessBaseAPI::rect_width_
int rect_width_
Definition: baseapi.h:909
tesseract::Tesseract::TidyUp
void TidyUp(PAGE_RES *page_res)
tesseract::ParamsVectors::string_params
GenericVector< StringParam * > string_params
Definition: params.h:59
ETEXT_DESC::cancel_this
void * cancel_this
monitor-aware progress callback
Definition: ocrclass.h:109
REJMAP::length
int32_t length() const
Definition: rejctmap.h:222
W_FUZZY_NON
fuzzy nonspace
Definition: werd.h:54
ROW::x_height
float x_height() const
Definition: ocrrow.h:63
tesseract::Tesseract::interactive_display_mode
bool interactive_display_mode
Definition: tesseractclass.h:1047
tesseract::TessBaseAPI::SetVariable
bool SetVariable(const char *name, const char *value)
Definition: baseapi.cpp:277
WERD_RES::best_choice
WERD_CHOICE * best_choice
Definition: pageres.h:235
WERD::space
uint8_t space()
Definition: werd.h:98
tesseract::Tesseract::tessedit_resegment_from_boxes
bool tessedit_resegment_from_boxes
Definition: tesseractclass.h:783
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
tesseract::Tesseract::ApplyBoxes
PAGE_RES * ApplyBoxes(const STRING &fname, bool find_segmentation, BLOCK_LIST *block_list)
Definition: applybox.cpp:108
file
Definition: include_gunit.h:22
OSBestResult::oconfidence
float oconfidence
Definition: osdetect.h:47
tesseract::TessBaseAPI::equ_detect_
EquationDetect * equ_detect_
The equation detector.
Definition: baseapi.h:888
tesstrain_utils.int
int
Definition: tesstrain_utils.py:154
PT_HEADING_TEXT
Definition: capi.h:110
tesseract::TessBaseAPI::osd_tesseract_
Tesseract * osd_tesseract_
For orientation & script detection.
Definition: baseapi.h:887
tesseract::TessLSTMBoxRenderer::TessLSTMBoxRenderer
TessLSTMBoxRenderer(const char *outputbase)
Definition: lstmboxrenderer.cpp:108
UNICHARSET::unichar_to_id
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:209
tesseract::ResultIterator::StartOfParagraph
static ResultIterator * StartOfParagraph(const LTRResultIterator &resit)
Definition: resultiterator.cpp:50
W_EOL
end of line
Definition: werd.h:47
tesseract::OEM_DEFAULT
Definition: publictypes.h:271
tesseract::TessBaseAPI::tesseract_
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:886
tesseract::TessBaseAPI::GetIterator
ResultIterator * GetIterator()
Definition: baseapi.cpp:1321
tesseract::Classify::AdaptiveClassifier
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
Definition: adaptmatch.cpp:191
tesseract::Wordrec::wordrec_run_blamer
bool wordrec_run_blamer
Definition: wordrec.h:232
tesseract::TessBaseAPI::input_file_
STRING * input_file_
Name used by training code.
Definition: baseapi.h:894
tesseract::Wordrec::fill_lattice_
void(Wordrec::* fill_lattice_)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
Definition: wordrec.h:480
kMinCredibleResolution
constexpr int kMinCredibleResolution
Definition: publictypes.h:37
tesseract::Tesseract::PrepareForPageseg
void PrepareForPageseg()
Definition: tesseractclass.cpp:641
MAX_NUM_INT_FEATURES
#define MAX_NUM_INT_FEATURES
Definition: intproto.h:128
GenericVector::empty
bool empty() const
Definition: genericvector.h:86
tesseract::Tesseract::getDict
Dict & getDict() override
Definition: tesseractclass.cpp:564
tesseract::TessBaseAPI::SetPageSegMode
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:506
tesseract::Tesseract::tessedit_ambigs_training
bool tessedit_ambigs_training
Definition: tesseractclass.h:809
tesseract::TessBaseAPI::recognition_done_
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:899
tesseract::ParamUtils::GetParamAsString
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:129
TBLOB::PolygonalCopy
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
Definition: blobs.cpp:326
GlobalParams
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
tesseract::Classify::AdaptedTemplates
ADAPT_TEMPLATES AdaptedTemplates
Definition: classify.h:515
ROW::bounding_box
TBOX bounding_box() const
Definition: ocrrow.h:87
tesseract::CCUtil::lang
STRING lang
Definition: ccutil.h:55
TBLOB::Normalize
void Normalize(const BLOCK *block, const FCOORD *rotation, const DENORM *predecessor, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift, bool inverse, Pix *pix)
Definition: blobs.cpp:396
tesseract::Tesseract::SetBlackAndWhitelist
void SetBlackAndWhitelist()
Definition: tesseractclass.cpp:614
WERD_RES::reject_map
REJMAP reject_map
Definition: pageres.h:288
tesseract::Classify::InitAdaptiveClassifier
void InitAdaptiveClassifier(TessdataManager *mgr)
Definition: adaptmatch.cpp:527
tesseract::kTesseractReject
const char kTesseractReject
Definition: baseapi.cpp:106
WERD::set_text
void set_text(const char *new_text)
Definition: werd.h:114
tesseract::Tesseract::AnyLSTMLang
bool AnyLSTMLang() const
Definition: tesseractclass.h:293
tesseract::Tesseract::mutable_pix_binary
Pix ** mutable_pix_binary()
Definition: tesseractclass.h:196
tesseract::kMaxBytesPerLine
const int kMaxBytesPerLine
Definition: baseapi.cpp:1508
tesseract::TessBaseAPI::SetInputName
void SetInputName(const char *name)
Definition: baseapi.cpp:262
tesseract::CCUtil::params
ParamsVectors * params()
Definition: ccutil.h:51
PAGE_RES
Definition: pageres.h:73
tesseract::Tesseract::MaximallyChopWord
void MaximallyChopWord(const GenericVector< TBOX > &boxes, BLOCK *block, ROW *row, WERD_RES *word_res)
Definition: applybox.cpp:242
tesseract::Tesseract::tessedit_page_number
int tessedit_page_number
Definition: tesseractclass.h:1045
BLOB_CHOICE::rating
float rating() const
Definition: ratngs.h:78
tesseract::TessBaseAPI::SetInputImage
void SetInputImage(Pix *pix)
Definition: baseapi.cpp:953
tesseract::RIL_TEXTLINE
Definition: publictypes.h:219
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:36
tesseract::Tesseract::tessedit_write_images
bool tessedit_write_images
Definition: tesseractclass.h:1046
TBLOB::bounding_box
TBOX bounding_box() const
Definition: blobs.cpp:466
tesseract::kMinRectSize
const int kMinRectSize
Definition: baseapi.cpp:104
tesseract::ImageThresholder::GetScaledEstimatedResolution
int GetScaledEstimatedResolution() const
Definition: thresholder.h:105
GenericVector< TBOX >
DIR_NEUTRAL
Definition: unichar.h:44
tesseract::ImageThresholder::GetPixRectGrey
virtual Pix * GetPixRectGrey()
Definition: thresholder.cpp:253
tesseract::TessBaseAPI::GetLTRIterator
TESS_LOCAL LTRResultIterator * GetLTRIterator()
Definition: baseapi.cpp:1304
tesseract::Tesseract::num_sub_langs
int num_sub_langs() const
Definition: tesseractclass.h:276
PAGE_RES_IT
Definition: pageres.h:668
tesseract::TessBaseAPI::thresholder_
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:890
tesseract::ImageThresholder::IsEmpty
bool IsEmpty() const
Return true if no image has been set.
Definition: thresholder.cpp:53
tesseract::Classify::WriteTRFile
bool WriteTRFile(const STRING &filename)
Definition: blobclass.cpp:98
tesseract::TessBaseAPI::rect_left_
int rect_left_
Definition: baseapi.h:907
tesseract::TessBaseAPI::GetBoolVariable
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:297
tesseract::Tesseract::Clear
void Clear()
Definition: tesseractclass.cpp:574
orientation_and_script_detection
int orientation_and_script_detection(STRING &filename, OSResults *, tesseract::Tesseract *)
Definition: osdetect.cpp:190
tesseract::PSM_AUTO_ONLY
Automatic page segmentation, but no OSD, or OCR.
Definition: publictypes.h:163
tesseract::TessBaseAPI::GetInputImage
Pix * GetInputImage()
Definition: baseapi.cpp:955
tesseract::TessBaseAPI::GetPageSegMode
PageSegMode GetPageSegMode() const
Definition: baseapi.cpp:513
tesseract::Tesseract::SetupApplyBoxes
PAGE_RES * SetupApplyBoxes(const GenericVector< TBOX > &boxes, BLOCK_LIST *block_list)
Definition: applybox.cpp:206
tesseract::Tesseract::TrainLineRecognizer
bool TrainLineRecognizer(const STRING &input_imagename, const STRING &output_basename, BLOCK_LIST *block_list)
Definition: linerec.cpp:43
tesseract::TessBaseAPI::reader_
FileReader reader_
Reads files from any filesystem.
Definition: baseapi.h:889
STRING::length
int32_t length() const
Definition: strngs.cpp:187
tesseract::Tesseract::set_pix_original
void set_pix_original(Pix *original_pix)
Definition: tesseractclass.h:214
WERD_CHOICE::length
int length() const
Definition: ratngs.h:291
W_FUZZY_SP
fuzzy space
Definition: werd.h:53
tesseract::ImageThresholder::SetImage
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: thresholder.cpp:65
BLOB_CHOICE
Definition: ratngs.h:49
TBLOB
Definition: blobs.h:282
tesseract::TessBaseAPI::ProcessPage
bool ProcessPage(Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1237
tesseract::TessBaseAPI::ProcessPagesInternal
bool ProcessPagesInternal(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1110
tesseract::PageSegMode
PageSegMode
Definition: publictypes.h:159
PTIsTextType
bool PTIsTextType(PolyBlockType type)
Definition: publictypes.h:81
WERD
Definition: werd.h:55
tesseract::Tesseract::pix_binary
Pix * pix_binary() const
Definition: tesseractclass.h:200
TBOX::left
int16_t left() const
Definition: rect.h:71
ROW
Definition: ocrrow.h:35
UNICHARSET::contains_unichar
bool contains_unichar(const char *const unichar_repr) const
Definition: unicharset.cpp:670
tesseract::ImageThresholder::ThresholdToPix
virtual bool ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
Returns false on error.
Definition: thresholder.cpp:185
tesseract::ImageThresholder::GetPixRect
Pix * GetPixRect()
Definition: thresholder.cpp:236
ETEXT_DESC::cancel
CANCEL_FUNC cancel
for errcode use
Definition: ocrclass.h:105
tesseract::Tesseract::ReSegmentByClassification
void ReSegmentByClassification(PAGE_RES *page_res)
GenericVector::clear
void clear()
Definition: genericvector.h:857
tesseract::ImageThresholder::GetScaleFactor
int GetScaleFactor() const
Definition: thresholder.h:78
TBOX::right
int16_t right() const
Definition: rect.h:78
tesseract::Tesseract::CorrectClassifyWords
void CorrectClassifyWords(PAGE_RES *page_res)
tesseract::Tesseract::set_source_resolution
void set_source_resolution(int ppi)
Definition: tesseractclass.h:247
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::PSM_OSD_ONLY
Orientation and script detection only.
Definition: publictypes.h:160
DIR_RIGHT_TO_LEFT
Definition: unichar.h:46
OSBestResult::sconfidence
float sconfidence
Definition: osdetect.h:46
tesseract::Dict::getUnicharset
const UNICHARSET & getUnicharset() const
Definition: dict.h:101
tesseract::Dict::GetDawg
const Dawg * GetDawg(int index) const
Return i-th dawg pointer recorded in the dawgs_ vector.
Definition: dict.h:432
tesseract::Tesseract::set_pix_thresholds
void set_pix_thresholds(Pix *thresholds)
Definition: tesseractclass.h:240
tesseract::Tesseract::recog_all_words
bool recog_all_words(PAGE_RES *page_res, ETEXT_DESC *monitor, const TBOX *target_word_box, const char *word_config, int dopasses)
Definition: control.cpp:302
DIR_MIX
Definition: unichar.h:47
tesseract::Tesseract::textord_equation_detect
bool textord_equation_detect
Definition: tesseractclass.h:1065
tesseract::DetectParagraphs
void DetectParagraphs(int debug_level, GenericVector< RowInfo > *row_infos, GenericVector< PARA * > *row_owners, PARA_LIST *paragraphs, GenericVector< ParagraphModel * > *models)
Definition: paragraphs.cpp:2284
tesseract::ParamUtils::SetParam
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:79
tesseract::Dict::valid_word
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
Definition: dict.cpp:778
tesseract::kUNLVSuspect
const char kUNLVSuspect
Definition: baseapi.cpp:110
tesseract::TessBaseAPI::FindLines
TESS_LOCAL int FindLines()
Definition: baseapi.cpp:2065
WERD_RES::word
WERD * word
Definition: pageres.h:180
tesseract::Tesseract::init_recog_training
FILE * init_recog_training(const STRING &fname)
Definition: recogtraining.cpp:36
PT_PULLOUT_TEXT
Definition: capi.h:111
tesseract::TessBaseAPI::ClearResults
void ClearResults()
Definition: baseapi.cpp:2141
UNICHARSET::id_to_unichar
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:290
tesseract::ImageThresholder::SetRectangle
void SetRectangle(int left, int top, int width, int height)
Definition: thresholder.cpp:123
TessBaseAPI
struct TessBaseAPI TessBaseAPI
Definition: capi.h:72
tesseract::TessBaseAPI::datapath_
STRING * datapath_
Current location of tessdata.
Definition: baseapi.h:896
tesseract::TessBaseAPI::GetAltoText
char * GetAltoText(ETEXT_DESC *monitor, int page_number)
Definition: altorenderer.cpp:126
tesseract::Tesseract::ResetAdaptiveClassifier
void ResetAdaptiveClassifier()
Definition: tesseractclass.cpp:597
tesseract::TessBaseAPI::GetUTF8Text
char * GetUTF8Text()
Definition: baseapi.cpp:1348
tesseract::Classify::classify_bln_numeric_mode
bool classify_bln_numeric_mode
Definition: classify.h:508
tesseract::TessBaseAPI::tesseract
Tesseract * tesseract() const
Definition: baseapi.h:798
GenericVector::sort
void sort()
Definition: genericvector.h:1102
CR_KEEP_SPACE
Definition: pageres.h:154
GenericVector::size
int size() const
Definition: genericvector.h:71
tesseract::OEM_TESSERACT_ONLY
Definition: publictypes.h:266
ROW::word_list
WERD_LIST * word_list()
Definition: ocrrow.h:54
tesseract::Classify::AdaptToChar
void AdaptToChar(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, float Threshold, ADAPT_TEMPLATES adaptive_templates)
Definition: adaptmatch.cpp:853
tesseract::TessBaseAPI::InternalSetImage
TESS_LOCAL bool InternalSetImage()
Definition: baseapi.cpp:1994
TessResultRenderer
struct TessResultRenderer TessResultRenderer
Definition: capi.h:71
tesseract::TessBaseAPI::oem
OcrEngineMode oem() const
Definition: baseapi.h:802
WERD_CHOICE::unichar_lengths
const STRING & unichar_lengths() const
Definition: ratngs.h:536
tesseract::RIL_PARA
Definition: publictypes.h:218
tesseract::ImageThresholder::GetScaledYResolution
int GetScaledYResolution() const
Definition: thresholder.h:92
tesseract::TessBaseAPI::ReadConfigFile
void ReadConfigFile(const char *filename)
Definition: baseapi.cpp:492
tesseract::TessBaseAPI::image_width_
int image_width_
Definition: baseapi.h:911
tesseract::TessBaseAPI::SetImage
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:571
tesseract::TessBaseAPI::Recognize
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:827
kBlnBaselineOffset
const int kBlnBaselineOffset
Definition: normalis.h:24
tesseract::TessBaseAPI::output_file_
STRING * output_file_
Name used by debug code.
Definition: baseapi.h:895
tesseract::TessBaseAPI::DetectParagraphs
TESS_LOCAL void DetectParagraphs(bool after_text_recognition)
Definition: baseapi.cpp:2275
tesseract::TessBaseAPI::language_
STRING * language_
Last initialized language.
Definition: baseapi.h:897
tesseract::TessBaseAPI::PrintVariables
void PrintVariables(FILE *fp) const
Definition: baseapi.cpp:325
tesseract::HOcrEscape
STRING HOcrEscape(const char *text)
Definition: baseapi.cpp:2307
tesseract::Tesseract::pix_original
Pix * pix_original() const
Definition: tesseractclass.h:210
W_BOL
start of line
Definition: werd.h:46
tesseract::ParamUtils::ResetToDefaults
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:199
tesseract::ParamsVectors::bool_params
GenericVector< BoolParam * > bool_params
Definition: params.h:58
TBOX
Definition: rect.h:33
tesseract::TessBaseAPI::SetRectangle
void SetRectangle(int left, int top, int width, int height)
Definition: baseapi.cpp:615
tesseract::Dict::letter_is_okay_
int(Dict::* letter_is_okay_)(void *void_dawg_args, const UNICHARSET &unicharset, UNICHAR_ID unichar_id, bool word_end) const
Definition: dict.h:372