当前位置:   article > 正文

Python OCR工具pytesseract详解_pytesseract中文文档

pytesseract中文文档

参考:Python OCR工具pytesseract详解_测试开发小记的博客-CSDN博客_pytesseract

一、引言

OCR(Optical character recognition,光学字符识别)是一种将图像中的手写字或者印刷文本转换为机器编码文本的技术。通过数字方式存储文本数据更容易保存和编辑,可以存储大量数据,比如1G的硬盘可以存储数百万本书。

OCR技术可以将图片,纸质文档中的文本转换为数字形式的文本。OCR过程一般包括以下步骤:

  1. 图像预处理
  2. 文本定位
  3. 字符分割
  4. 字符识别
  5. 后处理

最初由惠普开发,后来Google赞助的开源OCR引擎 tesseract 提供了比较精确的文字识别API,本文将要介绍的Python库Pytesseract就是基于Tesseract-OCR 引擎。

二、环境配置

环境要求:

  • Python 3.6+
  • PIL库
  • 安装Google Tesseract OCR
  • 系统:windows/mac/linux,我的系统是Windows10

2.1 安装Google Tesseract

安装时可以选择需要的语言包:

安装完成后,添加到环境变量PATH中,我的安装路径是:C:\Program Files\Tesseract-OCR 。

命令行窗口输入:tesseract ,查看是否安装成功。

  1. $ tesseract
  2. Usage:
  3. tesseract --help | --help-extra | --version
  4. tesseract --list-langs
  5. tesseract imagename outputbase [options...] [configfile...]
  6. OCR options:
  7. -l LANG[+LANG] Specify language(s) used for OCR.
  8. NOTE: These options must occur before any configfile.
  9. Single options:
  10. --help Show this help message.
  11. --help-extra Show extra help for advanced users.
  12. --version Show version information.
  13. --list-langs List available languages for tesseract engine.

2.2 安装pytesseract

Python tesseract:GitHub - madmaze/pytesseract: A Python wrapper for Google Tesseract

pip安装pytesseract

pip install pytesseract

另外需要安装一下Pillow库,用于图像处理。

pip install Pillow

三、文字识别小例子

先准备一张包含英文字符的图片,下面的代码实现提取图片中的中文和英文字符,并识别为字符串:

  1. import pytesseract
  2. try:
  3. from PIL import Image
  4. except ImportError:
  5. import Image
  6. # 列出支持的语言
  7. print(pytesseract.get_languages(config=''))
  8. print(pytesseract.image_to_string(Image.open('test.png'), lang='chi_sim+eng'))

识别下面图片中的文字(test.png):

执行结果:

  1. ['chi_sim', 'eng', 'osd']
  2. 拳 列出支持的语言
  3. print(pytesseract.get_languages (config=”))
  4. print(pytesseract.image_to_string(Image.open('test.png'), lang='chi_sim+eng'))

四、获取文字位置信息

  • image_to_boxes() 方法返回识别到的字符及字符边框信息。
  • image_to_data() 返回单词及单词位置信息。下面来看看这两种方法的执行效果,识别下图中的中文字符:

  1. img = Image.open('testimg2.png')
  2. print(pytesseract.image_to_boxes(img, output_type=Output.STRING, lang='chi_sim'))
  3. print("#"*30)
  4. print(pytesseract.image_to_data(img, output_type=Output.STRING, lang='chi_sim'))

执行结果:

  1. 生 63 211 80 227 0
  2. 存 81 209 118 227 0
  3. 是 122 211 139 226 0
  4. 文 126 200 154 231 0
  5. 明 142 210 157 226 0
  6. 的 162 209 197 227 0
  7. 第 200 217 218 219 0
  8. 一 221 209 236 226 0
  9. 需 217 200 253 231 0
  10. 要 239 209 259 226 0
  11. 。 260 211 266 216 0
  12. 猜 325 64 364 82 0
  13. 疑 364 64 481 82 0
  14. 链 373 54 393 86 0
  15. 和 383 54 403 86 0
  16. 技 403 54 435 86 0
  17. 术 419 54 451 86 0
  18. 爆 441 54 477 86 0
  19. 炸 469 54 485 86 0
  20. ##############################
  21. level page_num block_num par_num line_num word_num left top width height conf text
  22. 1 1 0 0 0 0 0 0 566 279 -1
  23. 2 1 1 0 0 0 63 52 203 18 -1
  24. 3 1 1 1 0 0 63 52 203 18 -1
  25. 4 1 1 1 1 0 63 52 203 18 -1
  26. 5 1 1 1 1 1 63 52 55 18 96 生存
  27. 5 1 1 1 1 2 122 53 17 15 96 是
  28. 5 1 1 1 1 3 126 48 31 31 96 文明
  29. 5 1 1 1 1 4 162 52 35 18 96 的
  30. 5 1 1 1 1 5 200 60 18 2 91 第
  31. 5 1 1 1 1 6 221 53 15 17 93 一
  32. 5 1 1 1 1 7 217 48 42 31 93 需要
  33. 5 1 1 1 1 8 260 63 6 5 91 。
  34. 2 1 2 0 0 0 325 197 156 18 -1
  35. 3 1 2 1 0 0 325 197 156 18 -1
  36. 4 1 2 1 1 0 325 197 156 18 -1
  37. 5 1 2 1 1 1 325 197 156 18 94 猜疑
  38. 5 1 2 1 1 2 373 193 20 32 77 链
  39. 5 1 2 1 1 3 383 193 20 32 92 和
  40. 5 1 2 1 1 4 403 193 48 32 96 技术
  41. 5 1 2 1 1 5 441 193 44 32 94 爆炸

