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的最佳选择。

相关推荐
会员源码网18 小时前
Python中生成器函数与普通函数的区别
python
Java水解18 小时前
Python开发从入门到精通:Web框架Django实战
后端·python
曲幽20 小时前
FastAPI + PostgreSQL 实战:给应用装上“缓存”和“日志”翅膀
redis·python·elasticsearch·postgresql·logging·fastapi·web·es·fastapi-cache
Lupino1 天前
别再只聊 AI 写代码了:技术负责人要把“变更治理”提到第一优先级
python·docker·容器
Flittly1 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(6)Context Compact (上下文压缩)
python·agent
曲幽1 天前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户8356290780512 天前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞2 天前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派2 天前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪2 天前
AI 数学辅导老师项目构想和初始化
前端·后端·python