Windows安装Tesseract OCR与Python中使用pytesseract进行文字识别

文章目录

前言

Tesseract OCR是一个开源OCR(Optical Character Recognition)引擎,用于从图像中提取文本。Pytesseract是Tesseract OCR的Python封装,它使得在Python中使用Tesseract OCR引擎变得容易。Pytesseract提供了简单的API,帮助开发者轻松地使用Tesseract OCR引擎来实现图像中文本的识别。本文主要介绍了Windows下安装Tesseract OCR、并在Python中使用pytesseract进行本地文字识别的流程。

一、下载并安装Tesseract OCR

在Tesseract OCR下载地址https://digi.bib.uni-mannheim.de/tesseract/下载合适的版本安装包,如下:

点击安装包进行安装:

语言选择英文:

如果需要识别中文,则可以在安装过程中勾选下载中文语言包和脚本(也可以按需选择繁体):


自定义安装路径:

然后一直选择默认选项进行安装即可。

二、配置环境变量

为了方便使用Tesseract,需要将软件安装目录添加到系统环境变量中,这样不必每次执行命令时都切换到Tesseract的安装路径,如下:

设置确定后之后,可以进行验证,打开CMD,输入tesseract --version,示意如下:

bash 复制代码
C:\Users\LENOVO>tesseract --version
tesseract v5.3.0.20221214
 leptonica-1.78.0
  libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0
 Found AVX2
 Found AVX
 Found FMA
 Found SSE4.1
 Found libarchive 3.5.0 zlib/1.2.11 liblzma/5.2.3 bz2lib/1.0.6 liblz4/1.7.5 libzstd/1.4.5
 Found libcurl/7.77.0-DEV Schannel zlib/1.2.11 zstd/1.4.5 libidn2/2.0.4 nghttp2/1.31.0

如果输出版本等信息,说明安装成功。

三、Python中安装使用pytesseract

Python通过API接入Tesseract OCR,就可以在Python中方便进行文字识别。在使用前需要进行安装,如下:

bash 复制代码
# 使用conda进行安装
conda install pytesseract -y
# 使用pip安装
pip install pytesseract

安装成功即可使用,OCR示例如下:

python 复制代码
In [1]: import pytesseract

In [2]: import re

In [3]: import requests

In [4]: from PIL import Image

In [5]: url = 'http://42.194.197.95:8001/static/imgs/phone_imgs/phone0.png'

In [6]: image = Image.open(requests.get(url, stream=True).raw)

In [7]: image
Out[7]: <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=94x20>

In [8]: res = pytesseract.image_to_string(image)

In [9]: res
Out[9]: '14770126139\n'

In [10]: re.search('\d+', res).group()
Out[10]: '14770126139'

总结

Tesseract OCR是一个本地的图片识别开源引擎,不需要额外的深度学习OCR模型即可实现简单、快速的识别,同时可以通过接口来与多种编程语言对接而集成,可以作为轻量OCR的最佳选择。

相关推荐
阿尔的代码屋4 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者21 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者21 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng82 天前
Python+Django+H5+MySQL项目搭建
python·django