根据image_to_data() 方法返回的位置信息,下面来标出识别出的词语位置。

  1. import numpy as np
  2. import pytesseract
  3. from pytesseract import Output
  4. import cv2
  5. try:
  6. from PIL import Image
  7. from PIL import ImageDraw
  8. from PIL import ImageFont
  9. except ImportError:
  10. import Image
  11. img = cv2.imread('testimg2.png')
  12. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  13. thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  14. cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  15. cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  16. width_list = []
  17. for c in cnts:
  18. _, _, w, _ = cv2.boundingRect(c)
  19. width_list.append(w)
  20. wm = np.median(width_list)
  21. tess_text = pytesseract.image_to_data(img, output_type=Output.DICT, lang='chi_sim')
  22. for i in range(len(tess_text['text'])):
  23. word_len = len(tess_text['text'][i])
  24. if word_len > 1:
  25. world_w = int(wm * word_len)
  26. (x, y, w, h) = (tess_text['left'][i], tess_text['top'][i], tess_text['width'][i], tess_text['height'][i])
  27. cv2.rectangle(img, (x, y), (x + world_w, y + h), (255, 0, 0), 1)
  28. im = Image.fromarray(img)
  29. draw = ImageDraw.Draw(im)
  30. font = ImageFont.truetype(font="simsun.ttc", size=18, encoding="utf-8")
  31. draw.text((x, y - 20), tess_text['text'][i], (255, 0, 0), font=font)
  32. img = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)
  33. cv2.imshow("TextBoundingBoxes", img)
  34. cv2.waitKey(0)

执行结果:

另外说明一下, ImageFont.truetype(font="simsun.ttc", size=18, encoding="utf-8") 用于设置字体及编码格式,原因是draw.text() 默认使用ISO-8859-1(latin-1)编码,中文需要使用UTF-8编码。Windows中,字体存放路径一般为C:\Windows\Fonts ,已经添加到了环境变量,直接写字体名称就可以了,simsun.ttc 表示宋体。

如果不知道字体对应名称可以进入注册表查看:运行窗口或者命令行窗口输入regedit打开注册表,进入如下路径:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts ,可以查看对应字体文件名称。

五、多语言识别

5.1 使用方法

图片中可能包含了多种语言,比如在上面的例子中,图片包含了中文和英文,lang='chi_sim+eng' 表示识别简体中文和英文。

简体中文chi_sim是在安装tesseract时勾选的,get_languages() 方法列出了支持的语言,也可以在命令行窗口执行 tesseract --list-langs 查看支持的语言:

  1. $ tesseract --list-langs
  2. List of available languages (3):
  3. chi_sim
  4. eng
  5. osd

除了使用 lang='chi_sim+eng' 方式指定语言外,也可以使用config='-l chi_sim+eng' 形式:

  1. img = Image.open('test.png')
  2. config = r'-l chi_sim+eng --psm 6'
  3. print(pytesseract.image_to_string(img, config=config))

执行结果和前面一样。

5.2 训练数据

如果需要下载其它语言包,可以到这里Traineddata Files for Version 4.00 + | tessdoc下载。

Tesseract 提供了三种训练数据:

训练数据训练模型识别速度正确率
tessdata_fastLSTM最快最低
tessdata_bestLSTM最慢最高
tessdataLegacy + LSTM中等略低于tesdata -best

根据自己的需要下载需要的模型文件,将traineddata文件放在 C:\Program Files\Tesseract-OCR\tessdata 目录(Tesseract安装目录)下就可以了。

tessdata_best可用来再训练字库,训练方法参考文档:How to train Tesseract 4.00 | tessdoc

七、OCR选项

多语言识别中使用了 -l 和 --psm 选项,tesseract还支持更多的OCR选项。

