Python如何实现OCR识别(ocr,python,移动开发)

时间:2024-04-27 16:49:34 作者 : 石家庄SEO 分类 : 移动开发
  • TAG :

Python实现OCR识别:pytesseract

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

fromPILimportImageimportpytesseracttext=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$pip3installtesseractRequirementalreadysatisfied:tesseractin/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages(0.1.3)alicedembp:~alice$tesseract-bash:tesseract:commandnotfound

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

  • 先安装brew

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

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

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

alicedembp:~alice$tesseractUsage:tesseract--help|--help-extra|--versiontesseract--list-langstesseractimagenameoutputbase[options...][configfile...]OCRoptions:-lLANG[+LANG]Specifylanguage(s)usedforOCR.NOTE:Theseoptionsmustoccurbeforeanyconfigfile.Singleoptions:--helpShowthishelpmessage.--help-extraShowextrahelpforadvancedusers.--versionShowversioninformation.--list-langsListavailablelanguagesfortesseractengine.alicedembp:~alice$tesseract-vtesseract4.0.0leptonica-1.78.0libgif5.1.4:libjpeg9c:libpng1.6.36:libtiff4.0.10:zlib1.2.11:libwebp1.0.2:libopenjp22.3.1FoundAVX2FoundAVXFoundSSE

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

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识别

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python如何实现OCR识别的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Lettuce开发框架怎么使用下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18