50 static const char* han_script =
"Han";
51 static const char* latin_script =
"Latin";
52 static const char* katakana_script =
"Katakana";
53 static const char* hiragana_script =
"Hiragana";
54 static const char* hangul_script =
"Hangul";
57 const char* ScriptDetector::korean_script_ =
"Korean";
58 const char* ScriptDetector::japanese_script_ =
"Japanese";
59 const char* ScriptDetector::fraktur_script_ =
"Fraktur";
70 for (
int i = 2; i < 4; ++i) {
103 }
else if (
scripts_na[orientation][i] > second) {
115 if (strcmp(script,
"Common") && strcmp(script,
"NULL")) {
126 for (
int i = 0; i < 4; ++i) {
127 tprintf(
"Orientation id #%d", i);
144 for (
int i = 0; i < 4; ++i) {
157 TO_BLOCK_LIST *to_blocks) {
162 tesseract::TabVector_LIST v_lines;
163 tesseract::TabVector_LIST h_lines;
167 tprintf(
"Warning. Invalid resolution %d dpi. Using %d instead.\n",
168 pixGetXRes(pix), resolution);
170 resolution = pixGetXRes(pix);
174 &vertical_x, &vertical_y,
175 NULL, &v_lines, &h_lines);
177 if (im_pix != NULL) {
178 pixSubtract(pix, pix, im_pix);
195 lastdot = strrchr (name.
string (),
'.');
197 name[lastdot-name.
string()] =
'\0';
201 int height = pixGetHeight(tess->
pix_binary());
208 TO_BLOCK_LIST land_blocks, port_blocks;
211 if (port_blocks.empty()) {
214 &blocks, &port_blocks);
225 return os_detect(&port_blocks, osr, tess);
234 TO_BLOCK_IT block_it;
235 block_it.set_to_list(port_blocks);
237 BLOBNBOX_CLIST filtered_list;
238 BLOBNBOX_C_IT filtered_it(&filtered_list);
240 for (block_it.mark_cycle_pt(); !block_it.cycled_list();
241 block_it.forward ()) {
242 TO_BLOCK* to_block = block_it.data();
246 bbox_it.set_to_list(&to_block->
blobs);
247 for (bbox_it.mark_cycle_pt (); !bbox_it.cycled_list ();
248 bbox_it.forward ()) {
254 float y_x = fabs((box.
height() * 1.0) / box.
width());
255 float x_y = 1.0f / y_x;
257 float ratio = x_y > y_x ? x_y : y_x;
261 filtered_it.add_to_end(bbox);
274 BLOBNBOX_CLIST* blob_list,
OSResults* osr,
284 BLOBNBOX_C_IT filtered_it(blob_list);
292 tprintf(
"Too few characters. Skipping this page\n");
297 int number_of_blobs = 0;
298 for (filtered_it.mark_cycle_pt (); !filtered_it.cycled_list ();
299 filtered_it.forward ()) {
300 blobs[number_of_blobs++] = (
BLOBNBOX*)filtered_it.data();
303 int num_blobs_evaluated = 0;
304 for (
int i = 0; i < real_max; ++i) {
309 ++num_blobs_evaluated;
316 return num_blobs_evaluated;
330 FCOORD current_rotation(1.0f, 0.0f);
331 FCOORD rotation90(0.0f, 1.0f);
332 BLOB_CHOICE_LIST ratings[4];
334 for (
int i = 0; i < 4; ++i) {
339 float x_origin = (box.
left() + box.
right()) / 2.0f;
340 float y_origin = (box.
bottom() + box.
top()) / 2.0f;
341 if (i == 0 || i == 2) {
343 y_origin = i == 0 ? box.
bottom() : box.
top();
347 x_origin = i == 1 ? box.
left() : box.
right();
350 rotated_blob->
Normalize(NULL, ¤t_rotation, NULL,
351 x_origin, y_origin, scaling, scaling,
356 current_rotation.
rotate(rotation90);
363 stop = s->
must_stop(orientation) && stop;
371 allowed_scripts_ = allowed_scripts;
377 float blob_o_score[4] = {0.0f, 0.0f, 0.0f, 0.0f};
378 float total_blob_o_score = 0.0f;
380 for (
int i = 0; i < 4; ++i) {
381 BLOB_CHOICE_IT choice_it(scores + i);
382 if (!choice_it.empty()) {
384 if (allowed_scripts_ != NULL && !allowed_scripts_->empty()) {
386 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list() &&
387 choice == NULL; choice_it.forward()) {
388 int choice_script = choice_it.data()->
script_id();
390 for (s = 0; s < allowed_scripts_->size(); ++s) {
391 if ((*allowed_scripts_)[s] == choice_script) {
392 choice = choice_it.data();
398 choice = choice_it.data();
400 if (choice != NULL) {
403 blob_o_score[i] = 1 + 0.05 * choice->
certainty();
404 total_blob_o_score += blob_o_score[i];
408 if (total_blob_o_score == 0.0)
return false;
411 float worst_score = 0.0f;
412 int num_good_scores = 0;
413 for (
int i = 0; i < 4; ++i) {
414 if (blob_o_score[i] > 0.0f) {
416 if (worst_score == 0.0f || blob_o_score[i] < worst_score)
417 worst_score = blob_o_score[i];
420 if (num_good_scores == 1) {
424 for (
int i = 0; i < 4; ++i) {
425 if (blob_o_score[i] == 0.0f) {
426 blob_o_score[i] = worst_score;
427 total_blob_o_score += worst_score;
432 for (
int i = 0; total_blob_o_score != 0 && i < 4; ++i) {
433 osr_->orientations[i] += log(blob_o_score[i] / total_blob_o_score);
442 osr_->update_best_orientation();
443 return osr_->best_result.orientation_id;
451 allowed_scripts_ = allowed_scripts;
452 katakana_id_ = tess_->unicharset.add_script(katakana_script);
453 hiragana_id_ = tess_->unicharset.add_script(hiragana_script);
454 han_id_ = tess_->unicharset.add_script(han_script);
455 hangul_id_ = tess_->unicharset.add_script(hangul_script);
456 japanese_id_ = tess_->unicharset.add_script(japanese_script_);
457 korean_id_ = tess_->unicharset.add_script(korean_script_);
458 latin_id_ = tess_->unicharset.add_script(latin_script);
459 fraktur_id_ = tess_->unicharset.add_script(fraktur_script_);
467 for (
int i = 0; i < 4; ++i) {
471 BLOB_CHOICE_IT choice_it;
472 choice_it.set_to_list(scores + i);
474 float prev_score = -1;
475 int script_count = 0;
477 int prev_fontinfo_id = -1;
478 const char* prev_unichar =
"";
479 const char* unichar =
"";
481 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
482 choice_it.forward()) {
485 if (allowed_scripts_ != NULL && !allowed_scripts_->empty()) {
488 for (s = 0; s < allowed_scripts_->size(); ++s) {
489 if ((*allowed_scripts_)[s] == id)
break;
491 if (s == allowed_scripts_->size())
continue;
494 if (done[
id])
continue;
497 unichar = tess_->unicharset.id_to_unichar(choice->
unichar_id());
499 if (prev_score < 0) {
503 prev_unichar = unichar;
509 if (strlen(prev_unichar) == 1)
510 if (unichar[0] >=
'0' && unichar[0] <=
'9')
515 if (script_count >= 2)
519 if (script_count == 1) {
521 osr_->scripts_na[i][prev_id] += 1.0;
524 if (prev_id == latin_id_) {
525 if (prev_fontinfo_id >= 0) {
527 tess_->get_fontinfo_table().get(prev_fontinfo_id);
533 osr_->scripts_na[i][prev_id] -= 1.0;
534 osr_->scripts_na[i][fraktur_id_] += 1.0;
540 if (prev_id == katakana_id_)
541 osr_->scripts_na[i][japanese_id_] += 1.0;
542 if (prev_id == hiragana_id_)
543 osr_->scripts_na[i][japanese_id_] += 1.0;
544 if (prev_id == hangul_id_)
545 osr_->scripts_na[i][korean_id_] += 1.0;
546 if (prev_id == han_id_) {
555 osr_->update_best_script(orientation);
556 return osr_->best_result.sconfidence > 1;
const int kMinCharactersToTry
const float kSizeRatioToReject
const ICOORD & topright() const
void Normalize(const BLOCK *block, const FCOORD *rotation, const DENORM *predecessor, float x_origin, float y_origin, float x_scale, float y_scale, float final_xshift, float final_yshift, bool inverse, Pix *pix)
constexpr int kMinCredibleResolution
const float kHanRatioInJapanese
void print_scores(void) const
const char * get_script_from_script_id(int id) const
void update_best_orientation()
void remove_nontext_regions(tesseract::Tesseract *tess, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
int OrientationIdToValue(const int &id)
void set_best_orientation(int orientation_id)
void filter_blobs(ICOORD page_tr, TO_BLOCK_LIST *blocks, BOOL8 testing_on)
bool poly_allow_detailed_fx
void accumulate(const OSResults &osr)
const float kNonAmbiguousMargin
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
void rotate(const FCOORD vec)
float scripts_na[4][kMaxNumberOfScripts]
const int kMinAcceptableBlobHeight
UNICHAR_ID unichar_id() const
const char * string() const
const float kHanRatioInKorean
bool read_unlv_file(STRING name, inT32 xsize, inT32 ysize, BLOCK_LIST *blocks)
void FullPageBlock(int width, int height, BLOCK_LIST *blocks)
static Pix * FindImages(Pix *pix, DebugPixa *pixa_debug)
void update_best_script(int orientation_id)
const int kMaxNumberOfScripts
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
inT16 fontinfo_id() const
const float kScriptAcceptRatio
TBOX bounding_box() const
OrientationDetector(const GenericVector< int > *allowed_scripts, OSResults *results)
Textord * mutable_textord()
bool detect_blob(BLOB_CHOICE_LIST *scores)
void detect_blob(BLOB_CHOICE_LIST *scores)
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *osr, tesseract::Tesseract *tess)
bool must_stop(int orientation)
POLY_BLOCK * poly_block() const
TBOX bounding_box() const
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
static void FindAndRemoveLines(int resolution, bool debug, Pix *pix, int *vertical_x, int *vertical_y, Pix **pix_music_mask, TabVector_LIST *v_lines, TabVector_LIST *h_lines)
const int kBlnBaselineOffset
int orientation_and_script_detection(STRING &filename, OSResults *osr, tesseract::Tesseract *tess)
const int kMaxCharactersToTry
ScriptDetector(const GenericVector< int > *allowed_scripts, OSResults *osr, tesseract::Tesseract *tess)
TESS_API int get_best_script(int orientation_id) const