7.1 OCR选项:

  • --tessdata-dir PATHSpecify the location of tessdata path.
  • --user-words PATHSpecify the location of user words file.
  • --user-patterns PATHSpecify the location of user patterns file.
  • --dpi VALUESpecify DPI for input image.
  • -l LANG[+LANG]Specify language(s) used for OCR.
  • -c VAR=VALUESet value for config variables. Multiple -c arguments are allowed.
  • --psm NUMSpecify page segmentation mode.
  • --oem NUMSpecify OCR Engine mode.

在pytesseract中的使用方法是添加config参数:config='--psm 0 -c min_characters_to_try=5'

下面介绍一下psm和oem这两个选项。

7.2 图片分割模式(PSM)

tesseract有13种图片分割模式(page segmentation mode,psm):

  • 0 – Orientation and script detection (OSD) only. 方向及语言检测(Orientation and script detection,OSD)
  • 1 – Automatic page segmentation with OSD. 自动图片分割
  • 2 – Automatic page segmentation, but no OSD, or OCR. 自动图片分割,没有OSD和OCR
  • 3 – Fully automatic page segmentation, but no OSD. (Default) 完全的自动图片分割,没有OSD
  • 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. OSD稀疏文本
  • 13 – Raw line. Treat the image as a single text line, bypassing hacks that are Tesseract-specific. 原始行。将图像视为单个文本行。

7.3 OCR引擎模式(OEM)

有4种OCR引擎模式:

  • 0 – Legacy engine only.
  • 1 – Neural nets LSTM engine only.
  • 2 – Legacy + LSTM engines.
  • 3 – Default, based on what is available.

八、方向及语言检测OSD

Tesseract支持方向及语言检测(Orientation and script detection,OSD) ,比如检测下面的图片:

  1. osd = pytesseract.image_to_osd('osd-example.png',config='--psm 0 -c min_characters_to_try=5')
  2. print(osd)

其中 min_characters_to_try 表示设置最小字符数,默认为50。

执行结果:

  1. Page number: 0
  2. Orientation in degrees: 90
  3. Rotate: 270
  4. Orientation confidence: 0.74
  5. Script: Han
  6. Script confidence: 0.83

结果是旋转了270度,识别到的语言为中文Han。

九、提取数字

只提取下面图片中的数字:

  1. img = Image.open('number-example.png')
  2. config = r'--oem 3 --psm 6 outputbase digits'
  3. osd = pytesseract.image_to_string(img, config=config)
  4. print(osd)

执行结果:

  1. 1200-.41194-.
  2. 4-.
  3. 12000000
  4. 11994933.
  5. -119940218
  6. 119932207
  7. 1199251
  8. 119915241
  9. 119907238
  10. -119853209
  11. 1119450495
  12. .-11941637

十、字符白名单

只检测特定的字符:只检测数字

  1. img = Image.open('number-example.png')
  2. config = r'-c tessedit_char_whitelist=0123456789 --psm 6'
  3. print(pytesseract.image_to_string(img, config=config))

执行结果:

  1. 12001194
  2. 12000000
  3. 11994933
  4. 11994018
  5. 11993207
  6. 11992851
  7. 11991541
  8. 11990738
  9. 11985309
  10. 11945049
  11. 11941637

发现识别精度比 outputbase digits 方法更加准确。

十一、字符黑名单

不检测数字:

  1. img = Image.open('number-example.png')
  2. config = r'-c tessedit_char_blacklist=0123456789 --psm 6'
  3. print(pytesseract.image_to_string(img, config=config, lang='chi_sim'))

执行结果:

  1. 胶片很快冲出来了,他开始查看哪张值得放大洗成照片,
  2. 在第一张就发现了一件离奇的事。一个倒计时。倒计时从
  3. 小时开始,到现在还剩余 小时。
  4. 这张拍的是一个大商场外的一小片草地,他看到底片正中
  5. 有一行白色的东西,
  6. 细看是一排数字: : :
  7. 第二张底片上也有数字: l]: : -。
  8. 第三张: l : : lg,
  9. 第四张: : : ,
  10. 第五张: ] : : l;
  11. 第六张: : : l,
  12. 第七张: l : o : g ;
  13. 第八张: lg : : ;
  14. 第三十四张: : :
  15. 第三十六张,也是最后一张: : :

十二、格式转换

pytesseract 支持将图片转换为PDF、HOCR以及ALTO XML格式。

  1. pdf = pytesseract.image_to_pdf_or_hocr('testimg2.png', extension='pdf')
  2. with open('test.pdf', 'w+b') as f:
  3. f.write(pdf)
  4. hocr = pytesseract.image_to_pdf_or_hocr('testimg2.png', extension='hocr')
  5. xml = pytesseract.image_to_alto_xml('testimg2.png')
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/231325
推荐阅读
相关标签
  

闽ICP备14008679号