TesseractDotnet Example
Download source (TesseractDotnetExample.zip)



The use of TesseractDotnet is quite simple, except for the lack of references (the Tesseract itself does as well). For example, to OCR an image which is treated as a single line text, the code below works well:

  1. using System;
  2. using System.Drawing;
  3. using tesseract;
  4.  
  5. // ...
  6.  
  7. TesseractProcessor processor = new TesseractProcessor();
  8.  
  9. bool succeed = processor.Init(@"..\tessdata\""eng", 3); // TesseractEngineMode: DEFAULT
  10. if (!succeed)
  11. {
  12.     // Deal with error
  13.     Application.Exit();
  14. }
  15.  
  16. processor.SetVariable("tessedit_pageseg_mode""3"); // TesseractPageSegMode: PSM_SINGLE_LINE
  17.  
  18. Image image = Image.FromFile("...");
  19.  
  20. processor.Clear();
  21. processor.ClearAdaptiveClassifier();
  22.  
  23. string result = processor.Apply(image);
  24.  
  25. // ...

But there is one point requiring particular attention. The first parameter of method TesseractProcessor.Init(), dataPath, must be ended with slash "/" or backslash "\", or the initialization will be failed. When it failed, the Init() method returns false. But if you did not deal with the returned value, then when the program runs to the line calling Apply(), an exception will be throwed:

AccessViolationException:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


Current language: English · also available in: Chinese (Simplified)
Tesseract 3 语言数据的训练方法
需要用到的程序

(1) Tesseract 3.00
(2) Tesseract 3.00 Bugfix
(3) CowBoxer 1.01
(4) Universal Extractor 1.61 (非必需)

使用 Universal Extractor 将 Tesseract 的安装包解开,再用 Bugfix 里的 tesseract.exe 覆盖原来的主程序,Tesseract 就可用了。CowBoxer 是用于修改 box 文件的程序。

生成第一个 box 文件

演示中将 Tesseract 解压到了 E:\tesseract-ocr 目录。然后在该目录中建立了一个 build 目录用于存放原始数据和训练过程中生成的文件。原始图片数据一个有 3 个 (test.001.tif - test.003.tif):





首先生成第一个图片 test.001.tif 的 box 文件,这里使用官方的 eng 语言数据进行文字识别:

E:\tesseract-ocr\build>..\tesseract test.001.tif test.001 -l eng batch.nochop makebox
Tesseract Open Source OCR Engine with Leptonica
Number of found pages: 1.

执行完这个命令之后,build 目录下就生成了一个 test.001.box。使用 CowBoxer 打开这个 box 文件,CowBoxer 会自动找到同名的 tif 文件显示出来。



CowBoxer 的使用方法可以看 Help -> About 中的说明。修改完成之后 File -> Save box file 保存文件。

生成初始的 traineddata

接下来使用这一个 box 文件先生成一个 traineddata,在接下来生成其他图片的 box 文件时,使用这个 traineddata 有利于提高识别的正确率,减少修改次数。

..\tesseract test.001.tif test.001 nobatch box.train
..\training\unicharset_extractor test.001.box
..\training\mftraining -U unicharset -O test.unicharset test.001.tr
..\training\cntraining test.001.tr
rename normproto test.normproto
rename Microfeat test.Microfeat
rename inttemp test.inttemp
rename pffmtable test.pffmtable
..\training\combine_tessdata test.

在 build 目录下执行完这一系列命令之后,就生成了可用的 test.traineddata。

生成其余 box 文件

将上一步生成的 test.traineddata 移动到 tesseract-ocr\tessdata 目录中,接下来生成其他 box 文件时就可以通过 -l test 参数使用它了。

..\tesseract test.002.tif test.002 -l test batch.nochop makebox
..\tesseract test.003.tif test.003 -l test batch.nochop makebox

这里仅仅是使用 3 个原始文件作为例子。实际制作训练文件时,什么时候生成一个 traineddata 根据情况而定。中途生成 traineddata 的目的只是为了提高文字识别的准确率,使后面生成的 box 文件能少做修改。

生成最终的 traineddata

在所有的 box 都制作完成后,就可以生成最终的 traineddata 了。

..\tesseract test.001.tif test.001 nobatch box.train
..\tesseract test.002.tif test.002 nobatch box.train
..\tesseract test.003.tif test.003 nobatch box.train
..\training\unicharset_extractor test.001.box test.002.box test.003.box
..\training\mftraining -U unicharset -O test.unicharset test.001.tr test.002.tr test.003.tr
..\training\cntraining test.001.tr test.002.tr test.003.tr
rename normproto test.normproto
rename Microfeat test.Microfeat
rename inttemp test.inttemp
rename pffmtable test.pffmtable
..\training\combine_tessdata test.

在文件较多时可以用程序生成这种脚本执行。
Current language: Chinese (Simplified)
DeBejeweled3 —— 自动玩 Bejeweled 3 的小程序
昨天夜里心血来潮,把好几年前就想写的自动玩 Bejeweled 的程序给写了。不过今天下午一搜,好像有人写过了,但看视频感觉是冲着刷分去的,没什么视觉效果,和我的目的不一样。



程序是 C# 写的,从 CodeProjects 上找来了抓屏、鼠标控制以及全局热键设置的代码,自己只写玩 Bejeweled 的算法。宝石的颜色通过取几个像素的色相平均值来判断,再遇到发光之类的非普通宝石时容易判断错误,大部分情况下还都可以,通过加入一定的随机性处理不至于让操作成为死循环。

宝石具体如何拖动,是靠对预先定义的模板进行匹配得到的。模板就像下面这个样子:

--A-
XXB-
--X-
--X-

程序启动时读取这些模板,对其进行水平/垂直翻转和旋转,以得到各种可能的情况。最终生成的可用数据是一个表示宝石分布的二进制序列 (如以上这个模板的二进制序列即为 0010 1100 0010 0010),一个表示要移动宝石的位置的整数 (以上这个模板中为 2),和一个表示目标位置的整数 (以上这个模板中为 6)。

程序对所有宝石进行 4x4 和 3x3 的分块后,也为每个分块生成一个二进制序列,若其与模板的二进制序列进行逻辑与运算的结果不为0,则表明该模板可应用于该分块。最终对所有可行的移动方法按优先级进行排序后,就能确定如何进行操作了。
Current language: Chinese (Simplified)
WUSN Detector —— 通过摄像头识别手写 WUSN 编码的程序


图像预处理使用 EmguCV 库 (OpenCV 的 .NET 封装),文字识别采用 tesseractdotnet (Tesseract 3.0 的 .NET 封装)。自己主要是写了 WUSN 编码定位的算法,同时训练了一个用于 Tesseract 的自己手写英文和数字的数据文件。
Current language: Chinese (Simplified)
简易音乐节拍跟踪器已经成功 80% 了
现在用的方法还是很朴素的,话筒放大 + 可控增益放大 + 300Hz低通滤波 + 包络检波 + AD采样 + 程序处理就能达到不错的效果,对节奏感很强的音乐基本都能跟踪正确。

目前就是不知道是这个话筒放大电路设计的有问题,还是驻极体话筒的频响不够好。使用话筒拾音时的跟踪的效果要比直接使用音频信号时差很多,这个只能等到暑假再仔细看看了。






Current language: Chinese (Simplified)
More entries: [1] [2] [3] [4] [5] [6] [7] ... [21]
« Previous page · Next page »