在VS2013的C#工程中使用Tesseract3.04
Tesseract3.04是Tesseract目前最新的版本,该开源项目的家已经从Google Code搬到了Github。今天在VS2013的C#项目中简单验证了Tesseract3.04的功能,在这里记录一下,供有兴趣的同学参考。
1. 参考资源说明
Tesseract开源项目:https://github.com/tesseract-ocr/tesseract,目前该项目中包含Tesseract3.04在java, android,VS等环境下的源码。
Tesseract帮助文档:http://tesseract-ocr.github.io/,值得深入学习。
A .Net wrapper for tesseract-ocr作者的Github:https://github.com/charlesw/。
2. 在VS2013的C#工程中使用Tesseract3.04
我仍然选择使用A .Net wrapper for tesseract-ocr来实现在VS2013的C#工程中使用Tesseract3.04,可以通过VS2013中的NuGet来安装该程序包。若有同学需要在C++工程中使用Tesseract3.04,可以在Tesseract在Github的仓库中下载VS相关工程自行编译。
2.1 在C#工程中使用NuGet安装A .Net wrapper for tesseract-ocr
A .Net wrapper for tesseract-ocr作者在项目说明中有如下描述:
Since tesseract and leptonica binaries are compiled with Visual Studio 2015 you’ll need to ensure you have the Visual Studio 2015 Runtime installed.
这是一个重要的提醒:这个tesseract 的dll是使用vs 2015编译的,所以必须安装其发行包,同样分X64,X86两个版本,还是依赖于开发环境,不依赖于操作系统。
因此如果开发环境是VS2013或者是VS2012,要想成功使用A .Net wrapper for tesseract-ocr,那么必须额外安装Visual Studio 2015 Runtime。
2.2 tessdata
Tesseract3.04在识别文字时,需要有相应语言的字库文件,相应文件必须放在名为”tessdata”的文件夹中。字库的版本必须与tesseract的版本一致,Tesseract在Github的仓库中有各种语言的字库可以下载。
我验证时用的是Github中Tesseract的英文字库,原始图片已经非常清晰了,但是识别结果一般般,也许字库的字体与待识别图片不一。
2.3 VS2013的C#工程中使用Tesseract3.04的实现代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/* -------------------------------------------------------- * author:livezingy * * BLOG:https://www.livezingy.com * * Development environment: * Visual Studio V2013 * * Revision History:20160813 by livezingy --------------------------------------------------------- */ using System; using System.Diagnostics; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Tesseract; namespace Tesseract304Demo { public partial class Form1 : Form { string imagePath; string tessdataPath; public Form1() { InitializeComponent(); tessdataPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))); tessdataPath = tessdataPath + @"\tessdata\"; } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { imagePath = openFileDialog.FileName; pictureBox1.Image = Image.FromFile(imagePath); } } private void button2_Click(object sender, EventArgs e) { try { using (var engine = new TesseractEngine(tessdataPath, "eng", EngineMode.Default)) { using (var img = Pix.LoadFromFile(imagePath)) { using (var page = engine.Process(img)) { richTextBox1.Text = page.GetText(); } } } } catch (Exception error) { } } } } |