51 static const char* han_script =
"Han";
52 static const char* latin_script =
"Latin";
53 static const char* katakana_script =
"Katakana";
54 static const char* hiragana_script =
"Hiragana";
55 static const char* hangul_script =
"Hangul";
58 const char* ScriptDetector::korean_script_ =
"Korean";
59 const char* ScriptDetector::japanese_script_ =
"Japanese";
60 const char* ScriptDetector::fraktur_script_ =
"Fraktur";
71 for (
int i = 2; i < 4; ++i) {
104 }
else if (
scripts_na[orientation][i] > second) {
116 if (strcmp(script,
"Common") && strcmp(script,
"NULL")) {
127 for (
int i = 0; i < 4; ++i) {
128 tprintf(
"Orientation id #%d", i);
145 for (
int i = 0; i < 4; ++i) {
159 TO_BLOCK_LIST *to_blocks) {
164 tesseract::TabVector_LIST v_lines;
165 tesseract::TabVector_LIST h_lines;
169 tprintf(
"Warning. Invalid resolution %d dpi. Using %d instead.\n",
170 pixGetXRes(pix), resolution);
172 resolution = pixGetXRes(pix);
176 &vertical_x, &vertical_y,
177 nullptr, &v_lines, &h_lines);
179 if (im_pix !=
nullptr) {
180 pixSubtract(pix, pix, im_pix);
197 lastdot = strrchr (name.
string (),
'.');
198 if (lastdot !=
nullptr)
199 name[lastdot-name.
string()] =
'\0';
203 int height = pixGetHeight(tess->
pix_binary());
210 TO_BLOCK_LIST land_blocks, port_blocks;
211 remove_nontext_regions(tess, &blocks, &port_blocks);
213 if (port_blocks.empty()) {
216 &blocks, &port_blocks);
227 return os_detect(&port_blocks, osr, tess);
236 TO_BLOCK_IT block_it;
237 block_it.set_to_list(port_blocks);
239 BLOBNBOX_CLIST filtered_list;
240 BLOBNBOX_C_IT filtered_it(&filtered_list);
242 for (block_it.mark_cycle_pt(); !block_it.cycled_list();
243 block_it.forward ()) {
244 TO_BLOCK* to_block = block_it.data();
248 bbox_it.set_to_list(&to_block->
blobs);
249 for (bbox_it.mark_cycle_pt (); !bbox_it.cycled_list ();
250 bbox_it.forward ()) {
257 if (box.
width() == 0)
continue;
259 float y_x = std::fabs((box.
height() * 1.0f) / box.
width());
260 float x_y = 1.0f / y_x;
262 float ratio = x_y > y_x ? x_y : y_x;
266 filtered_it.add_to_end(bbox);
279 BLOBNBOX_CLIST* blob_list,
OSResults* osr,
283 int maxCharactersToTry = 5 * minCharactersToTry;
291 BLOBNBOX_C_IT filtered_it(blob_list);
292 int real_max = std::min(filtered_it.length(), maxCharactersToTry);
298 if (real_max < minCharactersToTry / 2) {
299 tprintf(
"Too few characters. Skipping this page\n");
304 int number_of_blobs = 0;
305 for (filtered_it.mark_cycle_pt (); !filtered_it.cycled_list ();
306 filtered_it.forward ()) {
307 blobs[number_of_blobs++] = (
BLOBNBOX*)filtered_it.data();
310 int num_blobs_evaluated = 0;
311 for (
int i = 0; i < real_max; ++i) {
313 && i > minCharactersToTry) {
316 ++num_blobs_evaluated;
323 return num_blobs_evaluated;
337 FCOORD current_rotation(1.0f, 0.0f);
338 FCOORD rotation90(0.0f, 1.0f);
339 BLOB_CHOICE_LIST ratings[4];
341 for (
int i = 0; i < 4; ++i) {
346 float x_origin = (box.
left() + box.
right()) / 2.0f;
347 float y_origin = (box.
bottom() + box.
top()) / 2.0f;
348 if (i == 0 || i == 2) {
350 y_origin = i == 0 ? box.
bottom() : box.
top();
354 x_origin = i == 1 ? box.
left() : box.
right();
356 std::unique_ptr<TBLOB> rotated_blob(
new TBLOB(*tblob));
357 rotated_blob->
Normalize(
nullptr, ¤t_rotation,
nullptr,
358 x_origin, y_origin, scaling, scaling,
362 current_rotation.
rotate(rotation90);
369 stop = s->
must_stop(orientation) && stop;
377 allowed_scripts_ = allowed_scripts;
383 float blob_o_score[4] = {0.0f, 0.0f, 0.0f, 0.0f};
384 float total_blob_o_score = 0.0f;
386 for (
int i = 0; i < 4; ++i) {
387 BLOB_CHOICE_IT choice_it(scores + i);
388 if (!choice_it.empty()) {
390 if (allowed_scripts_ !=
nullptr && !allowed_scripts_->
empty()) {
392 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list() &&
393 choice ==
nullptr; choice_it.forward()) {
394 int choice_script = choice_it.data()->
script_id();
396 for (s = 0; s < allowed_scripts_->
size(); ++s) {
397 if ((*allowed_scripts_)[s] == choice_script) {
398 choice = choice_it.data();
404 choice = choice_it.data();
406 if (choice !=
nullptr) {
409 blob_o_score[i] = 1 + 0.05 * choice->
certainty();
410 total_blob_o_score += blob_o_score[i];
414 if (total_blob_o_score == 0.0)
return false;
417 float worst_score = 0.0f;
418 int num_good_scores = 0;
419 for (
int i = 0; i < 4; ++i) {
420 if (blob_o_score[i] > 0.0f) {
422 if (worst_score == 0.0f || blob_o_score[i] < worst_score)
423 worst_score = blob_o_score[i];
426 if (num_good_scores == 1) {
430 for (
int i = 0; i < 4; ++i) {
431 if (blob_o_score[i] == 0.0f) {
432 blob_o_score[i] = worst_score;
433 total_blob_o_score += worst_score;
438 for (
int i = 0; total_blob_o_score != 0 && i < 4; ++i) {
439 osr_->
orientations[i] += log(blob_o_score[i] / total_blob_o_score);
457 allowed_scripts_ = allowed_scripts;
473 for (
int i = 0; i < 4; ++i) {
477 BLOB_CHOICE_IT choice_it;
478 choice_it.set_to_list(scores + i);
480 float prev_score = -1;
481 int script_count = 0;
483 int prev_fontinfo_id = -1;
484 const char* prev_unichar =
"";
485 const char* unichar =
"";
487 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
488 choice_it.forward()) {
491 if (allowed_scripts_ !=
nullptr && !allowed_scripts_->
empty()) {
494 for (s = 0; s < allowed_scripts_->
size(); ++s) {
495 if ((*allowed_scripts_)[s] == id)
break;
497 if (s == allowed_scripts_->
size())
continue;
500 if (done[
id])
continue;
505 if (prev_score < 0) {
509 prev_unichar = unichar;
515 if (strlen(prev_unichar) == 1)
516 if (unichar[0] >=
'0' && unichar[0] <=
'9')
521 if (script_count >= 2)
525 if (script_count == 1) {
530 if (prev_id == latin_id_) {
531 if (prev_fontinfo_id >= 0) {
546 if (prev_id == katakana_id_)
548 if (prev_id == hiragana_id_)
550 if (prev_id == hangul_id_)
552 if (prev_id == han_id_) {
OrientationDetector(const GenericVector< int > *allowed_scripts, OSResults *results)
const ICOORD & topright() const
bool must_stop(int orientation)
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
void update_best_orientation()
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *osr, tesseract::Tesseract *tess)
UnicityTable< FontInfo > & get_fontinfo_table()
void set_best_orientation(int orientation_id)
const char * get_script_from_script_id(int id) const
const char * string() const
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
void rotate(const FCOORD vec)
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
const float kScriptAcceptRatio
void find_components(Pix *pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
float scripts_na[4][kMaxNumberOfScripts]
const int kBlnBaselineOffset
void print_scores(void) const
int orientation_and_script_detection(STRING &filename, 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)
int16_t fontinfo_id() const
ScriptDetector(const GenericVector< int > *allowed_scripts, OSResults *osr, tesseract::Tesseract *tess)
void update_best_script(int orientation_id)
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)
const int kMaxNumberOfScripts
void detect_blob(BLOB_CHOICE_LIST *scores)
POLY_BLOCK * poly_block() const
int min_characters_to_try
DLLSYM void tprintf(const char *format,...)
TBOX bounding_box() const
Textord * mutable_textord()
const float kSizeRatioToReject
void accumulate(const OSResults &osr)
void filter_blobs(ICOORD page_tr, TO_BLOCK_LIST *blocks, bool testing_on)
const float kHanRatioInJapanese
TBOX bounding_box() const
const char * id_to_unichar(UNICHAR_ID id) const
bool detect_blob(BLOB_CHOICE_LIST *scores)
bool read_unlv_file(STRING name, int32_t xsize, int32_t ysize, BLOCK_LIST *blocks)
const int kMinAcceptableBlobHeight
static Pix * FindImages(Pix *pix, DebugPixa *pixa_debug)
int OrientationIdToValue(const int &id)
void FullPageBlock(int width, int height, BLOCK_LIST *blocks)
TESS_API int get_best_script(int orientation_id) const
constexpr int kMinCredibleResolution
UNICHAR_ID unichar_id() const
const float kNonAmbiguousMargin
const float kHanRatioInKorean
int add_script(const char *script)
int os_detect_blobs(const GenericVector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
bool poly_allow_detailed_fx