Tesseract-OCR 图片数字识别的样本训练

最近想利用python写一段识别穿越火线交易所各种道具价格的代码。命令行执行:

tesseract.exe grab.jpg result -l eng

使用默认的Tesseract语言库总会识别成字母或者乱码,如下图:

于是参考https://blog.csdn.net/yasi_xi/article/details/8763385这篇帖子,训练了一个对游戏中数字识别度较高的样本库。

训练样本:

待识别的图像如下图中出售价格我的CF点

python代码:

 
   

import win32con
import win32gui
import pytesseract
from PIL import ImageGrab
import time

def get_bin_table(threshold=105):
# 获取灰度转二值的映射table
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return table

def grab():
hwnd = win32gui.FindWindow(0, “Crossfire20170910_0000.bmp - 画图”) # 获取句柄
print(hwnd)
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
print(left, top, right, bottom)
win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(hwnd)
time.sleep(0.2)
img = ImageGrab.grab((870,478,913,495)) # 截图,获取需要识别的区域
img.show()
imggray = img.convert(‘L’) # 转化为灰度图
table = get_bin_table()
out = imggray.point(table, ‘1’)
#out.show()
text = pytesseract.image_to_string(out) # 使用简体中文解析图片则改为chi_sim
text = text.upper()
print(text)

# img.save('C:/Users/Ysx/PycharmProjects/ocr/out/%s.jpg' % text)

if name == ‘main’:
grab()

使用默认的语言库识别成功率不高,会识别为乱码或者字母。所以计划自己训练一个只能识别数字的准确语言库。

1.训练环境:首先安装jdk-10.0.1_windows-x64_bin.exe,它是java的运行环境。然后下载工具jTessBoxEditor,它是训练样本的工具。

2.样本图像:从CF截图如下图像(越多越好,不过总共也就10个数字)。

3.合并图像:运行jTessBoxEditor,菜单栏中Tools–Merge TIFF。在弹出的对话框中选择样本图像(按Shift选择多张),合并名为num.font.exp0.tif文件。

4.生成Box file文件:

命令行执行:

 cd C:\Users\Ysx\PycharmProjects\ocr\train


tesseract.exe num.font.exp0.tif num.font.exp0 batch.nochop makebox

* 生成num.font.exp0.box,BOX文件为Tessercat识别出的文字和其坐标。

5.文字校正:jTessBoxEditor工具打开num.font.exp0.tif,增加未识别的、修改识别错误的数字。

6.定义字体特征文件:(当前目录下新建记事本,输入font 0 0 0 0 0,存为font_properties,注意删除.txt后缀)

格式应为<fontname> <italic> <bold> <fixed> <serif> <fraktur>,取值1/0代表是否拥有对应属性。

7.生成语言文件:(当前目录下新建记事本,输入下列代码,存为1.bat)

rem 执行改批处理前先要目录下创建font_properties文件

echo Run Tesseract for Training…
tesseract.exe num.font.exp0.tif num.font.exp0 nobatch box.train

echo Compute the Character Set…
unicharset_extractor.exe num.font.exp0.box
mftraining -F font_properties -U unicharset -O num.unicharset num.font.exp0.tr

echo Clustering…
cntraining.exe num.font.exp0.tr

echo Rename Files…
rename normproto num.normproto
rename inttemp num.inttemp
rename pffmtable num.pffmtable
rename shapetable num.shapetable

echo Create Tessdata…
combine_tessdata.exe num.

双击1.bat执行,生成的num.traineddata存到对应的Tesseract-OCR\tessdata目录。

8.使用训练得到的num语言库:

命令行执行:

tesseract.exe grab.jpg result -l num

原来不易识别的数字可以识别了,下一步就是做交易价格低于某值的时候自动操作购买道具的代码了。



Tesseract-OCR安装和pytesseract配置参考http://www.inimei.cn/archives/297.html
字库训练https://blog.csdn.net/a443475601/article/details/9854003








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

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值