Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 (const 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 (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 (PageIteratorLevel level, bool text_only, Pixa **pixa, int **blockids)
int tesseract::TessBaseAPI::GetThresholdedImageScaleFactor () const
void tesseract::TessBaseAPI::DumpPGM (const char *filename)
PageIterator * tesseract::TessBaseAPI::AnalyseLayout ()
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, STRING *text_out)
bool tesseract::TessBaseAPI::ProcessPage (Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, STRING *text_out)
ResultIterator * tesseract::TessBaseAPI::GetIterator ()
MutableIterator * tesseract::TessBaseAPI::GetMutableIterator ()
char * tesseract::TessBaseAPI::GetUTF8Text ()
char * tesseract::TessBaseAPI::GetHOCRText (int page_number)
char * tesseract::TessBaseAPI::GetBoxText (int page_number)
char * tesseract::TessBaseAPI::GetUNLVText ()
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 ()
int tesseract::TessBaseAPI::IsValidWord (const char *word)
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, const DENORM &denorm, INT_FEATURE_ARRAY int_features, int *num_features, int *FeatureOutlineIndex)
static ROWtesseract::TessBaseAPI::FindRowForBox (BLOCK_LIST *blocks, int left, int top, int right, int bottom)
void tesseract::TessBaseAPI::RunAdaptiveClassifier (TBLOB *blob, const DENORM &denorm, int num_max_matches, int *unichar_ids, float *ratings, int *num_matches_returned)
const char * tesseract::TessBaseAPI::GetUnichar (int unichar_id)
const Dawg * tesseract::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, DENORM *denorm)
Tesseract *const tesseract::TessBaseAPI::tesseract () const
OcrEngineMode const tesseract::TessBaseAPI::oem () const
void tesseract::TessBaseAPI::InitTruthCallback (TruthCallback *cb)
CubeRecoContext * tesseract::TessBaseAPI::GetCubeRecoContext () const
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

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

{
int debug = 0;
GetIntVariable("applybox_debug", &debug);
bool success = true;
PageSegMode current_psm = GetPageSegMode();
SetVariable("classify_enable_learning", "0");
char* text = GetUTF8Text();
if (debug) {
tprintf("Trying to adapt \"%s\" to \"%s\"\n", text, wordstr);
}
if (text != NULL) {
WERD_RES* word_res = it.word();
if (word_res != NULL) {
word_res->word->set_text(wordstr);
} else {
success = false;
}
// Check to see if text matches wordstr.
int w = 0;
int t = 0;
for (t = 0; text[t] != '\0'; ++t) {
if (text[t] == '\n' || text[t] == ' ')
continue;
while (wordstr[w] != '\0' && wordstr[w] == ' ')
++w;
if (text[t] != wordstr[w])
break;
++w;
}
if (text[t] != '\0' || wordstr[w] != '\0') {
// No match.
delete page_res_;
if (pr_it.word() == NULL)
success = false;
else
word_res = pr_it.word();
} else {
}
if (success) {
tesseract_->LearnWord(NULL, NULL, word_res);
}
delete [] text;
} else {
success = false;
}
SetPageSegMode(current_psm);
return success;
}
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 1362 of file baseapi.cpp.

