Tesseract源码分析(二)——识别与纠错

tess4.0中主要的数据结构

  1. Page analysis result: PAGE_RES (ccstruct/pageres.h).
  2. Page analysis result contains a list of block analysis result field: BLOCK_RES_LIST.
  3. Block analysis result: BLOCK_RES (ccstruct/pageres.h).
  4. Block analysis result contains a list of row analysis result field: ROW_RES_LIST.
  5. Row analysis result: ROW_RES (ccstruct/pageres.h).
  6. Row analysis result contains a list of word analysis result field: WERD_RES_LIST.
  7. WERD_RES(ccstruct/pageres.h) is a collection of publicly accessible members that gathers information about a word result.

源码分析

Tesseract主要文字识别主要流程:二值化,切分处理,识别,纠错等步骤。上篇文章总结了二值化与切分处理的过程,本文主要总结识别和纠错两部分步骤的处理过程。

字符识别

pass 1 recongnize

Classify the blobs in the word and permute the results. Find the worst blob in the word and chop it up. Continue this process until a good answer has been found or all the blobs have been chopped up enough. The results are returned in the WERD_RES.

  • 调用栈
    1. main [api/tesseractmain.cpp] ->
    2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
    3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
    4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
    5. Tesseract::recog_all_words [ccmain/control.cpp] ->
    6. Tesseract::RecogAllWordsPassN [ccmain/control.cpp] ->
    7. Tesseract::classify_word_and_language [ccmain/ control.cpp] ->
    8. Tesseract::classify_word_pass1 [ccmain/ control.cpp] ->
    9. Tesseract::match_word_pass_n [ccmain/ control.cpp] ->
    10. Tesseract::tess_segment_pass_n [ccmain/ tessbox.cpp] ->
    11. ** Wordrec::set_pass1() [wordrec/ tface.cpp] -> **
    12. Tesseract::recog_word [ccmain/ tfacepp.cpp] ->
    13. Tesseract::recog_word_recursive [ccmain/ tfacepp.cpp] ->
    14. Wordrec::cc_recog [wordrec/ tface.cpp] ->
    15. Wordrec::chop_word_main [wordrec/ chopper.cpp]

pass 2 recongnize

The processing difference of pass 1 and pass 2 is at the word set style which is in font-weight.

  • 调用栈
    1. main [api/tesseractmain.cpp] ->
    2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
    3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
    4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
    5. Tesseract::recog_all_words [ccmain/control.cpp] ->
    6. Tesseract::RecogAllWordsPassN [ccmain/control.cpp] ->
    7. Tesseract::classify_word_and_language [ccmain/ control.cpp] ->
    8. Tesseract::classify_word_pass2 [ccmain/ control.cpp] ->
    9. Tesseract::match_word_pass_n [ccmain/ control.cpp] ->
    10. Tesseract::tess_segment_pass_n [ccmain/ tessbox.cpp] ->
    11. ** Wordrec::set_pass2() [wordrec/ tface.cpp] -> **
    12. Tesseract::recog_word [ccmain/ tfacepp.cpp] ->
    13. Tesseract::recog_word_recursive [ccmain/ tfacepp.cpp] ->
    14. Wordrec::cc_recog [wordrec/ tface.cpp] ->
    15. Wordrec::chop_word_main [wordrec/ chopper.cpp]

LSTM recongnize contained in pass 1 recongnize

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::RecogAllWordsPassN [ccmain/control.cpp] ->
  7. Tesseract::classify_word_and_language [ccmain/ control.cpp] ->
  8. Tesseract::classify_word_pass1 [ccmain/ control.cpp] ->
  9. Tesseract::LSTMRecognizeWord [ccmain/linerec.cpp] ->
  10. LSTMRecognizer::RecognizeLine [lstm/lstmrecognizer.cpp] ->
  11. LSTMRecognizer::RecognizeLine [lstm/lstmrecognizer.cpp] ->
  12. Tesseract::SearchWords [ccmain/linerec.cpp]

The next passes are only required for Tess-only

pass 3 recongnize

Walk over the page finding sequences of words joined by fuzzy spaces. Extract them as a sublist, process the sublist to find the optimal arrangement of spaces then replace the sublist in the ROW_RES.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::fix_fuzzy_spaces [ccmain/fixspace.cpp] ->
  7. Tesseract::fix_sp_fp_word [ccmain/fixspace.cpp] ->
  8. Tesseract::fix_fuzzy_space_list [ccmain/fixspace.cpp]

pass 4 recongnize

dictionary_correction_pass

If a word has multiple alternates check if the best choice is in the dictionary. If not, replace it with an alternate that exists in the dictionary.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::dictionary_correction_pass [ccmain/control.cpp]
bigram_correction_pass
  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::bigram_correction_pass [ccmain/control.cpp]

pass 5 recongnize

Gather statistics on rejects.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::rejection_passes [ccmain/control.cpp] ->
  7. REJMAP::rej_word_bad_quality [ccstruct/rejctmap.cpp]

pass 6 recongnize

