Python實(shí)現(xiàn)OCR識(shí)別之pytesseract案例詳解
Python實(shí)現(xiàn)OCR識(shí)別:pytesseract
Python常用pytesseract進(jìn)行圖片上的文字識(shí)別,即OCR識(shí)別,完整的代碼比較簡(jiǎn)單,只要下面一行即可,但是實(shí)際使用時(shí)環(huán)境配置上容易出錯(cuò)。
from PIL import Image
import pytesseract
text = pytesseract.image_to_string(Image.open('/Users/alice/Documents/Develop/PythonCode/textinphoto.PNG'))
print(text)
因此使用前,需要先安裝pillow和pytesseract依賴(lài)包。
然而運(yùn)行時(shí)仍然報(bào)錯(cuò),raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path
原因是因?yàn)槲窗惭btesseract,然后使用pip3 install tesseract之后仍然提示錯(cuò)誤,如圖:
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
無(wú)法使用,往上找了很多教程,說(shuō)是要使用brew安裝,于是得以解決,步驟為:
- 先安裝brew
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
- 安裝成功,通過(guò)命令行tesseract -v的方式查看是否成功,出現(xiàn)版本號(hào)則為安裝成功
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
接下來(lái)就可以直接使用了,使用如下代碼:
alicedembp:~ alice$ tesseract /Users/alice/Documents/Develop/PythonCode/textinphoto.png /Users/alice/Documents/Develop/PythonCode/output.txt
打開(kāi)textinphoto.PNG的圖片,將文字輸出到output.txt,圖片如下

運(yùn)行成功,產(chǎn)生output.txt文檔,里面的文本為圖片中識(shí)別出的文字。

到此這篇關(guān)于Python實(shí)現(xiàn)OCR識(shí)別之pytesseract案例詳解的文章就介紹到這了,更多相關(guān)python OCR識(shí)別之pytesseract內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中的斷言(assert語(yǔ)句)
- Python+Selenium+Pytesseract實(shí)現(xiàn)圖片驗(yàn)證碼識(shí)別
- python assert斷言的實(shí)例用法
- Python中優(yōu)雅使用assert斷言的方法實(shí)例
- python pytesseract庫(kù)的實(shí)例用法
- 解決Python3.8運(yùn)行tornado項(xiàng)目報(bào)NotImplementedError錯(cuò)誤
- Python Tornado核心及相關(guān)原理詳解
- Python Tornado之跨域請(qǐng)求與Options請(qǐng)求方式
- Python Tornado 實(shí)現(xiàn)SSE服務(wù)端主動(dòng)推送方案
相關(guān)文章
Python退出時(shí)強(qiáng)制運(yùn)行一段代碼的實(shí)現(xiàn)方法
這篇文章主要介紹了Python退出時(shí)強(qiáng)制運(yùn)行一段代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
opencv調(diào)用yolov3模型深度學(xué)習(xí)目標(biāo)檢測(cè)實(shí)例詳解
這篇文章主要為大家介紹了opencv調(diào)用yolov3模型深度學(xué)習(xí)目標(biāo)檢測(cè)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Python程序中引用環(huán)境變量的方法實(shí)現(xiàn)
本文主要介紹了Python程序中引用環(huán)境變量的方法實(shí)現(xiàn),通過(guò)配置環(huán)境變量并在代碼中引用,可以避免將敏感信息直接寫(xiě)入代碼中,感興趣的可以了解一下2024-12-12
python實(shí)現(xiàn)126郵箱發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)126郵箱發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
利用Python代碼實(shí)現(xiàn)數(shù)據(jù)可視化的5種方法詳解
在數(shù)據(jù)科學(xué)中,有多種工具可以進(jìn)行可視化。下面這篇文章主要給大家介紹了關(guān)于利用Python代碼實(shí)現(xiàn)數(shù)據(jù)可視化的5種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2018-03-03
Python 函數(shù)繪圖及函數(shù)圖像微分與積分
今天小編就為大家分享一篇Python 函數(shù)繪圖及函數(shù)圖像微分與積分,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
linux mint中搜狗輸入法導(dǎo)致pycharm卡死的問(wèn)題
這篇文章主要介紹了linux mint中搜狗輸入法導(dǎo)致pycharm卡死的問(wèn)題,這篇文章給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

