tesseract  3.04.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
baseapi.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: baseapi.cpp
3  * Description: Simple API for calling tesseract.
4  * Author: Ray Smith
5  * Created: Fri Oct 06 15:35:01 PDT 2006
6  *
7  * (C) Copyright 2006, Google Inc.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 // Include automatically generated configuration file if running autoconf.
21 #ifdef HAVE_CONFIG_H
22 #include "config_auto.h"
23 #endif
24 
25 #ifdef __linux__
26 #include <signal.h>
27 #endif
28 
29 #if defined(_WIN32)
30 #ifdef _MSC_VER
31 #include "vcsversion.h"
32 #include "mathfix.h"
33 #elif MINGW
34 // workaround for stdlib.h with -std=c++11 for _splitpath and _MAX_FNAME
35 #undef __STRICT_ANSI__
36 #endif // _MSC_VER
37 #include <stdlib.h>
38 #include <windows.h>
39 #include <fcntl.h>
40 #include <io.h>
41 #else
42 #include <dirent.h>
43 #include <libgen.h>
44 #include <string.h>
45 #endif // _WIN32
46 
47 #include <iostream>
48 #include <string>
49 #include <iterator>
50 #include <fstream>
51 
52 #include "allheaders.h"
53 
54 #include "baseapi.h"
55 #include "blobclass.h"
56 #include "resultiterator.h"
57 #include "mutableiterator.h"
58 #include "thresholder.h"
59 #include "tesseractclass.h"
60 #include "pageres.h"
61 #include "paragraphs.h"
62 #include "tessvars.h"
63 #include "control.h"
64 #include "dict.h"
65 #include "pgedit.h"
66 #include "paramsd.h"
67 #include "output.h"
68 #include "globaloc.h"
69 #include "globals.h"
70 #include "edgblob.h"
71 #include "equationdetect.h"
72 #include "tessbox.h"
73 #include "makerow.h"
74 #include "otsuthr.h"
75 #include "osdetect.h"
76 #include "params.h"
77 #include "renderer.h"
78 #include "strngs.h"
79 #include "openclwrapper.h"
80 
81 BOOL_VAR(stream_filelist, FALSE, "Stream a filelist from stdin");
82 
83 namespace tesseract {
84 
86 const int kMinRectSize = 10;
88 const char kTesseractReject = '~';
90 const char kUNLVReject = '~';
92 const char kUNLVSuspect = '^';
97 const char* kInputFile = "noname.tif";
101 const char* kOldVarsFile = "failed_vars.txt";
103 const int kMaxIntSize = 22;
108 const int kMinCredibleResolution = 70;
110 const int kMaxCredibleResolution = 2400;
111 
113  : tesseract_(NULL),
114  osd_tesseract_(NULL),
115  equ_detect_(NULL),
116  // Thresholder is initialized to NULL here, but will be set before use by:
117  // A constructor of a derived API, SetThresholder(), or
118  // created implicitly when used in InternalSetImage.
119  thresholder_(NULL),
120  paragraph_models_(NULL),
121  block_list_(NULL),
122  page_res_(NULL),
123  input_file_(NULL),
124  input_image_(NULL),
125  output_file_(NULL),
126  datapath_(NULL),
127  language_(NULL),
128  last_oem_requested_(OEM_DEFAULT),
129  recognition_done_(false),
130  truth_cb_(NULL),
131  rect_left_(0), rect_top_(0), rect_width_(0), rect_height_(0),
132  image_width_(0), image_height_(0) {
133 }
134 
136  End();
137 }
138 
142 const char* TessBaseAPI::Version() {
143 #if defined(GIT_REV) && (defined(DEBUG) || defined(_DEBUG))
144  return GIT_REV;
145 #else
146  return TESSERACT_VERSION_STR;
147 #endif
148 }
149 
157 #ifdef USE_OPENCL
158 #if USE_DEVICE_SELECTION
159 #include "opencl_device_selection.h"
160 #endif
161 #endif
162 size_t TessBaseAPI::getOpenCLDevice(void **data) {
163 #ifdef USE_OPENCL
164 #if USE_DEVICE_SELECTION
165  ds_device device = OpenclDevice::getDeviceSelection();
166  if (device.type == DS_DEVICE_OPENCL_DEVICE) {
167  *data = reinterpret_cast<void*>(new cl_device_id);
168  memcpy(*data, &device.oclDeviceID, sizeof(cl_device_id));
169  return sizeof(cl_device_id);
170  }
171 #endif
172 #endif
173 
174  *data = NULL;
175  return 0;
176 }
177 
183 #ifdef __linux__
184  struct sigaction action;
185  memset(&action, 0, sizeof(action));
186  action.sa_handler = &signal_exit;
187  action.sa_flags = SA_RESETHAND;
188  sigaction(SIGSEGV, &action, NULL);
189  sigaction(SIGFPE, &action, NULL);
190  sigaction(SIGBUS, &action, NULL);
191 #else
192  // Warn API users that an implementation is needed.
193  tprintf("CatchSignals has no non-linux implementation!\n");
194 #endif
195 }
196 
201 void TessBaseAPI::SetInputName(const char* name) {
202  if (input_file_ == NULL)
203  input_file_ = new STRING(name);
204  else
205  *input_file_ = name;
206 }
207 
209 void TessBaseAPI::SetOutputName(const char* name) {
210  if (output_file_ == NULL)
211  output_file_ = new STRING(name);
212  else
213  *output_file_ = name;
214 }
215 
216 bool TessBaseAPI::SetVariable(const char* name, const char* value) {
217  if (tesseract_ == NULL) tesseract_ = new Tesseract;
219  tesseract_->params());
220 }
221 
222 bool TessBaseAPI::SetDebugVariable(const char* name, const char* value) {
223  if (tesseract_ == NULL) tesseract_ = new Tesseract;
225  tesseract_->params());
226 }
227 
228 bool TessBaseAPI::GetIntVariable(const char *name, int *value) const {
229  IntParam *p = ParamUtils::FindParam<IntParam>(
231  if (p == NULL) return false;
232  *value = (inT32)(*p);
233  return true;
234 }
235 
236 bool TessBaseAPI::GetBoolVariable(const char *name, bool *value) const {
237  BoolParam *p = ParamUtils::FindParam<BoolParam>(
239  if (p == NULL) return false;
240  *value = (BOOL8)(*p);
241  return true;
242 }
243 
244 const char *TessBaseAPI::GetStringVariable(const char *name) const {
245  StringParam *p = ParamUtils::FindParam<StringParam>(
247  return (p != NULL) ? p->string() : NULL;
248 }
249 
250 bool TessBaseAPI::GetDoubleVariable(const char *name, double *value) const {
251  DoubleParam *p = ParamUtils::FindParam<DoubleParam>(
253  if (p == NULL) return false;
254  *value = (double)(*p);
255  return true;
256 }
257 
260  return ParamUtils::GetParamAsString(name, tesseract_->params(), val);
261 }
262 
264 void TessBaseAPI::PrintVariables(FILE *fp) const {
266 }
267 
276 int TessBaseAPI::Init(const char* datapath, const char* language,
277  OcrEngineMode oem, char **configs, int configs_size,
278  const GenericVector<STRING> *vars_vec,
279  const GenericVector<STRING> *vars_values,
280  bool set_only_non_debug_params) {
281  PERF_COUNT_START("TessBaseAPI::Init")
282  // Default language is "eng".
283  if (language == NULL) language = "eng";
284  // If the datapath, OcrEngineMode or the language have changed - start again.
285  // Note that the language_ field stores the last requested language that was
286  // initialized successfully, while tesseract_->lang stores the language
287  // actually used. They differ only if the requested language was NULL, in
288  // which case tesseract_->lang is set to the Tesseract default ("eng").
289  if (tesseract_ != NULL &&
290  (datapath_ == NULL || language_ == NULL ||
291  *datapath_ != datapath || last_oem_requested_ != oem ||
292  (*language_ != language && tesseract_->lang != language))) {
293  delete tesseract_;
294  tesseract_ = NULL;
295  }
296  // PERF_COUNT_SUB("delete tesseract_")
297 #ifdef USE_OPENCL
298  OpenclDevice od;
299  od.InitEnv();
300 #endif
301  PERF_COUNT_SUB("OD::InitEnv()")
302  bool reset_classifier = true;
303  if (tesseract_ == NULL) {
304  reset_classifier = false;
305  tesseract_ = new Tesseract;
307  datapath, output_file_ != NULL ? output_file_->string() : NULL,
308  language, oem, configs, configs_size, vars_vec, vars_values,
309  set_only_non_debug_params) != 0) {
310  return -1;
311  }
312  }
313  PERF_COUNT_SUB("update tesseract_")
314  // Update datapath and language requested for the last valid initialization.
315  if (datapath_ == NULL)
316  datapath_ = new STRING(datapath);
317  else
318  *datapath_ = datapath;
319  if ((strcmp(datapath_->string(), "") == 0) &&
320  (strcmp(tesseract_->datadir.string(), "") != 0))
322 
323  if (language_ == NULL)
324  language_ = new STRING(language);
325  else
326  *language_ = language;
328  // PERF_COUNT_SUB("update last_oem_requested_")
329  // For same language and datapath, just reset the adaptive classifier.
330  if (reset_classifier) {
332  PERF_COUNT_SUB("tesseract_->ResetAdaptiveClassifier()")
333  }
335  return 0;
336 }
337 
347  return (language_ == NULL || language_->string() == NULL) ?
348  "" : language_->string();
349 }
350 
357  GenericVector<STRING>* langs) const {
358  langs->clear();
359  if (tesseract_ != NULL) {
360  langs->push_back(tesseract_->lang);
361  int num_subs = tesseract_->num_sub_langs();
362  for (int i = 0; i < num_subs; ++i)
363  langs->push_back(tesseract_->get_sub_lang(i)->lang);
364  }
365 }
366 
371  GenericVector<STRING>* langs) const {
372  langs->clear();
373  if (tesseract_ != NULL) {
374 #ifdef _WIN32
375  STRING pattern = tesseract_->datadir + "/*." + kTrainedDataSuffix;
376  char fname[_MAX_FNAME];
377  WIN32_FIND_DATA data;
378  BOOL result = TRUE;
379  HANDLE handle = FindFirstFile(pattern.string(), &data);
380  if (handle != INVALID_HANDLE_VALUE) {
381  for (; result; result = FindNextFile(handle, &data)) {
382  _splitpath(data.cFileName, NULL, NULL, fname, NULL);
383  langs->push_back(STRING(fname));
384  }
385  FindClose(handle);
386  }
387 #else // _WIN32
388  DIR *dir;
389  struct dirent *dirent;
390  char *dot;
391 
392  STRING extension = STRING(".") + kTrainedDataSuffix;
393 
394  dir = opendir(tesseract_->datadir.string());
395  if (dir != NULL) {
396  while ((dirent = readdir(dir))) {
397  // Skip '.', '..', and hidden files
398  if (dirent->d_name[0] != '.') {
399  if (strstr(dirent->d_name, extension.string()) != NULL) {
400  dot = strrchr(dirent->d_name, '.');
401  // This ensures that .traineddata is at the end of the file name
402  if (strncmp(dot, extension.string(),
403  strlen(extension.string())) == 0) {
404  *dot = '\0';
405  langs->push_back(STRING(dirent->d_name));
406  }
407  }
408  }
409  }
410  closedir(dir);
411  }
412 #endif
413  }
414 }
415 
422 int TessBaseAPI::InitLangMod(const char* datapath, const char* language) {
423  if (tesseract_ == NULL)
424  tesseract_ = new Tesseract;
425  else
427  return tesseract_->init_tesseract_lm(datapath, NULL, language);
428 }
429 
435  if (tesseract_ == NULL) {
436  tesseract_ = new Tesseract;
438  }
439 }
440 
448 }
449 
453 }
454 
461  if (tesseract_ == NULL)
462  tesseract_ = new Tesseract;
463  tesseract_->tessedit_pageseg_mode.set_value(mode);
464 }
465 
468  if (tesseract_ == NULL)
469  return PSM_SINGLE_BLOCK;
470  return static_cast<PageSegMode>(
471  static_cast<int>(tesseract_->tessedit_pageseg_mode));
472 }
473 
487 char* TessBaseAPI::TesseractRect(const unsigned char* imagedata,
488  int bytes_per_pixel,
489  int bytes_per_line,
490  int left, int top,
491  int width, int height) {
492  if (tesseract_ == NULL || width < kMinRectSize || height < kMinRectSize)
493  return NULL; // Nothing worth doing.
494 
495  // Since this original api didn't give the exact size of the image,
496  // we have to invent a reasonable value.
497  int bits_per_pixel = bytes_per_pixel == 0 ? 1 : bytes_per_pixel * 8;
498  SetImage(imagedata, bytes_per_line * 8 / bits_per_pixel, height + top,
499  bytes_per_pixel, bytes_per_line);
500  SetRectangle(left, top, width, height);
501 
502  return GetUTF8Text();
503 }
504 
510  if (tesseract_ == NULL)
511  return;
514 }
515 
525 void TessBaseAPI::SetImage(const unsigned char* imagedata,
526  int width, int height,
527  int bytes_per_pixel, int bytes_per_line) {
528  if (InternalSetImage())
529  thresholder_->SetImage(imagedata, width, height,
530  bytes_per_pixel, bytes_per_line);
531 }
532 
534  if (thresholder_)
536  else
537  tprintf("Please call SetImage before SetSourceResolution.\n");
538 }
539 
550 void TessBaseAPI::SetImage(Pix* pix) {
551  if (InternalSetImage())
552  thresholder_->SetImage(pix);
553  SetInputImage(pix);
554 }
555 
561 void TessBaseAPI::SetRectangle(int left, int top, int width, int height) {
562  if (thresholder_ == NULL)
563  return;
564  thresholder_->SetRectangle(left, top, width, height);
565  ClearResults();
566 }
567 
573  if (tesseract_ == NULL || thresholder_ == NULL)
574  return NULL;
575  if (tesseract_->pix_binary() == NULL)
577  return pixClone(tesseract_->pix_binary());
578 }
579 
585 Boxa* TessBaseAPI::GetRegions(Pixa** pixa) {
586  return GetComponentImages(RIL_BLOCK, false, pixa, NULL);
587 }
588 
597 Boxa* TessBaseAPI::GetTextlines(const bool raw_image, const int raw_padding,
598  Pixa** pixa, int** blockids, int** paraids) {
599  return GetComponentImages(RIL_TEXTLINE, true, raw_image, raw_padding,
600  pixa, blockids, paraids);
601 }
602 
611 Boxa* TessBaseAPI::GetStrips(Pixa** pixa, int** blockids) {
612  return GetComponentImages(RIL_TEXTLINE, false, pixa, blockids);
613 }
614 
620 Boxa* TessBaseAPI::GetWords(Pixa** pixa) {
621  return GetComponentImages(RIL_WORD, true, pixa, NULL);
622 }
623 
631  return GetComponentImages(RIL_SYMBOL, true, pixa, NULL);
632 }
633 
643  bool text_only, bool raw_image,
644  const int raw_padding,
645  Pixa** pixa, int** blockids,
646  int** paraids) {
647  PageIterator* page_it = GetIterator();
648  if (page_it == NULL)
649  page_it = AnalyseLayout();
650  if (page_it == NULL)
651  return NULL; // Failed.
652 
653  // Count the components to get a size for the arrays.
654  int component_count = 0;
655  int left, top, right, bottom;
656 
657  TessResultCallback<bool>* get_bbox = NULL;
658  if (raw_image) {
659  // Get bounding box in original raw image with padding.
661  level, raw_padding,
662  &left, &top, &right, &bottom);
663  } else {
664  // Get bounding box from binarized imaged. Note that this could be
665  // differently scaled from the original image.
666  get_bbox = NewPermanentTessCallback(page_it,
668  level, &left, &top, &right, &bottom);
669  }
670  do {
671  if (get_bbox->Run() &&
672  (!text_only || PTIsTextType(page_it->BlockType())))
673  ++component_count;
674  } while (page_it->Next(level));
675 
676  Boxa* boxa = boxaCreate(component_count);
677  if (pixa != NULL)
678  *pixa = pixaCreate(component_count);
679  if (blockids != NULL)
680  *blockids = new int[component_count];
681  if (paraids != NULL)
682  *paraids = new int[component_count];
683 
684  int blockid = 0;
685  int paraid = 0;
686  int component_index = 0;
687  page_it->Begin();
688  do {
689  if (get_bbox->Run() &&
690  (!text_only || PTIsTextType(page_it->BlockType()))) {
691  Box* lbox = boxCreate(left, top, right - left, bottom - top);
692  boxaAddBox(boxa, lbox, L_INSERT);
693  if (pixa != NULL) {
694  Pix* pix = NULL;
695  if (raw_image) {
696  pix = page_it->GetImage(level, raw_padding, input_image_,
697  &left, &top);
698  } else {
699  pix = page_it->GetBinaryImage(level);
700  }
701  pixaAddPix(*pixa, pix, L_INSERT);
702  pixaAddBox(*pixa, lbox, L_CLONE);
703  }
704  if (paraids != NULL) {
705  (*paraids)[component_index] = paraid;
706  if (page_it->IsAtFinalElement(RIL_PARA, level))
707  ++paraid;
708  }
709  if (blockids != NULL) {
710  (*blockids)[component_index] = blockid;
711  if (page_it->IsAtFinalElement(RIL_BLOCK, level)) {
712  ++blockid;
713  paraid = 0;
714  }
715  }
716  ++component_index;
717  }
718  } while (page_it->Next(level));
719  delete page_it;
720  delete get_bbox;
721  return boxa;
722 }
723 
725  if (thresholder_ == NULL) {
726  return 0;
727  }
728  return thresholder_->GetScaleFactor();
729 }
730 
732 void TessBaseAPI::DumpPGM(const char* filename) {
733  if (tesseract_ == NULL)
734  return;
735  FILE *fp = fopen(filename, "wb");
736  Pix* pix = tesseract_->pix_binary();
737  int width = pixGetWidth(pix);
738  int height = pixGetHeight(pix);
739  l_uint32* data = pixGetData(pix);
740  fprintf(fp, "P5 %d %d 255\n", width, height);
741  for (int y = 0; y < height; ++y, data += pixGetWpl(pix)) {
742  for (int x = 0; x < width; ++x) {
743  uinT8 b = GET_DATA_BIT(data, x) ? 0 : 255;
744  fwrite(&b, 1, 1, fp);
745  }
746  }
747  fclose(fp);
748 }
749 
750 #ifndef ANDROID_BUILD
751 
757 int CubeAPITest(Boxa* boxa_blocks, Pixa* pixa_blocks,
758  Boxa* boxa_words, Pixa* pixa_words,
759  const FCOORD& reskew, Pix* page_pix,
760  PAGE_RES* page_res) {
761  int block_count = boxaGetCount(boxa_blocks);
762  ASSERT_HOST(block_count == pixaGetCount(pixa_blocks));
763  // Write each block to the current directory as junk_write_display.nnn.png.
764  for (int i = 0; i < block_count; ++i) {
765  Pix* pix = pixaGetPix(pixa_blocks, i, L_CLONE);
766  pixDisplayWrite(pix, 1);
767  }
768  int word_count = boxaGetCount(boxa_words);
769  ASSERT_HOST(word_count == pixaGetCount(pixa_words));
770  int pr_word = 0;
771  PAGE_RES_IT page_res_it(page_res);
772  for (page_res_it.restart_page(); page_res_it.word () != NULL;
773  page_res_it.forward(), ++pr_word) {
774  WERD_RES *word = page_res_it.word();
775  WERD_CHOICE* choice = word->best_choice;
776  // Write the first 100 words to files names wordims/<wordstring>.tif.
777  if (pr_word < 100) {
778  STRING filename("wordims/");
779  if (choice != NULL) {
780  filename += choice->unichar_string();
781  } else {
782  char numbuf[32];
783  filename += "unclassified";
784  snprintf(numbuf, 32, "%03d", pr_word);
785  filename += numbuf;
786  }
787  filename += ".tif";
788  Pix* pix = pixaGetPix(pixa_words, pr_word, L_CLONE);
789  pixWrite(filename.string(), pix, IFF_TIFF_G4);
790  }
791  }
792  ASSERT_HOST(pr_word == word_count);
793  return 0;
794 }
795 #endif
796 
812 PageIterator* TessBaseAPI::AnalyseLayout(bool merge_similar_words) {
813  if (FindLines() == 0) {
814  if (block_list_->empty())
815  return NULL; // The page was empty.
816  page_res_ = new PAGE_RES(merge_similar_words, block_list_, NULL);
817  DetectParagraphs(false);
818  return new PageIterator(
822  }
823  return NULL;
824 }
825 
831  if (tesseract_ == NULL)
832  return -1;
833  if (FindLines() != 0)
834  return -1;
835  if (page_res_ != NULL)
836  delete page_res_;
837  if (block_list_->empty()) {
838  page_res_ = new PAGE_RES(false, block_list_,
840  return 0; // Empty page.
841  }
842 
844  recognition_done_ = true;
849  } else {
850  // TODO(rays) LSTM here.
851  page_res_ = new PAGE_RES(false,
853  }
856  return 0;
857  }
858 
859  if (truth_cb_ != NULL) {
860  tesseract_->wordrec_run_blamer.set_value(true);
861  PageIterator *page_it = new PageIterator(
866  image_height_, page_it, this->tesseract()->pix_grey());
867  delete page_it;
868  }
869 
870  int result = 0;
872  #ifndef GRAPHICS_DISABLED
874  #endif // GRAPHICS_DISABLED
875  // The page_res is invalid after an interactive session, so cleanup
876  // in a way that lets us continue to the next page without crashing.
877  delete page_res_;
878  page_res_ = NULL;
879  return -1;
881  STRING fontname;
882  ExtractFontName(*output_file_, &fontname);
884  } else if (tesseract_->tessedit_ambigs_training) {
885  FILE *training_output_file = tesseract_->init_recog_training(*input_file_);
886  // OCR the page segmented into words by tesseract.
888  *input_file_, page_res_, monitor, training_output_file);
889  fclose(training_output_file);
890  } else {
891  // Now run the main recognition.
892  bool wait_for_text = true;
893  GetBoolVariable("paragraph_text_based", &wait_for_text);
894  if (!wait_for_text) DetectParagraphs(false);
895  if (tesseract_->recog_all_words(page_res_, monitor, NULL, NULL, 0)) {
896  if (wait_for_text) DetectParagraphs(true);
897  } else {
898  result = -1;
899  }
900  }
901  return result;
902 }
903 
906  if (tesseract_ == NULL)
907  return -1;
908  if (thresholder_ == NULL || thresholder_->IsEmpty()) {
909  tprintf("Please call SetImage before attempting recognition.");
910  return -1;
911  }
912  if (page_res_ != NULL)
913  ClearResults();
914  if (FindLines() != 0)
915  return -1;
916  // Additional conditions under which chopper test cannot be run
917  if (tesseract_->interactive_display_mode) return -1;
918 
919  recognition_done_ = true;
920 
921  page_res_ = new PAGE_RES(false, block_list_,
923 
924  PAGE_RES_IT page_res_it(page_res_);
925 
926  while (page_res_it.word() != NULL) {
927  WERD_RES *word_res = page_res_it.word();
928  GenericVector<TBOX> boxes;
929  tesseract_->MaximallyChopWord(boxes, page_res_it.block()->block,
930  page_res_it.row()->row, word_res);
931  page_res_it.forward();
932  }
933  return 0;
934 }
935 
937  if (input_image_)
938  pixDestroy(&input_image_);
939  input_image_ = NULL;
940  if (pix)
941  input_image_ = pixCopy(NULL, pix);
942 }
943 
945  return input_image_;
946 }
947 
949  if (input_file_)
950  return input_file_->c_str();
951  return NULL;
952 }
953 
954 const char * TessBaseAPI::GetDatapath() {
955  return tesseract_->datadir.c_str();
956 }
957 
960 }
961 
962 // If flist exists, get data from there. Otherwise get data from buf.
963 // Seems convoluted, but is the easiest way I know of to meet multiple
964 // goals. Support streaming from stdin, and also work on platforms
965 // lacking fmemopen.
966 bool TessBaseAPI::ProcessPagesFileList(FILE *flist,
967  STRING *buf,
968  const char* retry_config,
969  int timeout_millisec,
970  TessResultRenderer* renderer,
971  int tessedit_page_number) {
972  if (!flist && !buf) return false;
973  int page = (tessedit_page_number >= 0) ? tessedit_page_number : 0;
974  char pagename[MAX_PATH];
975 
976  GenericVector<STRING> lines;
977  if (!flist) {
978  buf->split('\n', &lines);
979  if (lines.empty()) return false;
980  }
981 
982  // Skip to the requested page number.
983  for (int i = 0; i < page; i++) {
984  if (flist) {
985  if (fgets(pagename, sizeof(pagename), flist) == NULL) break;
986  }
987  }
988 
989  // Begin producing output
990  const char* kUnknownTitle = "";
991  if (renderer && !renderer->BeginDocument(kUnknownTitle)) {
992  return false;
993  }
994 
995  // Loop over all pages - or just the requested one
996  while (true) {
997  if (flist) {
998  if (fgets(pagename, sizeof(pagename), flist) == NULL) break;
999  } else {
1000  if (page >= lines.size()) break;
1001  snprintf(pagename, sizeof(pagename), "%s", lines[page].c_str());
1002  }
1003  chomp_string(pagename);
1004  Pix *pix = pixRead(pagename);
1005  if (pix == NULL) {
1006  tprintf("Image file %s cannot be read!\n", pagename);
1007  return false;
1008  }
1009  tprintf("Page %d : %s\n", page, pagename);
1010  bool r = ProcessPage(pix, page, pagename, retry_config,
1011  timeout_millisec, renderer);
1012  pixDestroy(&pix);
1013  if (!r) return false;
1014  if (tessedit_page_number >= 0) break;
1015  ++page;
1016  }
1017 
1018  // Finish producing output
1019  if (renderer && !renderer->EndDocument()) {
1020  return false;
1021  }
1022  return true;
1023 }
1024 
1025 bool TessBaseAPI::ProcessPagesMultipageTiff(const l_uint8 *data,
1026  size_t size,
1027  const char* filename,
1028  const char* retry_config,
1029  int timeout_millisec,
1030  TessResultRenderer* renderer,
1031  int tessedit_page_number) {
1032 #ifndef ANDROID_BUILD
1033  Pix *pix = NULL;
1034 #ifdef USE_OPENCL
1035  OpenclDevice od;
1036 #endif
1037  int page = (tessedit_page_number >= 0) ? tessedit_page_number : 0;
1038  for (; ; ++page) {
1039  if (tessedit_page_number >= 0)
1040  page = tessedit_page_number;
1041 #ifdef USE_OPENCL
1042  if ( od.selectedDeviceIsOpenCL() ) {
1043  // FIXME(jbreiden) Not implemented.
1044  pix = od.pixReadMemTiffCl(data, size, page);
1045  } else {
1046 #endif
1047  pix = pixReadMemTiff(data, size, page);
1048 #ifdef USE_OPENCL
1049  }
1050 #endif
1051  if (pix == NULL) break;
1052  tprintf("Page %d\n", page + 1);
1053  char page_str[kMaxIntSize];
1054  snprintf(page_str, kMaxIntSize - 1, "%d", page);
1055  SetVariable("applybox_page", page_str);
1056  bool r = ProcessPage(pix, page, filename, retry_config,
1057  timeout_millisec, renderer);
1058  pixDestroy(&pix);
1059  if (!r) return false;
1060  if (tessedit_page_number >= 0) break;
1061  }
1062  return true;
1063 #else
1064  return false;
1065 #endif
1066 }
1067 
1068 // Master ProcessPages calls ProcessPagesInternal and then does any post-
1069 // processing required due to being in a training mode.
1070 bool TessBaseAPI::ProcessPages(const char* filename, const char* retry_config,
1071  int timeout_millisec,
1072  TessResultRenderer* renderer) {
1073  bool result =
1074  ProcessPagesInternal(filename, retry_config, timeout_millisec, renderer);
1075  if (result) {
1078  tprintf("Write of TR file failed: %s\n", output_file_->string());
1079  return false;
1080  }
1081  }
1082  return result;
1083 }
1084 
1085 // In the ideal scenario, Tesseract will start working on data as soon
1086 // as it can. For example, if you steam a filelist through stdin, we
1087 // should start the OCR process as soon as the first filename is
1088 // available. This is particularly useful when hooking Tesseract up to
1089 // slow hardware such as a book scanning machine.
1090 //
1091 // Unfortunately there are tradeoffs. You can't seek on stdin. That
1092 // makes automatic detection of datatype (TIFF? filelist? PNG?)
1093 // impractical. So we support a command line flag to explicitly
1094 // identify the scenario that really matters: filelists on
1095 // stdin. We'll still do our best if the user likes pipes. That means
1096 // piling up any data coming into stdin into a memory buffer.
1097 bool TessBaseAPI::ProcessPagesInternal(const char* filename,
1098  const char* retry_config,
1099  int timeout_millisec,
1100  TessResultRenderer* renderer) {
1101 #ifndef ANDROID_BUILD
1102  PERF_COUNT_START("ProcessPages")
1103  bool stdInput = !strcmp(filename, "stdin") || !strcmp(filename, "-");
1104  if (stdInput) {
1105 #ifdef WIN32
1106  if (_setmode(_fileno(stdin), _O_BINARY) == -1)
1107  tprintf("ERROR: cin to binary: %s", strerror(errno));
1108 #endif // WIN32
1109  }
1110 
1111  if (stream_filelist) {
1112  return ProcessPagesFileList(stdin, NULL, retry_config,
1113  timeout_millisec, renderer,
1115  }
1116 
1117  // At this point we are officially in autodection territory.
1118  // That means we are going to buffer stdin so that it is
1119  // seekable. To keep code simple we will also buffer data
1120  // coming from a file.
1121  std::string buf;
1122  if (stdInput) {
1123  buf.assign((std::istreambuf_iterator<char>(std::cin)),
1124  (std::istreambuf_iterator<char>()));
1125  } else {
1126  std::ifstream ifs(filename, std::ios::binary);
1127  if (ifs) {
1128  buf.assign((std::istreambuf_iterator<char>(ifs)),
1129  (std::istreambuf_iterator<char>()));
1130  } else {
1131  tprintf("ERROR: Can not open input file %s\n", filename);
1132  return false;
1133  }
1134  }
1135 
1136  // Here is our autodetection
1137  int format;
1138  const l_uint8 * data = reinterpret_cast<const l_uint8 *>(buf.c_str());
1139  findFileFormatBuffer(data, &format);
1140 
1141  // Maybe we have a filelist
1142  if (format == IFF_UNKNOWN) {
1143  STRING s(buf.c_str());
1144  return ProcessPagesFileList(NULL, &s, retry_config,
1145  timeout_millisec, renderer,
1147  }
1148 
1149  // Maybe we have a TIFF which is potentially multipage
1150  bool tiff = (format == IFF_TIFF || format == IFF_TIFF_PACKBITS ||
1151  format == IFF_TIFF_RLE || format == IFF_TIFF_G3 ||
1152  format == IFF_TIFF_G4 || format == IFF_TIFF_LZW ||
1153  format == IFF_TIFF_ZIP);
1154 
1155  // Fail early if we can, before producing any output
1156  Pix *pix = NULL;
1157  if (!tiff) {
1158  pix = pixReadMem(data, buf.size());
1159  if (pix == NULL) {
1160  return false;
1161  }
1162  }
1163 
1164  // Begin the output
1165  const char* kUnknownTitle = "";
1166  if (renderer && !renderer->BeginDocument(kUnknownTitle)) {
1167  pixDestroy(&pix);
1168  return false;
1169  }
1170 
1171  // Produce output
1172  bool r = false;
1173  if (tiff) {
1174  r = ProcessPagesMultipageTiff(data, buf.size(), filename, retry_config,
1175  timeout_millisec, renderer,
1177  } else {
1178  r = ProcessPage(pix, 0, filename, retry_config,
1179  timeout_millisec, renderer);
1180  pixDestroy(&pix);
1181  }
1182 
1183  // End the output
1184  if (!r || (renderer && !renderer->EndDocument())) {
1185  return false;
1186  }
1188  return true;
1189 #else
1190  return false;
1191 #endif
1192 }
1193 
1194 bool TessBaseAPI::ProcessPage(Pix* pix, int page_index, const char* filename,
1195  const char* retry_config, int timeout_millisec,
1196  TessResultRenderer* renderer) {
1197  PERF_COUNT_START("ProcessPage")
1198  SetInputName(filename);
1199  SetImage(pix);
1200  bool failed = false;
1201  if (timeout_millisec > 0) {
1202  // Running with a timeout.
1203  ETEXT_DESC monitor;
1204  monitor.cancel = NULL;
1205  monitor.cancel_this = NULL;
1206  monitor.set_deadline_msecs(timeout_millisec);
1207  // Now run the main recognition.
1208  failed = Recognize(&monitor) < 0;
1211  // Disabled character recognition.
1212  PageIterator* it = AnalyseLayout();
1213  if (it == NULL) {
1214  failed = true;
1215  } else {
1216  delete it;
1218  return true;
1219  }
1220  } else {
1221  // Normal layout and character recognition with no timeout.
1222  failed = Recognize(NULL) < 0;
1223  }
1225 #ifndef ANDROID_BUILD
1226  Pix* page_pix = GetThresholdedImage();
1227  pixWrite("tessinput.tif", page_pix, IFF_TIFF_G4);
1228 #endif
1229  }
1230  if (failed && retry_config != NULL && retry_config[0] != '\0') {
1231  // Save current config variables before switching modes.
1232  FILE* fp = fopen(kOldVarsFile, "wb");
1233  PrintVariables(fp);
1234  fclose(fp);
1235  // Switch to alternate mode for retry.
1236  ReadConfigFile(retry_config);
1237  SetImage(pix);
1238  Recognize(NULL);
1239  // Restore saved config variables.
1241  }
1242 
1243  if (renderer && !failed) {
1244  failed = !renderer->AddImage(this);
1245  }
1247  return !failed;
1248 }
1249 
1255  if (tesseract_ == NULL || page_res_ == NULL)
1256  return NULL;
1257  return new LTRResultIterator(
1261 }
1262 
1272  if (tesseract_ == NULL || page_res_ == NULL)
1273  return NULL;
1278 }
1279 
1289  if (tesseract_ == NULL || page_res_ == NULL)
1290  return NULL;
1291  return new MutableIterator(page_res_, tesseract_,
1295 }
1296 
1299  if (tesseract_ == NULL ||
1300  (!recognition_done_ && Recognize(NULL) < 0))
1301  return NULL;
1302  STRING text("");
1303  ResultIterator *it = GetIterator();
1304  do {
1305  if (it->Empty(RIL_PARA)) continue;
1306  char *para_text = it->GetUTF8Text(RIL_PARA);
1307  text += para_text;
1308  delete []para_text;
1309  } while (it->Next(RIL_PARA));
1310  char* result = new char[text.length() + 1];
1311  strncpy(result, text.string(), text.length() + 1);
1312  delete it;
1313  return result;
1314 }
1315 
1319 static tesseract::Orientation GetBlockTextOrientation(const PageIterator *it) {
1320  tesseract::Orientation orientation;
1321  tesseract::WritingDirection writing_direction;
1322  tesseract::TextlineOrder textline_order;
1323  float deskew_angle;
1324  it->Orientation(&orientation, &writing_direction, &textline_order,
1325  &deskew_angle);
1326  return orientation;
1327 }
1328 
1337 static void AddBaselineCoordsTohOCR(const PageIterator *it,
1338  PageIteratorLevel level,
1339  STRING* hocr_str) {
1340  tesseract::Orientation orientation = GetBlockTextOrientation(it);
1341  if (orientation != ORIENTATION_PAGE_UP) {
1342  hocr_str->add_str_int("; textangle ", 360 - orientation * 90);
1343  return;
1344  }
1345 
1346  int left, top, right, bottom;
1347  it->BoundingBox(level, &left, &top, &right, &bottom);
1348 
1349  // Try to get the baseline coordinates at this level.
1350  int x1, y1, x2, y2;
1351  if (!it->Baseline(level, &x1, &y1, &x2, &y2))
1352  return;
1353  // Following the description of this field of the hOCR spec, we convert the
1354  // baseline coordinates so that "the bottom left of the bounding box is the
1355  // origin".
1356  x1 -= left;
1357  x2 -= left;
1358  y1 -= bottom;
1359  y2 -= bottom;
1360 
1361  // Now fit a line through the points so we can extract coefficients for the
1362  // equation: y = p1 x + p0
1363  double p1 = 0;
1364  double p0 = 0;
1365  if (x1 == x2) {
1366  // Problem computing the polynomial coefficients.
1367  return;
1368  }
1369  p1 = (y2 - y1) / static_cast<double>(x2 - x1);
1370  p0 = y1 - static_cast<double>(p1 * x1);
1371 
1372  hocr_str->add_str_double("; baseline ", round(p1 * 1000.0) / 1000.0);
1373  hocr_str->add_str_double(" ", round(p0 * 1000.0) / 1000.0);
1374 }
1375 
1376 static void AddBoxTohOCR(const PageIterator *it,
1377  PageIteratorLevel level,
1378  STRING* hocr_str) {
1379  int left, top, right, bottom;
1380  it->BoundingBox(level, &left, &top, &right, &bottom);
1381  hocr_str->add_str_int("' title=\"bbox ", left);
1382  hocr_str->add_str_int(" ", top);
1383  hocr_str->add_str_int(" ", right);
1384  hocr_str->add_str_int(" ", bottom);
1385  // Add baseline coordinates for textlines only.
1386  if (level == RIL_TEXTLINE)
1387  AddBaselineCoordsTohOCR(it, level, hocr_str);
1388  *hocr_str += "\">";
1389 }
1390 
1399 char* TessBaseAPI::GetHOCRText(int page_number) {
1400  if (tesseract_ == NULL ||
1401  (page_res_ == NULL && Recognize(NULL) < 0))
1402  return NULL;
1403 
1404  int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
1405  int page_id = page_number + 1; // hOCR uses 1-based page numbers.
1406  bool font_info = false;
1407  GetBoolVariable("hocr_font_info", &font_info);
1408 
1409  STRING hocr_str("");
1410 
1411  if (input_file_ == NULL)
1412  SetInputName(NULL);
1413 
1414 #ifdef _WIN32
1415  // convert input name from ANSI encoding to utf-8
1416  int str16_len = MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1,
1417  NULL, NULL);
1418  wchar_t *uni16_str = new WCHAR[str16_len];
1419  str16_len = MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1,
1420  uni16_str, str16_len);
1421  int utf8_len = WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, NULL,
1422  NULL, NULL, NULL);
1423  char *utf8_str = new char[utf8_len];
1424  WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, utf8_str,
1425  utf8_len, NULL, NULL);
1426  *input_file_ = utf8_str;
1427  delete[] uni16_str;
1428  delete[] utf8_str;
1429 #endif
1430 
1431  hocr_str.add_str_int(" <div class='ocr_page' id='page_", page_id);
1432  hocr_str += "' title='image \"";
1433  if (input_file_) {
1434  hocr_str += HOcrEscape(input_file_->string());
1435  } else {
1436  hocr_str += "unknown";
1437  }
1438  hocr_str.add_str_int("\"; bbox ", rect_left_);
1439  hocr_str.add_str_int(" ", rect_top_);
1440  hocr_str.add_str_int(" ", rect_width_);
1441  hocr_str.add_str_int(" ", rect_height_);
1442  hocr_str.add_str_int("; ppageno ", page_number);
1443  hocr_str += "'>\n";
1444 
1445  ResultIterator *res_it = GetIterator();
1446  while (!res_it->Empty(RIL_BLOCK)) {
1447  if (res_it->Empty(RIL_WORD)) {
1448  res_it->Next(RIL_WORD);
1449  continue;
1450  }
1451 
1452  // Open any new block/paragraph/textline.
1453  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
1454  hocr_str.add_str_int(" <div class='ocr_carea' id='block_", page_id);
1455  hocr_str.add_str_int("_", bcnt);
1456  AddBoxTohOCR(res_it, RIL_BLOCK, &hocr_str);
1457  }
1458  if (res_it->IsAtBeginningOf(RIL_PARA)) {
1459  if (res_it->ParagraphIsLtr()) {
1460  hocr_str.add_str_int("\n <p class='ocr_par' dir='ltr' id='par_",
1461  page_id);
1462  hocr_str.add_str_int("_", pcnt);
1463  } else {
1464  hocr_str.add_str_int("\n <p class='ocr_par' dir='rtl' id='par_",
1465  page_id);
1466  hocr_str.add_str_int("_", pcnt);
1467  }
1468  AddBoxTohOCR(res_it, RIL_PARA, &hocr_str);
1469  }
1470  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
1471  hocr_str.add_str_int("\n <span class='ocr_line' id='line_", page_id);
1472  hocr_str.add_str_int("_", lcnt);
1473  AddBoxTohOCR(res_it, RIL_TEXTLINE, &hocr_str);
1474  }
1475 
1476  // Now, process the word...
1477  hocr_str.add_str_int("<span class='ocrx_word' id='word_", page_id);
1478  hocr_str.add_str_int("_", wcnt);
1479  int left, top, right, bottom;
1480  bool bold, italic, underlined, monospace, serif, smallcaps;
1481  int pointsize, font_id;
1482  const char *font_name;
1483  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
1484  font_name = res_it->WordFontAttributes(&bold, &italic, &underlined,
1485  &monospace, &serif, &smallcaps,
1486  &pointsize, &font_id);
1487  hocr_str.add_str_int("' title='bbox ", left);
1488  hocr_str.add_str_int(" ", top);
1489  hocr_str.add_str_int(" ", right);
1490  hocr_str.add_str_int(" ", bottom);
1491  hocr_str.add_str_int("; x_wconf ", res_it->Confidence(RIL_WORD));
1492  if (font_info) {
1493  hocr_str += "; x_font ";
1494  hocr_str += HOcrEscape(font_name);
1495  hocr_str.add_str_int("; x_fsize ", pointsize);
1496  }
1497  hocr_str += "'";
1498  if (res_it->WordRecognitionLanguage()) {
1499  hocr_str += " lang='";
1500  hocr_str += res_it->WordRecognitionLanguage();
1501  hocr_str += "'";
1502  }
1503  switch (res_it->WordDirection()) {
1504  case DIR_LEFT_TO_RIGHT: hocr_str += " dir='ltr'"; break;
1505  case DIR_RIGHT_TO_LEFT: hocr_str += " dir='rtl'"; break;
1506  default: // Do nothing.
1507  break;
1508  }
1509  hocr_str += ">";
1510  bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
1511  bool last_word_in_para = res_it->IsAtFinalElement(RIL_PARA, RIL_WORD);
1512  bool last_word_in_block = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
1513  if (bold) hocr_str += "<strong>";
1514  if (italic) hocr_str += "<em>";
1515  do {
1516  const char *grapheme = res_it->GetUTF8Text(RIL_SYMBOL);
1517  if (grapheme && grapheme[0] != 0) {
1518  hocr_str += HOcrEscape(grapheme);
1519  }
1520  delete []grapheme;
1521  res_it->Next(RIL_SYMBOL);
1522  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
1523  if (italic) hocr_str += "</em>";
1524  if (bold) hocr_str += "</strong>";
1525  hocr_str += "</span> ";
1526  wcnt++;
1527  // Close any ending block/paragraph/textline.
1528  if (last_word_in_line) {
1529  hocr_str += "\n </span>";
1530  lcnt++;
1531  }
1532  if (last_word_in_para) {
1533  hocr_str += "\n </p>\n";
1534  pcnt++;
1535  }
1536  if (last_word_in_block) {
1537  hocr_str += " </div>\n";
1538  bcnt++;
1539  }
1540  }
1541  hocr_str += " </div>\n";
1542 
1543  char *ret = new char[hocr_str.length() + 1];
1544  strcpy(ret, hocr_str.string());
1545  delete res_it;
1546  return ret;
1547 }
1548 
1550 const int kNumbersPerBlob = 5;
1555 const int kBytesPerNumber = 5;
1564 const int kBytesPer64BitNumber = 20;
1572  UNICHAR_LEN;
1573 
1579 char* TessBaseAPI::GetBoxText(int page_number) {
1580  if (tesseract_ == NULL ||
1581  (!recognition_done_ && Recognize(NULL) < 0))
1582  return NULL;
1583  int blob_count;
1584  int utf8_length = TextLength(&blob_count);
1585  int total_length = blob_count * kBytesPerBoxFileLine + utf8_length +
1587  char* result = new char[total_length];
1588  strcpy(result, "\0");
1589  int output_length = 0;
1591  do {
1592  int left, top, right, bottom;
1593  if (it->BoundingBox(RIL_SYMBOL, &left, &top, &right, &bottom)) {
1594  char* text = it->GetUTF8Text(RIL_SYMBOL);
1595  // Tesseract uses space for recognition failure. Fix to a reject
1596  // character, kTesseractReject so we don't create illegal box files.
1597  for (int i = 0; text[i] != '\0'; ++i) {
1598  if (text[i] == ' ')
1599  text[i] = kTesseractReject;
1600  }
1601  snprintf(result + output_length, total_length - output_length,
1602  "%s %d %d %d %d %d\n",
1603  text, left, image_height_ - bottom,
1604  right, image_height_ - top, page_number);
1605  output_length += strlen(result + output_length);
1606  delete [] text;
1607  // Just in case...
1608  if (output_length + kMaxBytesPerLine > total_length)
1609  break;
1610  }
1611  } while (it->Next(RIL_SYMBOL));
1612  delete it;
1613  return result;
1614 }
1615 
1621 const int kUniChs[] = {
1622  0x20ac, 0x201c, 0x201d, 0x2018, 0x2019, 0x2022, 0x2014, 0
1623 };
1625 const int kLatinChs[] = {
1626  0x00a2, 0x0022, 0x0022, 0x0027, 0x0027, 0x00b7, 0x002d, 0
1627 };
1628 
1635  if (tesseract_ == NULL ||
1636  (!recognition_done_ && Recognize(NULL) < 0))
1637  return NULL;
1638  bool tilde_crunch_written = false;
1639  bool last_char_was_newline = true;
1640  bool last_char_was_tilde = false;
1641 
1642  int total_length = TextLength(NULL);
1643  PAGE_RES_IT page_res_it(page_res_);
1644  char* result = new char[total_length];
1645  char* ptr = result;
1646  for (page_res_it.restart_page(); page_res_it.word () != NULL;
1647  page_res_it.forward()) {
1648  WERD_RES *word = page_res_it.word();
1649  // Process the current word.
1650  if (word->unlv_crunch_mode != CR_NONE) {
1651  if (word->unlv_crunch_mode != CR_DELETE &&
1652  (!tilde_crunch_written ||
1653  (word->unlv_crunch_mode == CR_KEEP_SPACE &&
1654  word->word->space() > 0 &&
1655  !word->word->flag(W_FUZZY_NON) &&
1656  !word->word->flag(W_FUZZY_SP)))) {
1657  if (!word->word->flag(W_BOL) &&
1658  word->word->space() > 0 &&
1659  !word->word->flag(W_FUZZY_NON) &&
1660  !word->word->flag(W_FUZZY_SP)) {
1661  /* Write a space to separate from preceeding good text */
1662  *ptr++ = ' ';
1663  last_char_was_tilde = false;
1664  }
1665  if (!last_char_was_tilde) {
1666  // Write a reject char.
1667  last_char_was_tilde = true;
1668  *ptr++ = kUNLVReject;
1669  tilde_crunch_written = true;
1670  last_char_was_newline = false;
1671  }
1672  }
1673  } else {
1674  // NORMAL PROCESSING of non tilde crunched words.
1675  tilde_crunch_written = false;
1677  const char* wordstr = word->best_choice->unichar_string().string();
1678  const STRING& lengths = word->best_choice->unichar_lengths();
1679  int length = lengths.length();
1680  int i = 0;
1681  int offset = 0;
1682 
1683  if (last_char_was_tilde &&
1684  word->word->space() == 0 && wordstr[offset] == ' ') {
1685  // Prevent adjacent tilde across words - we know that adjacent tildes
1686  // within words have been removed.
1687  // Skip the first character.
1688  offset = lengths[i++];
1689  }
1690  if (i < length && wordstr[offset] != 0) {
1691  if (!last_char_was_newline)
1692  *ptr++ = ' ';
1693  else
1694  last_char_was_newline = false;
1695  for (; i < length; offset += lengths[i++]) {
1696  if (wordstr[offset] == ' ' ||
1697  wordstr[offset] == kTesseractReject) {
1698  *ptr++ = kUNLVReject;
1699  last_char_was_tilde = true;
1700  } else {
1701  if (word->reject_map[i].rejected())
1702  *ptr++ = kUNLVSuspect;
1703  UNICHAR ch(wordstr + offset, lengths[i]);
1704  int uni_ch = ch.first_uni();
1705  for (int j = 0; kUniChs[j] != 0; ++j) {
1706  if (kUniChs[j] == uni_ch) {
1707  uni_ch = kLatinChs[j];
1708  break;
1709  }
1710  }
1711  if (uni_ch <= 0xff) {
1712  *ptr++ = static_cast<char>(uni_ch);
1713  last_char_was_tilde = false;
1714  } else {
1715  *ptr++ = kUNLVReject;
1716  last_char_was_tilde = true;
1717  }
1718  }
1719  }
1720  }
1721  }
1722  if (word->word->flag(W_EOL) && !last_char_was_newline) {
1723  /* Add a new line output */
1724  *ptr++ = '\n';
1725  tilde_crunch_written = false;
1726  last_char_was_newline = true;
1727  last_char_was_tilde = false;
1728  }
1729  }
1730  *ptr++ = '\n';
1731  *ptr = '\0';
1732  return result;
1733 }
1734 
1737  int* conf = AllWordConfidences();
1738  if (!conf) return 0;
1739  int sum = 0;
1740  int *pt = conf;
1741  while (*pt >= 0) sum += *pt++;
1742  if (pt != conf) sum /= pt - conf;
1743  delete [] conf;
1744  return sum;
1745 }
1746 
1749  if (tesseract_ == NULL ||
1750  (!recognition_done_ && Recognize(NULL) < 0))
1751  return NULL;
1752  int n_word = 0;
1753  PAGE_RES_IT res_it(page_res_);
1754  for (res_it.restart_page(); res_it.word() != NULL; res_it.forward())
1755  n_word++;
1756 
1757  int* conf = new int[n_word+1];
1758  n_word = 0;
1759  for (res_it.restart_page(); res_it.word() != NULL; res_it.forward()) {
1760  WERD_RES *word = res_it.word();
1761  WERD_CHOICE* choice = word->best_choice;
1762  int w_conf = static_cast<int>(100 + 5 * choice->certainty());
1763  // This is the eq for converting Tesseract confidence to 1..100
1764  if (w_conf < 0) w_conf = 0;
1765  if (w_conf > 100) w_conf = 100;
1766  conf[n_word++] = w_conf;
1767  }
1768  conf[n_word] = -1;
1769  return conf;
1770 }
1771 
1782 bool TessBaseAPI::AdaptToWordStr(PageSegMode mode, const char* wordstr) {
1783  int debug = 0;
1784  GetIntVariable("applybox_debug", &debug);
1785  bool success = true;
1786  PageSegMode current_psm = GetPageSegMode();
1787  SetPageSegMode(mode);
1788  SetVariable("classify_enable_learning", "0");
1789  char* text = GetUTF8Text();
1790  if (debug) {
1791  tprintf("Trying to adapt \"%s\" to \"%s\"\n", text, wordstr);
1792  }
1793  if (text != NULL) {
1794  PAGE_RES_IT it(page_res_);
1795  WERD_RES* word_res = it.word();
1796  if (word_res != NULL) {
1797  word_res->word->set_text(wordstr);
1798  } else {
1799  success = false;
1800  }
1801  // Check to see if text matches wordstr.
1802  int w = 0;
1803  int t = 0;
1804  for (t = 0; text[t] != '\0'; ++t) {
1805  if (text[t] == '\n' || text[t] == ' ')
1806  continue;
1807  while (wordstr[w] != '\0' && wordstr[w] == ' ')
1808  ++w;
1809  if (text[t] != wordstr[w])
1810  break;
1811  ++w;
1812  }
1813  if (text[t] != '\0' || wordstr[w] != '\0') {
1814  // No match.
1815  delete page_res_;
1816  GenericVector<TBOX> boxes;
1820  PAGE_RES_IT pr_it(page_res_);
1821  if (pr_it.word() == NULL)
1822  success = false;
1823  else
1824  word_res = pr_it.word();
1825  } else {
1826  word_res->BestChoiceToCorrectText();
1827  }
1828  if (success) {
1829  tesseract_->EnableLearning = true;
1830  tesseract_->LearnWord(NULL, word_res);
1831  }
1832  delete [] text;
1833  } else {
1834  success = false;
1835  }
1836  SetPageSegMode(current_psm);
1837  return success;
1838 }
1839 
1847  if (thresholder_ != NULL)
1848  thresholder_->Clear();
1849  ClearResults();
1851 }
1852 
1860  if (thresholder_ != NULL) {
1861  delete thresholder_;
1862  thresholder_ = NULL;
1863  }
1864  if (page_res_ != NULL) {
1865  delete page_res_;
1866  page_res_ = NULL;
1867  }
1868  if (block_list_ != NULL) {
1869  delete block_list_;
1870  block_list_ = NULL;
1871  }
1872  if (paragraph_models_ != NULL) {
1874  delete paragraph_models_;
1876  }
1877  if (tesseract_ != NULL) {
1878  delete tesseract_;
1879  if (osd_tesseract_ == tesseract_)
1880  osd_tesseract_ = NULL;
1881  tesseract_ = NULL;
1882  }
1883  if (osd_tesseract_ != NULL) {
1884  delete osd_tesseract_;
1885  osd_tesseract_ = NULL;
1886  }
1887  if (equ_detect_ != NULL) {
1888  delete equ_detect_;
1889  equ_detect_ = NULL;
1890  }
1891  if (input_file_ != NULL) {
1892  delete input_file_;
1893  input_file_ = NULL;
1894  }
1895  if (input_image_ != NULL) {
1896  pixDestroy(&input_image_);
1897  input_image_ = NULL;
1898  }
1899  if (output_file_ != NULL) {
1900  delete output_file_;
1901  output_file_ = NULL;
1902  }
1903  if (datapath_ != NULL) {
1904  delete datapath_;
1905  datapath_ = NULL;
1906  }
1907  if (language_ != NULL) {
1908  delete language_;
1909  language_ = NULL;
1910  }
1911 }
1912 
1913 // Clear any library-level memory caches.
1914 // There are a variety of expensive-to-load constant data structures (mostly
1915 // language dictionaries) that are cached globally -- surviving the Init()
1916 // and End() of individual TessBaseAPI's. This function allows the clearing
1917 // of these caches.
1920 }
1921 
1926 int TessBaseAPI::IsValidWord(const char *word) {
1927  return tesseract_->getDict().valid_word(word);
1928 }
1929 // Returns true if utf8_character is defined in the UniCharset.
1930 bool TessBaseAPI::IsValidCharacter(const char *utf8_character) {
1931  return tesseract_->unicharset.contains_unichar(utf8_character);
1932 }
1933 
1934 
1935 // TODO(rays) Obsolete this function and replace with a more aptly named
1936 // function that returns image coordinates rather than tesseract coordinates.
1937 bool TessBaseAPI::GetTextDirection(int* out_offset, float* out_slope) {
1938  PageIterator* it = AnalyseLayout();
1939  if (it == NULL) {
1940  return false;
1941  }
1942  int x1, x2, y1, y2;
1943  it->Baseline(RIL_TEXTLINE, &x1, &y1, &x2, &y2);
1944  // Calculate offset and slope (NOTE: Kind of ugly)
1945  if (x2 <= x1) x2 = x1 + 1;
1946  // Convert the point pair to slope/offset of the baseline (in image coords.)
1947  *out_slope = static_cast<float>(y2 - y1) / (x2 - x1);
1948  *out_offset = static_cast<int>(y1 - *out_slope * x1);
1949  // Get the y-coord of the baseline at the left and right edges of the
1950  // textline's bounding box.
1951  int left, top, right, bottom;
1952  if (!it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom)) {
1953  delete it;
1954  return false;
1955  }
1956  int left_y = IntCastRounded(*out_slope * left + *out_offset);
1957  int right_y = IntCastRounded(*out_slope * right + *out_offset);
1958  // Shift the baseline down so it passes through the nearest bottom-corner
1959  // of the textline's bounding box. This is the difference between the y
1960  // at the lowest (max) edge of the box and the actual box bottom.
1961  *out_offset += bottom - MAX(left_y, right_y);
1962  // Switch back to bottom-up tesseract coordinates. Requires negation of
1963  // the slope and height - offset for the offset.
1964  *out_slope = -*out_slope;
1965  *out_offset = rect_height_ - *out_offset;
1966  delete it;
1967 
1968  return true;
1969 }
1970 
1973  if (tesseract_ != NULL) {
1975  }
1976 }
1977 
1987  if (tesseract_ != NULL) {
1989  // Set it for the sublangs too.
1990  int num_subs = tesseract_->num_sub_langs();
1991  for (int i = 0; i < num_subs; ++i) {
1993  }
1994  }
1995 }
1996 
1999  if (tesseract_ != NULL) tesseract_->fill_lattice_ = f;
2000 }
2001 
2004  if (tesseract_ == NULL) {
2005  tprintf("Please call Init before attempting to set an image.");
2006  return false;
2007  }
2008  if (thresholder_ == NULL)
2010  ClearResults();
2011  return true;
2012 }
2013 
2020 void TessBaseAPI::Threshold(Pix** pix) {
2021  ASSERT_HOST(pix != NULL);
2022  if (*pix != NULL)
2023  pixDestroy(pix);
2024  // Zero resolution messes up the algorithms, so make sure it is credible.
2025  int y_res = thresholder_->GetScaledYResolution();
2026  if (y_res < kMinCredibleResolution || y_res > kMaxCredibleResolution) {
2027  // Use the minimum default resolution, as it is safer to under-estimate
2028  // than over-estimate resolution.
2030  }
2031  PageSegMode pageseg_mode =
2032  static_cast<PageSegMode>(
2033  static_cast<int>(tesseract_->tessedit_pageseg_mode));
2034  thresholder_->ThresholdToPix(pageseg_mode, pix);
2038  if (!thresholder_->IsBinary()) {
2041  } else {
2044  }
2045  // Set the internal resolution that is used for layout parameters from the
2046  // estimated resolution, rather than the image resolution, which may be
2047  // fabricated, but we will use the image resolution, if there is one, to
2048  // report output point sizes.
2049  int estimated_res = ClipToRange(thresholder_->GetScaledEstimatedResolution(),
2052  if (estimated_res != thresholder_->GetScaledEstimatedResolution()) {
2053  tprintf("Estimated resolution %d out of range! Corrected to %d\n",
2054  thresholder_->GetScaledEstimatedResolution(), estimated_res);
2055  }
2056  tesseract_->set_source_resolution(estimated_res);
2057  SavePixForCrash(estimated_res, *pix);
2058 }
2059 
2062  if (thresholder_ == NULL || thresholder_->IsEmpty()) {
2063  tprintf("Please call SetImage before attempting recognition.");
2064  return -1;
2065  }
2066  if (recognition_done_)
2067  ClearResults();
2068  if (!block_list_->empty()) {
2069  return 0;
2070  }
2071  if (tesseract_ == NULL) {
2072  tesseract_ = new Tesseract;
2074  }
2075  if (tesseract_->pix_binary() == NULL)
2077  if (tesseract_->ImageWidth() > MAX_INT16 ||
2079  tprintf("Image too large: (%d, %d)\n",
2081  return -1;
2082  }
2083 
2085 
2087  if (equ_detect_ == NULL && datapath_ != NULL) {
2089  }
2091  }
2092 
2093  Tesseract* osd_tess = osd_tesseract_;
2094  OSResults osr;
2095  if (PSM_OSD_ENABLED(tesseract_->tessedit_pageseg_mode) && osd_tess == NULL) {
2096  if (strcmp(language_->string(), "osd") == 0) {
2097  osd_tess = tesseract_;
2098  } else {
2099  osd_tesseract_ = new Tesseract;
2102  NULL, 0, NULL, NULL, false) == 0) {
2103  osd_tess = osd_tesseract_;
2106  } else {
2107  tprintf("Warning: Auto orientation and script detection requested,"
2108  " but osd language failed to load\n");
2109  delete osd_tesseract_;
2110  osd_tesseract_ = NULL;
2111  }
2112  }
2113  }
2114 
2115  if (tesseract_->SegmentPage(input_file_, block_list_, osd_tess, &osr) < 0)
2116  return -1;
2117  // If Devanagari is being recognized, we use different images for page seg
2118  // and for OCR.
2119  tesseract_->PrepareForTessOCR(block_list_, osd_tess, &osr);
2120  return 0;
2121 }
2122 
2125  if (tesseract_ != NULL) {
2126  tesseract_->Clear();
2127  }
2128  if (page_res_ != NULL) {
2129  delete page_res_;
2130  page_res_ = NULL;
2131  }
2132  recognition_done_ = false;
2133  if (block_list_ == NULL)
2134  block_list_ = new BLOCK_LIST;
2135  else
2136  block_list_->clear();
2137  if (paragraph_models_ != NULL) {
2139  delete paragraph_models_;
2141  }
2142  SavePixForCrash(0, NULL);
2143 }
2144 
2152 int TessBaseAPI::TextLength(int* blob_count) {
2153  if (tesseract_ == NULL || page_res_ == NULL)
2154  return 0;
2155 
2156  PAGE_RES_IT page_res_it(page_res_);
2157  int total_length = 2;
2158  int total_blobs = 0;
2159  // Iterate over the data structures to extract the recognition result.
2160  for (page_res_it.restart_page(); page_res_it.word () != NULL;
2161  page_res_it.forward()) {
2162  WERD_RES *word = page_res_it.word();
2163  WERD_CHOICE* choice = word->best_choice;
2164  if (choice != NULL) {
2165  total_blobs += choice->length() + 2;
2166  total_length += choice->unichar_string().length() + 2;
2167  for (int i = 0; i < word->reject_map.length(); ++i) {
2168  if (word->reject_map[i].rejected())
2169  ++total_length;
2170  }
2171  }
2172  }
2173  if (blob_count != NULL)
2174  *blob_count = total_blobs;
2175  return total_length;
2176 }
2177 
2183  if (tesseract_ == NULL)
2184  return false;
2185  ClearResults();
2186  if (tesseract_->pix_binary() == NULL)
2188  if (input_file_ == NULL)
2189  input_file_ = new STRING(kInputFile);
2191 }
2192 
2194  tesseract_->min_orientation_margin.set_value(margin);
2195 }
2196 
2211 void TessBaseAPI::GetBlockTextOrientations(int** block_orientation,
2212  bool** vertical_writing) {
2213  delete[] *block_orientation;
2214  *block_orientation = NULL;
2215  delete[] *vertical_writing;
2216  *vertical_writing = NULL;
2217  BLOCK_IT block_it(block_list_);
2218 
2219  block_it.move_to_first();
2220  int num_blocks = 0;
2221  for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
2222  if (!block_it.data()->poly_block()->IsText()) {
2223  continue;
2224  }
2225  ++num_blocks;
2226  }
2227  if (!num_blocks) {
2228  tprintf("WARNING: Found no blocks\n");
2229  return;
2230  }
2231  *block_orientation = new int[num_blocks];
2232  *vertical_writing = new bool[num_blocks];
2233  block_it.move_to_first();
2234  int i = 0;
2235  for (block_it.mark_cycle_pt(); !block_it.cycled_list();
2236  block_it.forward()) {
2237  if (!block_it.data()->poly_block()->IsText()) {
2238  continue;
2239  }
2240  FCOORD re_rotation = block_it.data()->re_rotation();
2241  float re_theta = re_rotation.angle();
2242  FCOORD classify_rotation = block_it.data()->classify_rotation();
2243  float classify_theta = classify_rotation.angle();
2244  double rot_theta = - (re_theta - classify_theta) * 2.0 / PI;
2245  if (rot_theta < 0) rot_theta += 4;
2246  int num_rotations = static_cast<int>(rot_theta + 0.5);
2247  (*block_orientation)[i] = num_rotations;
2248  // The classify_rotation is non-zero only if the text has vertical
2249  // writing direction.
2250  (*vertical_writing)[i] = classify_rotation.y() != 0.0f;
2251  ++i;
2252  }
2253 }
2254 
2255 // ____________________________________________________________________________
2256 // Ocropus add-ons.
2257 
2260  FindLines();
2261  BLOCK_LIST* result = block_list_;
2262  block_list_ = NULL;
2263  return result;
2264 }
2265 
2271 void TessBaseAPI::DeleteBlockList(BLOCK_LIST *block_list) {
2272  delete block_list;
2273 }
2274 
2275 
2277  float xheight,
2278  float descender,
2279  float ascender) {
2280  inT32 xstarts[] = {-32000};
2281  double quad_coeffs[] = {0, 0, baseline};
2282  return new ROW(1,
2283  xstarts,
2284  quad_coeffs,
2285  xheight,
2286  ascender - (baseline + xheight),
2287  descender - baseline,
2288  0,
2289  0);
2290 }
2291 
2294  int width = pixGetWidth(pix);
2295  int height = pixGetHeight(pix);
2296  BLOCK block("a character", TRUE, 0, 0, 0, 0, width, height);
2297 
2298  // Create C_BLOBs from the page
2299  extract_edges(pix, &block);
2300 
2301  // Merge all C_BLOBs
2302  C_BLOB_LIST *list = block.blob_list();
2303  C_BLOB_IT c_blob_it(list);
2304  if (c_blob_it.empty())
2305  return NULL;
2306  // Move all the outlines to the first blob.
2307  C_OUTLINE_IT ol_it(c_blob_it.data()->out_list());
2308  for (c_blob_it.forward();
2309  !c_blob_it.at_first();
2310  c_blob_it.forward()) {
2311  C_BLOB *c_blob = c_blob_it.data();
2312  ol_it.add_list_after(c_blob->out_list());
2313  }
2314  // Convert the first blob to the output TBLOB.
2315  return TBLOB::PolygonalCopy(false, c_blob_it.data());
2316 }
2317 
2323 void TessBaseAPI::NormalizeTBLOB(TBLOB *tblob, ROW *row, bool numeric_mode) {
2324  TBOX box = tblob->bounding_box();
2325  float x_center = (box.left() + box.right()) / 2.0f;
2326  float baseline = row->base_line(x_center);
2327  float scale = kBlnXHeight / row->x_height();
2328  tblob->Normalize(NULL, NULL, NULL, x_center, baseline, scale, scale,
2329  0.0f, static_cast<float>(kBlnBaselineOffset), false, NULL);
2330 }
2331 
2336 TBLOB *make_tesseract_blob(float baseline, float xheight,
2337  float descender, float ascender,
2338  bool numeric_mode, Pix* pix) {
2339  TBLOB *tblob = TessBaseAPI::MakeTBLOB(pix);
2340 
2341  // Normalize TBLOB
2342  ROW *row =
2343  TessBaseAPI::MakeTessOCRRow(baseline, xheight, descender, ascender);
2344  TessBaseAPI::NormalizeTBLOB(tblob, row, numeric_mode);
2345  delete row;
2346  return tblob;
2347 }
2348 
2354 void TessBaseAPI::AdaptToCharacter(const char *unichar_repr,
2355  int length,
2356  float baseline,
2357  float xheight,
2358  float descender,
2359  float ascender) {
2360  UNICHAR_ID id = tesseract_->unicharset.unichar_to_id(unichar_repr, length);
2361  TBLOB *blob = make_tesseract_blob(baseline, xheight, descender, ascender,
2363  tesseract_->pix_binary());
2364  float threshold;
2365  float best_rating = -100;
2366 
2367 
2368  // Classify to get a raw choice.
2369  BLOB_CHOICE_LIST choices;
2370  tesseract_->AdaptiveClassifier(blob, &choices);
2371  BLOB_CHOICE_IT choice_it;
2372  choice_it.set_to_list(&choices);
2373  for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
2374  choice_it.forward()) {
2375  if (choice_it.data()->rating() > best_rating) {
2376  best_rating = choice_it.data()->rating();
2377  }
2378  }
2379 
2380  threshold = tesseract_->matcher_good_threshold;
2381 
2382  if (blob->outlines)
2383  tesseract_->AdaptToChar(blob, id, kUnknownFontinfoId, threshold,
2385  delete blob;
2386 }
2387 
2388 
2389 PAGE_RES* TessBaseAPI::RecognitionPass1(BLOCK_LIST* block_list) {
2390  PAGE_RES *page_res = new PAGE_RES(false, block_list,
2392  tesseract_->recog_all_words(page_res, NULL, NULL, NULL, 1);
2393  return page_res;
2394 }
2395 
2396 PAGE_RES* TessBaseAPI::RecognitionPass2(BLOCK_LIST* block_list,
2397  PAGE_RES* pass1_result) {
2398  if (!pass1_result)
2399  pass1_result = new PAGE_RES(false, block_list,
2401  tesseract_->recog_all_words(pass1_result, NULL, NULL, NULL, 2);
2402  return pass1_result;
2403 }
2404 
2405 void TessBaseAPI::DetectParagraphs(bool after_text_recognition) {
2406  int debug_level = 0;
2407  GetIntVariable("paragraph_debug_level", &debug_level);
2408  if (paragraph_models_ == NULL)
2410  MutableIterator *result_it = GetMutableIterator();
2411  do { // Detect paragraphs for this block
2413  ::tesseract::DetectParagraphs(debug_level, after_text_recognition,
2414  result_it, &models);
2415  *paragraph_models_ += models;
2416  } while (result_it->Next(RIL_BLOCK));
2417  delete result_it;
2418 }
2419 
2422  int length; // of unicode_repr
2423  float cost;
2425 
2426  TESS_CHAR(float _cost, const char *repr, int len = -1) : cost(_cost) {
2427  length = (len == -1 ? strlen(repr) : len);
2428  unicode_repr = new char[length + 1];
2429  strncpy(unicode_repr, repr, length);
2430  }
2431 
2432  TESS_CHAR() { // Satisfies ELISTIZE.
2433  }
2435  delete [] unicode_repr;
2436  }
2437 };
2438 
2439 ELISTIZEH(TESS_CHAR)
2440 ELISTIZE(TESS_CHAR)
2441 
2442 static void add_space(TESS_CHAR_IT* it) {
2443  TESS_CHAR *t = new TESS_CHAR(0, " ");
2444  it->add_after_then_move(t);
2445 }
2446 
2447 
2448 static float rating_to_cost(float rating) {
2449  rating = 100 + rating;
2450  // cuddled that to save from coverage profiler
2451  // (I have never seen ratings worse than -100,
2452  // but the check won't hurt)
2453  if (rating < 0) rating = 0;
2454  return rating;
2455 }
2456 
2461 static void extract_result(TESS_CHAR_IT* out,
2462  PAGE_RES* page_res) {
2463  PAGE_RES_IT page_res_it(page_res);
2464  int word_count = 0;
2465  while (page_res_it.word() != NULL) {
2466  WERD_RES *word = page_res_it.word();
2467  const char *str = word->best_choice->unichar_string().string();
2468  const char *len = word->best_choice->unichar_lengths().string();
2469  TBOX real_rect = word->word->bounding_box();
2470 
2471  if (word_count)
2472  add_space(out);
2473  int n = strlen(len);
2474  for (int i = 0; i < n; i++) {
2475  TESS_CHAR *tc = new TESS_CHAR(rating_to_cost(word->best_choice->rating()),
2476  str, *len);
2477  tc->box = real_rect.intersection(word->box_word->BlobBox(i));
2478  out->add_after_then_move(tc);
2479  str += *len;
2480  len++;
2481  }
2482  page_res_it.forward();
2483  word_count++;
2484  }
2485 }
2486 
2492  int** lengths,
2493  float** costs,
2494  int** x0,
2495  int** y0,
2496  int** x1,
2497  int** y1,
2498  PAGE_RES* page_res) {
2499  TESS_CHAR_LIST tess_chars;
2500  TESS_CHAR_IT tess_chars_it(&tess_chars);
2501  extract_result(&tess_chars_it, page_res);
2502  tess_chars_it.move_to_first();
2503  int n = tess_chars.length();
2504  int text_len = 0;
2505  *lengths = new int[n];
2506  *costs = new float[n];
2507  *x0 = new int[n];
2508  *y0 = new int[n];
2509  *x1 = new int[n];
2510  *y1 = new int[n];
2511  int i = 0;
2512  for (tess_chars_it.mark_cycle_pt();
2513  !tess_chars_it.cycled_list();
2514  tess_chars_it.forward(), i++) {
2515  TESS_CHAR *tc = tess_chars_it.data();
2516  text_len += (*lengths)[i] = tc->length;
2517  (*costs)[i] = tc->cost;
2518  (*x0)[i] = tc->box.left();
2519  (*y0)[i] = tc->box.bottom();
2520  (*x1)[i] = tc->box.right();
2521  (*y1)[i] = tc->box.top();
2522  }
2523  char *p = *text = new char[text_len];
2524 
2525  tess_chars_it.move_to_first();
2526  for (tess_chars_it.mark_cycle_pt();
2527  !tess_chars_it.cycled_list();
2528  tess_chars_it.forward()) {
2529  TESS_CHAR *tc = tess_chars_it.data();
2530  strncpy(p, tc->unicode_repr, tc->length);
2531  p += tc->length;
2532  }
2533  return n;
2534 }
2535 
2537 // The resulting features are returned in int_features, which must be
2538 // of size MAX_NUM_INT_FEATURES. The number of features is returned in
2539 // num_features (or 0 if there was a failure).
2540 // On return feature_outline_index is filled with an index of the outline
2541 // corresponding to each feature in int_features.
2542 // TODO(rays) Fix the caller to out outline_counts instead.
2544  INT_FEATURE_STRUCT* int_features,
2545  int* num_features,
2546  int* feature_outline_index) {
2547  GenericVector<int> outline_counts;
2550  INT_FX_RESULT_STRUCT fx_info;
2551  tesseract_->ExtractFeatures(*blob, false, &bl_features,
2552  &cn_features, &fx_info, &outline_counts);
2553  if (cn_features.size() == 0 || cn_features.size() > MAX_NUM_INT_FEATURES) {
2554  *num_features = 0;
2555  return; // Feature extraction failed.
2556  }
2557  *num_features = cn_features.size();
2558  memcpy(int_features, &cn_features[0], *num_features * sizeof(cn_features[0]));
2559  // TODO(rays) Pass outline_counts back and simplify the calling code.
2560  if (feature_outline_index != NULL) {
2561  int f = 0;
2562  for (int i = 0; i < outline_counts.size(); ++i) {
2563  while (f < outline_counts[i])
2564  feature_outline_index[f++] = i;
2565  }
2566  }
2567 }
2568 
2569 // This method returns the row to which a box of specified dimensions would
2570 // belong. If no good match is found, it returns NULL.
2571 ROW* TessBaseAPI::FindRowForBox(BLOCK_LIST* blocks,
2572  int left, int top, int right, int bottom) {
2573  TBOX box(left, bottom, right, top);
2574  BLOCK_IT b_it(blocks);
2575  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
2576  BLOCK* block = b_it.data();
2577  if (!box.major_overlap(block->bounding_box()))
2578  continue;
2579  ROW_IT r_it(block->row_list());
2580  for (r_it.mark_cycle_pt(); !r_it.cycled_list(); r_it.forward()) {
2581  ROW* row = r_it.data();
2582  if (!box.major_overlap(row->bounding_box()))
2583  continue;
2584  WERD_IT w_it(row->word_list());
2585  for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
2586  WERD* word = w_it.data();
2587  if (box.major_overlap(word->bounding_box()))
2588  return row;
2589  }
2590  }
2591  }
2592  return NULL;
2593 }
2594 
2597  int num_max_matches,
2598  int* unichar_ids,
2599  float* ratings,
2600  int* num_matches_returned) {
2601  BLOB_CHOICE_LIST* choices = new BLOB_CHOICE_LIST;
2602  tesseract_->AdaptiveClassifier(blob, choices);
2603  BLOB_CHOICE_IT choices_it(choices);
2604  int& index = *num_matches_returned;
2605  index = 0;
2606  for (choices_it.mark_cycle_pt();
2607  !choices_it.cycled_list() && index < num_max_matches;
2608  choices_it.forward()) {
2609  BLOB_CHOICE* choice = choices_it.data();
2610  unichar_ids[index] = choice->unichar_id();
2611  ratings[index] = choice->rating();
2612  ++index;
2613  }
2614  *num_matches_returned = index;
2615  delete choices;
2616 }
2617 
2619 const char* TessBaseAPI::GetUnichar(int unichar_id) {
2620  return tesseract_->unicharset.id_to_unichar(unichar_id);
2621 }
2622 
2624 const Dawg *TessBaseAPI::GetDawg(int i) const {
2625  if (tesseract_ == NULL || i >= NumDawgs()) return NULL;
2626  return tesseract_->getDict().GetDawg(i);
2627 }
2628 
2631  return tesseract_ == NULL ? 0 : tesseract_->getDict().NumDawgs();
2632 }
2633 
2634 #ifndef ANDROID_BUILD
2635 
2637  return (tesseract_ == NULL) ? NULL : tesseract_->GetCubeRecoContext();
2638 }
2639 #endif
2640 
2642 STRING HOcrEscape(const char* text) {
2643  STRING ret;
2644  const char *ptr;
2645  for (ptr = text; *ptr; ptr++) {
2646  switch (*ptr) {
2647  case '<': ret += "&lt;"; break;
2648  case '>': ret += "&gt;"; break;
2649  case '&': ret += "&amp;"; break;
2650  case '"': ret += "&quot;"; break;
2651  case '\'': ret += "&#39;"; break;
2652  default: ret += *ptr;
2653  }
2654  }
2655  return ret;
2656 }
2657 
2658 } // namespace tesseract.
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:642
#define PI
Definition: const.h:19
const int kBlnXHeight
Definition: normalis.h:28
Definition: blobs.h:261
C_BLOB_LIST * blob_list()
get blobs
Definition: ocrblock.h:132
void SavePixForCrash(int resolution, Pix *pix)
Definition: globaloc.cpp:34
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:205
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:160
static const char * Version()
Definition: baseapi.cpp:142
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:357
bool GetIntVariable(const char *name, int *value) const
Definition: baseapi.cpp:228
int size() const
Definition: genericvector.h:72
void CorrectClassifyWords(PAGE_RES *page_res)
Definition: applybox.cpp:755
static size_t getOpenCLDevice(void **device)
Definition: baseapi.cpp:162
tesseract::BoxWord * box_word
Definition: pageres.h:250
const char * WordFontAttributes(bool *is_bold, bool *is_italic, bool *is_underlined, bool *is_monospace, bool *is_serif, bool *is_smallcaps, int *pointsize, int *font_id) const
static void NormalizeTBLOB(TBLOB *tblob, ROW *row, bool numeric_mode)
Definition: baseapi.cpp:2323
bool classify_bln_numeric_mode
Definition: classify.h:500
TBLOB * make_tesseract_blob(float baseline, float xheight, float descender, float ascender, bool numeric_mode, Pix *pix)
Definition: baseapi.cpp:2336
float rating() const
Definition: ratngs.h:324
static TESS_LOCAL int TesseractExtractResult(char **text, int **lengths, float **costs, int **x0, int **y0, int **x1, int **y1, PAGE_RES *page_res)
Definition: baseapi.cpp:2491
Boxa * GetTextlines(const bool raw_image, const int raw_padding, Pixa **pixa, int **blockids, int **paraids)
Definition: baseapi.cpp:597
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:194
#define BOOL
Definition: capi.h:27
EquationDetect * equ_detect_
The equation detector.
Definition: baseapi.h:840
bool stream_filelist
Definition: baseapi.cpp:81
inT32 length() const
Definition: rejctmap.h:237
char * GetBoxText(int page_number)
Definition: baseapi.cpp:1579
BLOCK_LIST * FindLinesCreateBlockList()
Definition: baseapi.cpp:2259
void(Wordrec::* fill_lattice_)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
Definition: wordrec.h:420
#define MAX(x, y)
Definition: ndminx.h:24
void set_text(const char *new_text)
Definition: werd.h:126
const char kTesseractReject
Definition: baseapi.cpp:88
void set_deadline_msecs(inT32 deadline_msecs)
Definition: ocrclass.h:132
void recog_training_segmented(const STRING &fname, PAGE_RES *page_res, volatile ETEXT_DESC *monitor, FILE *output_file)
struct TessResultRenderer TessResultRenderer
Definition: capi.h:61
bool IsValidCharacter(const char *utf8_character)
Definition: baseapi.cpp:1930
void GetAvailableLanguagesAsVector(GenericVector< STRING > *langs) const
Definition: baseapi.cpp:370
TESS_LOCAL void AdaptToCharacter(const char *unichar_repr, int length, float baseline, float xheight, float descender, float ascender)
Definition: baseapi.cpp:2354
void GetFeaturesForBlob(TBLOB *blob, INT_FEATURE_STRUCT *int_features, int *num_features, int *feature_outline_index)
Definition: baseapi.cpp:2543
int length() const
Definition: ratngs.h:300
WERD_CHOICE * best_choice
Definition: pageres.h:219
OcrEngineMode last_oem_requested_
Last ocr language mode requested.
Definition: baseapi.h:850
void set_pix_thresholds(Pix *thresholds)
virtual Pix * GetPixRectThresholds()
int push_back(T object)
REJMAP reject_map
Definition: pageres.h:271
TruthCallback * truth_cb_
Definition: baseapi.h:852
ELISTIZEH(AmbigSpec)
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
Definition: dict.cpp:705
Orientation and script detection only.
Definition: publictypes.h:152
bool BoundingBoxInternal(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
void SetRectangle(int left, int top, int width, int height)
Definition: baseapi.cpp:561
#define tprintf(...)
Definition: tprintf.h:31
int ImageHeight() const
const char * WordRecognitionLanguage() const
bool AddImage(TessBaseAPI *api)
Definition: renderer.cpp:64
const TBOX & BlobBox(int index) const
Definition: boxword.h:88
const int kMaxCredibleResolution
Definition: baseapi.cpp:110
bool ProcessPages(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1070
const Dawg * GetDawg(int i) const
Definition: baseapi.cpp:2624
double matcher_good_threshold
Definition: classify.h:420
UNICHARSET unicharset
Definition: ccutil.h:72
void * cancel_this
Definition: ocrclass.h:120
int IsValidWord(const char *word)
Definition: baseapi.cpp:1926
bool Empty(PageIteratorLevel level) const
virtual void GetImageSizes(int *left, int *top, int *width, int *height, int *imagewidth, int *imageheight)
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:180
void split(const char c, GenericVector< STRING > *splited)
Definition: strngs.cpp:281
virtual bool Next(PageIteratorLevel level)
void ReadConfigFile(const char *filename)
Definition: baseapi.cpp:446
const int kBytesPerNumber
Definition: baseapi.cpp:1555
const STRING & unichar_lengths() const
Definition: ratngs.h:531
#define BOOL_VAR(name, val, comment)
Definition: params.h:280
GenericVector< ParagraphModel * > * paragraph_models_
Definition: baseapi.h:842
unsigned char BOOL8
Definition: host.h:113
virtual R Run()=0
Boxa * GetWords(Pixa **pixa)
Definition: baseapi.cpp:620
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
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
TBOX bounding_box() const
Definition: werd.cpp:160
ImageThresholder * thresholder_
Image thresholding module.
Definition: baseapi.h:841
static ResultIterator * StartOfParagraph(const LTRResultIterator &resit)
inT32 length() const
Definition: strngs.cpp:188
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:142
virtual char * GetUTF8Text(PageIteratorLevel level) const
bool SetVariable(const char *name, const char *value)
Definition: baseapi.cpp:216
static ROW * FindRowForBox(BLOCK_LIST *blocks, int left, int top, int right, int bottom)
Definition: baseapi.cpp:2571
Boxa * GetStrips(Pixa **pixa, int **blockids)
Definition: baseapi.cpp:611
TESS_LOCAL int TextLength(int *blob_count)
Definition: baseapi.cpp:2152
void set_pix_grey(Pix *grey_pix)
const int kNumbersPerBlob
Definition: baseapi.cpp:1550
void set_source_resolution(int ppi)
TESS_CHAR(float _cost, const char *repr, int len=-1)
Definition: baseapi.cpp:2426
Pix * input_image_
Image used for searchable PDF.
Definition: baseapi.h:846
STRING * language_
Last initialized language.
Definition: baseapi.h:849
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: thresholder.cpp:62
CMD_EVENTS mode
Definition: pgedit.cpp:116
bool GetDoubleVariable(const char *name, double *value) const
Definition: baseapi.cpp:250
Tesseract * get_sub_lang(int index) const
const int kBytesPerBlob
Definition: baseapi.cpp:1561
char * GetHOCRText(int page_number)
Definition: baseapi.cpp:1399
FILE * init_recog_training(const STRING &fname)
inT16 right() const
Definition: rect.h:75
PAGE_RES * ApplyBoxes(const STRING &fname, bool find_segmentation, BLOCK_LIST *block_list)
Definition: applybox.cpp:110
void(Wordrec::* FillLatticeFunc)(const MATRIX &ratings, const WERD_CHOICE_LIST &best_choices, const UNICHARSET &unicharset, BlamerBundle *blamer_bundle)
Definition: baseapi.h:90
#define GIT_REV
Definition: vcsversion.h:1
Pix * GetBinaryImage(PageIteratorLevel level) const
const char * GetInitLanguagesAsString() const
Definition: baseapi.cpp:346
const int kLatinChs[]
Definition: baseapi.cpp:1625
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:293
int SegmentPage(const STRING *input_file, BLOCK_LIST *blocks, Tesseract *osd_tess, OSResults *osr)
OcrEngineMode const oem() const
Definition: baseapi.h:732
void RunAdaptiveClassifier(TBLOB *blob, int num_max_matches, int *unichar_ids, float *ratings, int *num_matches_returned)
Definition: baseapi.cpp:2596
BLOCK * block
Definition: pageres.h:99
#define round(x)
Definition: mathfix.h:34
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
const char * GetInputName()
Definition: baseapi.cpp:948
TESS_LOCAL bool InternalSetImage()
Definition: baseapi.cpp:2003
#define ASSERT_HOST(x)
Definition: errcode.h:84
Definition: ocrrow.h:32
bool IsBinary() const
Returns true if the source image is binary.
Definition: thresholder.h:75
int GetScaledYResolution() const
Definition: thresholder.h:93
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
Definition: blobs.cpp:344
bool GetVariableAsString(const char *name, STRING *val)
Definition: baseapi.cpp:259
float base_line(float xpos) const
Definition: ocrrow.h:56
bool GetTextDirection(int *out_offset, float *out_slope)
Definition: baseapi.cpp:1937
const char * GetDatapath()
Definition: baseapi.cpp:954
int NumDawgs() const
Definition: baseapi.cpp:2630
WERD_CHOICE * prev_word_best_choice_
Definition: wordrec.h:416
const STRING & unichar_string() const
Definition: ratngs.h:524
Definition: werd.h:35
static void ClearPersistentCache()
Definition: baseapi.cpp:1918
PageSegMode GetPageSegMode() const
Definition: baseapi.cpp:467
PolyBlockType BlockType() const
void SetFillLatticeFunc(FillLatticeFunc f)
Definition: baseapi.cpp:1998
#define DIR
Definition: polyaprx.cpp:39
#define MAX_PATH
Definition: platform.h:41
int ImageWidth() const
float angle() const
find angle
Definition: points.h:249
BLOCK_RES * block() const
Definition: pageres.h:739
STRING * output_file_
Name used by debug code.
Definition: baseapi.h:847
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:64
StrongScriptDirection WordDirection() const
WERD_RES * forward()
Definition: pageres.h:713
void SetRectangle(int left, int top, int width, int height)
void ClearAdaptiveClassifier()
Definition: baseapi.cpp:509
bool GetBoolVariable(const char *name, bool *value) const
Definition: baseapi.cpp:236
bool ProcessPagesInternal(const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1097
float rating() const
Definition: ratngs.h:79
void TidyUp(PAGE_RES *page_res)
Definition: applybox.cpp:689
bool wordrec_run_blamer
Definition: wordrec.h:168
CANCEL_FUNC cancel
Definition: ocrclass.h:119
const int kMinRectSize
Definition: baseapi.cpp:86
WERD_RES * restart_page()
Definition: pageres.h:680
static DawgCache * GlobalDawgCache()
Definition: dict.cpp:186
virtual Pix * GetPixRectGrey()
CubeRecoContext * GetCubeRecoContext()
const int kBytesPerBoxFileLine
Definition: baseapi.cpp:1562
Pix * GetThresholdedImage()
Definition: baseapi.cpp:572
void chomp_string(char *str)
Definition: helpers.h:75
void SetInputName(const char *name)
Definition: baseapi.cpp:201
Definition: werd.h:36
virtual bool IsAtBeginningOf(PageIteratorLevel level) const
STRING datadir
Definition: ccutil.h:67
inT16 left() const
Definition: rect.h:68
static ROW * MakeTessOCRRow(float baseline, float xheight, float descender, float ascender)
Definition: baseapi.cpp:2276
void PrepareForTessOCR(BLOCK_LIST *block_list, Tesseract *osd_tess, OSResults *osr)
float certainty() const
Definition: ratngs.h:327
int num_sub_langs() const
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
Definition: adaptmatch.cpp:185
bool tessedit_resegment_from_line_boxes
void delete_data_pointers()
STRING HOcrEscape(const char *text)
Definition: baseapi.cpp:2642
int GetScaledEstimatedResolution() const
Definition: thresholder.h:106
const char *const id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:266
int first_uni() const
Definition: unichar.cpp:97
GenericVector< IntParam * > int_params
Definition: params.h:44
const int kMinCredibleResolution
Minimum believable resolution.
Definition: baseapi.cpp:108
Definition: ocrblock.h:30
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:844
PageIterator * AnalyseLayout()
Definition: baseapi.h:498
int GetSourceYResolution() const
Definition: thresholder.h:90
const char * kInputFile
Definition: baseapi.cpp:97
#define PERF_COUNT_END
virtual ~TessBaseAPI()
Definition: baseapi.cpp:135
ROW_RES * row() const
Definition: pageres.h:736
Boxa * GetConnectedComponents(Pixa **cc)
Definition: baseapi.cpp:630
virtual TESS_LOCAL void Threshold(Pix **pix)
Definition: baseapi.cpp:2020
void read_config_file(const char *filename, SetParamConstraint constraint)
Definition: tessedit.cpp:52
int GetThresholdedImageScaleFactor() const
Definition: baseapi.cpp:724
STRING * datapath_
Current location of tessdata.
Definition: baseapi.h:848
virtual bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const
Pix * GetImage(PageIteratorLevel level, int padding, Pix *original_img, int *left, int *top) const
TBOX bounding_box() const
Definition: ocrrow.h:85
double(Dict::* ProbabilityInContextFunc)(const char *lang, const char *context, int context_bytes, const char *character, int character_bytes)
Definition: baseapi.h:83
bool ProcessPage(Pix *pix, int page_index, const char *filename, const char *retry_config, int timeout_millisec, TessResultRenderer *renderer)
Definition: baseapi.cpp:1194
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:67
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:98
void DeleteUnusedDawgs()
Definition: dawg_cache.h:46
MutableIterator * GetMutableIterator()
Definition: baseapi.cpp:1288
bool Baseline(PageIteratorLevel level, int *x1, int *y1, int *x2, int *y2) const
void pgeditor_main(int width, int height, PAGE_RES *page_res)
Definition: pgedit.cpp:337
Pix * pix_grey() const
void AdaptToChar(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, FLOAT32 Threshold, ADAPT_TEMPLATES adaptive_templates)
Definition: adaptmatch.cpp:887
static void DeleteBlockList(BLOCK_LIST *block_list)
Definition: baseapi.cpp:2271
Dict & getDict()
Definition: classify.h:65
#define MAX_NUM_INT_FEATURES
Definition: intproto.h:132
TESS_LOCAL int FindLines()
Definition: baseapi.cpp:2061
#define PERF_COUNT_SUB(SUB)
int orientation_and_script_detection(STRING &filename, OSResults *osr, tesseract::Tesseract *tess)
Definition: osdetect.cpp:189
void DumpPGM(const char *filename)
Definition: baseapi.cpp:732
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
const char * GetUnichar(int unichar_id)
Definition: baseapi.cpp:2619
const int kBlnBaselineOffset
Definition: normalis.h:29
virtual void ThresholdToPix(PageSegMode pageseg_mode, Pix **pix)
GenericVector< BoolParam * > bool_params
Definition: params.h:45
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: baseapi.cpp:525
int UNICHAR_ID
Definition: unichar.h:33
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:830
ResultIterator * GetIterator()
Definition: baseapi.cpp:1271
void signal_exit(int signal_code)
Definition: globaloc.cpp:52
void SetSourceYResolution(int ppi)
Definition: thresholder.h:86
bool AdaptToWordStr(PageSegMode mode, const char *wordstr)
Definition: baseapi.cpp:1782
Definition: werd.h:60
TESS_LOCAL PAGE_RES * RecognitionPass1(BLOCK_LIST *block_list)
Definition: baseapi.cpp:2389
int CubeAPITest(Boxa *boxa_blocks, Pixa *pixa_blocks, Boxa *boxa_words, Pixa *pixa_words, const FCOORD &reskew, Pix *page_pix, PAGE_RES *page_res)
Definition: baseapi.cpp:757
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:276
inT16 bottom() const
Definition: rect.h:61
void set_unlv_suspects(WERD_RES *word)
Definition: output.cpp:307
bool major_overlap(const TBOX &box) const
Definition: rect.h:358
void ReadDebugConfigFile(const char *filename)
Definition: baseapi.cpp:451
void SetOutputName(const char *name)
Definition: baseapi.cpp:209
WERD * word
Definition: pageres.h:175
virtual void Run(A1, A2, A3, A4)=0
bool SetDebugVariable(const char *name, const char *value)
Definition: baseapi.cpp:222
const char * GetStringVariable(const char *name) const
Definition: baseapi.cpp:244
bool empty() const
Definition: genericvector.h:84
ParamsVectors * params()
Definition: ccutil.h:65
bool DetectOS(OSResults *)
Definition: baseapi.cpp:2182
static TBLOB * MakeTBLOB(Pix *pix)
Definition: baseapi.cpp:2293
const int NumDawgs() const
Return the number of dawgs in the dawgs_ vector.
Definition: dict.h:404
void add_str_int(const char *str, int number)
Definition: strngs.cpp:376
bool IsEmpty() const
Return true if no image has been set.
Definition: thresholder.cpp:50
#define TESSERACT_VERSION_STR
Definition: baseapi.h:23
void assign(const char *cstr, int len)
Definition: strngs.cpp:417
TBOX intersection(const TBOX &box) const
Definition: rect.cpp:87
void ApplyBoxTraining(const STRING &fontname, PAGE_RES *page_res)
Definition: applybox.cpp:779
void GetBlockTextOrientations(int **block_orientation, bool **vertical_writing)
Definition: baseapi.cpp:2211
int(Dict::* letter_is_okay_)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
Definition: dict.h:347
#define FALSE
Definition: capi.h:29
int init_tesseract(const char *arg0, const char *textbase, const char *language, OcrEngineMode oem, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_init_params)
Definition: tessedit.cpp:285
const UNICHARSET & getUnicharset() const
Definition: dict.h:96
void SetProbabilityInContextFunc(ProbabilityInContextFunc f)
Definition: baseapi.cpp:1986
Tesseract * osd_tesseract_
For orientation & script detection.
Definition: baseapi.h:839
TESS_LOCAL PAGE_RES * RecognitionPass2(BLOCK_LIST *block_list, PAGE_RES *pass1_result)
Definition: baseapi.cpp:2396
CubeRecoContext * GetCubeRecoContext() const
Definition: baseapi.cpp:2636
GenericVector< DoubleParam * > double_params
Definition: params.h:47
int IntCastRounded(double x)
Definition: helpers.h:172
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
void SetEquationDetect(EquationDetect *detector)
void DetectParagraphs(int debug_level, GenericVector< RowInfo > *row_infos, GenericVector< PARA * > *row_owners, PARA_LIST *paragraphs, GenericVector< ParagraphModel * > *models)
ROW * row
Definition: pageres.h:127
const char kUNLVSuspect
Definition: baseapi.cpp:92
BLOCK_LIST * block_list_
The page layout.
Definition: baseapi.h:843
Definition: rect.h:30
void SetDictFunc(DictFunc f)
Definition: baseapi.cpp:1972
#define TRUE
Definition: capi.h:28
#define PERF_COUNT_START(FUNCT_NAME)
#define MAX_INT16
Definition: host.h:119
void MaximallyChopWord(const GenericVector< TBOX > &boxes, BLOCK *block, ROW *row, WERD_RES *word_res)
Definition: applybox.cpp:246
Automatic page segmentation, but no OSD, or OCR.
Definition: publictypes.h:155
float y() const
Definition: points.h:212
virtual void Clear()
Destroy the Pix if there is one, freeing memory.
Definition: thresholder.cpp:45
void Orientation(tesseract::Orientation *orientation, tesseract::WritingDirection *writing_direction, tesseract::TextlineOrder *textline_order, float *deskew_angle) const
bool PTIsTextType(PolyBlockType type)
Definition: publictypes.h:70
STRING lang
Definition: ccutil.h:69
static void CatchSignals()
Definition: baseapi.cpp:182
uinT8 space()
Definition: werd.h:104
const int kBytesPer64BitNumber
Definition: baseapi.cpp:1564
const Dawg * GetDawg(int index) const
Return i-th dawg pointer recorded in the dawgs_ vector.
Definition: dict.h:406
int InitLangMod(const char *datapath, const char *language)
Definition: baseapi.cpp:422
void extract_edges(Pix *pix, BLOCK *block)
Definition: edgblob.cpp:334
BOOL8 flag(WERD_FLAGS mask) const
Definition: werd.h:128
Boxa * GetRegions(Pixa **pixa)
Definition: baseapi.cpp:585
TESS_LOCAL void DetectParagraphs(bool after_text_recognition)
Definition: baseapi.cpp:2405
bool WriteTRFile(const STRING &filename)
Definition: blobclass.cpp:97
name_table name
virtual bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const
bool contains_unichar(const char *const unichar_repr) const
Definition: unicharset.cpp:644
Definition: strngs.h:44
void add_str_double(const char *str, double number)
Definition: strngs.cpp:386
float Confidence(PageIteratorLevel level) const
const char kUNLVReject
Definition: baseapi.cpp:90
char * GetUTF8Text(PageIteratorLevel level) const
void SetSourceResolution(int ppi)
Definition: baseapi.cpp:533
CRUNCH_MODE unlv_crunch_mode
Definition: pageres.h:294
void GetLoadedLanguagesAsVector(GenericVector< STRING > *langs) const
Definition: baseapi.cpp:356
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:33
#define NULL
Definition: host.h:144
const char * kOldVarsFile
Definition: baseapi.cpp:101
void InitAdaptiveClassifier(bool load_pre_trained_templates)
Definition: adaptmatch.cpp:527
bool BeginDocument(const char *title)
Definition: renderer.cpp:53
virtual bool Next(PageIteratorLevel level)
void ExtractFontName(const STRING &filename, STRING *fontname)
Definition: blobclass.cpp:46
const int kMaxBytesPerLine
Definition: baseapi.cpp:1571
Pix * pix_binary() const
void LearnWord(const char *fontname, WERD_RES *word)
Definition: adaptmatch.cpp:244
#define UNICHAR_LEN
Definition: unichar.h:30
int(Dict::* DictFunc)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
Definition: baseapi.h:81
TBOX bounding_box() const
Definition: blobs.cpp:482
TESSLINE * outlines
Definition: blobs.h:377
PAGE_RES * SetupApplyBoxes(const GenericVector< TBOX > &boxes, BLOCK_LIST *block_list)
Definition: applybox.cpp:210
ROW_LIST * row_list()
get rows
Definition: ocrblock.h:120
void set_min_orientation_margin(double margin)
Definition: baseapi.cpp:2193
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:833
const char * string() const
Definition: strngs.cpp:193
char * TesseractRect(const unsigned char *imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height)
Definition: baseapi.cpp:487
inT16 top() const
Definition: rect.h:54
bool PSM_OSD_ENABLED(int pageseg_mode)
Definition: publictypes.h:179
Tesseract *const tesseract() const
Definition: baseapi.h:728
ELISTIZE(AmbigSpec)
const int kMaxIntSize
Definition: baseapi.cpp:103
void PrintVariables(FILE *fp) const
Definition: baseapi.cpp:264
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:460
STRING * input_file_
Name used by training code.
Definition: baseapi.h:845
const int kUniChs[]
Definition: baseapi.cpp:1621
void BestChoiceToCorrectText()
Definition: pageres.cpp:917
GenericVector< StringParam * > string_params
Definition: params.h:46
UNICHAR_ID unichar_id() const
Definition: ratngs.h:76
ADAPT_TEMPLATES AdaptedTemplates
Definition: classify.h:473
Definition: points.h:189
bool recognition_done_
page_res_ contains recognition data.
Definition: baseapi.h:851
const char * string() const
Definition: params.h:203
int init_tesseract_lm(const char *arg0, const char *textbase, const char *language)
Definition: tessedit.cpp:460
int RecognizeForChopTest(ETEXT_DESC *monitor)
Definition: baseapi.cpp:905
WERD_LIST * word_list()
Definition: ocrrow.h:52
WERD_RES * word() const
Definition: pageres.h:733
void ReSegmentByClassification(PAGE_RES *page_res)
Definition: applybox.cpp:500
TESS_LOCAL LTRResultIterator * GetLTRIterator()
Definition: baseapi.cpp:1254
int inT32
Definition: host.h:102
unsigned char uinT8
Definition: host.h:99
const char * c_str() const
Definition: strngs.cpp:204