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

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)


4664

被折叠的 条评论
为什么被折叠?



