tessercat安装教程

#leptonica

##下载

leptonica下载链接

##编译和安装

阅读 README 文件
此处可以用j4加速

1
2
3
4
5
./configure
make
sudo make install
make check
```

#tesseract

##下载

tesseract项目地址

1
git clone git@github.com:tesseract-ocr/tesseract.git

##下载语言包

google code 上的语言包比较好用

英文的词包地址为:

https://github.com/tesseract-ocr/langdata/tree/master/eng
https://code.google.com/p/tesseract-ocr/downloads/list

利用svn同步语言包到本地

1
svn checkout https://github.com/tesseract-ocr/langdata/trunk/eng

如果是英语,把语言包放在 tessercat/tessdata/eng 目录下,汉语就是最后改为chi_sim

##编译和安装

安装命令如下

1
2
3
4
./configure
make
sudo make install
sudo ldconfig

##配置环境变量

环境变量配置如下

1
export TESSDATA_PREFIX="directory in which your tessdata resides/"

举个例子就是

1
export TESSDATA_PREFIX="/home/username/dev/CBIR/tesseract/"

#训练数据

##安装依赖

需要以下依赖库

1
2
3
sudo apt-get install libicu-dev
sudo apt-get install libpango1.0-dev
sudo apt-get install libcairo2-dev

##编译和安装训练工具

1
2
make training
sudo make training-install

python 调用 tesseract 识别验证码

有很多实验性的代码

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding:utf-8 -*-
# author: BLKStone
# date: 2015-12-16
import cv2
import numpy as np
import pytesseract
from PIL import Image
from PIL import ImageFilter
import os
class Analyzer(object):
def __init__(self, img_path):
self.img_path = img_path
self.img = cv2.imread(img_path)
self.imgPreprocess = None
self.rows, self.cols, self.channels = self.img.shape
self.height, self.width = self.img.shape[:2]
self.m_debug = True
def th1(self,img):
# 大津算法
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 0, 255,
cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return thresh
def preprocess(self):
self.resizeCaptcha()
# 二值化
img_thresh = self.th1(self.img)
if self.m_debug:
cv2.imshow("thres", img_thresh)
cv2.imwrite("debug/threshold.png", img_thresh)
# 形态学运算 闭操作
kernel = np.ones((2,2),np.uint8)
closing = cv2.morphologyEx(img_thresh , cv2.MORPH_CLOSE, kernel)
if self.m_debug:
cv2.imshow("close", closing)
cv2.imwrite("debug/closing.png",closing)
# 扩大边界
constant = cv2.copyMakeBorder(closing ,30,30,30,30,cv2.BORDER_CONSTANT,value=255)
# inverse binary image
constant = self.inverseColor(constant)
# http://www.bubuko.com/infodetail-1004382.html
# 合并
constantSrc = cv2.merge((constant,constant,constant))
self.imgPreprocess = constantSrc.copy()
def tessRecognize(self):
# tesseract debug/closing.png ./result
# os.system('export TESSDATA_PREFIX="/home/ray/dev/CBIR/tesseract/"')
os.system('tesseract debug/closing.png ./result')
try:
with open('result.txt', 'r') as f :
for line in f:
print line
except:
pass
cv2.imshow('orignal',self.img)
cv2.waitKey(0)
# 反转二值图像 0 变 255, 255 变 0
def inverseColor(self,img):
if len(img.shape)==2:
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img[i][j]==255:
img[i][j]=0
else:
img[i][j]=255
elif len(img.shape)==3:
for i in range(img.shape[0]):
for j in range(img.shape[1]):
for k in range(img.shape[2]):
if img[i][j][k]==255:
img[i][j][k]=0
else:
img[i][j][k]=255
return img
def analyze(self):
self.resizeCaptcha()
# 二值化
img_thresh = self.th1(self.img)
if self.m_debug:
cv2.imshow("thres", img_thresh)
cv2.imwrite("debug/threshold.png", img_thresh)
# 形态学运算 闭操作
kernel = np.ones((2,2),np.uint8)
closing = cv2.morphologyEx(img_thresh , cv2.MORPH_CLOSE, kernel)
if self.m_debug:
cv2.imshow("close", closing)
cv2.imwrite("debug/closing.png",closing)
# closing = img_thresh
# 扩大边界
constant = cv2.copyMakeBorder(closing ,30,30,30,30,cv2.BORDER_CONSTANT,value=255)
# inverse binary image
constant = self.inverseColor(constant)
# http://www.bubuko.com/infodetail-1004382.html
# 合并
constantSrc = cv2.merge((constant,constant,constant))
self.imgPreprocess = constantSrc.copy()
if self.m_debug:
cv2.imwrite('debug/constant.png',constant)
cv2.imwrite('debug/constantSrc.png',constant)
# 求轮廓
# contours[0] 中存储的是每个矩形顶点的坐标,如下所示
# [[[ 1 1]]
# [[ 1 38]]
# [[78 38]]
# [[78 1]]]
constant, contours, hierarchy = cv2.findContours(constant,
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
if self.m_debug:
for i in range(len(contours)):
print '绘制第',i,'个轮廓'
imgContours = cv2.drawContours(constantSrc, contours, i, (0,255,0), 2)
cv2.imshow('contours',imgContours)
cv2.imwrite('debug/contours.png',imgContours)
cv2.waitKey(0)
rotate_rects = []
box_rects = []
# 筛选轮廓
for i in range(0,len(contours)):
# mr的结构为 (top-left corner(x,y), (width, height), angle of rotation )
mr = cv2.minAreaRect(contours[i])
if self.verifySize(mr):
box = cv2.boxPoints(mr) # if you are use opencv 3.0.0
# box = cv2.cv.boxPoints(mr) # if your are using opencv 2.4.11
box = np.int0(box)
rotate_rects.append(mr)
box_rects.append(box)
else:
pass
# 排序
rotate_rects = sorted(rotate_rects,key = lambda x:x[0][0])
box_rects = sorted(box_rects,key = lambda x:x[0][0])
# 绘制选择后的
if self.m_debug:
for i in range(len(box_rects)):
print "绘制第",i,"个矩形"
# print box_rects[i]
# print 'rotate rect',rotate_rects[i]
mr = rotate_rects[i]
print mr
print box_rects[i]
# print mr[1][0]*mr[1][1]
imgContoursChosen = cv2.drawContours(constantSrc, box_rects, i, (255,0,0), 1)
cv2.imshow('contours chosen',imgContoursChosen)
cv2.imwrite('debug/contoursChosen.png',imgContoursChosen)
cv2.waitKey(0)
# 提取目标
charTarget = [] # 存储结果的list
for i in range(0,len(rotate_rects)):
mr = rotate_rects[i]
# 防止出现除以 0 的错误
if mr[1][1] == 0:
continue
ratio = mr[1][0] / mr[1][1]
angle = mr[2]
rect_size = [mr[1][0],mr[1][1]]
print '正在处理第',i,'个矩形'
if ( ratio > 1 ):
angle = 90 + angle
rect_size[0],rect_size[1] = rect_size[1],rect_size[0] # swap height and width
# 计算矩形中心点
center_x = (box_rects[i][0][0]+box_rects[i][1][0]+box_rects[i][2][0]+box_rects[i][3][0])/4
center_y = (box_rects[i][0][1]+box_rects[i][1][1]+box_rects[i][2][1]+box_rects[i][3][1])/4
center = (center_x,center_y)
#cv2.getRotationMatrix2D(center, angle, scale) → retval
# 获取2*3旋转矩阵
rotmat = cv2.getRotationMatrix2D(center,angle,1)
#cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) → dst
imgSrc = self.imgPreprocess
rows,cols,channels = imgSrc.shape
rotated = cv2.warpAffine(imgSrc, rotmat,(cols,rows))
print rotated .shape
if self.m_debug:
picname = 'debug/rotate_'+str(i)+'.png'
cv2.imwrite(picname,rotated)
# 接下来的目标是获取到字符碎块
imgResized = self.showResultMat(rotated, (int(rect_size[0]),int(rect_size[1])),center , i)
charTarget.append(imgResized)
def verifySize(self, minAreaRect):
# mr的结构为 (top-left corner(x,y), (width, height), angle of rotation )
area = minAreaRect[1][0] * minAreaRect[1][1]
if area > 6000 and area < 15000:
return True
return False
def captureChar(self, img, rect_size, center, index):
imgCorp = cv2.getRectSubPix(imgRotated,rect_size,center)
def showResultMat(self, imgRotated, rect_size, center, index):
m_width = 136
m_height = 36
imgCorp = cv2.getRectSubPix(imgRotated,rect_size,center)
imgCorp = cv2.copyMakeBorder(imgCorp ,30,30,30,30,cv2.BORDER_CONSTANT,value=(0,0,0))
#constant = cv2.copyMakeBorder(closing ,30,30,30,30,cv2.BORDER_CONSTANT,value=255)
imgCorp = self.inverseColor(imgCorp)
print 'resize',imgCorp.shape
if self.m_debug:
picname = 'debug/rotate_fragment_'+str(index)+'.png'
cv2.imwrite(picname,imgCorp)
# imgResized = cv2.resize(imgCorp,(m_width,m_height))
# if self.m_debug:
# picname = 'debug/rotate_fragment_resize_'+str(index)+'.png'
# cv2.imwrite(picname,imgResized)
return imgCorp
def show(self):
cv2.namedWindow("Image")
cv2.imshow("Image", self.img)
cv2.waitKey(0)
def resizeCaptcha(self):
rows,cols,channels = self.img.shape
# print rows,cols,channels
img_resize = cv2.resize(self.img,(cols*10,rows*10),interpolation = cv2.INTER_LINEAR)
self.img = img_resize
if __name__ == '__main__':
analyzer = Analyzer('./testpic/4.jpg')
analyzer.preprocess()
analyzer.tessRecognize()

#参考资料

[1] Setting Up a Simple OCR Server
[2] How to use the tools provided to train Tesseract 3.0x for a new language.
[3] google code
[4] leptonica