{
if (tesseract_ == NULL ||
return NULL;
int n_word = 0;
for (res_it.restart_page(); res_it.word() != NULL; res_it.forward())
n_word++;
int* conf = new int[n_word+1];
n_word = 0;
for (res_it.restart_page(); res_it.word() != NULL; res_it.forward()) {
WERD_RES *word = res_it.word();
WERD_CHOICE* choice = word->best_choice;
int w_conf = static_cast<int>(100 + 5 * choice->certainty());
// This is the eq for converting Tesseract confidence to 1..100
if (w_conf < 0) w_conf = 0;
if (w_conf > 100) w_conf = 100;
conf[n_word++] = w_conf;
}
conf[n_word] = -1;
return conf;
}
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. Returns NULL on error. 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.

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

{
if (FindLines() == 0) {
if (block_list_->empty())
return NULL; // The page was empty.
return new PageIterator(
}
return NULL;
}
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 1460 of file baseapi.cpp.

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

{
delete block_list;
}
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 1756 of file baseapi.cpp.

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

{
if (tesseract_ == NULL)
return;
FILE *fp = fopen(filename, "wb");
Pix* pix = tesseract_->pix_binary();
int width = pixGetWidth(pix);
int height = pixGetHeight(pix);
l_uint32* data = pixGetData(pix);
fprintf(fp, "P5 %d %d 255\n", width, height);
for (int y = 0; y < height; ++y, data += pixGetWpl(pix)) {
for (int x = 0; x < width; ++x) {
uinT8 b = GET_DATA_BIT(data, x) ? 0 : 255;
fwrite(&b, 1, 1, fp);
}
}
fclose(fp);
}
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 1472 of file baseapi.cpp.

BLOCK_LIST * tesseract::TessBaseAPI::FindLinesCreateBlockList ( )

Find lines from the image making the BLOCK_LIST.

Definition at line 1833 of file baseapi.cpp.

{
BLOCK_LIST* result = block_list_;
return result;
}
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 2138 of file baseapi.cpp.

{
TBOX box(left, bottom, right, top);
BLOCK_IT b_it(blocks);
for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
BLOCK* block = b_it.data();
if (!box.major_overlap(block->bounding_box()))
continue;
ROW_IT r_it(block->row_list());
for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward()) {
ROW* row = r_it.data();
if (!box.major_overlap(row->bounding_box()))
continue;
WERD_IT w_it(row->word_list());
for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
WERD* word = w_it.data();
if (box.major_overlap(word->bounding_box()))
return row;
}
}
}
return NULL;
}
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 1785 of file baseapi.cpp.

{
delete[] *block_orientation;
*block_orientation = NULL;
delete[] *vertical_writing;
*vertical_writing = NULL;
BLOCK_IT block_it(block_list_);
block_it.move_to_first();
int num_blocks = 0;
for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
if (!block_it.data()->poly_block()->IsText()) {
continue;
}
++num_blocks;
}
if (!num_blocks) {
tprintf("WARNING: Found no blocks\n");
return;
}
*block_orientation = new int[num_blocks];
*vertical_writing = new bool[num_blocks];
block_it.move_to_first();
int i = 0;
for (block_it.mark_cycle_pt(); !block_it.cycled_list();
block_it.forward()) {
if (!block_it.data()->poly_block()->IsText()) {
continue;
}
FCOORD re_rotation = block_it.data()->re_rotation();
float re_theta = re_rotation.angle();
FCOORD classify_rotation = block_it.data()->classify_rotation();
float classify_theta = classify_rotation.angle();
double rot_theta = - (re_theta - classify_theta) * 2.0 / PI;
if (rot_theta < 0) rot_theta += 4;
int num_rotations = static_cast<int>(rot_theta + 0.5);
(*block_orientation)[i] = num_rotations;
// The classify_rotation is non-zero only if the text has vertical
// writing direction.
(*vertical_writing)[i] = classify_rotation.y() != 0.0f;
++i;
}
}
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. Returned string must be freed with the delete [] operator. 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.

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

Definition at line 1193 of file baseapi.cpp.

