在Windows 10上安装Tesseract并用pytesseract运行OCR任务

诸神缄默不语-个人CSDN博文目录

1. Tesseract安装

Tesseract官方GitHub项目链接:https://github.com/tesseract-ocr/tesseract

官方安装教程(包括各种操作系统上的):https://tesseract-ocr.github.io/tessdoc/Installation.html

本文用的是Windows系统上的安装包:https://github.com/UB-Mannheim/tesseract/wiki
下载软件:https://github.com/UB-Mannheim/tesseract/releases/download/v5.4.0.20240606/tesseract-ocr-w64-setup-5.4.0.20240606.exe

需要注意的是,Tesseract卸载时会删除整个安装目录,所以需要安装在新文件夹(默认路径就可以)

安装过程:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
(↑在这一步可以选择想要安装的其他语种,不过我选择根据需要随时安装)

在这里插入图片描述

下一步点击Install

在这里插入图片描述

在这里插入图片描述

2. pytesseract的安装与使用

pytesseract官方pip网站:https://pypi.org/project/pytesseract/

我用的Python版本是3.9.18

下载方式:pip install pytesseract

打印图片中文字的示例代码(test.png是https://github.com/madmaze/pytesseract/blob/master/tests/data/test.png):

from PIL import Image

import pytesseract

pytesseract.pytesseract.tesseract_cmd=r'D:\allApplications\forTesseract\newFolderForTesseract\tesseract'

print(pytesseract.image_to_string(Image.open('test.png')))

需要注意的是,pytesseract.pytesseract.tesseract_cmd是tesseract.exe的完整路径去掉.exe。如果只写到tesseract.exe所处的文件夹路径,会报PermissionError: [WinError 5] 拒绝访问。

原图:
在这里插入图片描述

输出:

This is a lot of 12 point text to test the
ocr code and see if it works on all types 
of file format.

The quick brown dog jumped over the
lazy fox. The quick brown dog jumped
over the lazy fox. The quick brown dog
jumped over the lazy fox. The quick
brown dog jumped over the lazy fox.

3. 手动安装其他语种并在pytesseract中调用

参考官方文档:https://github.com/UB-Mannheim/tesseract/wiki/Install-additional-language-and-script-models
https://github.com/tesseract-ocr/tessdoc/blob/main/Data-Files.md

官方给出了一套数据:https://github.com/tesseract-ocr/tessdata_fast

当然也可以用民间出的数据。

下载所需语言的数据(traineddata)到tessdata路径下(如在本文中,就是D:\allApplications\forTesseract\newFolderForTesseract\tessdata)

在pytesseract中检查现在支持哪些语种:

import pytesseract

pytesseract.pytesseract.tesseract_cmd=r'D:\allApplications\forTesseract\newFolderForTesseract\tesseract'

print(pytesseract.get_languages(config=''))

输出:

['eng', 'fra', 'osd']

(其中osd是检测文本方向,比如当文本横或者斜过去的时候就可以检测到正确的文字)

1. 法语

我下载的是https://github.com/tesseract-ocr/tessdata_fast/blob/main/fra.traineddata

在pytesseract中识别法语图片(图片来源于https://github.com/madmaze/pytesseract/blob/master/tests/data/test-european.jpg):

from PIL import Image

import pytesseract

pytesseract.pytesseract.tesseract_cmd=r'D:\allApplications\forTesseract\newFolderForTesseract\tesseract'

print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))

原图:
在这里插入图片描述

输出:

The (quick) [brown] {fox} jumps!
Over the $43,456.78 <lazy> #90 dog
& duck/goose, as 12.5% of E-mail
from aspammer@website.com is spam.
Der .schnelle” braune Fuchs springt
über den faulen Hund. Le renard brun
«rapide» saute par-dessus le chien
paresseux. La volpe marrone rapida
salta sopra il cane pigro. El zorro
marrôn râpido salta sobre el perro
perezoso. À raposa marrom räâpida
salta sobre o câo preguiçoso.

2. 日语(竖排)

我下的是GitHub上一个民间出的竖排日语训练数据包:https://raw.githubusercontent.com/zodiac3539/jpn_vert/master/jpn_ver5.traineddata

下载后需要把文件名改成jpn_vert.traineddata才能正常识别为语言。

代码也类似。我感觉识别效果还不错,例图因为比较私人化就不放了:

from PIL import Image

import pytesseract

pytesseract.pytesseract.tesseract_cmd=r'D:\allApplications\forTesseract\newFolderForTesseract\tesseract'

print(pytesseract.image_to_string(Image.open(r'日语竖排图片\p1.png'), lang='jpn_vert'))

4. 本文撰写过程中参考的其他网络资料

  1. python - Tesseract installation in windows - Stack Overflow
  2. [WinError 5] 拒绝访问。_pytesseract 拒绝访问-CSDN博客
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值