Tesseract是一款开源 OCR(Optical Character Recognition,光学字符识别)软件。所谓OCR是图像识别领域中一个子领域,该领域专注于对图片中的文字信息进行识别并转换成能被常规文本编辑器编辑的文本。
Tesseract 已经有30年历史,开始它是惠普实验室的一款专利软件,然后在 2005 年开源,自 2006 年后由 Google 赞助进行后续的开发和维护。在 1995 年 Tesseract 曾是世界前三的 OCR 引擎,而且在现在的免费 OCR 引擎中,其识别精度也仍然是出类拔萃的。因为其免费与较好的效果,许多的个人开发者以及一些较小的团队在使用着 Tesseract ,诸如验证码识别、车牌号识别等应用中,不难见到 Tesseract 的身影。
安装pytesseract
python2
- pip install pytesseract
try:
from PIL import Image
except ImportError:
import Image
from pytesseract import image_to_string
import pytesseract
# 只能识别一些简单的验证码 比较正规的图片
img = Image.open('验证码.jpg')
img_grey = img.convert('L')
threshold = 140
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
img_out = img_grey.point(table, '1')
text = image_to_string(img_grey) # 将图片转成字符串
print text
image = Image.open('t.png')
# 使用中文库
code = pytesseract.image_to_string(image, lang='chi_sim')
print(code)
性能
经测试,pytesser适用于比较规范的英文/数字,对于中文的识别能力较差,对于有干扰素的验证码图片性能较差,可以用来提取简单的英文/数字。