{
if (tesseract_ == NULL ||
return NULL;
int blob_count;
int utf8_length = TextLength(&blob_count);
int total_length = blob_count * kBytesPerBoxFileLine + utf8_length +
char* result = new char[total_length];
strcpy(result, "\0");
int output_length = 0;
LTRResultIterator* it = GetLTRIterator();
do {
int left, top, right, bottom;
if (it->BoundingBox(RIL_SYMBOL, &left, &top, &right, &bottom)) {
char* text = it->GetUTF8Text(RIL_SYMBOL);
// Tesseract uses space for recognition failure. Fix to a reject
// character, kTesseractReject so we don't create illegal box files.
for (int i = 0; text[i] != '\0'; ++i) {
if (text[i] == ' ')
text[i] = kTesseractReject;
}
snprintf(result + output_length, total_length - output_length,
"%s %d %d %d %d %d\n",
text, left, image_height_ - bottom,
right, image_height_ - top, page_number);
output_length += strlen(result + output_length);
delete [] text;
// Just in case...
if (output_length + kMaxBytesPerLine > total_length)
break;
}
} while (it->Next(RIL_SYMBOL));
delete it;
return result;
}
Boxa * tesseract::TessBaseAPI::GetComponentImages ( PageIteratorLevel  level,
bool  text_only,
Pixa **  pixa,
int **  blockids 
)

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

{
PageIterator* page_it = GetIterator();
if (page_it == NULL)
page_it = AnalyseLayout();
if (page_it == NULL)
return NULL; // Failed.
// Count the components to get a size for the arrays.
int component_count = 0;
int left, top, right, bottom;
do {
if (page_it->BoundingBoxInternal(level, &left, &top, &right, &bottom) &&
(!text_only || PTIsTextType(page_it->BlockType())))
++component_count;
} while (page_it->Next(level));
Boxa* boxa = boxaCreate(component_count);
if (pixa != NULL)
*pixa = pixaCreate(component_count);
if (blockids != NULL)
*blockids = new int[component_count];
int blockid = 0;
int component_index = 0;
page_it->Begin();
do {
if (page_it->BoundingBoxInternal(level, &left, &top, &right, &bottom) &&
(!text_only || PTIsTextType(page_it->BlockType()))) {
Box* lbox = boxCreate(left, top, right - left, bottom - top);
boxaAddBox(boxa, lbox, L_INSERT);
if (pixa != NULL) {
Pix* pix = page_it->GetBinaryImage(level);
pixaAddPix(*pixa, pix, L_INSERT);
pixaAddBox(*pixa, lbox, L_CLONE);
}
if (blockids != NULL) {
(*blockids)[component_index] = blockid;
if (page_it->IsAtFinalElement(RIL_BLOCK, level))
++blockid;
}
++component_index;
}
} while (page_it->Next(level));
delete page_it;
return boxa;
}
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 535 of file baseapi.cpp.

{
return GetComponentImages(RIL_SYMBOL, true, pixa, NULL);
}
CubeRecoContext * tesseract::TessBaseAPI::GetCubeRecoContext ( ) const

Return a pointer to underlying CubeRecoContext object if present.

Definition at line 2202 of file baseapi.cpp.

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

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

Definition at line 2191 of file baseapi.cpp.

{
if (tesseract_ == NULL || i >= NumDawgs()) return NULL;
return tesseract_->getDict().GetDawg(i);
}
void tesseract::TessBaseAPI::GetFeaturesForBlob ( TBLOB blob,
const DENORM denorm,
INT_FEATURE_ARRAY  int_features,
int *  num_features,
int *  FeatureOutlineIndex 
)

This method returns the features associated with the input image.

This method returns the features associated with the input blob.

Definition at line 2119 of file baseapi.cpp.

{
if (tesseract_) {
}
uinT8* norm_array = new uinT8[MAX_NUM_CLASSES];
inT32 len;
*num_features = tesseract_->GetCharNormFeatures(
int_features, norm_array, norm_array, &len, FeatureOutlineIndex);
delete [] norm_array;
}
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.

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.

Definition at line 1055 of file baseapi.cpp.

{
if (tesseract_ == NULL ||
(page_res_ == NULL && Recognize(NULL) < 0))
return NULL;
int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
int page_id = page_number + 1; // hOCR uses 1-based page numbers.
STRING hocr_str("");
if (input_file_ == NULL)
hocr_str.add_str_int(" <div class='ocr_page' id='page_", page_id);
hocr_str += "' title='image \"";
hocr_str += input_file_ ? *input_file_ : "unknown";
hocr_str.add_str_int("\"; bbox ", rect_left_);
hocr_str.add_str_int(" ", rect_top_);
hocr_str.add_str_int(" ", rect_width_);
hocr_str.add_str_int(" ", rect_height_);
hocr_str.add_str_int("; ppageno ", page_number);
hocr_str += "'>\n";
ResultIterator *res_it = GetIterator();
while (!res_it->Empty(RIL_BLOCK)) {
if (res_it->Empty(RIL_WORD)) {
res_it->Next(RIL_WORD);
continue;
}
// Open any new block/paragraph/textline.
if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
hocr_str.add_str_int(" <div class='ocr_carea' id='block_", bcnt);
hocr_str.add_str_int("_", bcnt);
AddBoxTohOCR(res_it, RIL_BLOCK, &hocr_str);
}
if (res_it->IsAtBeginningOf(RIL_PARA)) {
if (res_it->ParagraphIsLtr()) {
hocr_str.add_str_int("\n <p class='ocr_par' dir='ltr' id='par_", pcnt);
} else {
hocr_str.add_str_int("\n <p class='ocr_par' dir='rtl' id='par_", pcnt);
}
AddBoxTohOCR(res_it, RIL_PARA, &hocr_str);
}
if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
hocr_str.add_str_int("\n <span class='ocr_line' id='line_", lcnt);
AddBoxTohOCR(res_it, RIL_TEXTLINE, &hocr_str);
}
// Now, process the word...
hocr_str.add_str_int("<span class='ocrx_word' id='word_", wcnt);
AddBoxTohOCR(res_it, RIL_WORD, &hocr_str);
const char *font_name;
bool bold, italic, underlined, monospace, serif, smallcaps;
int pointsize, font_id;
font_name = res_it->WordFontAttributes(&bold, &italic, &underlined,
&monospace, &serif, &smallcaps,
&pointsize, &font_id);
bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
bool last_word_in_para = res_it->IsAtFinalElement(RIL_PARA, RIL_WORD);
bool last_word_in_block = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
if (bold) hocr_str += "<strong>";
if (italic) hocr_str += "<em>";
do {
const char *grapheme = res_it->GetUTF8Text(RIL_SYMBOL);
if (grapheme && grapheme[0] != 0) {
if (grapheme[1] == 0) {
switch (grapheme[0]) {
case '<': hocr_str += "&lt;"; break;
case '>': hocr_str += "&gt;"; break;
case '&': hocr_str += "&amp;"; break;
case '"': hocr_str += "&quot;"; break;
case '\'': hocr_str += "&#39;"; break;
default: hocr_str += grapheme;
}
} else {
hocr_str += grapheme;
}
}
delete []grapheme;
res_it->Next(RIL_SYMBOL);
} while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
if (italic) hocr_str += "</em>";
if (bold) hocr_str += "</strong>";
hocr_str += "</span> ";
wcnt++;
// Close any ending block/paragraph/textline.
if (last_word_in_line) {
hocr_str += "\n </span>";
lcnt++;
}
if (last_word_in_para) {
hocr_str += "\n </p>\n";
pcnt++;
}
if (last_word_in_block) {
hocr_str += " </div>\n";
bcnt++;
}
}
hocr_str += " </div>\n";
char *ret = new char[hocr_str.length() + 1];
strcpy(ret, hocr_str.string());
delete res_it;
return ret;
}
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 990 of file baseapi.cpp.

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

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

