tesseract  4.00.00dev
Advanced API

Functions

void tesseract::TessBaseAPI::SetImage (const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
 
void tesseract::TessBaseAPI::SetImage (Pix *pix)
 
void tesseract::TessBaseAPI::SetSourceResolution (int ppi)
 
void tesseract::TessBaseAPI::SetRectangle (int left, int top, int width, int height)
 
void tesseract::TessBaseAPI::SetThresholder (ImageThresholder *thresholder)
 
Pix * tesseract::TessBaseAPI::GetThresholdedImage ()
 
Boxa * tesseract::TessBaseAPI::GetRegions (Pixa **pixa)
 
Boxa * tesseract::TessBaseAPI::GetTextlines (const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
 
Boxa * tesseract::TessBaseAPI::GetTextlines (Pixa **pixa, int **blockids)
 
Boxa * tesseract::TessBaseAPI::GetStrips (Pixa **pixa, int **blockids)
 
Boxa * tesseract::TessBaseAPI::GetWords (Pixa **pixa)
 
Boxa * tesseract::TessBaseAPI::GetConnectedComponents (Pixa **cc)
 
Boxa * tesseract::TessBaseAPI::GetComponentImages (const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
 
Boxa * tesseract::TessBaseAPI::GetComponentImages (const PageIteratorLevel level, const bool text_only, Pixa **pixa, int **blockids)
 
int tesseract::TessBaseAPI::GetThresholdedImageScaleFactor () const
 
void tesseract::TessBaseAPI::DumpPGM (const char *filename)
 
PageIteratortesseract::TessBaseAPI::AnalyseLayout ()
 
PageIteratortesseract::TessBaseAPI::AnalyseLayout (bool merge_similar_words)
 
int tesseract::TessBaseAPI::Recognize (ETEXT_DESC *monitor)
 
int tesseract::TessBaseAPI::RecognizeForChopTest (ETEXT_DESC *monitor)
 
bool tesseract::TessBaseAPI::ProcessPages (const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
 
bool tesseract::TessBaseAPI::ProcessPagesInternal (const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
 
bool tesseract::TessBaseAPI::ProcessPage (Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
 
ResultIteratortesseract::TessBaseAPI::GetIterator ()
 
MutableIteratortesseract::TessBaseAPI::GetMutableIterator ()
 
char * tesseract::TessBaseAPI::GetUTF8Text ()
 
char * tesseract::TessBaseAPI::GetHOCRText (ETEXT_DESC *monitor, int page_number)
 
char * tesseract::TessBaseAPI::GetHOCRText (int page_number)
 
char * tesseract::TessBaseAPI::GetTSVText (int page_number)
 
char * tesseract::TessBaseAPI::GetBoxText (int page_number)
 
char * tesseract::TessBaseAPI::GetUNLVText ()
 
bool tesseract::TessBaseAPI::DetectOrientationScript (int *orient_deg, float *orient_conf, const char **script_name, float *script_conf)
 
char * tesseract::TessBaseAPI::GetOsdText (int page_number)
 
int tesseract::TessBaseAPI::MeanTextConf ()
 
int * tesseract::TessBaseAPI::AllWordConfidences ()
 
bool tesseract::TessBaseAPI::AdaptToWordStr (PageSegMode mode, const char *wordstr)
 
void tesseract::TessBaseAPI::Clear ()
 
void tesseract::TessBaseAPI::End ()
 
static void tesseract::TessBaseAPI::ClearPersistentCache ()
 
int tesseract::TessBaseAPI::IsValidWord (const char *word)
 
bool tesseract::TessBaseAPI::IsValidCharacter (const char *utf8_character)
 
bool tesseract::TessBaseAPI::GetTextDirection (int *out_offset, float *out_slope)
 
void tesseract::TessBaseAPI::SetDictFunc (DictFunc f)
 
void tesseract::TessBaseAPI::SetProbabilityInContextFunc (ProbabilityInContextFunc f)
 
void tesseract::TessBaseAPI::SetFillLatticeFunc (FillLatticeFunc f)
 
bool tesseract::TessBaseAPI::DetectOS (OSResults *)
 
void tesseract::TessBaseAPI::GetFeaturesForBlob (TBLOB *blob, INT_FEATURE_STRUCT *int_features, int *num_features, int *feature_outline_index)
 
static ROWtesseract::TessBaseAPI::FindRowForBox (BLOCK_LIST *blocks, int left, int top, int right, int bottom)
 
void tesseract::TessBaseAPI::RunAdaptiveClassifier (TBLOB *blob, int num_max_matches, int *unichar_ids, float *ratings, int *num_matches_returned)
 
const char * tesseract::TessBaseAPI::GetUnichar (int unichar_id)
 
const Dawgtesseract::TessBaseAPI::GetDawg (int i) const
 
int tesseract::TessBaseAPI::NumDawgs () const
 
static ROWtesseract::TessBaseAPI::MakeTessOCRRow (float baseline, float xheight, float descender, float ascender)
 
static TBLOBtesseract::TessBaseAPI::MakeTBLOB (Pix *pix)
 
static void tesseract::TessBaseAPI::NormalizeTBLOB (TBLOB *tblob, ROW *row, bool numeric_mode)
 
Tesseracttesseract::TessBaseAPI::tesseract () const
 
OcrEngineMode tesseract::TessBaseAPI::oem () const
 
void tesseract::TessBaseAPI::InitTruthCallback (TruthCallback *cb)
 
void tesseract::TessBaseAPI::set_min_orientation_margin (double margin)
 
void tesseract::TessBaseAPI::GetBlockTextOrientations (int **block_orientation, bool **vertical_writing)
 
BLOCK_LIST * tesseract::TessBaseAPI::FindLinesCreateBlockList ()
 
static void tesseract::TessBaseAPI::DeleteBlockList (BLOCK_LIST *block_list)
 

Detailed Description

The following methods break TesseractRect into pieces, so you can get hold of the thresholded image, get the text in different formats, get bounding boxes, confidences etc.

Function Documentation

◆ 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 1997 of file baseapi.cpp.

1997  {
1998  int debug = 0;
1999  GetIntVariable("applybox_debug", &debug);
2000  bool success = true;
2001  PageSegMode current_psm = GetPageSegMode();
2003  SetVariable("classify_enable_learning", "0");
2004  const std::unique_ptr<const char[]> text(GetUTF8Text());
2005  if (debug) {
2006  tprintf("Trying to adapt \"%s\" to \"%s\"\n", text.get(), wordstr);
2007  }
2008  if (text != NULL) {
2009  PAGE_RES_IT it(page_res_);
2010  WERD_RES* word_res = it.word();
2011  if (word_res != NULL) {
2012  word_res->word->set_text(wordstr);
2013  } else {
2014  success = false;
2015  }
2016  // Check to see if text matches wordstr.
2017  int w = 0;
2018  int t = 0;
2019  for (t = 0; text[t] != '\0'; ++t) {
2020  if (text[t] == '\n' || text[t] == ' ')
2021  continue;
2022  while (wordstr[w] == ' ') ++w;
2023  if (text[t] != wordstr[w])
2024  break;
2025  ++w;
2026  }
2027  if (text[t] != '\0' || wordstr[w] != '\0') {
2028  // No match.
2029  delete page_res_;
2030  GenericVector<TBOX> boxes;
2034  PAGE_RES_IT pr_it(page_res_);
2035  if (pr_it.word() == NULL)
2036  success = false;
2037  else
2038  word_res = pr_it.word();
2039  } else {
2040  word_res->BestChoiceToCorrectText();
2041  }
2042  if (success) {
2043  tesseract_->EnableLearning = true;
2044  tesseract_->LearnWord(NULL, word_res);
2045  }
2046  } else {
2047  success = false;
2048  }
2049  SetPageSegMode(current_psm);
2050  return success;
2051 }
void LearnWord(const char *fontname, WERD_RES *word)
Definition: adaptmatch.cpp:244
PAGE_RES * SetupApplyBoxes(const GenericVector< TBOX > &boxes, BLOCK_LIST *block_list)
Definition: applybox.cpp:217
bool GetIntVariable(const char *name, int *value) const
Definition: baseapi.cpp:284
void ReSegmentByClassification(PAGE_RES *page_res)
Definition: applybox.cpp:509
void TidyUp(PAGE_RES *page_res)
Definition: applybox.cpp:706
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:498
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
#define tprintf(...)
Definition: tprintf.h:31
PageSegMode GetPageSegMode() const
Definition: baseapi.cpp:505
WERD * word
Definition: pageres.h:175
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:871
void BestChoiceToCorrectText()
Definition: pageres.cpp:918
CMD_EVENTS mode
Definition: pgedit.cpp:116
void set_text(const char *new_text)
Definition: werd.h:126
bool SetVariable(const char *name, const char *value)
Definition: baseapi.cpp:272

◆ 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 1963 of file baseapi.cpp.

1963  {
1964  if (tesseract_ == NULL ||
1965  (!recognition_done_ && Recognize(NULL) < 0))
1966  return NULL;
1967  int n_word = 0;
1968  PAGE_RES_IT res_it(page_res_);
1969  for (res_it.restart_page(); res_it.word() != NULL; res_it.forward())
1970  n_word++;
1971 
1972  int* conf = new int[n_word+1];
1973  n_word = 0;
1974  for (res_it.restart_page(); res_it.word() != NULL; res_it.forward()) {
1975  WERD_RES *word = res_it.word();
1976  WERD_CHOICE* choice = word->best_choice;
1977  int w_conf = static_cast<int>(100 + 5 * choice->certainty());
1978  // This is the eq for converting Tesseract confidence to 1..100
1979  if (w_conf < 0) w_conf = 0;
1980  if (w_conf > 100) w_conf = 100;
1981  conf[n_word++] = w_conf;
1982  }
1983  conf[n_word] = -1;
1984  return conf;
1985 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:823
float certainty() const
Definition: ratngs.h:326
WERD_CHOICE * best_choice
Definition: pageres.h:219
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:878
WERD * word
Definition: pageres.h:175

◆ 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 NULL 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 803 of file baseapi.cpp.

803 { return AnalyseLayout(false); }
PageIterator * AnalyseLayout()
Definition: baseapi.cpp:803

◆ AnalyseLayout() [2/2]

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

Definition at line 805 of file baseapi.cpp.

805  {
806  if (FindLines() == 0) {
807  if (block_list_->empty())
808  return NULL; // The page was empty.
809  page_res_ = new PAGE_RES(merge_similar_words, block_list_, NULL);
810  DetectParagraphs(false);
811  return new PageIterator(
815  }
816  return NULL;
817 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
TESS_LOCAL void DetectParagraphs(bool after_text_recognition)
Definition: baseapi.cpp:2607
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:871
int GetScaledYResolution() const
Definition: thresholder.h:93
TESS_LOCAL int FindLines()
Definition: baseapi.cpp:2253

◆ 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 2059 of file baseapi.cpp.

2059  {
2060  if (thresholder_ != NULL)
2061  thresholder_->Clear();
2062  ClearResults();
2063  if (tesseract_ != NULL) SetInputImage(NULL);
2064 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
Definition: thresholder.cpp:45
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
void SetInputImage(Pix *pix)
Definition: baseapi.cpp:936

◆ 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 2107 of file baseapi.cpp.

2107  {
2109 }
void DeleteUnusedDawgs()
Definition: dawg_cache.h:43
static DawgCache * GlobalDawgCache()
Definition: dict.cpp:198

◆ 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 2473 of file baseapi.cpp.

2473  {
2474  delete block_list;
2475 }

◆ 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 1891 of file baseapi.cpp.

1893  {
1894  OSResults osr;
1895 
1896  bool osd = DetectOS(&osr);
1897  if (!osd) {
1898  return false;
1899  }
1900 
1901  int orient_id = osr.best_result.orientation_id;
1902  int script_id = osr.get_best_script(orient_id);
1903  if (orient_conf) *orient_conf = osr.best_result.oconfidence;
1904  if (orient_deg) *orient_deg = orient_id * 90; // convert quadrant to degrees
1905 
1906  if (script_name) {
1907  const char* script = osr.unicharset->get_script_from_script_id(script_id);
1908 
1909  *script_name = script;
1910  }
1911 
1912  if (script_conf) *script_conf = osr.best_result.sconfidence;
1913 
1914  return true;
1915 }
UNICHARSET * unicharset
Definition: osdetect.h:78
const char * get_script_from_script_id(int id) const
Definition: unicharset.h:853
float sconfidence
Definition: osdetect.h:43
OSBestResult best_result
Definition: osdetect.h:79
int orientation_id
Definition: osdetect.h:41
float oconfidence
Definition: osdetect.h:44
bool DetectOS(OSResults *)
Definition: baseapi.cpp:2382
TESS_API int get_best_script(int orientation_id) const
Definition: osdetect.cpp:111

◆ 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 2382 of file baseapi.cpp.

2382  {
2383  if (tesseract_ == NULL)
2384  return false;
2385  ClearResults();
2386  if (tesseract_->pix_binary() == NULL &&
2388  return false;
2389  }
2390  if (input_file_ == NULL)
2391  input_file_ = new STRING(kInputFile);
2393 }
Pix * pix_binary() const
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
STRING * input_file_
Name used by training code.
Definition: baseapi.h:873
virtual TESS_LOCAL bool Threshold(Pix **pix)
Definition: baseapi.cpp:2209
Definition: strngs.h:45
int orientation_and_script_detection(STRING &filename, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:188
const char * kInputFile
Definition: baseapi.cpp:98

◆ DumpPGM()

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

Dump the internal binary image to a PGM file.

Deprecated:
Use GetThresholdedImage and write the image using pixWrite instead if possible.

Dump the internal binary image to a PGM file.

Definition at line 770 of file baseapi.cpp.

770  {
771  if (tesseract_ == NULL)
772  return;
773  FILE *fp = fopen(filename, "wb");
774  Pix* pix = tesseract_->pix_binary();
775  int width = pixGetWidth(pix);
776  int height = pixGetHeight(pix);
777  l_uint32* data = pixGetData(pix);
778  fprintf(fp, "P5 %d %d 255\n", width, height);
779  for (int y = 0; y < height; ++y, data += pixGetWpl(pix)) {
780  for (int x = 0; x < width; ++x) {
781  uint8_t b = GET_DATA_BIT(data, x) ? 0 : 255;
782  fwrite(&b, 1, 1, fp);
783  }
784  }
785  fclose(fp);
786 }
Pix * pix_binary() const
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865

◆ 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 2072 of file baseapi.cpp.

2072  {
2073  Clear();
2074  delete thresholder_;
2075  thresholder_ = NULL;
2076  delete page_res_;
2077  page_res_ = NULL;
2078  delete block_list_;
2079  block_list_ = NULL;
2080  if (paragraph_models_ != NULL) {
2082  delete paragraph_models_;
2083  paragraph_models_ = NULL;
2084  }
2085  if (osd_tesseract_ == tesseract_) osd_tesseract_ = nullptr;
2086  delete tesseract_;
2087  tesseract_ = nullptr;
2088  delete osd_tesseract_;
2089  osd_tesseract_ = NULL;
2090  delete equ_detect_;
2091  equ_detect_ = NULL;
2092  delete input_file_;
2093  input_file_ = NULL;
2094  delete output_file_;
2095  output_file_ = NULL;
2096  delete datapath_;
2097  datapath_ = NULL;
2098  delete language_;
2099  language_ = NULL;
2100 }
Tesseract * osd_tesseract_
For orientation & script detection.
Definition: baseapi.h:866
STRING * language_
Last initialized language.
Definition: baseapi.h:876
GenericVector< ParagraphModel * > * paragraph_models_
Definition: baseapi.h:870
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
EquationDetect * equ_detect_
The equation detector.
Definition: baseapi.h:867
STRING * output_file_
Name used by debug code.
Definition: baseapi.h:874
STRING * input_file_
Name used by training code.
Definition: baseapi.h:873
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
void delete_data_pointers()
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
STRING * datapath_
Current location of tessdata.
Definition: baseapi.h:875
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:871

◆ FindLinesCreateBlockList()

BLOCK_LIST * tesseract::TessBaseAPI::FindLinesCreateBlockList ( )

Find lines from the image making the BLOCK_LIST.

Definition at line 2461 of file baseapi.cpp.

2461  {
2462  FindLines();
2463  BLOCK_LIST* result = block_list_;
2464  block_list_ = NULL;
2465  return result;
2466 }
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:871
TESS_LOCAL int FindLines()
Definition: baseapi.cpp:2253

◆ 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 NULL.

Definition at line 2773 of file baseapi.cpp.

2774  {
2775  TBOX box(left, bottom, right, top);
2776  BLOCK_IT b_it(blocks);
2777  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
2778  BLOCK* block = b_it.data();
2779  if (!box.major_overlap(block->bounding_box()))
2780  continue;
2781  ROW_IT r_it(block->row_list());
2782  for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward()) {
2783  ROW* row = r_it.data();
2784  if (!box.major_overlap(row->bounding_box()))
2785  continue;
2786  WERD_IT w_it(row->word_list());
2787  for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
2788  WERD* word = w_it.data();
2789  if (box.major_overlap(word->bounding_box()))
2790  return row;
2791  }
2792  }
2793  }
2794  return NULL;
2795 }
ROW_LIST * row_list()
get rows
Definition: ocrblock.h:120
Definition: ocrrow.h:32
Definition: ocrblock.h:30
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:59
Definition: rect.h:30
Definition: werd.h:60
TBOX bounding_box() const
Definition: werd.cpp:160
WERD_LIST * word_list()
Definition: ocrrow.h:52
TBOX bounding_box() const
Definition: ocrrow.h:85

◆ 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 2413 of file baseapi.cpp.

2414  {
2415  delete[] *block_orientation;
2416  *block_orientation = NULL;
2417  delete[] *vertical_writing;
2418  *vertical_writing = NULL;
2419  BLOCK_IT block_it(block_list_);
2420 
2421  block_it.move_to_first();
2422  int num_blocks = 0;
2423  for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
2424  if (!block_it.data()->poly_block()->IsText()) {
2425  continue;
2426  }
2427  ++num_blocks;
2428  }
2429  if (!num_blocks) {
2430  tprintf("WARNING: Found no blocks\n");
2431  return;
2432  }
2433  *block_orientation = new int[num_blocks];
2434  *vertical_writing = new bool[num_blocks];
2435  block_it.move_to_first();
2436  int i = 0;
2437  for (block_it.mark_cycle_pt(); !block_it.cycled_list();
2438  block_it.forward()) {
2439  if (!block_it.data()->poly_block()->IsText()) {
2440  continue;
2441  }
2442  FCOORD re_rotation = block_it.data()->re_rotation();
2443  float re_theta = re_rotation.angle();
2444  FCOORD classify_rotation = block_it.data()->classify_rotation();
2445  float classify_theta = classify_rotation.angle();
2446  double rot_theta = - (re_theta - classify_theta) * 2.0 / PI;
2447  if (rot_theta < 0) rot_theta += 4;
2448  int num_rotations = static_cast<int>(rot_theta + 0.5);
2449  (*block_orientation)[i] = num_rotations;
2450  // The classify_rotation is non-zero only if the text has vertical
2451  // writing direction.
2452  (*vertical_writing)[i] = classify_rotation.y() != 0.0f;
2453  ++i;
2454  }
2455 }
Definition: points.h:189
float y() const
Definition: points.h:212
#define tprintf(...)
Definition: tprintf.h:31
float angle() const
find angle
Definition: points.h:249
#define PI
Definition: const.h:19
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:871

◆ 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 1727 of file baseapi.cpp.

1727  {
1728  if (tesseract_ == NULL ||
1729  (!recognition_done_ && Recognize(NULL) < 0))
1730  return NULL;
1731  int blob_count;
1732  int utf8_length = TextLength(&blob_count);
1733  int total_length = blob_count * kBytesPerBoxFileLine + utf8_length +
1735  char* result = new char[total_length];
1736  result[0] = '\0';
1737  int output_length = 0;
1738  LTRResultIterator* it = GetLTRIterator();
1739  do {
1740  int left, top, right, bottom;
1741  if (it->BoundingBox(RIL_SYMBOL, &left, &top, &right, &bottom)) {
1742  const std::unique_ptr</*non-const*/ char[]> text(
1743  it->GetUTF8Text(RIL_SYMBOL));
1744  // Tesseract uses space for recognition failure. Fix to a reject
1745  // character, kTesseractReject so we don't create illegal box files.
1746  for (int i = 0; text[i] != '\0'; ++i) {
1747  if (text[i] == ' ')
1748  text[i] = kTesseractReject;
1749  }
1750  snprintf(result + output_length, total_length - output_length,
1751  "%s %d %d %d %d %d\n", text.get(), left, image_height_ - bottom,
1752  right, image_height_ - top, page_number);
1753  output_length += strlen(result + output_length);
1754  // Just in case...
1755  if (output_length + kMaxBytesPerLine > total_length)
1756  break;
1757  }
1758  } while (it->Next(RIL_SYMBOL));
1759  delete it;
1760  return result;
1761 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
const char kTesseractReject
Definition: baseapi.cpp:89
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:823
TESS_LOCAL int TextLength(int *blob_count)
Definition: baseapi.cpp:2352
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:878
TESS_LOCAL LTRResultIterator * GetLTRIterator()
Definition: baseapi.cpp:1235
const int kMaxBytesPerLine
Definition: baseapi.cpp:1718
const int kBytesPerBoxFileLine
Definition: baseapi.cpp:1709

◆ GetComponentImages() [1/2]

Boxa * tesseract::TessBaseAPI::GetComponentImages ( const PageIteratorLevel  level,
const bool  text_only,
const bool  raw_image,
const 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 NULL, the block-id of each component is also returned as an array of one element per component. delete [] after use. If blockids is not NULL, 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 NULL, 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 680 of file baseapi.cpp.

684  {
685  PageIterator* page_it = GetIterator();
686  if (page_it == NULL)
687  page_it = AnalyseLayout();
688  if (page_it == NULL)
689  return NULL; // Failed.
690 
691  // Count the components to get a size for the arrays.
692  int component_count = 0;
693  int left, top, right, bottom;
694 
695  TessResultCallback<bool>* get_bbox = NULL;
696  if (raw_image) {
697  // Get bounding box in original raw image with padding.
699  level, raw_padding,
700  &left, &top, &right, &bottom);
701  } else {
702  // Get bounding box from binarized imaged. Note that this could be
703  // differently scaled from the original image.
704  get_bbox = NewPermanentTessCallback(page_it,
706  level, &left, &top, &right, &bottom);
707  }
708  do {
709  if (get_bbox->Run() &&
710  (!text_only || PTIsTextType(page_it->BlockType())))
711  ++component_count;
712  } while (page_it->Next(level));
713 
714  Boxa* boxa = boxaCreate(component_count);
715  if (pixa != NULL)
716  *pixa = pixaCreate(component_count);
717  if (blockids != NULL)
718  *blockids = new int[component_count];
719  if (paraids != NULL)
720  *paraids = new int[component_count];
721 
722  int blockid = 0;
723  int paraid = 0;
724  int component_index = 0;
725  page_it->Begin();
726  do {
727  if (get_bbox->Run() &&
728  (!text_only || PTIsTextType(page_it->BlockType()))) {
729  Box* lbox = boxCreate(left, top, right - left, bottom - top);
730  boxaAddBox(boxa, lbox, L_INSERT);
731  if (pixa != NULL) {
732  Pix* pix = NULL;
733  if (raw_image) {
734  pix = page_it->GetImage(level, raw_padding, GetInputImage(), &left,
735  &top);
736  } else {
737  pix = page_it->GetBinaryImage(level);
738  }
739  pixaAddPix(*pixa, pix, L_INSERT);
740  pixaAddBox(*pixa, lbox, L_CLONE);
741  }
742  if (paraids != NULL) {
743  (*paraids)[component_index] = paraid;
744  if (page_it->IsAtFinalElement(RIL_PARA, level))
745  ++paraid;
746  }
747  if (blockids != NULL) {
748  (*blockids)[component_index] = blockid;
749  if (page_it->IsAtFinalElement(RIL_BLOCK, level)) {
750  ++blockid;
751  paraid = 0;
752  }
753  }
754  ++component_index;
755  }
756  } while (page_it->Next(level));
757  delete page_it;
758  delete get_bbox;
759  return boxa;
760 }
bool PTIsTextType(PolyBlockType type)
Definition: publictypes.h:82
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
virtual R Run()=0
PageIterator * AnalyseLayout()
Definition: baseapi.cpp:803
ResultIterator * GetIterator()
Definition: baseapi.cpp:1252

◆ GetComponentImages() [2/2]

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

Definition at line 460 of file baseapi.h.

462  {
463  return GetComponentImages(level, text_only, false, 0, pixa, blockids, NULL);
464  }
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:680

◆ 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 668 of file baseapi.cpp.

668  {
669  return GetComponentImages(RIL_SYMBOL, true, pixa, NULL);
670 }
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:680

◆ GetDawg()

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

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

Definition at line 2826 of file baseapi.cpp.

2826  {
2827  if (tesseract_ == NULL || i >= NumDawgs()) return NULL;
2828  return tesseract_->getDict().GetDawg(i);
2829 }
Dict & getDict() override
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
const Dawg * GetDawg(int index) const
Return i-th dawg pointer recorded in the dawgs_ vector.
Definition: dict.h:414
int NumDawgs() const
Definition: baseapi.cpp:2832

◆ 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 2745 of file baseapi.cpp.

2748  {
2749  GenericVector<int> outline_counts;
2752  INT_FX_RESULT_STRUCT fx_info;
2753  tesseract_->ExtractFeatures(*blob, false, &bl_features,
2754  &cn_features, &fx_info, &outline_counts);
2755  if (cn_features.empty() || cn_features.size() > MAX_NUM_INT_FEATURES) {
2756  *num_features = 0;
2757  return; // Feature extraction failed.
2758  }
2759  *num_features = cn_features.size();
2760  memcpy(int_features, &cn_features[0], *num_features * sizeof(cn_features[0]));
2761  // TODO(rays) Pass outline_counts back and simplify the calling code.
2762  if (feature_outline_index != NULL) {
2763  int f = 0;
2764  for (int i = 0; i < outline_counts.size(); ++i) {
2765  while (f < outline_counts[i])
2766  feature_outline_index[f++] = i;
2767  }
2768  }
2769 }
bool empty() const
Definition: genericvector.h:91
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
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:445
int size() const
Definition: genericvector.h:72
#define MAX_NUM_INT_FEATURES
Definition: intproto.h:132

◆ 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 1427 of file baseapi.cpp.

1427  {
1428  if (tesseract_ == NULL || (page_res_ == NULL && Recognize(monitor) < 0))
1429  return NULL;
1430 
1431  int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
1432  int page_id = page_number + 1; // hOCR uses 1-based page numbers.
1433  bool para_is_ltr = true; // Default direction is LTR
1434  const char* paragraph_lang = NULL;
1435  bool font_info = false;
1436  GetBoolVariable("hocr_font_info", &font_info);
1437 
1438  STRING hocr_str("");
1439 
1440  if (input_file_ == NULL)
1441  SetInputName(NULL);
1442 
1443 #ifdef _WIN32
1444  // convert input name from ANSI encoding to utf-8
1445  int str16_len =
1446  MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1, NULL, 0);
1447  wchar_t *uni16_str = new WCHAR[str16_len];
1448  str16_len = MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1,
1449  uni16_str, str16_len);
1450  int utf8_len = WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, NULL, 0,
1451  NULL, NULL);
1452  char *utf8_str = new char[utf8_len];
1453  WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, utf8_str,
1454  utf8_len, NULL, NULL);
1455  *input_file_ = utf8_str;
1456  delete[] uni16_str;
1457  delete[] utf8_str;
1458 #endif
1459 
1460  hocr_str += " <div class='ocr_page'";
1461  AddIdTohOCR(&hocr_str, "page", page_id, -1);
1462  hocr_str += " title='image \"";
1463  if (input_file_) {
1464  hocr_str += HOcrEscape(input_file_->string());
1465  } else {
1466  hocr_str += "unknown";
1467  }
1468  hocr_str.add_str_int("\"; bbox ", rect_left_);
1469  hocr_str.add_str_int(" ", rect_top_);
1470  hocr_str.add_str_int(" ", rect_width_);
1471  hocr_str.add_str_int(" ", rect_height_);
1472  hocr_str.add_str_int("; ppageno ", page_number);
1473  hocr_str += "'>\n";
1474 
1475  ResultIterator *res_it = GetIterator();
1476  while (!res_it->Empty(RIL_BLOCK)) {
1477  if (res_it->Empty(RIL_WORD)) {
1478  res_it->Next(RIL_WORD);
1479  continue;
1480  }
1481 
1482  // Open any new block/paragraph/textline.
1483  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
1484  para_is_ltr = true; // reset to default direction
1485  hocr_str += " <div class='ocr_carea'";
1486  AddIdTohOCR(&hocr_str, "block", page_id, bcnt);
1487  AddBoxTohOCR(res_it, RIL_BLOCK, &hocr_str);
1488  }
1489  if (res_it->IsAtBeginningOf(RIL_PARA)) {
1490  hocr_str += "\n <p class='ocr_par'";
1491  para_is_ltr = res_it->ParagraphIsLtr();
1492  if (!para_is_ltr) {
1493  hocr_str += " dir='rtl'";
1494  }
1495  AddIdTohOCR(&hocr_str, "par", page_id, pcnt);
1496  paragraph_lang = res_it->WordRecognitionLanguage();
1497  if (paragraph_lang) {
1498  hocr_str += " lang='";
1499  hocr_str += paragraph_lang;
1500  hocr_str += "'";
1501  }
1502  AddBoxTohOCR(res_it, RIL_PARA, &hocr_str);
1503  }
1504  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
1505  hocr_str += "\n <span class='ocr_line'";
1506  AddIdTohOCR(&hocr_str, "line", page_id, lcnt);
1507  AddBoxTohOCR(res_it, RIL_TEXTLINE, &hocr_str);
1508  }
1509 
1510  // Now, process the word...
1511  hocr_str += "<span class='ocrx_word'";
1512  AddIdTohOCR(&hocr_str, "word", page_id, wcnt);
1513  int left, top, right, bottom;
1514  bool bold, italic, underlined, monospace, serif, smallcaps;
1515  int pointsize, font_id;
1516  const char *font_name;
1517  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
1518  font_name = res_it->WordFontAttributes(&bold, &italic, &underlined,
1519  &monospace, &serif, &smallcaps,
1520  &pointsize, &font_id);
1521  hocr_str.add_str_int(" title='bbox ", left);
1522  hocr_str.add_str_int(" ", top);
1523  hocr_str.add_str_int(" ", right);
1524  hocr_str.add_str_int(" ", bottom);
1525  hocr_str.add_str_int("; x_wconf ", res_it->Confidence(RIL_WORD));
1526  if (font_info) {
1527  if (font_name) {
1528  hocr_str += "; x_font ";
1529  hocr_str += HOcrEscape(font_name);
1530  }
1531  hocr_str.add_str_int("; x_fsize ", pointsize);
1532  }
1533  hocr_str += "'";
1534  const char* lang = res_it->WordRecognitionLanguage();
1535  if (lang && (!paragraph_lang || strcmp(lang, paragraph_lang))) {
1536  hocr_str += " lang='";
1537  hocr_str += lang;
1538  hocr_str += "'";
1539  }
1540  switch (res_it->WordDirection()) {
1541  // Only emit direction if different from current paragraph direction
1542  case DIR_LEFT_TO_RIGHT:
1543  if (!para_is_ltr) hocr_str += " dir='ltr'";
1544  break;
1545  case DIR_RIGHT_TO_LEFT:
1546  if (para_is_ltr) hocr_str += " dir='rtl'";
1547  break;
1548  case DIR_MIX:
1549  case DIR_NEUTRAL:
1550  default: // Do nothing.
1551  break;
1552  }
1553  hocr_str += ">";
1554  bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
1555  bool last_word_in_para = res_it->IsAtFinalElement(RIL_PARA, RIL_WORD);
1556  bool last_word_in_block = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
1557  if (bold) hocr_str += "<strong>";
1558  if (italic) hocr_str += "<em>";
1559  do {
1560  const std::unique_ptr<const char[]> grapheme(
1561  res_it->GetUTF8Text(RIL_SYMBOL));
1562  if (grapheme && grapheme[0] != 0) {
1563  hocr_str += HOcrEscape(grapheme.get());
1564  }
1565  res_it->Next(RIL_SYMBOL);
1566  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
1567  if (italic) hocr_str += "</em>";
1568  if (bold) hocr_str += "</strong>";
1569  hocr_str += "</span> ";
1570  wcnt++;
1571  // Close any ending block/paragraph/textline.
1572  if (last_word_in_line) {
1573  hocr_str += "\n </span>";
1574  lcnt++;
1575  }
1576  if (last_word_in_para) {
1577  hocr_str += "\n </p>\n";
1578  pcnt++;
1579  para_is_ltr = true; // back to default direction
1580  }
1581  if (last_word_in_block) {
1582  hocr_str += " </div>\n";
1583  bcnt++;
1584  }
1585  }
1586  hocr_str += " </div>\n";
1587 
1588  char *ret = new char[hocr_str.length() + 1];
1589  strcpy(ret, hocr_str.string());
1590  delete res_it;
1591  return ret;
1592 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
STRING * input_file_
Name used by training code.
Definition: baseapi.h:873
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:823
STRING HOcrEscape(const char *text)
Definition: baseapi.cpp:2837
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
void SetInputName(const char *name)
Definition: baseapi.cpp:257
const char * string() const
Definition: strngs.cpp:198
Definition: strngs.h:45
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:292
ResultIterator * GetIterator()
Definition: baseapi.cpp:1252

◆ 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 1414 of file baseapi.cpp.

1414  {
1415  return GetHOCRText(NULL, page_number);
1416 }
char * GetHOCRText(ETEXT_DESC *monitor, int page_number)
Definition: baseapi.cpp:1427

◆ 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 1252 of file baseapi.cpp.

1252  {
1253  if (tesseract_ == NULL || page_res_ == NULL)
1254  return NULL;
1255  return ResultIterator::StartOfParagraph(LTRResultIterator(
1259 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
static ResultIterator * StartOfParagraph(const LTRResultIterator &resit)
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
int GetScaledYResolution() const
Definition: thresholder.h:93

◆ 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 1269 of file baseapi.cpp.

1269  {
1270  if (tesseract_ == NULL || page_res_ == NULL)
1271  return NULL;
1272  return new MutableIterator(page_res_, tesseract_,
1276 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
int GetScaledYResolution() const
Definition: thresholder.h:93

◆ 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 1922 of file baseapi.cpp.

1922  {
1923  int orient_deg;
1924  float orient_conf;
1925  const char* script_name;
1926  float script_conf;
1927 
1928  if (!DetectOrientationScript(&orient_deg, &orient_conf, &script_name,
1929  &script_conf))
1930  return NULL;
1931 
1932  // clockwise rotation needed to make the page upright
1933  int rotate = OrientationIdToValue(orient_deg / 90);
1934 
1935  const int kOsdBufsize = 255;
1936  char* osd_buf = new char[kOsdBufsize];
1937  snprintf(osd_buf, kOsdBufsize,
1938  "Page number: %d\n"
1939  "Orientation in degrees: %d\n"
1940  "Rotate: %d\n"
1941  "Orientation confidence: %.2f\n"
1942  "Script: %s\n"
1943  "Script confidence: %.2f\n",
1944  page_number, orient_deg, rotate, orient_conf, script_name,
1945  script_conf);
1946 
1947  return osd_buf;
1948 }
bool DetectOrientationScript(int *orient_deg, float *orient_conf, const char **script_name, float *script_conf)
Definition: baseapi.cpp:1891
int OrientationIdToValue(const int &id)
Definition: osdetect.cpp:562

◆ 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 623 of file baseapi.cpp.

623  {
624  return GetComponentImages(RIL_BLOCK, false, pixa, NULL);
625 }
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:680

◆ 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 NULL, the block-id of each line is also returned as an array of one element per line. delete [] after use.

Definition at line 649 of file baseapi.cpp.

649  {
650  return GetComponentImages(RIL_TEXTLINE, false, pixa, blockids);
651 }
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:680

◆ GetTextDirection()

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

Definition at line 2126 of file baseapi.cpp.

2126  {
2127  PageIterator* it = AnalyseLayout();
2128  if (it == NULL) {
2129  return false;
2130  }
2131  int x1, x2, y1, y2;
2132  it->Baseline(RIL_TEXTLINE, &x1, &y1, &x2, &y2);
2133  // Calculate offset and slope (NOTE: Kind of ugly)
2134  if (x2 <= x1) x2 = x1 + 1;
2135  // Convert the point pair to slope/offset of the baseline (in image coords.)
2136  *out_slope = static_cast<float>(y2 - y1) / (x2 - x1);
2137  *out_offset = static_cast<int>(y1 - *out_slope * x1);
2138  // Get the y-coord of the baseline at the left and right edges of the
2139  // textline's bounding box.
2140  int left, top, right, bottom;
2141  if (!it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom)) {
2142  delete it;
2143  return false;
2144  }
2145  int left_y = IntCastRounded(*out_slope * left + *out_offset);
2146  int right_y = IntCastRounded(*out_slope * right + *out_offset);
2147  // Shift the baseline down so it passes through the nearest bottom-corner
2148  // of the textline's bounding box. This is the difference between the y
2149  // at the lowest (max) edge of the box and the actual box bottom.
2150  *out_offset += bottom - MAX(left_y, right_y);
2151  // Switch back to bottom-up tesseract coordinates. Requires negation of
2152  // the slope and height - offset for the offset.
2153  *out_slope = -*out_slope;
2154  *out_offset = rect_height_ - *out_offset;
2155  delete it;
2156 
2157  return true;
2158 }
#define MAX(x, y)
Definition: ndminx.h:24
PageIterator * AnalyseLayout()
Definition: baseapi.cpp:803
int IntCastRounded(double x)
Definition: helpers.h:179

◆ GetTextlines() [1/2]

Boxa * tesseract::TessBaseAPI::GetTextlines ( const bool  raw_image,
const 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 NULL, the block-id of each line is also returned as an array of one element per line. delete [] after use. If paraids is not NULL, 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 NULL, the block-id of each line is also returned as an array of one element per line. delete [] after use. If paraids is not NULL, 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 635 of file baseapi.cpp.

636  {
637  return GetComponentImages(RIL_TEXTLINE, true, raw_image, raw_padding,
638  pixa, blockids, paraids);
639 }
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:680

◆ GetTextlines() [2/2]

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

Definition at line 411 of file baseapi.h.

411  {
412  return GetTextlines(false, 0, pixa, blockids, NULL);
413  }
Boxa * GetTextlines(const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:635

◆ 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 609 of file baseapi.cpp.

609  {
610  if (tesseract_ == nullptr || thresholder_ == nullptr) return nullptr;
611  if (tesseract_->pix_binary() == nullptr &&
613  return nullptr;
614  }
615  return pixClone(tesseract_->pix_binary());
616 }
Pix * pix_binary() const
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
virtual TESS_LOCAL bool Threshold(Pix **pix)
Definition: baseapi.cpp:2209
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869

◆ 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 762 of file baseapi.cpp.

762  {
763  if (thresholder_ == NULL) {
764  return 0;
765  }
766  return thresholder_->GetScaleFactor();
767 }
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869

◆ 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 1599 of file baseapi.cpp.

1599  {
1600  if (tesseract_ == NULL || (page_res_ == NULL && Recognize(NULL) < 0))
1601  return NULL;
1602 
1603  int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
1604  int page_id = page_number + 1; // we use 1-based page numbers.
1605 
1606  STRING tsv_str("");
1607 
1608  int page_num = page_id, block_num = 0, par_num = 0, line_num = 0,
1609  word_num = 0;
1610 
1611  tsv_str.add_str_int("1\t", page_num); // level 1 - page
1612  tsv_str.add_str_int("\t", block_num);
1613  tsv_str.add_str_int("\t", par_num);
1614  tsv_str.add_str_int("\t", line_num);
1615  tsv_str.add_str_int("\t", word_num);
1616  tsv_str.add_str_int("\t", rect_left_);
1617  tsv_str.add_str_int("\t", rect_top_);
1618  tsv_str.add_str_int("\t", rect_width_);
1619  tsv_str.add_str_int("\t", rect_height_);
1620  tsv_str += "\t-1\t\n";
1621 
1622  ResultIterator* res_it = GetIterator();
1623  while (!res_it->Empty(RIL_BLOCK)) {
1624  if (res_it->Empty(RIL_WORD)) {
1625  res_it->Next(RIL_WORD);
1626  continue;
1627  }
1628 
1629  // Add rows for any new block/paragraph/textline.
1630  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
1631  block_num++, par_num = 0, line_num = 0, word_num = 0;
1632  tsv_str.add_str_int("2\t", page_num); // level 2 - block
1633  tsv_str.add_str_int("\t", block_num);
1634  tsv_str.add_str_int("\t", par_num);
1635  tsv_str.add_str_int("\t", line_num);
1636  tsv_str.add_str_int("\t", word_num);
1637  AddBoxToTSV(res_it, RIL_BLOCK, &tsv_str);
1638  tsv_str += "\t-1\t\n"; // end of row for block
1639  }
1640  if (res_it->IsAtBeginningOf(RIL_PARA)) {
1641  par_num++, line_num = 0, word_num = 0;
1642  tsv_str.add_str_int("3\t", page_num); // level 3 - paragraph
1643  tsv_str.add_str_int("\t", block_num);
1644  tsv_str.add_str_int("\t", par_num);
1645  tsv_str.add_str_int("\t", line_num);
1646  tsv_str.add_str_int("\t", word_num);
1647  AddBoxToTSV(res_it, RIL_PARA, &tsv_str);
1648  tsv_str += "\t-1\t\n"; // end of row for para
1649  }
1650  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
1651  line_num++, word_num = 0;
1652  tsv_str.add_str_int("4\t", page_num); // level 4 - line
1653  tsv_str.add_str_int("\t", block_num);
1654  tsv_str.add_str_int("\t", par_num);
1655  tsv_str.add_str_int("\t", line_num);
1656  tsv_str.add_str_int("\t", word_num);
1657  AddBoxToTSV(res_it, RIL_TEXTLINE, &tsv_str);
1658  tsv_str += "\t-1\t\n"; // end of row for line
1659  }
1660 
1661  // Now, process the word...
1662  int left, top, right, bottom;
1663  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
1664  word_num++;
1665  tsv_str.add_str_int("5\t", page_num); // level 5 - word
1666  tsv_str.add_str_int("\t", block_num);
1667  tsv_str.add_str_int("\t", par_num);
1668  tsv_str.add_str_int("\t", line_num);
1669  tsv_str.add_str_int("\t", word_num);
1670  tsv_str.add_str_int("\t", left);
1671  tsv_str.add_str_int("\t", top);
1672  tsv_str.add_str_int("\t", right - left);
1673  tsv_str.add_str_int("\t", bottom - top);
1674  tsv_str.add_str_int("\t", res_it->Confidence(RIL_WORD));
1675  tsv_str += "\t";
1676 
1677  // Increment counts if at end of block/paragraph/textline.
1678  if (res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD)) lcnt++;
1679  if (res_it->IsAtFinalElement(RIL_PARA, RIL_WORD)) pcnt++;
1680  if (res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD)) bcnt++;
1681 
1682  do {
1683  tsv_str +=
1684  std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_SYMBOL)).get();
1685  res_it->Next(RIL_SYMBOL);
1686  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
1687  tsv_str += "\n"; // end of row
1688  wcnt++;
1689  }
1690 
1691  char* ret = new char[tsv_str.length() + 1];
1692  strcpy(ret, tsv_str.string());
1693  delete res_it;
1694  return ret;
1695 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:823
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
Definition: strngs.h:45
ResultIterator * GetIterator()
Definition: baseapi.cpp:1252

◆ GetUnichar()

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

This method returns the string form of the specified unichar.

Definition at line 2821 of file baseapi.cpp.

2821  {
2822  return tesseract_->unicharset.id_to_unichar(unichar_id);
2823 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
UNICHARSET unicharset
Definition: ccutil.h:68
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:288

◆ 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 1781 of file baseapi.cpp.

1781  {
1782  if (tesseract_ == NULL ||
1783  (!recognition_done_ && Recognize(NULL) < 0))
1784  return NULL;
1785  bool tilde_crunch_written = false;
1786  bool last_char_was_newline = true;
1787  bool last_char_was_tilde = false;
1788 
1789  int total_length = TextLength(NULL);
1790  PAGE_RES_IT page_res_it(page_res_);
1791  char* result = new char[total_length];
1792  char* ptr = result;
1793  for (page_res_it.restart_page(); page_res_it.word () != NULL;
1794  page_res_it.forward()) {
1795  WERD_RES *word = page_res_it.word();
1796  // Process the current word.
1797  if (word->unlv_crunch_mode != CR_NONE) {
1798  if (word->unlv_crunch_mode != CR_DELETE &&
1799  (!tilde_crunch_written ||
1800  (word->unlv_crunch_mode == CR_KEEP_SPACE &&
1801  word->word->space() > 0 &&
1802  !word->word->flag(W_FUZZY_NON) &&
1803  !word->word->flag(W_FUZZY_SP)))) {
1804  if (!word->word->flag(W_BOL) &&
1805  word->word->space() > 0 &&
1806  !word->word->flag(W_FUZZY_NON) &&
1807  !word->word->flag(W_FUZZY_SP)) {
1808  /* Write a space to separate from preceding good text */
1809  *ptr++ = ' ';
1810  last_char_was_tilde = false;
1811  }
1812  if (!last_char_was_tilde) {
1813  // Write a reject char.
1814  last_char_was_tilde = true;
1815  *ptr++ = kUNLVReject;
1816  tilde_crunch_written = true;
1817  last_char_was_newline = false;
1818  }
1819  }
1820  } else {
1821  // NORMAL PROCESSING of non tilde crunched words.
1822  tilde_crunch_written = false;
1824  const char* wordstr = word->best_choice->unichar_string().string();
1825  const STRING& lengths = word->best_choice->unichar_lengths();
1826  int length = lengths.length();
1827  int i = 0;
1828  int offset = 0;
1829 
1830  if (last_char_was_tilde &&
1831  word->word->space() == 0 && wordstr[offset] == ' ') {
1832  // Prevent adjacent tilde across words - we know that adjacent tildes
1833  // within words have been removed.
1834  // Skip the first character.
1835  offset = lengths[i++];
1836  }
1837  if (i < length && wordstr[offset] != 0) {
1838  if (!last_char_was_newline)
1839  *ptr++ = ' ';
1840  else
1841  last_char_was_newline = false;
1842  for (; i < length; offset += lengths[i++]) {
1843  if (wordstr[offset] == ' ' ||
1844  wordstr[offset] == kTesseractReject) {
1845  *ptr++ = kUNLVReject;
1846  last_char_was_tilde = true;
1847  } else {
1848  if (word->reject_map[i].rejected())
1849  *ptr++ = kUNLVSuspect;
1850  UNICHAR ch(wordstr + offset, lengths[i]);
1851  int uni_ch = ch.first_uni();
1852  for (int j = 0; kUniChs[j] != 0; ++j) {
1853  if (kUniChs[j] == uni_ch) {
1854  uni_ch = kLatinChs[j];
1855  break;
1856  }
1857  }
1858  if (uni_ch <= 0xff) {
1859  *ptr++ = static_cast<char>(uni_ch);
1860  last_char_was_tilde = false;
1861  } else {
1862  *ptr++ = kUNLVReject;
1863  last_char_was_tilde = true;
1864  }
1865  }
1866  }
1867  }
1868  }
1869  if (word->word->flag(W_EOL) && !last_char_was_newline) {
1870  /* Add a new line output */
1871  *ptr++ = '\n';
1872  tilde_crunch_written = false;
1873  last_char_was_newline = true;
1874  last_char_was_tilde = false;
1875  }
1876  }
1877  *ptr++ = '\n';
1878  *ptr = '\0';
1879  return result;
1880 }
void set_unlv_suspects(WERD_RES *word)
Definition: output.cpp:305
Definition: werd.h:36
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
const char kTesseractReject
Definition: baseapi.cpp:89
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:823
const int kLatinChs[]
Definition: baseapi.cpp:1772
TESS_LOCAL int TextLength(int *blob_count)
Definition: baseapi.cpp:2352
WERD_CHOICE * best_choice
Definition: pageres.h:219
REJMAP reject_map
Definition: pageres.h:271
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:878
Definition: werd.h:35
WERD * word
Definition: pageres.h:175
const char * string() const
Definition: strngs.cpp:198
const char kUNLVReject
Definition: baseapi.cpp:91
const STRING & unichar_lengths() const
Definition: ratngs.h:544
Definition: strngs.h:45
CRUNCH_MODE unlv_crunch_mode
Definition: pageres.h:294
const char kUNLVSuspect
Definition: baseapi.cpp:93
uinT8 space()
Definition: werd.h:104
BOOL8 flag(WERD_FLAGS mask) const
Definition: werd.h:128
inT32 length() const
Definition: strngs.cpp:193
const int kUniChs[]
Definition: baseapi.cpp:1768
const STRING & unichar_string() const
Definition: ratngs.h:537

◆ 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 1279 of file baseapi.cpp.

1279  {
1280  if (tesseract_ == NULL ||
1281  (!recognition_done_ && Recognize(NULL) < 0))
1282  return NULL;
1283  STRING text("");
1284  ResultIterator *it = GetIterator();
1285  do {
1286  if (it->Empty(RIL_PARA)) continue;
1287  const std::unique_ptr<const char[]> para_text(it->GetUTF8Text(RIL_PARA));
1288  text += para_text.get();
1289  } while (it->Next(RIL_PARA));
1290  char* result = new char[text.length() + 1];
1291  strncpy(result, text.string(), text.length() + 1);
1292  delete it;
1293  return result;
1294 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:823
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:878
Definition: strngs.h:45
ResultIterator * GetIterator()
Definition: baseapi.cpp:1252

◆ 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 658 of file baseapi.cpp.

658  {
659  return GetComponentImages(RIL_WORD, true, pixa, NULL);
660 }
Boxa * GetComponentImages(const PageIteratorLevel level, const bool text_only, const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:680

◆ InitTruthCallback()

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

Definition at line 768 of file baseapi.h.

768 { truth_cb_ = cb; }
TruthCallback * truth_cb_
Definition: baseapi.h:879

◆ IsValidCharacter()

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

Definition at line 2119 of file baseapi.cpp.

2119  {
2120  return tesseract_->unicharset.contains_unichar(utf8_character);
2121 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
bool contains_unichar(const char *const unichar_repr) const
Definition: unicharset.cpp:668
UNICHARSET unicharset
Definition: ccutil.h:68

◆ 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 2115 of file baseapi.cpp.

2115  {
2116  return tesseract_->getDict().valid_word(word);
2117 }
Dict & getDict() override
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
Definition: dict.cpp:752

◆ 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 2495 of file baseapi.cpp.

2495  {
2496  int width = pixGetWidth(pix);
2497  int height = pixGetHeight(pix);
2498  BLOCK block("a character", TRUE, 0, 0, 0, 0, width, height);
2499 
2500  // Create C_BLOBs from the page
2501  extract_edges(pix, &block);
2502 
2503  // Merge all C_BLOBs
2504  C_BLOB_LIST *list = block.blob_list();
2505  C_BLOB_IT c_blob_it(list);
2506  if (c_blob_it.empty())
2507  return NULL;
2508  // Move all the outlines to the first blob.
2509  C_OUTLINE_IT ol_it(c_blob_it.data()->out_list());
2510  for (c_blob_it.forward();
2511  !c_blob_it.at_first();
2512  c_blob_it.forward()) {
2513  C_BLOB *c_blob = c_blob_it.data();
2514  ol_it.add_list_after(c_blob->out_list());
2515  }
2516  // Convert the first blob to the output TBLOB.
2517  return TBLOB::PolygonalCopy(false, c_blob_it.data());
2518 }
#define TRUE
Definition: capi.h:45
Definition: ocrblock.h:30
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:64
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
Definition: blobs.cpp:344
void extract_edges(Pix *pix, BLOCK *block)
Definition: edgblob.cpp:334

◆ 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 2478 of file baseapi.cpp.

2481  {
2482  inT32 xstarts[] = {-32000};
2483  double quad_coeffs[] = {0, 0, baseline};
2484  return new ROW(1,
2485  xstarts,
2486  quad_coeffs,
2487  xheight,
2488  ascender - (baseline + xheight),
2489  descender - baseline,
2490  0,
2491  0);
2492 }
Definition: ocrrow.h:32
int32_t inT32
Definition: host.h:38

◆ 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 1951 of file baseapi.cpp.

1951  {
1952  int* conf = AllWordConfidences();
1953  if (!conf) return 0;
1954  int sum = 0;
1955  int *pt = conf;
1956  while (*pt >= 0) sum += *pt++;
1957  if (pt != conf) sum /= pt - conf;
1958  delete [] conf;
1959  return sum;
1960 }

◆ 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 2525 of file baseapi.cpp.

2525  {
2526  TBOX box = tblob->bounding_box();
2527  float x_center = (box.left() + box.right()) / 2.0f;
2528  float baseline = row->base_line(x_center);
2529  float scale = kBlnXHeight / row->x_height();
2530  tblob->Normalize(NULL, NULL, NULL, x_center, baseline, scale, scale,
2531  0.0f, static_cast<float>(kBlnBaselineOffset), false, NULL);
2532 }
float x_height() const
Definition: ocrrow.h:61
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:413
const int kBlnXHeight
Definition: normalis.h:28
Definition: rect.h:30
inT16 left() const
Definition: rect.h:68
float base_line(float xpos) const
Definition: ocrrow.h:56
TBOX bounding_box() const
Definition: blobs.cpp:482
const int kBlnBaselineOffset
Definition: normalis.h:29
inT16 right() const
Definition: rect.h:75

◆ NumDawgs()

int tesseract::TessBaseAPI::NumDawgs ( ) const

Return the number of dawgs loaded into tesseract_ object.

Definition at line 2832 of file baseapi.cpp.

2832  {
2833  return tesseract_ == NULL ? 0 : tesseract_->getDict().NumDawgs();
2834 }
Dict & getDict() override
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
int NumDawgs() const
Return the number of dawgs in the dawgs_ vector.
Definition: dict.h:412

◆ oem()

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

Definition at line 766 of file baseapi.h.

766 { return last_oem_requested_; }
OcrEngineMode last_oem_requested_
Last ocr language mode requested.
Definition: baseapi.h:877

◆ 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 1170 of file baseapi.cpp.

1172  {
1173  PERF_COUNT_START("ProcessPage")
1175  SetImage(pix);
1176  bool failed = false;
1177 
1178  if (tesseract_->tessedit_pageseg_mode == PSM_AUTO_ONLY) {
1179  // Disabled character recognition
1180  PageIterator* it = AnalyseLayout();
1181 
1182  if (it == NULL) {
1183  failed = true;
1184  } else {
1185  delete it;
1186  }
1188  failed = FindLines() != 0;
1189  } else if (timeout_millisec > 0) {
1190  // Running with a timeout.
1191  ETEXT_DESC monitor;
1192  monitor.cancel = NULL;
1193  monitor.cancel_this = NULL;
1194  monitor.set_deadline_msecs(timeout_millisec);
1195 
1196  // Now run the main recognition.
1197  failed = Recognize(&monitor) < 0;
1198  } else {
1199  // Normal layout and character recognition with no timeout.
1200  failed = Recognize(NULL) < 0;
1201  }
1202 
1204 #ifndef ANDROID_BUILD
1205  Pix* page_pix = GetThresholdedImage();
1206  pixWrite("tessinput.tif", page_pix, IFF_TIFF_G4);
1207 #endif // ANDROID_BUILD
1208  }
1209 
1210  if (failed && retry_config != NULL && retry_config[0] != '\0') {
1211  // Save current config variables before switching modes.
1212  FILE* fp = fopen(kOldVarsFile, "wb");
1213  PrintVariables(fp);
1214  fclose(fp);
1215  // Switch to alternate mode for retry.
1216  ReadConfigFile(retry_config);
1217  SetImage(pix);
1218  Recognize(NULL);
1219  // Restore saved config variables.
1221  }
1222 
1223  if (renderer && !failed) {
1224  failed = !renderer->AddImage(this);
1225  }
1226 
1228  return !failed;
1229 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
#define PERF_COUNT_END
void * cancel_this
called whenever progress increases
Definition: ocrclass.h:127
void PrintVariables(FILE *fp) const
Definition: baseapi.cpp:320
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:823
void SetInputName(const char *name)
Definition: baseapi.cpp:257
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:561
void set_deadline_msecs(inT32 deadline_msecs)
Definition: ocrclass.h:146
Pix * GetThresholdedImage()
Definition: baseapi.cpp:609
#define PERF_COUNT_START(FUNCT_NAME)
Automatic page segmentation, but no OSD, or OCR.
Definition: publictypes.h:167
CANCEL_FUNC cancel
for errcode use
Definition: ocrclass.h:125
PageIterator * AnalyseLayout()
Definition: baseapi.cpp:803
void ReadConfigFile(const char *filename)
Definition: baseapi.cpp:484
const char * kOldVarsFile
Definition: baseapi.cpp:102
TESS_LOCAL int FindLines()
Definition: baseapi.cpp:2253
Orientation and script detection only.
Definition: publictypes.h:164

◆ 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 NULL, 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 1052 of file baseapi.cpp.

1054  {
1055  bool result =
1056  ProcessPagesInternal(filename, retry_config, timeout_millisec, renderer);
1057  if (result) {
1060  tprintf("Write of TR file failed: %s\n", output_file_->string());
1061  return false;
1062  }
1063  }
1064  return result;
1065 }
bool ProcessPagesInternal(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1078
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
STRING * output_file_
Name used by debug code.
Definition: baseapi.h:874
#define tprintf(...)
Definition: tprintf.h:31
bool WriteTRFile(const STRING &filename)
Definition: blobclass.cpp:97
const char * string() const
Definition: strngs.cpp:198

◆ ProcessPagesInternal()

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

Definition at line 1078 of file baseapi.cpp.

1081  {
1082  PERF_COUNT_START("ProcessPages")
1083  bool stdInput = !strcmp(filename, "stdin") || !strcmp(filename, "-");
1084  if (stdInput) {
1085 #ifdef WIN32
1086  if (_setmode(_fileno(stdin), _O_BINARY) == -1)
1087  tprintf("ERROR: cin to binary: %s", strerror(errno));
1088 #endif // WIN32
1089  }
1090 
1091  if (stream_filelist) {
1092  return ProcessPagesFileList(stdin, NULL, retry_config,
1093  timeout_millisec, renderer,
1095  }
1096 
1097  // At this point we are officially in autodection territory.
1098  // That means any data in stdin must be buffered, to make it
1099  // seekable.
1100  std::string buf;
1101  const l_uint8 *data = NULL;
1102  if (stdInput) {
1103  buf.assign((std::istreambuf_iterator<char>(std::cin)),
1104  (std::istreambuf_iterator<char>()));
1105  data = reinterpret_cast<const l_uint8 *>(buf.data());
1106  }
1107 
1108  // Here is our autodetection
1109  int format;
1110  int r = (stdInput) ?
1111  findFileFormatBuffer(data, &format) :
1112  findFileFormat(filename, &format);
1113 
1114  // Maybe we have a filelist
1115  if (r != 0 || format == IFF_UNKNOWN) {
1116  STRING s;
1117  if (stdInput) {
1118  s = buf.c_str();
1119  } else {
1120  std::ifstream t(filename);
1121  std::string u((std::istreambuf_iterator<char>(t)),
1122  std::istreambuf_iterator<char>());
1123  s = u.c_str();
1124  }
1125  return ProcessPagesFileList(NULL, &s, retry_config,
1126  timeout_millisec, renderer,
1128  }
1129 
1130  // Maybe we have a TIFF which is potentially multipage
1131  bool tiff = (format == IFF_TIFF || format == IFF_TIFF_PACKBITS ||
1132  format == IFF_TIFF_RLE || format == IFF_TIFF_G3 ||
1133  format == IFF_TIFF_G4 || format == IFF_TIFF_LZW ||
1134  format == IFF_TIFF_ZIP);
1135 
1136  // Fail early if we can, before producing any output
1137  Pix *pix = NULL;
1138  if (!tiff) {
1139  pix = (stdInput) ? pixReadMem(data, buf.size()) : pixRead(filename);
1140  if (pix == NULL) {
1141  return false;
1142  }
1143  }
1144 
1145  // Begin the output
1146  if (renderer && !renderer->BeginDocument(unknown_title_)) {
1147  pixDestroy(&pix);
1148  return false;
1149  }
1150 
1151  // Produce output
1152  r = (tiff) ?
1153  ProcessPagesMultipageTiff(data, buf.size(), filename, retry_config,
1154  timeout_millisec, renderer,
1156  ProcessPage(pix, 0, filename, retry_config,
1157  timeout_millisec, renderer);
1158 
1159  // Clean up memory as needed
1160  pixDestroy(&pix);
1161 
1162  // End the output
1163  if (!r || (renderer && !renderer->EndDocument())) {
1164  return false;
1165  }
1167  return true;
1168 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
#define PERF_COUNT_END
#define tprintf(...)
Definition: tprintf.h:31
#define PERF_COUNT_START(FUNCT_NAME)
bool ProcessPage(Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1170
Definition: strngs.h:45
bool stream_filelist
Definition: baseapi.cpp:82
const char * c_str() const
Definition: strngs.cpp:209

◆ 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 823 of file baseapi.cpp.

823  {
824  if (tesseract_ == NULL)
825  return -1;
826  if (FindLines() != 0)
827  return -1;
828  delete page_res_;
829  if (block_list_->empty()) {
830  page_res_ = new PAGE_RES(false, block_list_,
832  return 0; // Empty page.
833  }
834 
836  recognition_done_ = true;
841  } else {
844  }
845  if (page_res_ == NULL) {
846  return -1;
847  }
851  return 0;
852  }
855  return 0;
856  }
857 
858  if (truth_cb_ != NULL) {
859  tesseract_->wordrec_run_blamer.set_value(true);
860  PageIterator *page_it = new PageIterator(
865  image_height_, page_it, this->tesseract()->pix_grey());
866  delete page_it;
867  }
868 
869  int result = 0;
871  #ifndef GRAPHICS_DISABLED
873  #endif // GRAPHICS_DISABLED
874  // The page_res is invalid after an interactive session, so cleanup
875  // in a way that lets us continue to the next page without crashing.
876  delete page_res_;
877  page_res_ = NULL;
878  return -1;
880  STRING fontname;
881  ExtractFontName(*output_file_, &fontname);
883  } else if (tesseract_->tessedit_ambigs_training) {
884  FILE *training_output_file = tesseract_->init_recog_training(*input_file_);
885  // OCR the page segmented into words by tesseract.
887  *input_file_, page_res_, monitor, training_output_file);
888  fclose(training_output_file);
889  } else {
890  // Now run the main recognition.
891  bool wait_for_text = true;
892  GetBoolVariable("paragraph_text_based", &wait_for_text);
893  if (!wait_for_text) DetectParagraphs(false);
894  if (tesseract_->recog_all_words(page_res_, monitor, NULL, NULL, 0)) {
895  if (wait_for_text) DetectParagraphs(true);
896  } else {
897  result = -1;
898  }
899  }
900  return result;
901 }
void recog_training_segmented(const STRING &fname, PAGE_RES *page_res, volatile ETEXT_DESC *monitor, FILE *output_file)
Tesseract * tesseract() const
Definition: baseapi.h:764
Dict & getDict() override
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
Pix * pix_grey() const
WERD_CHOICE * prev_word_best_choice_
Definition: wordrec.h:412
TruthCallback * truth_cb_
Definition: baseapi.h:879
STRING * output_file_
Name used by debug code.
Definition: baseapi.h:874
void pgeditor_main(int width, int height, PAGE_RES *page_res)
Definition: pgedit.cpp:337
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:300
STRING * input_file_
Name used by training code.
Definition: baseapi.h:873
bool tessedit_resegment_from_line_boxes
FILE * init_recog_training(const STRING &fname)
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
void CorrectClassifyWords(PAGE_RES *page_res)
Definition: applybox.cpp:772
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:878
bool AnyLSTMLang() const
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
PAGE_RES * ApplyBoxes(const STRING &fname, bool find_segmentation, BLOCK_LIST *block_list)
Definition: applybox.cpp:117
TESS_LOCAL void DetectParagraphs(bool after_text_recognition)
Definition: baseapi.cpp:2607
Definition: strngs.h:45
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:871
virtual void Run(A1, A2, A3, A4)=0
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:292
void ExtractFontName(const STRING &filename, STRING *fontname)
Definition: blobclass.cpp:46
int GetScaledYResolution() const
Definition: thresholder.h:93
bool wordrec_run_blamer
Definition: wordrec.h:168
void TrainLineRecognizer(const STRING &input_imagename, const STRING &output_basename, BLOCK_LIST *block_list)
Definition: linerec.cpp:42
void ApplyBoxTraining(const STRING &fontname, PAGE_RES *page_res)
Definition: applybox.cpp:796
const UNICHARSET & getUnicharset() const
Definition: dict.h:97
TESS_LOCAL int FindLines()
Definition: baseapi.cpp:2253

◆ 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 904 of file baseapi.cpp.

904  {
905  if (tesseract_ == NULL)
906  return -1;
907  if (thresholder_ == NULL || thresholder_->IsEmpty()) {
908  tprintf("Please call SetImage before attempting recognition.\n");
909  return -1;
910  }
911  if (page_res_ != NULL)
912  ClearResults();
913  if (FindLines() != 0)
914  return -1;
915  // Additional conditions under which chopper test cannot be run
916  if (tesseract_->interactive_display_mode) return -1;
917 
918  recognition_done_ = true;
919 
920  page_res_ = new PAGE_RES(false, block_list_,
922 
923  PAGE_RES_IT page_res_it(page_res_);
924 
925  while (page_res_it.word() != NULL) {
926  WERD_RES *word_res = page_res_it.word();
927  GenericVector<TBOX> boxes;
928  tesseract_->MaximallyChopWord(boxes, page_res_it.block()->block,
929  page_res_it.row()->row, word_res);
930  page_res_it.forward();
931  }
932  return 0;
933 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
WERD_CHOICE * prev_word_best_choice_
Definition: wordrec.h:412
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:872
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:878
#define tprintf(...)
Definition: tprintf.h:31
WERD * word
Definition: pageres.h:175
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
bool IsEmpty() const
Return true if no image has been set.
Definition: thresholder.cpp:50
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:871
void MaximallyChopWord(const GenericVector< TBOX > &boxes, BLOCK *block, ROW *row, WERD_RES *word_res)
Definition: applybox.cpp:253
TESS_LOCAL int FindLines()
Definition: baseapi.cpp:2253

◆ 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 2798 of file baseapi.cpp.

2802  {
2803  BLOB_CHOICE_LIST* choices = new BLOB_CHOICE_LIST;
2804  tesseract_->AdaptiveClassifier(blob, choices);
2805  BLOB_CHOICE_IT choices_it(choices);
2806  int& index = *num_matches_returned;
2807  index = 0;
2808  for (choices_it.mark_cycle_pt();
2809  !choices_it.cycled_list() && index < num_max_matches;
2810  choices_it.forward()) {
2811  BLOB_CHOICE* choice = choices_it.data();
2812  unichar_ids[index] = choice->unichar_id();
2813  ratings[index] = choice->rating();
2814  ++index;
2815  }
2816  *num_matches_returned = index;
2817  delete choices;
2818 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
Definition: adaptmatch.cpp:185
UNICHAR_ID unichar_id() const
Definition: ratngs.h:76
float rating() const
Definition: ratngs.h:79

◆ set_min_orientation_margin()

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

Definition at line 2395 of file baseapi.cpp.

2395  {
2396  tesseract_->min_orientation_margin.set_value(margin);
2397 }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865

◆ SetDictFunc()

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

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

Definition at line 2161 of file baseapi.cpp.

2161  {
2162  if (tesseract_ != NULL) {
2164  }
2165 }
Dict & getDict() override
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
int(Dict::* letter_is_okay_)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
Definition: dict.h:356

◆ SetFillLatticeFunc()

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

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

Definition at line 2187 of file baseapi.cpp.

2187  {
2188  if (tesseract_ != NULL) tesseract_->fill_lattice_ = f;
2189 }
void(Wordrec::* fill_lattice_)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
Definition: wordrec.h:416
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865

◆ 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 561 of file baseapi.cpp.

563  {
564  if (InternalSetImage()) {
565  thresholder_->SetImage(imagedata, width, height,
566  bytes_per_pixel, bytes_per_line);
568  }
569 }
TESS_LOCAL bool InternalSetImage()
Definition: baseapi.cpp:2192
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
void SetInputImage(Pix *pix)
Definition: baseapi.cpp:936
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: thresholder.cpp:62

◆ 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 586 of file baseapi.cpp.

586  {
587  if (InternalSetImage()) {
588  thresholder_->SetImage(pix);
590  }
591 }
TESS_LOCAL bool InternalSetImage()
Definition: baseapi.cpp:2192
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869
void SetInputImage(Pix *pix)
Definition: baseapi.cpp:936
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: thresholder.cpp:62

◆ 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 2175 of file baseapi.cpp.

2175  {
2176  if (tesseract_ != NULL) {
2178  // Set it for the sublangs too.
2179  int num_subs = tesseract_->num_sub_langs();
2180  for (int i = 0; i < num_subs; ++i) {
2182  }
2183  }
2184 }
Dict & getDict() override
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865
Tesseract * get_sub_lang(int index) const
int num_sub_langs() const
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:366

◆ 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 598 of file baseapi.cpp.

598  {
599  if (thresholder_ == NULL)
600  return;
601  thresholder_->SetRectangle(left, top, width, height);
602  ClearResults();
603 }
void SetRectangle(int left, int top, int width, int height)
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869

◆ 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 571 of file baseapi.cpp.

571  {
572  if (thresholder_)
574  else
575  tprintf("Please call SetImage before SetSourceResolution.\n");
576 }
void SetSourceYResolution(int ppi)
Definition: thresholder.h:86
#define tprintf(...)
Definition: tprintf.h:31
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869

◆ 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 375 of file baseapi.h.

375  {
376  delete thresholder_;
377  thresholder_ = thresholder;
378  ClearResults();
379  }
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:869

◆ tesseract()

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

Definition at line 764 of file baseapi.h.

764 { return tesseract_; }
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:865