Do whole document or whole block rejection pass

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::rejection_passes [ccmain/control.cpp] ->
  7. Tesseract::quality_based_rejection [ccmain/docqual.cpp] ->
  8. Tesseract::doc_and_block_rejection [ccmain/docqual.cpp] ->
  9. reject_whole_page [ccmain/docqual.cpp] ->
  10. REJMAP::rej_word_block_rej [ccstruct/rejctmap.cpp]

It seems to lack the pass 7 recongnize in the source code.

pass 8 recongnize

Smooth the fonts for the document.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::font_recognition_pass [ccmain/control.cpp]

pass 9 recongnize

Check the correctness of the final results.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::blamer_pass [ccmain/control.cpp] ->
  7. Tesseract::script_pos_pass [ccmain/control.cpp]

After all the recongnization, Tess removes empty words, as these mess up the result iterators.

段落检测

This is called after rows have been identified and words are recognized. Much of this could be implemented before word recognition, but text helps to identify bulleted lists and gives good signals for sentence boundaries.

pass 1 detection

Detect sequences of lines that all contain leader dots (.....) These are likely Tables of Contents. If there are three text lines in a row with leader dots, it's pretty safe to say the middle one should be a paragraph of its own.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. TessBaseAPI::DetectParagraphs [api/baseapi.cpp] ->
  6. DetectParagraphs [ccmain/paragraphs.cpp] ->
  7. DetectParagraphs [ccmain/paragraphs.cpp] ->
  8. SeparateSimpleLeaderLines [ccmain/paragraphs.cpp] ->
  9. LeftoverSegments [ccmain/paragraphs.cpp]

pass 2a detection

Find any strongly evidenced start-of-paragraph lines. If they're followed by two lines that look like body lines, make a paragraph model for that and see if that model applies throughout the text (that is, "smear" it).

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. TessBaseAPI::DetectParagraphs [api/baseapi.cpp] ->
  6. DetectParagraphs [ccmain/paragraphs.cpp] ->
  7. DetectParagraphs [ccmain/paragraphs.cpp] ->
  8. StrongEvidenceClassify [ccmain/paragraphs.cpp]

pass 2b detection

If we had any luck in pass 2a, we got part of the page and didn't know how to classify a few runs of rows. Take the segments that didn't find a model and reprocess them individually.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. TessBaseAPI::DetectParagraphs [api/baseapi.cpp] ->
  6. DetectParagraphs [ccmain/paragraphs.cpp] ->
  7. DetectParagraphs [ccmain/paragraphs.cpp] ->
  8. LeftoverSegments [ccmain/paragraphs.cpp] ->
  9. StrongEvidenceClassify [ccmain/paragraphs.cpp]

pass 3 detection

These are the dregs for which we didn't have enough strong textual and geometric clues to form matching models for. Let's see if the geometric clues are simple enough that we could just use those.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. TessBaseAPI::DetectParagraphs [api/baseapi.cpp] ->
  6. DetectParagraphs [ccmain/paragraphs.cpp] ->
  7. DetectParagraphs [ccmain/paragraphs.cpp] ->
  8. LeftoverSegments [ccmain/paragraphs.cpp] ->
  9. GeometricClassify [ccmain/paragraphs.cpp] ->
  10. DowngradeWeakestToCrowns [ccmain/paragraphs.cpp]

pass 4 detection

Take everything that's still not marked up well and clear all markings.

  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. TessBaseAPI::DetectParagraphs [api/baseapi.cpp] ->
  6. DetectParagraphs [ccmain/paragraphs.cpp] ->
  7. DetectParagraphs [ccmain/paragraphs.cpp] ->
  8. LeftoverSegments [ccmain/paragraphs.cpp] ->
  9. SetUnknown [ccmain/paragraphs_internal.h]

Convert all of the unique hypothesis runs to PARAs.

ConvertHypothesizedModelRunsToParagraphs [ccmain/paragraphs.cpp]

Finally, clean up any dangling NULL row paragraph parents.

CanonicalizeDetectionResults [ccmain/paragraphs.cpp]

纠错

dictionary error correction

Verify whether the recongnized word is in the word_dic (unicharset)

  • 调用栈
  1. main [api/tesseractmain.cpp] ->
  2. TessBaseAPI::ProcessPages [api/baseapi.cpp] ->
  3. TessBaseAPI::ProcessPage [api/baseapi.cpp] ->
  4. TessBaseAPI::Recognize [api/baseapi.cpp] ->
  5. Tesseract::recog_all_words [ccmain/control.cpp] ->
  6. Tesseract::RecogAllWordsPassN [ccmain/control.cpp] ->
  7. Tesseract::classify_word_and_language [ccmain/ control.cpp] ->
  8. Tesseract::classify_word_pass1 [ccmain/ control.cpp] ->
  9. Tesseract::tess_segment_pass_n [ccmain/ tessbox.cpp] ->
  10. Tesseract::recog_word [ccmain/ tfacepp.cpp] ->
  11. Wordrec::dict_word [wordrec/ tface.cpp] ->
  12. Dict::valid_word [dict/ dict.cpp]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容