{
return GetComponentImages(RIL_BLOCK, false, pixa, NULL);
}
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 516 of file baseapi.cpp.

{
return GetComponentImages(RIL_TEXTLINE, false, pixa, blockids);
}
bool tesseract::TessBaseAPI::GetTextDirection ( int *  out_offset,
float *  out_slope 
)

Definition at line 1531 of file baseapi.cpp.

{
if (page_res_ == NULL)
if (block_list_->length() < 1) {
return false;
}
// Get first block
BLOCK_IT block_it(block_list_);
block_it.move_to_first();
ROW_LIST* rows = block_it.data()->row_list();
if (rows->length() < 1) {
return false;
}
// Get first line of block
ROW_IT row_it(rows);
row_it.move_to_first();
ROW* row = row_it.data();
// Calculate offset and slope (NOTE: Kind of ugly)
*out_offset = static_cast<int>(row->base_line(0.0));
*out_slope = row->base_line(1.0) - row->base_line(0.0);
return true;
}
Boxa * tesseract::TessBaseAPI::GetTextlines ( Pixa **  pixa,
int **  blockids 
)

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.

Definition at line 504 of file baseapi.cpp.

{
return GetComponentImages(RIL_TEXTLINE, true, pixa, blockids);
}
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 if you have Leptonica installed. Get a copy of the internal thresholded image from Tesseract.

Definition at line 481 of file baseapi.cpp.

{
if (tesseract_ == NULL)
return NULL;
return pixClone(tesseract_->pix_binary());
}
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 596 of file baseapi.cpp.

