52static const char *han_script =
"Han";
53static const char *latin_script =
"Latin";
54static const char *katakana_script =
"Katakana";
55static const char *hiragana_script =
"Hiragana";
56static const char *hangul_script =
"Hangul";
59const char *ScriptDetector::korean_script_ =
"Korean";
60const char *ScriptDetector::japanese_script_ =
"Japanese";
61const char *ScriptDetector::fraktur_script_ =
"Fraktur";
72 for (
int i = 2;
i < 4; ++
i) {
117 if (strcmp(script,
"Common") && strcmp(script,
"NULL")) {
128 for (
int i = 0;
i < 4; ++
i) {
146 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", pixGetXRes(pix), resolution);
172 resolution = pixGetXRes(pix);
176 nullptr, &v_lines, &h_lines);
178 if (im_pix !=
nullptr) {
179 pixSubtract(pix, pix, im_pix);
190 std::string name = filename;
192 const char *lastdot = strrchr(name.c_str(),
'.');
193 if (lastdot !=
nullptr) {
194 name[lastdot - name.c_str()] =
'\0';
199 int height = pixGetHeight(tess->
pix_binary());
207 TO_BLOCK_LIST land_blocks, port_blocks;
208 remove_nontext_regions(tess, &blocks, &port_blocks);
210 if (port_blocks.empty()) {
214 TBOX page_box(0, 0, width, height);
219 return os_detect(&port_blocks, osr, tess);
227 TO_BLOCK_IT block_it;
228 block_it.set_to_list(port_blocks);
230 BLOBNBOX_CLIST filtered_list;
231 BLOBNBOX_C_IT filtered_it(&filtered_list);
233 for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
234 TO_BLOCK *to_block = block_it.data();
239 bbox_it.set_to_list(&to_block->
blobs);
240 for (bbox_it.mark_cycle_pt(); !bbox_it.cycled_list(); bbox_it.forward()) {
247 if (box.
width() == 0) {
251 float y_x = std::fabs((box.
height() * 1.0f) / box.
width());
252 float x_y = 1.0f / y_x;
254 float ratio = x_y > y_x ? x_y : y_x;
262 filtered_it.add_to_end(bbox);
274int os_detect_blobs(
const std::vector<int> *allowed_scripts, BLOBNBOX_CLIST *blob_list,
277 int minCharactersToTry = tess->min_characters_to_try;
278 int maxCharactersToTry = 5 * minCharactersToTry;
279 if (osr ==
nullptr) {
287 BLOBNBOX_C_IT filtered_it(blob_list);
288 int real_max = std::min(filtered_it.length(), maxCharactersToTry);
294 if (real_max < minCharactersToTry / 2) {
295 tprintf(
"Too few characters. Skipping this page\n");
299 auto **blobs =
new BLOBNBOX *[filtered_it.length()];
300 int number_of_blobs = 0;
301 for (filtered_it.mark_cycle_pt(); !filtered_it.cycled_list(); filtered_it.forward()) {
302 blobs[number_of_blobs++] = filtered_it.data();
305 int num_blobs_evaluated = 0;
306 for (
int i = 0;
i < real_max; ++
i) {
310 ++num_blobs_evaluated;
317 return num_blobs_evaluated;
325 tess->tess_cn_matching.set_value(
true);
326 tess->tess_bn_matching.set_value(
false);
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) {
349 std::unique_ptr<TBLOB> rotated_blob(
new TBLOB(*tblob));
350 rotated_blob->Normalize(
nullptr, ¤t_rotation,
nullptr, x_origin, y_origin, scaling,
353 current_rotation.
rotate(rotation90);
360 stop = s->
must_stop(orientation) && stop;
366 allowed_scripts_ = allowed_scripts;
372 float blob_o_score[4] = {0.0f, 0.0f, 0.0f, 0.0f};
373 float total_blob_o_score = 0.0f;
375 for (
int i = 0;
i < 4; ++
i) {
376 BLOB_CHOICE_IT choice_it(scores +
i);
377 if (!choice_it.empty()) {
379 if (allowed_scripts_ !=
nullptr && !allowed_scripts_->empty()) {
381 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list() && choice ==
nullptr;
382 choice_it.forward()) {
383 int choice_script = choice_it.data()->
script_id();
385 for (s = 0; s < allowed_scripts_->size(); ++s) {
386 if ((*allowed_scripts_)[s] == choice_script) {
387 choice = choice_it.data();
393 choice = choice_it.data();
395 if (choice !=
nullptr) {
398 blob_o_score[
i] = 1 + 0.05 * choice->
certainty();
399 total_blob_o_score += blob_o_score[
i];
403 if (total_blob_o_score == 0.0) {
408 float worst_score = 0.0f;
409 int num_good_scores = 0;
410 for (
float f : blob_o_score) {
413 if (worst_score == 0.0f || f < worst_score) {
418 if (num_good_scores == 1) {
422 for (
float &f : blob_o_score) {
425 total_blob_o_score += worst_score;
430 for (
int i = 0; total_blob_o_score != 0 &&
i < 4; ++
i) {
431 osr_->
orientations[
i] += std::log(blob_o_score[
i] / total_blob_o_score);
448 allowed_scripts_ = allowed_scripts;
462 for (
int i = 0;
i < 4; ++
i) {
465 BLOB_CHOICE_IT choice_it;
466 choice_it.set_to_list(scores +
i);
468 float prev_score = -1;
469 int script_count = 0;
471 int prev_fontinfo_id = -1;
472 const char *prev_unichar =
"";
473 const char *unichar =
"";
475 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list(); choice_it.forward()) {
478 if (allowed_scripts_ !=
nullptr && !allowed_scripts_->empty()) {
481 for (s = 0; s < allowed_scripts_->size(); ++s) {
482 if ((*allowed_scripts_)[s] ==
id) {
486 if (s == allowed_scripts_->size()) {
498 if (prev_score < 0) {
502 prev_unichar = unichar;
508 if (strlen(prev_unichar) == 1) {
509 if (unichar[0] >=
'0' && unichar[0] <=
'9') {
516 if (script_count >= 2) {
521 if (script_count == 1) {
526 if (prev_id == latin_id_) {
527 if (prev_fontinfo_id >= 0) {
541 if (prev_id == katakana_id_) {
544 if (prev_id == hiragana_id_) {
547 if (prev_id == hangul_id_) {
550 if (prev_id == han_id_) {
const int kMinAcceptableBlobHeight
TESS_API int OrientationIdToValue(const int &id)
bool read_unlv_file(std::string &name, int32_t xsize, int32_t ysize, BLOCK_LIST *blocks)
void tprintf(const char *format,...)
const float kNonAmbiguousMargin
int os_detect_blobs(const std::vector< int > *allowed_scripts, BLOBNBOX_CLIST *blob_list, OSResults *osr, tesseract::Tesseract *tess)
const int kMaxNumberOfScripts
const float kScriptAcceptRatio
bool os_detect_blob(BLOBNBOX *bbox, OrientationDetector *o, ScriptDetector *s, OSResults *, tesseract::Tesseract *tess)
int orientation_and_script_detection(const char *filename, OSResults *, tesseract::Tesseract *)
const float kHanRatioInJapanese
const float kHanRatioInKorean
int os_detect(TO_BLOCK_LIST *port_blocks, OSResults *osr, tesseract::Tesseract *tess)
void FullPageBlock(int width, int height, BLOCK_LIST *blocks)
constexpr int kMinCredibleResolution
const int kBlnBaselineOffset
const float kSizeRatioToReject
TESS_API int get_best_script(int orientation_id) const
float scripts_na[4][kMaxNumberOfScripts]
void accumulate(const OSResults &osr)
void print_scores(void) const
void update_best_orientation()
void set_best_orientation(int orientation_id)
void update_best_script(int orientation_id)
bool detect_blob(BLOB_CHOICE_LIST *scores)
OrientationDetector(const std::vector< int > *allowed_scripts, OSResults *results)
void detect_blob(BLOB_CHOICE_LIST *scores)
bool must_stop(int orientation) const
ScriptDetector(const std::vector< int > *allowed_scripts, OSResults *osr, tesseract::Tesseract *tess)
Textord * mutable_textord()
TBOX bounding_box() const
static TBLOB * PolygonalCopy(bool allow_detailed_fx, C_BLOB *src)
PDBLK pdblk
Page Description Block.
POLY_BLOCK * poly_block() const
void rotate(const FCOORD vec)
UNICHAR_ID unichar_id() const
int16_t fontinfo_id() const
TDimension height() const
const ICOORD & topright() const
TDimension bottom() const
TBOX bounding_box() const
const char * get_script_from_script_id(int id) const
const char * id_to_unichar(UNICHAR_ID id) const
int add_script(const char *script)
void AdaptiveClassifier(TBLOB *Blob, BLOB_CHOICE_LIST *Choices)
UnicityTable< FontInfo > & get_fontinfo_table()
static Image FindImages(Image pix, DebugPixa *pixa_debug)
static void FindAndRemoveLines(int resolution, bool debug, Image pix, int *vertical_x, int *vertical_y, Image *pix_music_mask, TabVector_LIST *v_lines, TabVector_LIST *h_lines)
void filter_blobs(ICOORD page_tr, TO_BLOCK_LIST *blocks, bool testing_on)
void find_components(Image pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)