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

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

文章目录

  • [1. Tesseract安装](#1. Tesseract安装)
  • [2. pytesseract的安装与使用](#2. pytesseract的安装与使用)
  • [3. 手动安装其他语种并在pytesseract中调用](#3. 手动安装其他语种并在pytesseract中调用)
  • [4. 本文撰写过程中参考的其他网络资料](#4. 本文撰写过程中参考的其他网络资料)

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):

python 复制代码
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

以法语为例,下载https://github.com/tesseract-ocr/tessdata_fast/blob/main/fra.traineddata到tessdata路径下(如在本文中,就是D:\allApplications\forTesseract\newFolderForTesseract\tessdata)

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

python 复制代码
import pytesseract

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

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

输出:

复制代码
['eng', 'fra', 'osd']

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

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

python 复制代码
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 [email protected] 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.

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

  1. python - Tesseract installation in windows - Stack Overflow
  2. [WinError 5] 拒绝访问。_pytesseract 拒绝访问-CSDN博客
相关推荐
ruyingcai6666663 分钟前
用python进行OCR识别
开发语言·python·ocr
大数据魔法师1 小时前
Hadoop生态圈框架部署 - Windows上部署Hadoop
大数据·hadoop·windows
江沉晚呤时2 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
聂 可 以8 小时前
推荐几个可以免费下载视频的软件(Neat Download Manager、蜗牛下载助手、bilidown)
windows·开源软件
菜鸟射手16 小时前
QT creater和vs2017文件路径问题
linux·c++·windows·qt
爱编程的鱼16 小时前
Windows 各版本查找计算机 IP 地址指南
人工智能·windows·网络协议·tcp/ip·tensorflow
sukalot16 小时前
Windows 图形显示驱动开发-WDDM 1.2功能—Windows 8 中的 DirectX 功能改进(九)
windows
simple_whu16 小时前
解决编译pcl时报错‘chrono_literals‘: is not a member of ‘std‘
c++·windows·visual studio
dpxiaolong17 小时前
RK3588平台用v4l工具调试USB摄像头实践(亮度,饱和度,对比度,色相等)
android·windows
辣香牛肉面18 小时前
识别图片内容OCR并重命名文件
ocr·图片识别