zdenop

tesseract-ocr API example in C++ of changing init parameters

Jul 4th, 2014
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. /*
  2.  * https://groups.google.com/d/msgid/tesseract-ocr/7dd534f7-3e85-480f-bb81-3d34c7af0c05%40googlegroups.com?utm_medium=email&utm_source=footer
  3.  * Compile and run with:
  4.  *
  5.  * $ g++ -o init_para_example init_para_example.cpp -ltesseract -llept && ./init_para_example
  6.  */
  7.  
  8. #include "leptonica/allheaders.h"
  9. #include "tesseract/baseapi.h"
  10. #include "tesseract/strngs.h"
  11. #include "tesseract/genericvector.h"
  12.  
  13. int main(int argc, char **argv) {
  14.     PIX  *pixs;
  15.     char *outText;
  16.     const char *filename = "/usr/src/tesseract-ocr.qmake/test/12OUT_2.jpg";
  17.     const char *language = "ita";
  18.     const char *datapath = "/usr/src/tesseract-ocr/";
  19.     tesseract::PageSegMode pagesegmode = tesseract::PSM_AUTO;
  20.  
  21.     if (argv[1])
  22.         filename = argv[1];
  23.  
  24.     if ((pixs = pixRead(filename)) == NULL) {
  25.         printf("Unsupported image type of file %s.\n", filename);
  26.         exit(3);
  27.     }
  28.  
  29.     // turn of dictionaries -> only possible during init
  30.     GenericVector<STRING> vars_vec;
  31.     vars_vec.push_back("load_system_dawg");
  32.     vars_vec.push_back("load_freq_dawg");
  33.     vars_vec.push_back("load_punc_dawg");
  34.     vars_vec.push_back("load_number_dawg");
  35.     vars_vec.push_back("load_unambig_dawg");
  36.     vars_vec.push_back("load_bigram_dawg");
  37.     vars_vec.push_back("load_fixed_length_dawgs");
  38.     //vars_vec.push_back("user_patterns_suffix");
  39.  
  40.     GenericVector<STRING> vars_values;
  41.     vars_values.push_back("F");
  42.     vars_values.push_back("F");
  43.     vars_values.push_back("F");
  44.     vars_values.push_back("F");
  45.     vars_values.push_back("F");
  46.     vars_values.push_back("F");
  47.     vars_values.push_back("F");
  48.     //vars_values.push_back("pharma-words");
  49.  
  50.     tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
  51.     printf("Using tesseract c++ API: %s\n", api->Version());
  52.  
  53.     api->Init(datapath, language, tesseract::OEM_DEFAULT,
  54.               NULL, 0, &vars_vec, &vars_values, false);
  55.     api->SetVariable("tessedit_char_whitelist",
  56.                      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  57.                      "0123456789,®");
  58.     api->SetVariable("language_model_penalty_non_dict_word", "0");
  59.     api->SetPageSegMode(pagesegmode);
  60.     api->SetImage(pixs);
  61.  
  62.     // Check if change of init parameters was successful
  63.     STRING var_value;
  64.     api->GetVariableAsString("load_system_dawg", &var_value);
  65.     printf("Variable 'load_system_dawg' is set to '%s'\n", var_value.string());
  66.  
  67.     outText = api->GetUTF8Text();
  68.     printf("OCR output: \n%s", outText);
  69.  
  70.     api->End();
  71.     delete [] outText;
  72.     pixDestroy (&pixs);
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment