tesseract
5.0.0-alpha-619-ge9db
|
Go to the documentation of this file.
21 # include "config_auto.h"
26 #include "allheaders.h"
42 #include <unordered_set>
60 training_iteration_(0),
68 debug_win_(nullptr) {}
82 if (lang ==
nullptr)
return true;
90 bool include_charsets = mgr ==
nullptr ||
111 if (
network_ ==
nullptr)
return false;
112 bool include_charsets = mgr ==
nullptr ||
125 if (include_charsets && !
LoadRecoder(fp))
return false;
126 if (!include_charsets && !
LoadCharsets(mgr))
return false;
149 tprintf(
"Space was garbled in recoding!!\n");
178 tprintf(
"Failed to load any lstm-specific dictionaries for lang %s!!\n",
188 bool debug,
double worst_dict_cert,
189 const TBOX& line_box,
191 int lstm_choice_mode,
192 int lstm_choice_amount) {
196 if (!
RecognizeLine(image_data, invert, debug,
false,
false, &scale_factor,
208 if (lstm_choice_mode){
210 for (
int i = 0; i < lstm_choice_amount; ++i) {
218 for (
int i = 0; i < words->
size(); ++i) {
219 for (
int j = 0; j < words->
get(i)->end; ++j) {
220 if (char_it < search_->ctc_choices.size())
221 words->
get(i)->CTC_symbol_choices.push_back(
223 if (char_it < search_->segmentedTimesteps.size())
224 words->
get(i)->segmented_timesteps.push_back(
229 &words->
get(i)->segmented_timesteps);
239 float* mean_output,
float* sd) {
240 const int kOutputScale = INT8_MAX;
241 STATS stats(0, kOutputScale + 1);
242 for (
int t = 0; t < outputs.
Width(); ++t) {
243 int best_label = outputs.
BestLabel(t,
nullptr);
245 float best_output = outputs.
f(t)[best_label];
246 stats.
add(static_cast<int>(kOutputScale * best_output), 1);
256 *min_output = static_cast<float>(stats.
min_bucket()) / kOutputScale;
257 *mean_output = stats.
mean() / kOutputScale;
258 *sd = stats.
sd() / kOutputScale;
265 bool debug,
bool re_invert,
bool upside_down,
269 const int kMaxImageWidth = 2560;
275 if (pix ==
nullptr) {
276 tprintf(
"Line cannot be recognized!!\n");
280 tprintf(
"Image too large to learn!! Size = %dx%d\n", pixGetWidth(pix),
285 if (upside_down) pixRotate180(pix, pix);
287 *scale_factor = min_width / *scale_factor;
293 float pos_min, pos_mean, pos_sd;
294 OutputStats(*outputs, &pos_min, &pos_mean, &pos_sd);
295 if (invert && pos_min < 0.5) {
305 float inv_min, inv_mean, inv_sd;
306 OutputStats(inv_outputs, &inv_min, &inv_mean, &inv_sd);
307 if (inv_min > pos_min && inv_mean > pos_mean && inv_sd < pos_sd) {
310 tprintf(
"Inverting image: old min=%g, mean=%g, sd=%g, inv %g,%g,%g\n",
311 pos_min, pos_mean, pos_sd, inv_min, inv_mean, inv_sd);
313 *outputs = inv_outputs;
314 *inputs = inv_inputs;
315 }
else if (re_invert) {
337 for (
int start = 0; start < labels.
size(); start = end) {
341 result +=
DecodeLabel(labels, start, &end,
nullptr);
352 const char* window_name,
354 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
355 Pix* input_pix = inputs.
ToPix();
357 pixGetHeight(input_pix), window);
360 #endif // GRAPHICS_DISABLED
368 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
372 for (
int start = 0; start < labels.
size(); start = end) {
373 int xpos = xcoords[start] * x_scale;
379 const char* str =
DecodeLabel(labels, start, &end,
nullptr);
380 if (*str ==
'\\') str =
"\\\\";
381 xpos = xcoords[(start + end) / 2] * x_scale;
382 window->
Text(xpos, height, str);
384 window->
Line(xpos, 0, xpos, height * 3 / 2);
387 #endif // GRAPHICS_DISABLED
398 for (
int start = 0; start < labels.
size(); start = end) {
406 const char* label =
DecodeLabel(labels, start, &end, &decoded);
409 for (
int i = start + 1; i < end; ++i) {
411 xcoords[i], xcoords[i + 1]);
420 const char* label,
int best_choice,
421 int x_start,
int x_end) {
422 tprintf(
"%s=%d On [%d, %d), scores=", label, best_choice, x_start, x_end);
423 double max_score = 0.0;
424 double mean_score = 0.0;
425 const int width = x_end - x_start;
426 for (
int x = x_start; x < x_end; ++x) {
427 const float* line = outputs.
f(x);
428 const double score = line[best_choice] * 100.0;
429 if (score > max_score) max_score = score;
430 mean_score += score / width;
432 double best_score = 0.0;
434 if (c != best_choice && line[c] > best_score) {
436 best_score = line[c];
442 tprintf(
", Mean=%g, max=%g\n", mean_score, max_score);
448 #if 0 // TODO: unused, remove if still unused after 2020.
449 static bool NullIsBest(
const NetworkIO& output,
float null_thr,
450 int null_char,
int t) {
451 if (output.
f(t)[null_char] >= null_thr)
return true;
493 const int width = output.
Width();
494 for (
int t = 0; t < width; ++t) {
496 const int label = output.
BestLabel(t, &score);
508 int start,
int* end,
int* decoded) {
514 if (decoded !=
nullptr) {
521 while (index < labels.
size() &&
523 code.
Set(code.
length(), labels[index++]);
524 while (index < labels.
size() && labels[index] ==
null_char_) ++index;
528 if (uni_id != INVALID_UNICHAR_ID &&
529 (index == labels.
size() ||
533 if (decoded !=
nullptr) *decoded = uni_id;
538 return "<Undecodable>";
540 if (decoded !=
nullptr) *decoded = labels[start];
541 if (labels[start] ==
null_char_)
return "<null>";
556 if (label == INVALID_UNICHAR_ID)
return "..";
bool load_from_file(const char *const filename, bool skip_fragments)
static constexpr float kMinCertainty
int32_t get_total() const
std::vector< std::unordered_set< int > > excludedUnichars
int32_t min_bucket() const
virtual void SetRandomizer(TRand *randomizer)
void DebugActivationPath(const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
std::vector< std::vector< std::pair< const char *, float > > > ctc_choices
void LabelsViaSimpleText(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
bool DeSerialize(TFile *fp)
void set_int_mode(bool is_quantized)
RecodeBeamSearch * search_
virtual int XScaleFactor() const
bool LoadDictionary(const ParamsVectors *params, const char *lang, TessdataManager *mgr)
int32_t sample_iteration_
void Decode(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
char * user_patterns_file
void DebugActivationRange(const NetworkIO &outputs, const char *label, int best_choice, int x_start, int x_end)
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
static TESS_API DawgCache * GlobalDawgCache()
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
STRING language_data_path_prefix
static void PreparePixInput(const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
const UNICHARSET & GetUnicharset() const
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
static Pix * PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
bool DeSerialize(bool swap, FILE *fp)
bool IsValidFirstCode(int code) const
bool Serialize(FILE *fp) const
bool save_to_file(const char *const filename) const
bool LoadRecoder(TFile *fp)
virtual void CacheXScaleFactor(int factor)
bool LoadCharsets(const TessdataManager *mgr)
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
void RecognizeLine(const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words, int lstm_choice_mode=0, int lstm_choice_amount=5)
NetworkScratch scratch_space_
static Network * CreateFromFile(TFile *fp)
void extractSymbolChoices(const UNICHARSET *unicharset)
int DecodeUnichar(const RecodedCharID &code) const
bool DeSerialize(char *data, size_t count=1)
bool Serialize(const char *data, size_t count=1)
void Set(int index, int value)
bool GetComponent(TessdataType type, TFile *fp)
virtual StaticShape InputShape() const
void SetupForLoad(DawgCache *dawg_cache)
bool Serialize(const TessdataManager *mgr, TFile *fp) const
int BestLabel(int t, float *score) const
int32_t training_iteration_
bool IsComponentAvailable(TessdataType type) const
void OutputStats(const NetworkIO &outputs, float *min_output, float *mean_output, float *sd)
bool SimpleTextOutput() const
void DecodeSecondaryBeams(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
void segmentTimestepsByCharacters()
void LoadLSTM(const STRING &lang, TessdataManager *data_file)
void LabelsViaReEncode(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
static const int kMaxCodeLen
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
const char * DecodeSingleLabel(int label)
void add(int32_t value, int32_t count)
std::vector< std::vector< std::pair< const char *, float > > > combineSegmentedTimesteps(std::vector< std::vector< std::vector< std::pair< const char *, float >>>> *segmentedTimesteps)
void SetupPassThrough(const UNICHARSET &unicharset)
void Line(int x1, int y1, int x2, int y2)
static int DisplayImage(Pix *pix, ScrollView *window)
DLLSYM void tprintf(const char *format,...)
void ExtractBestPathAsWords(const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
void DisplayLSTMOutput(const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window)
const char * DecodeLabel(const GenericVector< int > &labels, int start, int *end, int *decoded)
virtual bool Serialize(TFile *fp) const
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
void Text(int x, int y, const char *mystring)
bool Load(const ParamsVectors *params, const char *lang, TessdataManager *mgr)
char * user_patterns_suffix
void ExtractBestPathAsLabels(GenericVector< int > *labels, GenericVector< int > *xcoords) const
std::vector< std::vector< std::vector< std::pair< const char *, float > > > > segmentedTimesteps
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)=0
bool Serialize(TFile *fp) const
STRING DecodeLabels(const GenericVector< int > &labels)