{
if (thresholder_ == NULL) {
return 0;
}
}
const char * tesseract::TessBaseAPI::GetUnichar ( int  unichar_id)

This method returns the string form of the specified unichar.

Definition at line 2186 of file baseapi.cpp.

{
return tesseract_->unicharset.id_to_unichar(unichar_id);
}
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 and must be freed with the delete [] operator.

Definition at line 1248 of file baseapi.cpp.

{
if (tesseract_ == NULL ||
return NULL;
bool tilde_crunch_written = false;
bool last_char_was_newline = true;
bool last_char_was_tilde = false;
int total_length = TextLength(NULL);
PAGE_RES_IT page_res_it(page_res_);
char* result = new char[total_length];
char* ptr = result;
for (page_res_it.restart_page(); page_res_it.word () != NULL;
page_res_it.forward()) {
WERD_RES *word = page_res_it.word();
// Process the current word.
if (word->unlv_crunch_mode != CR_NONE) {
if (word->unlv_crunch_mode != CR_DELETE &&
(!tilde_crunch_written ||
word->word->space() > 0 &&
!word->word->flag(W_FUZZY_NON) &&
!word->word->flag(W_FUZZY_SP)))) {
if (!word->word->flag(W_BOL) &&
word->word->space() > 0 &&
!word->word->flag(W_FUZZY_NON) &&
!word->word->flag(W_FUZZY_SP)) {
/* Write a space to separate from preceeding good text */
*ptr++ = ' ';
last_char_was_tilde = false;
}
if (!last_char_was_tilde) {
// Write a reject char.
last_char_was_tilde = true;
*ptr++ = kUNLVReject;
tilde_crunch_written = true;
last_char_was_newline = false;
}
}
} else {
// NORMAL PROCESSING of non tilde crunched words.
tilde_crunch_written = false;
const char* wordstr = word->best_choice->unichar_string().string();
const STRING& lengths = word->best_choice->unichar_lengths();
int length = lengths.length();
int i = 0;
int offset = 0;
if (last_char_was_tilde &&
word->word->space() == 0 && wordstr[offset] == ' ') {
// Prevent adjacent tilde across words - we know that adjacent tildes
// within words have been removed.
// Skip the first character.
offset = lengths[i++];
}
if (i < length && wordstr[offset] != 0) {
if (!last_char_was_newline)
*ptr++ = ' ';
else
last_char_was_newline = false;
for (; i < length; offset += lengths[i++]) {
if (wordstr[offset] == ' ' ||
wordstr[offset] == kTesseractReject) {
*ptr++ = kUNLVReject;
last_char_was_tilde = true;
} else {
if (word->reject_map[i].rejected())
*ptr++ = kUNLVSuspect;
UNICHAR ch(wordstr + offset, lengths[i]);
int uni_ch = ch.first_uni();
for (int j = 0; kUniChs[j] != 0; ++j) {
if (kUniChs[j] == uni_ch) {
uni_ch = kLatinChs[j];
break;
}
}
if (uni_ch <= 0xff) {
*ptr++ = static_cast<char>(uni_ch);
last_char_was_tilde = false;
} else {
*ptr++ = kUNLVReject;
last_char_was_tilde = true;
}
}
}
}
}
if (word->word->flag(W_EOL) && !last_char_was_newline) {
/* Add a new line output */
*ptr++ = '\n';
tilde_crunch_written = false;
last_char_was_newline = true;
last_char_was_tilde = false;
}
}
*ptr++ = '\n';
*ptr = '\0';
return result;
}
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 1017 of file baseapi.cpp.

