MCPcopy Create free account
hub / github.com/creatale/node-dv / ReadTrainingSamples

Method ReadTrainingSamples

deps/tesseract/classify/mastertrainer.cpp:135–182  ·  view source on GitHub ↗

Reads the samples and their features from the given .tr format file, adding them to the trainer with the font_id from the content of the file. See mftraining.cpp for a description of the file format. If verification, then these are verification samples, not training.

Source from the content-addressed store, hash-verified

133// See mftraining.cpp for a description of the file format.
134// If verification, then these are verification samples, not training.
135void MasterTrainer::ReadTrainingSamples(const char* page_name,
136 const FEATURE_DEFS_STRUCT& feature_defs,
137 bool verification) {
138 char buffer[2048];
139 int int_feature_type = ShortNameToFeatureType(feature_defs, kIntFeatureType);
140 int micro_feature_type = ShortNameToFeatureType(feature_defs,
141 kMicroFeatureType);
142 int cn_feature_type = ShortNameToFeatureType(feature_defs, kCNFeatureType);
143 int geo_feature_type = ShortNameToFeatureType(feature_defs, kGeoFeatureType);
144
145 FILE* fp = Efopen(page_name, "rb");
146 if (fp == NULL) {
147 tprintf("Failed to open tr file: %s\n", page_name);
148 return;
149 }
150 tr_filenames_.push_back(STRING(page_name));
151 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
152 if (buffer[0] == '\n')
153 continue;
154
155 char* space = strchr(buffer, ' ');
156 if (space == NULL) {
157 tprintf("Bad format in tr file, reading fontname, unichar\n");
158 continue;
159 }
160 *space++ = '\0';
161 int font_id = GetFontInfoId(buffer);
162 if (font_id < 0) font_id = 0;
163 int page_number;
164 STRING unichar;
165 TBOX bounding_box;
166 if (!ParseBoxFileStr(space, &page_number, &unichar, &bounding_box)) {
167 tprintf("Bad format in tr file, reading box coords\n");
168 continue;
169 }
170 CHAR_DESC char_desc = ReadCharDescription(feature_defs, fp);
171 TrainingSample* sample = new TrainingSample;
172 sample->set_font_id(font_id);
173 sample->set_page_num(page_number + page_images_.size());
174 sample->set_bounding_box(bounding_box);
175 sample->ExtractCharDesc(int_feature_type, micro_feature_type,
176 cn_feature_type, geo_feature_type, char_desc);
177 AddSample(verification, unichar.string(), sample);
178 FreeCharDescription(char_desc);
179 }
180 charsetsize_ = unicharset_.size();
181 fclose(fp);
182}
183
184// Adds the given single sample to the trainer, setting the classid
185// appropriately from the given unichar_str.

Callers

nothing calls this directly

Calls 13

ShortNameToFeatureTypeFunction · 0.85
EfopenFunction · 0.85
ParseBoxFileStrFunction · 0.85
ReadCharDescriptionFunction · 0.85
FreeCharDescriptionFunction · 0.85
set_font_idMethod · 0.80
set_page_numMethod · 0.80
ExtractCharDescMethod · 0.80
STRINGClass · 0.70
push_backMethod · 0.45
sizeMethod · 0.45
set_bounding_boxMethod · 0.45

Tested by

no test coverage detected