Python如何实现OCR识别

发布时间:2022-03-29 17:26:11 作者:iii
来源:亿速云 阅读:332

这篇文章主要讲解了“Python如何实现OCR识别”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python如何实现OCR识别”吧!

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安装,于是得以解决,步骤为:

alicedembp:~ alice$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
alicedembp:~ alice$ brew install leptonica
alicedembp:~ alice$ brew install tesseract
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识别

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

Python如何实现OCR识别

感谢各位的阅读,以上就是“Python如何实现OCR识别”的内容了,经过本文的学习后,相信大家对Python如何实现OCR识别这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Python用百度AI怎么实现OCR文字识别
  2. python3怎么安装OCR识别库tesserocr

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python ocr

上一篇:python方差检验方法怎么使用

下一篇:java缓存雪崩和缓存击穿怎么解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》