{
if (tesseract_ == NULL ||
return NULL;
STRING text("");
ResultIterator *it = GetIterator();
do {
if (it->Empty(RIL_PARA)) continue;
char *para_text = it->GetUTF8Text(RIL_PARA);
text += para_text;
delete []para_text;
} while (it->Next(RIL_PARA));
char* result = new char[text.length() + 1];
strncpy(result, text.string(), text.length() + 1);
delete it;
return result;
}
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 525 of file baseapi.cpp.

{
return GetComponentImages(RIL_WORD, true, pixa, NULL);
}
void tesseract::TessBaseAPI::InitTruthCallback ( TruthCallback cb)
inline

Definition at line 661 of file baseapi.h.

{ truth_cb_ = cb; }
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 1526 of file baseapi.cpp.

{
return tesseract_->getDict().valid_word(word);
}
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 1867 of file baseapi.cpp.

{
int width = pixGetWidth(pix);
int height = pixGetHeight(pix);
BLOCK block("a character", TRUE, 0, 0, 0, 0, width, height);
// Create C_BLOBs from the page
extract_edges(pix, &block);
// Merge all C_BLOBs
C_BLOB_LIST *list = block.blob_list();
C_BLOB_IT c_blob_it(list);
if (c_blob_it.empty())
return NULL;
// Move all the outlines to the first blob.
C_OUTLINE_IT ol_it(c_blob_it.data()->out_list());
for (c_blob_it.forward();
!c_blob_it.at_first();
c_blob_it.forward()) {
C_BLOB *c_blob = c_blob_it.data();
ol_it.add_list_after(c_blob->out_list());
}
// Convert the first blob to the output TBLOB.
return TBLOB::PolygonalCopy(c_blob_it.data());
}
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 1850 of file baseapi.cpp.

{
inT32 xstarts[] = {-32000};
double quad_coeffs[] = {0, 0, baseline};
return new ROW(1,
xstarts,
quad_coeffs,
xheight,
ascender - (baseline + xheight),
descender - baseline,
0,
0);
}
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 1350 of file baseapi.cpp.

{
int* conf = AllWordConfidences();
if (!conf) return 0;
int sum = 0;
int *pt = conf;
while (*pt >= 0) sum += *pt++;
if (pt != conf) sum /= pt - conf;
delete [] conf;
return sum;
}
void tesseract::TessBaseAPI::NormalizeTBLOB ( TBLOB tblob,
ROW row,
bool  numeric_mode,
DENORM denorm 
)
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 1897 of file baseapi.cpp.

{
TWERD word;
word.blobs = tblob;
if (denorm != NULL) {
word.SetupBLNormalize(NULL, row, row->x_height(), numeric_mode, denorm);
word.Normalize(*denorm);
} else {
DENORM normer;
word.SetupBLNormalize(NULL, row, row->x_height(), numeric_mode, &normer);
word.Normalize(normer);
}
word.blobs = NULL;
}
int tesseract::TessBaseAPI::NumDawgs ( ) const

Return the number of dawgs loaded into tesseract_ object.

Definition at line 2197 of file baseapi.cpp.

{
return tesseract_ == NULL ? 0 : tesseract_->getDict().NumDawgs();
}
OcrEngineMode const tesseract::TessBaseAPI::oem ( ) const
inline

Definition at line 657 of file baseapi.h.

{
}
bool tesseract::TessBaseAPI::ProcessPage ( Pix *  pix,
int  page_index,
const char *  filename,
const char *  retry_config,
int  timeout_millisec,
STRING text_out 
)

Recognizes a single page for ProcessPages, appending the text to text_out. 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. If non-zero timeout_millisec terminates processing after the timeout. If non-NULL and non-empty, and some page fails for some reason, the page is reprocessed with the retry_config config file. Useful for interactively debugging a bad page. The text is returned in text_out. Returns false on error.

Definition at line 905 of file baseapi.cpp.

{
SetImage(pix);
bool failed = false;
if (timeout_millisec > 0) {
// Running with a timeout.
ETEXT_DESC monitor;
monitor.cancel = NULL;
monitor.cancel_this = NULL;
monitor.set_deadline_msecs(timeout_millisec);
// Now run the main recognition.
failed = Recognize(&monitor) < 0;
// Disabled character recognition.
PageIterator* it = AnalyseLayout();
if (it == NULL) {
failed = true;
} else {
delete it;
return true;
}
} else {
// Normal layout and character recognition with no timeout.
failed = Recognize(NULL) < 0;
}
Pix* page_pix = GetThresholdedImage();
pixWrite("tessinput.tif", page_pix, IFF_TIFF_G4);
}
if (failed && retry_config != NULL && retry_config[0] != '\0') {
// Save current config variables before switching modes.
FILE* fp = fopen(kOldVarsFile, "wb");
fclose(fp);
// Switch to alternate mode for retry.
ReadConfigFile(retry_config);
SetImage(pix);
// Restore saved config variables.
}
// Get text only if successful.
if (!failed) {
char* text;
text = GetBoxText(page_index);
text = GetUNLVText();
text = GetHOCRText(page_index);
} else {
text = GetUTF8Text();
}
*text_out += text;
delete [] text;
return true;
}
return false;
}
bool tesseract::TessBaseAPI::ProcessPages ( const char *  filename,
const char *  retry_config,
int  timeout_millisec,
STRING text_out 
)

