【Python】从零配置图片的OCR识别

为了提取一本俄文书扫描件的文字内容,在新电脑上从零开始配置基于Python的OCR识别过程。
特此记录。

软件情况

系统为win11
Python为3.12.x
Anaconda为2.6.x
图片处理包:pillow
OCR包:tesseract、pytesseract

软件下载和安装

直接下载最新版anaconda:https://www.anaconda.com/download

python为最新版anaconda自带的版本。

Tesseract是实际处理OCR的软件,Pytesseract用于提供python接口。
安装流程参考 https://stitch.blog.csdn.net/article/details/118751833?fromshare=blogdetail&sharetype=blogdetail&sharerId=118751833&sharerefer=PC&sharesource=weixin_44489443&sharefrom=from_link

Tesseract需要单独下载。
Tesseract OCR github地址:https://github.com/tesseract-ocr/tesseract
Windows Tesseract下载地址:https://digi.bib.uni-mannheim.de/tesseract/
Mac和Linux安装方法参考:https://tesseract-ocr.github.io/tessdoc/Installation.html

安装时注意勾选需要的语言包!!!

Anaconda没有提供Pytesseract的包,需要单独下载。
Pytesseract地址:https://pypi.org/project/pytesseract/#files
我采用的是whl包安装,在Anaconda Prompt里运行安装,

cd whl文件的路径(C:\...)
pip install 文件名.whl

最新版anaconda自带pillow包,没有单独安装。

环境配置

需要配置两个环境变量

Path:
Path变量增加tesseract的安装路径
TESSDATA_PREFIX:
添加TESSDATA_PREFIX变量

修改pytesseract.py文件中的tesseract_cmd的值

非常重要,不修改会报错!!!

在pytesseract.py文件中查找变量 tesseract_cmd,修改赋值。

pytesseract.py的位置在anaconda安装位置下的lib文件夹中:
路径:…\Lib\site-packages\pytesseract\pytesseract.py

赋值内容为tesseract安装位置下的tesseract.exe可执行文件:

tesseract_cmd = r'C:\software\OCR\TesseractOCR\tesseract.exe'

文字提取示例

import pytesseract
from PIL import Image
img=Image.open('路径'+'\\'+'文件名.jpg')
text=pytesseract.image_to_string(img)
print(text)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值