Python实现OCR识别之pytesseract案例详解

作者:Holidaylovesam 时间:2021-03-02 02:14:35 

Python实现OCR识别:pytesseract

Python常用pytesseract进行图片上的文字识别,即OCR识别,完整的代码比较简单,只要下面一行即可,但是实际使用时环境配置上容易出错。


from PIL import Image
import pytesseract

text = pytesseract.image_to_string(Image.open('/Users/alice/Documents/Develop/PythonCode/textinphoto.PNG'))
print(text)

因此使用前,需要先安装pillow和pytesseract依赖包。

然而运行时仍然报错,raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

原因是因为未安装tesseract,然后使用pip3 install tesseract之后仍然提示错误,如图:


alicedembp:~ alice$ pip3 install tesseract
Requirement already satisfied: tesseract in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (0.1.3)
alicedembp:~ alice$ tesseract
-bash: tesseract: command not found

无法使用,往上找了很多教程,说是要使用brew安装,于是得以解决,步骤为:

  • 先安 * rew


alicedembp:~ alice$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 再使用brew安装leptonica


alicedembp:~ alice$ brew install leptonica
  • 使用brew安装tesseract


alicedembp:~ alice$ brew install tesseract
  • 安装成功,通过命令行tesseract -v的方式查看是否成功,出现版本号则为安装成功


alicedembp:~ alice$ tesseract
Usage:
 tesseract --help | --help-extra | --version
 tesseract --list-langs
 tesseract imagename outputbase [options...] [configfile...]

OCR options:
 -l LANG[+LANG]        Specify language(s) used for OCR.
NOTE: These options must occur before any configfile.

Single options:
 --help                Show this help message.
 --help-extra          Show extra help for advanced users.
 --version             Show version information.
 --list-langs          List available languages for tesseract engine.

alicedembp:~ alice$ tesseract -v
tesseract 4.0.0
leptonica-1.78.0
 libgif 5.1.4 : libjpeg 9c : libpng 1.6.36 : libtiff 4.0.10 : zlib 1.2.11 : libwebp 1.0.2 : libopenjp2 2.3.1
Found AVX2
Found AVX
Found SSE

接下来就可以直接使用了,使用如下代码:


alicedembp:~ alice$ tesseract /Users/alice/Documents/Develop/PythonCode/textinphoto.png /Users/alice/Documents/Develop/PythonCode/output.txt

打开textinphoto.PNG的图片,将文字输出到output.txt,图片如下

Python实现OCR识别之pytesseract案例详解

运行成功,产生output.txt文档,里面的文本为图片中识别出的文字。

Python实现OCR识别之pytesseract案例详解

来源:https://blog.csdn.net/alice_tl/article/details/89299405

标签:python,OCR,pytesseract
0
投稿

猜你喜欢

  • python对站点数据做EOF且做插值绘制填色图

    2023-03-05 03:30:56
  • Python爬虫获取基金变动信息

    2022-08-15 21:57:15
  • 教你用python控制安卓手机

    2023-11-10 10:13:39
  • Python使用docx模块处理word文档流程详解

    2023-03-08 15:45:06
  • 可用于监控 mysql Master Slave 状态的python代码

    2023-01-05 07:39:30
  • Python模块WSGI使用详解

    2021-02-19 00:05:27
  • taobao cdn的缓存?

    2009-09-19 17:21:00
  • Appium+Python自动化测试之运行App程序示例

    2023-07-29 01:54:20
  • mysql: 安装后的目录结构

    2011-03-08 09:46:00
  • 一文搞懂Go Exec 僵尸与孤儿进程

    2023-10-21 07:14:07
  • window.top[_CACHE]实现多个jsp页面共享一个js对象

    2023-07-18 09:25:14
  • 基于tensorflow指定GPU运行及GPU资源分配的几种方式小结

    2021-10-03 10:22:27
  • asp,php 和 jsp 比较 之我见

    2007-08-10 13:35:00
  • 谈谈如何管理门户级网站的CSS/IMG/JS文件

    2009-09-03 11:48:00
  • Python判断变量名是否合法的方法示例

    2022-07-31 19:05:12
  • ADO.NET数据连接池剖析

    2023-06-27 17:23:29
  • python基础while循环及if判断的实例讲解

    2021-02-18 06:56:06
  • 用Python生成会跳舞的美女

    2023-04-04 11:52:09
  • Pandas索引排序 df.sort_index()的实现

    2023-02-13 23:49:48
  • PyTorch策略梯度算法详情

    2022-12-20 14:35:12
  • asp之家 网络编程 m.aspxhome.com