Recognizes all the pages in the named file, as a multi-page tiff or list of filenames, or single image, and gets the appropriate kind of text according to parameters: tessedit_create_boxfile, tessedit_make_boxes_from_boxes, tessedit_write_unlv, tessedit_create_hocr. Calls ProcessPage on each page in the input file, which may be a multi-page tiff, single-page other file format, or a plain text list of images to read. If tessedit_page_number is non-negative, processing begins at that page of a multi-page tiff file, or filelist. The text is returned in text_out. Returns false on error. If non-zero timeout_millisec terminates processing after the timeout on a single page. If non-NULL and non-empty, and some page fails for some reason, the page is reprocessed with the retry_config config file. Useful for interactively debugging a bad page.

Definition at line 803 of file baseapi.cpp.

{
if (page < 0)
page = 0;
FILE* fp = fopen(filename, "rb");
if (fp == NULL) {
tprintf(_("Image file %s cannot be opened!\n"), filename);
return false;
}
// Find the number of pages if a tiff file, or zero otherwise.
int npages = CountTiffPages(fp);
fclose(fp);
*text_out =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" "
"lang=\"en\">\n <head>\n <title></title>\n"
" <meta http-equiv=\"Content-Type\" content=\"text/html; "
"charset=utf-8\" />\n"
" <meta name='ocr-system' content='tesseract " VERSION "' />\n"
" <meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par"
" ocr_line ocrx_word'/>\n"
" </head>\n <body>\n";
} else {
*text_out = "";
}
bool success = true;
Pix *pix;
if (npages > 0) {
for (; page < npages && (pix = pixReadTiff(filename, page)) != NULL;
++page) {
if ((page >= 0) && (npages > 1))
tprintf(_("Page %d of %d\n"), page + 1, npages);
char page_str[kMaxIntSize];
snprintf(page_str, kMaxIntSize - 1, "%d", page);
SetVariable("applybox_page", page_str);
success &= ProcessPage(pix, page, filename, retry_config,
timeout_millisec, text_out);
pixDestroy(&pix);
if (tesseract_->tessedit_page_number >= 0 || npages == 1) {
break;
}
}
} else {
// The file is not a tiff file, so use the general pixRead function.
pix = pixRead(filename);
if (pix != NULL) {
success &= ProcessPage(pix, 0, filename, retry_config,
timeout_millisec, text_out);
pixDestroy(&pix);
} else {
// The file is not an image file, so try it as a list of filenames.
FILE* fimg = fopen(filename, "rb");
if (fimg == NULL) {
tprintf(_("File %s cannot be opened!\n"), filename);
return false;
}
tprintf(_("Reading %s as a list of filenames...\n"), filename);
char pagename[MAX_PATH];
// Skip to the requested page number.
for (int i = 0; i < page &&
fgets(pagename, sizeof(pagename), fimg) != NULL;
++i);
while (fgets(pagename, sizeof(pagename), fimg) != NULL) {
chomp_string(pagename);
pix = pixRead(pagename);
if (pix == NULL) {
tprintf(_("Image file %s cannot be read!\n"), pagename);
fclose(fimg);
return false;
}
tprintf(_("Page %d : %s\n"), page, pagename);
success &= ProcessPage(pix, page, pagename, retry_config,
timeout_millisec, text_out);
pixDestroy(&pix);
++page;
}
fclose(fimg);
}
}
*text_out += " </body>\n</html>\n";
return success;
}
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 696 of file baseapi.cpp.

