前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tesseract-文字识别工具

Tesseract-文字识别工具

作者头像
悠扬前奏
发布2019-05-30 20:21:00
2.6K0
发布2019-05-30 20:21:00
举报

0. 背景

最近在准备一个爬虫项目,准备阶段了解到一个文字识别工具,用在验证码方面很方便。 现在主力开发机是mac,本文流程都是基于mac。

1 安装

代码语言:javascript
复制
//安装tesseract的同时安装训练工具
brew install --with-training-tools tesseract

//安装tesseract的同时安装所有语言,语言包比较大,如果安装的话时间较长,建议不安装,按需选择
brew install  --all-languages tesseract

//安装tesseract,并安装训练工具和语言
brew install --all-languages --with-training-tools tesseract 

//只安装tesseract,不安装训练工具
brew install tesseract

2. 语言库

作为文字识别工具,需要安装识别的语言库。 下载需要的语言之后,放到/usr/local/Cellar/tesseract/3.05.01/share/tessdata路径下。 常用的如下:

库名

语言

chi_sim.traineddata

中文

chi_sim_vert.traineddata

中文精简集

eng.traineddata

英文

3.Tesseract的使用

  • 帮助文档
代码语言:javascript
复制
~:Tesseract pengjunzhe$ tesseract help
Usage:
  tesseract --help | --help-psm | --help-oem | --version
  tesseract --list-langs [--tessdata-dir PATH]
  tesseract --print-parameters [options...] [configfile...]
  tesseract imagename|stdin outputbase|stdout [options...] [configfile...]

OCR options:
  --tessdata-dir PATH   Specify the location of tessdata path.
  --user-words PATH     Specify the location of user words file.
  --user-patterns PATH  Specify the location of user patterns file.
  -l LANG[+LANG]        Specify language(s) used for OCR.
  -c VAR=VALUE          Set value for config variables.
                        Multiple -c arguments are allowed.
  --psm NUM             Specify page segmentation mode.
  --oem NUM             Specify OCR Engine mode.
NOTE: These options must occur before any configfile.

Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
            bypassing hacks that are Tesseract-specific.
OCR Engine modes:
  0    Original Tesseract only.
  1    Cube only.
  2    Tesseract + cube.
  3    Default, based on what is available.

Single options:
  -h, --help            Show this help message.
  --help-psm            Show page segmentation modes.
  --help-oem            Show OCR Engine modes.
  -v, --version         Show version information.
  --list-langs          List available languages for tesseract engine.
  --print-parameters    Print tesseract parameters to stdout.
  • 默认使用
代码语言:javascript
复制
# 默认使用eng(英文)文字库,imgName是图片地址,result是识别结果
tesseract imgName result
  • 指定语言
代码语言:javascript
复制
//指定使用简体中文
tesseract -l chi_sim imgName result

//查看本地存在的语言库
tesseract --list-langs
  • psm参数(page segmentation modes) help文档中的介绍如下
代码语言:javascript
复制
Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.

解释:

代码语言:javascript
复制
0 - 仅做定位和脚本检测(OSD)
1 - 使用OSD自动分页
2 - 自动分页,但是不使用OSD或者OCR
3 - 全自动分页,没使用OSD
4 - 假定是一列可变大小文本
5 - 假定是一块垂直对齐的文本
6 - 假定是一块统一的格式的文本
7 - 视图像为一行文本
8 - 视图像为一个单词
9 - 使图像为环形排布的单词
10 - 视图像为单个字符

4. 字符训练

字符训练是一个很重要,也很复杂的话题。以后深入学习了单开话题进行补充。

5. Python库

安装好tesseract之后就可以在Python中通过库文件很方便的把这个功能做到程序中了。

  1. pip install pytesseract不多说。
  2. 简单的源码:
代码语言:javascript
复制
# -*-encoding:utf-8-*-
import pytesseract
from PIL import Image


def main():
    # 打开图片
    image0 = Image.open("./img/0.jpg")
    image1 = Image.open("./img/1.jpg")
    # 使用默认字符集(英文)识别图片
    text0 = pytesseract.image_to_string(image0)
    # 使用默认字符集(中文)识别图片
    text1 = pytesseract.image_to_string(image1, lang='chi_sim')
    # 输出
    print(text0)
    print(text1)


if __name__ == '__main__':
    main()
  1. 结果
  • 英文原图:

831524628903_.pic.jpg

  • 识别结果: Hello worldl
  • 中文原图:

891524629631_.pic.jpg

  • 识别结果: 2018年清明节工作 日历女口下图二

可见,英文识别还可以,中文适应度不是很高。对于左右结构的字识别能力较差。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.04.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0. 背景
  • 1 安装
  • 2. 语言库
  • 3.Tesseract的使用
  • 4. 字符训练
  • 5. Python库
相关产品与服务
文字识别
文字识别(Optical Character Recognition,OCR)基于腾讯优图实验室的深度学习技术,将图片上的文字内容,智能识别成为可编辑的文本。OCR 支持身份证、名片等卡证类和票据类的印刷体识别,也支持运单等手写体识别,支持提供定制化服务,可以有效地代替人工录入信息。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档