This commit is contained in:
Balearica 2025-06-02 09:44:20 +04:00 committed by GitHub
commit c6f509a177
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 70 additions and 16 deletions

View File

@ -147,11 +147,19 @@ public:
#endif
/**
* Print Tesseract parameters to the given file.
/**
* Print Tesseract parameters to the given file with descriptions of each option.
* Cannot be used as Tesseract configuration file due to descriptions
* (use DumpVariables instead to create config files).
*/
void PrintVariables(FILE *fp) const;
/**
* Print Tesseract parameters to the given file without descriptions.
* Can be used as Tesseract configuration file.
*/
void DumpVariables(FILE *fp) const;
/**
* Get value of named variable as a string, if it exists.
*/

View File

@ -216,6 +216,10 @@ TESS_API void TessBaseAPIPrintVariables(const TessBaseAPI *handle, FILE *fp);
TESS_API BOOL TessBaseAPIPrintVariablesToFile(const TessBaseAPI *handle,
const char *filename);
TESS_API void TessBaseAPIDumpVariables(const TessBaseAPI *handle, FILE *fp);
TESS_API BOOL TessBaseAPIDumpVariablesToFile(const TessBaseAPI *handle,
const char *filename);
TESS_API int TessBaseAPIInit1(TessBaseAPI *handle, const char *datapath,
const char *language, TessOcrEngineMode oem,
char **configs, int configs_size);

View File

@ -280,9 +280,21 @@ void TessBaseAPI::PrintFontsTable(FILE *fp) const {
#endif
/** Print Tesseract parameters to the given file. */
/**
* Print Tesseract parameters to the given file with descriptions of each option.
* Cannot be used as Tesseract configuration file due to descriptions
* (use DumpVariables instead to create config files).
*/
void TessBaseAPI::PrintVariables(FILE *fp) const {
ParamUtils::PrintParams(fp, tesseract_->params());
ParamUtils::PrintParams(fp, tesseract_->params(), true);
}
/**
* Print Tesseract parameters to the given file without descriptions.
* Can be used as Tesseract configuration file.
*/
void TessBaseAPI::DumpVariables(FILE *fp) const {
ParamUtils::PrintParams(fp, tesseract_->params(), false);
}
/**
@ -1234,7 +1246,7 @@ bool TessBaseAPI::ProcessPage(Pix *pix, int page_index, const char *filename,
if (fp == nullptr) {
tprintf("Error, failed to open file \"%s\"\n", kOldVarsFile);
} else {
PrintVariables(fp);
DumpVariables(fp);
fclose(fp);
}
// Switch to alternate mode for retry.

View File

@ -212,6 +212,20 @@ BOOL TessBaseAPIPrintVariablesToFile(const TessBaseAPI *handle, const char *file
return FALSE;
}
void TessBaseAPIDumpVariables(const TessBaseAPI *handle, FILE *fp) {
handle->DumpVariables(fp);
}
BOOL TessBaseAPIDumpVariablesToFile(const TessBaseAPI *handle, const char *filename) {
FILE *fp = fopen(filename, "w");
if (fp != nullptr) {
handle->DumpVariables(fp);
fclose(fp);
return TRUE;
}
return FALSE;
}
int TessBaseAPIInit4(TessBaseAPI *handle, const char *datapath, const char *language,
TessOcrEngineMode mode, char **configs, int configs_size, char **vars_vec,
char **vars_values, size_t vars_vec_size, BOOL set_only_non_debug_params) {

View File

@ -127,7 +127,7 @@ bool Tesseract::ProcessTargetWord(const TBOX &word_box, const TBOX &target_word_
if (config_fp == nullptr) {
tprintf("Error, failed to open file \"%s\"\n", backup_config_file_);
} else {
ParamUtils::PrintParams(config_fp, params());
ParamUtils::PrintParams(config_fp, params(), false);
fclose(config_fp);
}
ParamUtils::ReadParamsFile(word_config, SET_PARAM_CONSTRAINT_DEBUG_ONLY, params());

View File

@ -161,27 +161,43 @@ bool ParamUtils::GetParamAsString(const char *name, const ParamsVectors *member_
return false;
}
void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params, bool print_info) {
int num_iterations = (member_params == nullptr) ? 1 : 2;
std::ostringstream stream;
stream.imbue(std::locale::classic());
for (int v = 0; v < num_iterations; ++v) {
const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
for (auto int_param : vec->int_params) {
stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\t'
<< int_param->info_str() << '\n';
if (print_info) {
stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\t'
<< int_param->info_str() << '\n';
} else {
stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\n';
}
}
for (auto bool_param : vec->bool_params) {
stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\t'
<< bool_param->info_str() << '\n';
if (print_info) {
stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\t'
<< bool_param->info_str() << '\n';
} else {
stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\n';
}
}
for (auto string_param : vec->string_params) {
stream << string_param->name_str() << '\t' << string_param->c_str() << '\t'
<< string_param->info_str() << '\n';
if (print_info) {
stream << string_param->name_str() << '\t' << string_param->c_str() << '\t'
<< string_param->info_str() << '\n';
} else {
stream << string_param->name_str() << '\t' << string_param->c_str() << '\n';
}
}
for (auto double_param : vec->double_params) {
stream << double_param->name_str() << '\t' << (double)(*double_param) << '\t'
<< double_param->info_str() << '\n';
if (print_info) {
stream << double_param->name_str() << '\t' << (double)(*double_param) << '\t'
<< double_param->info_str() << '\n';
} else {
stream << double_param->name_str() << '\t' << (double)(*double_param) << '\n';
}
}
}
fprintf(fp, "%s", stream.str().c_str());

View File

@ -103,7 +103,7 @@ public:
std::string *value);
// Print parameters to the given file.
static void PrintParams(FILE *fp, const ParamsVectors *member_params);
static void PrintParams(FILE *fp, const ParamsVectors *member_params, bool print_info = true);
// Resets all parameters back to default values;
static void ResetToDefaults(ParamsVectors *member_params);