{
if (tesseract_ == NULL)
return -1;
if (FindLines() != 0)
return -1;
if (page_res_ != NULL)
delete page_res_;
if (block_list_->empty()) {
return 0; // Empty page.
}
else
return 0;
}
if (truth_cb_ != NULL) {
tesseract_->wordrec_run_blamer.set_value(true);
}
int result = 0;
#ifndef GRAPHICS_DISABLED
#endif // GRAPHICS_DISABLED
// The page_res is invalid after an interactive session, so cleanup
// in a way that lets us continue to the next page without crashing.
delete page_res_;
return -1;
FILE *training_output_file = tesseract_->init_recog_training(*input_file_);
// OCR the page segmented into words by tesseract.
*input_file_, page_res_, monitor, training_output_file);
fclose(training_output_file);
} else {
// Now run the main recognition.
if (tesseract_->recog_all_words(page_res_, monitor, NULL, NULL, 0)) {
} else {
result = -1;
}
}
return result;
}
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 757 of file baseapi.cpp.

{
if (tesseract_ == NULL)
return -1;
tprintf("Please call SetImage before attempting recognition.");
return -1;
}
if (page_res_ != NULL)
if (FindLines() != 0)
return -1;
// Additional conditions under which chopper test cannot be run
PAGE_RES_IT page_res_it(page_res_);
while (page_res_it.word() != NULL) {
WERD_RES *word_res = page_res_it.word();
tesseract_->MaximallyChopWord(boxes, page_res_it.block()->block,
page_res_it.row()->row, word_res);
page_res_it.forward();
}
return 0;
}
void tesseract::TessBaseAPI::RunAdaptiveClassifier ( TBLOB blob,
const DENORM denorm,
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 2163 of file baseapi.cpp.

{
BLOB_CHOICE_LIST* choices = new BLOB_CHOICE_LIST;
tesseract_->AdaptiveClassifier(blob, denorm, choices, NULL);
BLOB_CHOICE_IT choices_it(choices);
int& index = *num_matches_returned;
index = 0;
for (choices_it.mark_cycle_pt();
!choices_it.cycled_list() && index < num_max_matches;
choices_it.forward()) {
BLOB_CHOICE* choice = choices_it.data();
unichar_ids[index] = choice->unichar_id();
ratings[index] = choice->rating();
++index;
}
*num_matches_returned = index;
delete choices;
}
void tesseract::TessBaseAPI::set_min_orientation_margin ( double  margin)

Definition at line 1767 of file baseapi.cpp.

{
tesseract_->min_orientation_margin.set_value(margin);
}
void tesseract::TessBaseAPI::SetDictFunc ( DictFunc  f)

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

Definition at line 1559 of file baseapi.cpp.

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

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

Definition at line 1581 of file baseapi.cpp.

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. Does not copy the image buffer, or take ownership. The source image may be destroyed after Recognize is called, either explicitly or implicitly via one of the Get*Text functions. 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 435 of file baseapi.cpp.

{
thresholder_->SetImage(imagedata, width, height,
bytes_per_pixel, bytes_per_line);
}
void tesseract::TessBaseAPI::SetImage ( const Pix *  pix)

Provide an image for Tesseract to recognize. As with SetImage above, Tesseract doesn't take a copy or ownership or pixDestroy the image, so it must persist until after Recognize. Pix vs raw, which to use? Use Pix where possible. A future version of Tesseract may choose to use Pix as its internal representation and discard IMAGE altogether. Because of that, an implementation that sources and targets Pix may end up with less copies than an implementation that does not.

Definition at line 460 of file baseapi.cpp.

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

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

Definition at line 1569 of file baseapi.cpp.

{
if (tesseract_ != NULL) {
// Set it for the sublangs too.
int num_subs = tesseract_->num_sub_langs();
for (int i = 0; i < num_subs; ++i) {
}
}
}
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 470 of file baseapi.cpp.

{
return;
thresholder_->SetRectangle(left, top, width, height);
}
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 443 of file baseapi.cpp.

{
else
tprintf("Please call SetImage before SetSourceResolution.\n");
}
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 348 of file baseapi.h.

{
delete thresholder_;
thresholder_ = thresholder;
}
Tesseract* const tesseract::TessBaseAPI::tesseract ( ) const
inline

Definition at line 653 of file baseapi.h.

{
return tesseract_;
}