Skip to content

Commit

Permalink
Set default language for tesseract only if required
Browse files Browse the repository at this point in the history
When running with --list-langs, --print-parameters or --print-fonts-table
no default language is needed.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Oct 26, 2021
1 parent f5d22d0 commit d6de055
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/api/tesseractmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,14 @@ int main(int argc, char **argv) {
return EXIT_FAILURE;
}

if (lang == nullptr) {
// Set default language if none was given.
bool in_recognition_mode = !list_langs && !print_parameters && !print_fonts_table;

if (lang == nullptr && in_recognition_mode) {
// Set default language model if none was given and a model file is needed.
lang = "eng";
}

if (image == nullptr && !list_langs && !print_parameters && !print_fonts_table) {
if (image == nullptr && in_recognition_mode) {
return EXIT_SUCCESS;
}

Expand Down
12 changes: 6 additions & 6 deletions src/ccmain/tessedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,12 @@ void Tesseract::read_config_file(const char *filename, SetParamConstraint constr
// from the language-specific config file (stored in [lang].traineddata), from
// the config files specified on the command line or left as the default
// OEM_TESSERACT_ONLY if none of the configs specify this variable.
bool Tesseract::init_tesseract_lang_data(const std::string &arg0, const std::string &textbase,
bool Tesseract::init_tesseract_lang_data(const std::string &arg0,
const std::string &language, OcrEngineMode oem,
char **configs, int configs_size,
const std::vector<std::string> *vars_vec,
const std::vector<std::string> *vars_values,
bool set_only_non_debug_params, TessdataManager *mgr) {
// Set the basename, compute the data directory.
main_setup(arg0, textbase);

// Set the language data path prefix
lang = !language.empty() ? language : "eng";
language_data_path_prefix = datadir;
Expand Down Expand Up @@ -300,6 +297,9 @@ int Tesseract::init_tesseract(const std::string &arg0, const std::string &textba
std::vector<std::string> langs_not_to_load;
ParseLanguageString(language, &langs_to_load, &langs_not_to_load);

// Set the basename, compute the data directory.
main_setup(arg0, textbase);

for (auto *lang : sub_langs_) {
delete lang;
}
Expand Down Expand Up @@ -348,7 +348,7 @@ int Tesseract::init_tesseract(const std::string &arg0, const std::string &textba
}
}
}
if (!loaded_primary) {
if (!loaded_primary && !langs_to_load.empty()) {
tprintf("Tesseract couldn't load any languages!\n");
return -1; // Couldn't load any language!
}
Expand Down Expand Up @@ -399,7 +399,7 @@ int Tesseract::init_tesseract_internal(const std::string &arg0, const std::strin
const std::vector<std::string> *vars_vec,
const std::vector<std::string> *vars_values,
bool set_only_non_debug_params, TessdataManager *mgr) {
if (!init_tesseract_lang_data(arg0, textbase, language, oem, configs, configs_size, vars_vec,
if (!init_tesseract_lang_data(arg0, language, oem, configs, configs_size, vars_vec,
vars_values, set_only_non_debug_params, mgr)) {
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ccmain/tesseractclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ class TESS_API Tesseract : public Wordrec {
void recognize_page(std::string &image_name);
void end_tesseract();

bool init_tesseract_lang_data(const std::string &arg0, const std::string &textbase,
bool init_tesseract_lang_data(const std::string &arg0,
const std::string &language, OcrEngineMode oem, char **configs,
int configs_size, const std::vector<std::string> *vars_vec,
const std::vector<std::string> *vars_values,
Expand Down

0 comments on commit d6de055

Please sign in to comment.