测试C#图像文本识别模块Tesseract的基本用法

  微信公众号“dotNET跨平台”的文章《c#实现图片文体提取》(参考文献3)介绍了C#图像文本识别模块Tesseract,后者是tesseract-ocr(参考文献2) 的C#封装版本,目前版本为5.2,关于Tesseract的详细介绍请见参考文献1和5,本文主要测试Tesseract模块的基本用法。
  之前测试过IronOCR和PaddleSharp,本文复用IronOCR的测试代码,在NuGet包管理器中安装Tesseract包,如下图所示:

在这里插入图片描述
  主要调用TesseractEngine类加载语言包、设置识别语言等,其构造函数原型如下所示。其中输入参数中的datapath指定语言包路径,安装NuGet包时不会安装语言包,需要单独下载,可以在参考文献2或者Tesseract官网中下载,language参数指定识别语言,语言名称对应语言包中文件名的开头部分(如chi_sim.traineddata对应的简体中文语言为chi_sim,eng.traineddata对应的英文为eng),engineMode参数对应的是识别引擎类型,其可选值如下所示。

public TesseractEngine(string datapath, string language, EngineMode engineMode)

public enum EngineMode
{
    //
    // 摘要:
    //     Only the legacy tesseract OCR engine is used.
    TesseractOnly,
    //
    // 摘要:
    //     Only the new LSTM-based OCR engine is used.
    LstmOnly,
    //
    // 摘要:
    //     Both the legacy and new LSTM based OCR engine is used.
    TesseractAndLstm,
    //
    // 摘要:
    //     The default OCR engine is used (currently LSTM-ased OCR engine).
    Default
}

  调用TesseractEngine类中的Process函数识别图片内容,其函数原型如下,第一个参数指定待识别的图片(Pix类为Tesseract模块自带类,可以调用Pix.LoadFromFile函数生成实例对象),第二个参数为图片处理方式,取值见下面代码所示。Process函数返回Page类实例对象,从中可以获取识别结果。

public Page Process(Pix image, PageSegMode? pageSegMode = null)

public enum PageSegMode
{
    //
    // 摘要:
    //     Orientation and script detection (OSD) only.
    OsdOnly,
    //
    // 摘要:
    //     Automatic page sementation with orientantion and script detection (OSD).
    AutoOsd,
    //
    // 摘要:
    //     Automatic page segmentation, but no OSD, or OCR.
    AutoOnly,
    //
    // 摘要:
    //     Fully automatic page segmentation, but no OSD.
    Auto,
    //
    // 摘要:
    //     Assume a single column of text of variable sizes.
    SingleColumn,
    //
    // 摘要:
    //     Assume a single uniform block of vertically aligned text.
    SingleBlockVertText,
    //
    // 摘要:
    //     Assume a single uniform block of text.
    SingleBlock,
    //
    // 摘要:
    //     Treat the image as a single text line.
    SingleLine,
    //
    // 摘要:
    //     Treat the image as a single word.
    SingleWord,
    //
    // 摘要:
    //     Treat the image as a single word in a circle.
    CircleWord,
    //
    // 摘要:
    //     Treat the image as a single character.
    SingleChar,
    SparseText,
    //
    // 摘要:
    //     Sparse text with orientation and script detection.
    SparseTextOsd,
    //
    // 摘要:
    //     Treat the image as a single text line, bypassing hacks that are specific to Tesseract.
    RawLine,
    //
    // 摘要:
    //     Number of enum entries.
    Count
}

  Page类中通过调用GetText函数获取图片中所有识别出的汇总的文本,其还支持如下函数获取文本内容或文本区域信息,后续会继续学习其用法。
在这里插入图片描述
  采用Tesseract模块的基本用法得到的图片文字识别效果如下所示。大部分可以识别出来,但是识别带车牌的图片不管是Tesseract,还是之前的IronOCR和PaddleSharp,效果都比较差,不清楚是设置的问题,还是语言包用得不对。
在这里插入图片描述
在这里插入图片描述

参考文献
[1]https://github.com/charlesw/tesseract/
[2]https://github.com/tesseract-ocr/tesseract/tree/main/src
[3]https://www.sohu.com/a/722507167_121124363
[4]https://github.com/tesseract-ocr/tessdata/
[5]https://zhuanlan.zhihu.com/p/578700314

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值