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";
73 for (
int i = 2; i < 4; ++i) {
106 }
else if (
scripts_na[orientation][i] > second) {
118 if (strcmp(script,
"Common") && strcmp(script,
"NULL")) {
129 for (
int i = 0; i < 4; ++i) {
130 tprintf(
"Orientation id #%d", i);
147 for (
int i = 0; i < 4; ++i) {
160 TO_BLOCK_LIST *to_blocks) {
165 tesseract::TabVector_LIST v_lines;
166 tesseract::TabVector_LIST h_lines;
170 tprintf(
"Warning. Invalid resolution %d dpi. Using %d instead.\n",
171 pixGetXRes(pix), resolution);
173 resolution = pixGetXRes(pix);
177 &vertical_x, &vertical_y,
178 NULL, &v_lines, &h_lines);
180 if (im_pix != NULL) {
181 pixSubtract(pix, pix, im_pix);
198 lastdot = strrchr (name.
string (),
'.');
200 name[lastdot-name.
string()] =
'\0';
204 int height = pixGetHeight(tess->
pix_binary());
211 TO_BLOCK_LIST land_blocks, port_blocks;
214 if (port_blocks.empty()) {
217 &blocks, &port_blocks);
228 return os_detect(&port_blocks, osr, tess);
237 TO_BLOCK_IT block_it;
238 block_it.set_to_list(port_blocks);
240 BLOBNBOX_CLIST filtered_list;
241 BLOBNBOX_C_IT filtered_it(&filtered_list);
243 for (block_it.mark_cycle_pt(); !block_it.cycled_list();
244 block_it.forward ()) {
245 TO_BLOCK* to_block = block_it.data();
249 bbox_it.set_to_list(&to_block->
blobs);
250 for (bbox_it.mark_cycle_pt (); !bbox_it.cycled_list ();
251 bbox_it.forward ()) {
257 float y_x = fabs((box.
height() * 1.0) / box.
width());
258 float x_y = 1.0f / y_x;
260 float ratio = x_y > y_x ? x_y : y_x;
264 filtered_it.add_to_end(bbox);
277 BLOBNBOX_CLIST* blob_list,
OSResults* osr,
287 BLOBNBOX_C_IT filtered_it(blob_list);
295 tprintf(
"Too few characters. Skipping this page\n");
300 int number_of_blobs = 0;
301 for (filtered_it.mark_cycle_pt (); !filtered_it.cycled_list ();
302 filtered_it.forward ()) {
303 blobs[number_of_blobs++] = (
BLOBNBOX*)filtered_it.data();
306 int num_blobs_evaluated = 0;
307 for (
int i = 0; i < real_max; ++i) {
312 ++num_blobs_evaluated;
319 return num_blobs_evaluated;
333 FCOORD current_rotation(1.0f, 0.0f);
334 FCOORD rotation90(0.0f, 1.0f);
335 BLOB_CHOICE_LIST ratings[4];
337 for (
int i = 0; i < 4; ++i) {
342 float x_origin = (box.
left() + box.
right()) / 2.0f;
343 float y_origin = (box.
bottom() + box.
top()) / 2.0f;
344 if (i == 0 || i == 2) {
346 y_origin = i == 0 ? box.
bottom() : box.
top();
350 x_origin = i == 1 ? box.
left() : box.
right();
353 rotated_blob->
Normalize(NULL, ¤t_rotation, NULL,
354 x_origin, y_origin, scaling, scaling,
359 current_rotation.
rotate(rotation90);
366 stop = s->
must_stop(orientation) && stop;
374 allowed_scripts_ = allowed_scripts;
380 float blob_o_score[4] = {0.0f, 0.0f, 0.0f, 0.0f};
381 float total_blob_o_score = 0.0f;
383 for (
int i = 0; i < 4; ++i) {
384 BLOB_CHOICE_IT choice_it(scores + i);
385 if (!choice_it.empty()) {
387 if (allowed_scripts_ != NULL && !allowed_scripts_->
empty()) {
389 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list() &&
390 choice == NULL; choice_it.forward()) {
391 int choice_script = choice_it.data()->
script_id();
393 for (s = 0; s < allowed_scripts_->
size(); ++s) {
394 if ((*allowed_scripts_)[s] == choice_script) {
395 choice = choice_it.data();
401 choice = choice_it.data();
403 if (choice != NULL) {
406 blob_o_score[i] = 1 + 0.05 * choice->
certainty();
407 total_blob_o_score += blob_o_score[i];
411 if (total_blob_o_score == 0.0)
return false;
414 float worst_score = 0.0f;
415 int num_good_scores = 0;
416 for (
int i = 0; i < 4; ++i) {
417 if (blob_o_score[i] > 0.0f) {
419 if (worst_score == 0.0f || blob_o_score[i] < worst_score)
420 worst_score = blob_o_score[i];
423 if (num_good_scores == 1) {
427 for (
int i = 0; i < 4; ++i) {
428 if (blob_o_score[i] == 0.0f) {
429 blob_o_score[i] = worst_score;
430 total_blob_o_score += worst_score;
435 for (
int i = 0; total_blob_o_score != 0 && i < 4; ++i) {
436 osr_->
orientations[i] += log(blob_o_score[i] / total_blob_o_score);
454 allowed_scripts_ = allowed_scripts;
470 for (
int i = 0; i < 4; ++i) {
474 BLOB_CHOICE_IT choice_it;
475 choice_it.set_to_list(scores + i);
477 float prev_score = -1;
478 int script_count = 0;
480 int prev_fontinfo_id = -1;
481 const char* prev_unichar =
"";
482 const char* unichar =
"";
484 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
485 choice_it.forward()) {
488 if (allowed_scripts_ != NULL && !allowed_scripts_->
empty()) {
491 for (s = 0; s < allowed_scripts_->
size(); ++s) {
492 if ((*allowed_scripts_)[s] == id)
break;
494 if (s == allowed_scripts_->
size())
continue;
497 if (done[
id])
continue;
502 if (prev_score < 0) {
506 prev_unichar = unichar;
512 if (strlen(prev_unichar) == 1)
513 if (unichar[0] >=
'0' && unichar[0] <=
'9')
518 if (script_count >= 2)
522 if (script_count == 1) {
527 if (prev_id == latin_id_) {
528 if (prev_fontinfo_id >= 0) {
543 if (prev_id == katakana_id_)
545 if (prev_id == hiragana_id_)
547 if (prev_id == hangul_id_)
549 if (prev_id == han_id_) {
bool detect_blob(BLOB_CHOICE_LIST *scores)
TESS_API int get_best_script(int orientation_id) const
const float kHanRatioInJapanese
const int kBlnBaselineOffset
void update_best_orientation()
int OrientationIdToValue(const int &id)
OrientationDetector(const GenericVector< int > *allowed_scripts, OSResults *results)
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
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)
static Pix * FindImages(Pix *pix)
bool poly_allow_detailed_fx
bool must_stop(int orientation)
ScriptDetector(const GenericVector< int > *allowed_scripts, OSResults *osr, tesseract::Tesseract *tess)
TBOX bounding_box() const
const char * get_script_from_script_id(int id) const
void accumulate(const OSResults &osr)
Textord * mutable_textord()
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
void set_best_orientation(int orientation_id)
float scripts_na[4][kMaxNumberOfScripts]
const int kMinCredibleResolution
TBOX bounding_box() const
const float kNonAmbiguousMargin
int orientation_and_script_detection(STRING &filename, OSResults *osr, tesseract::Tesseract *tess)
bool read_unlv_file(STRING name, inT32 xsize, inT32 ysize, BLOCK_LIST *blocks)
void detect_blob(BLOB_CHOICE_LIST *scores)
UNICHAR_ID unichar_id() const
const int kMaxCharactersToTry
POLY_BLOCK * poly_block() const
void print_scores(void) const
const int kMaxNumberOfScripts
void update_best_script(int orientation_id)
const char * string() const
void rotate(const FCOORD vec)
void filter_blobs(ICOORD page_tr, TO_BLOCK_LIST *blocks, BOOL8 testing_on)
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
const float kScriptAcceptRatio
void FullPageBlock(int width, int height, BLOCK_LIST *blocks)
const float kSizeRatioToReject
void remove_nontext_regions(tesseract::Tesseract *tess, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
const float kHanRatioInKorean
int add_script(const char *script)
const int kMinCharactersToTry
inT16 fontinfo_id() const
UnicityTable< FontInfo > & get_fontinfo_table()
const int kMinAcceptableBlobHeight
const char * id_to_unichar(UNICHAR_ID id) const
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, 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)