Python3.7 tesseract-ocr 驗證碼識別配置以及常見錯誤


當前為Windows環境

1.首先安裝Tesseract-OCR,鏈接:https://pan.baidu.com/s/12zazgAYWsNnxn8AxPjGfaw 提取碼: esif 

  下載后默認安裝就好,在安裝過程中存在選項安裝的情況,此時為選擇語言,可略過

2.Tesseract-OCR安裝完畢后,設置環境變量

  設置tesseract.exe的環境變量

  新建TESSDATA_PREFIX環境變量,值為C:\Program Files (x86)\Tesseract-OCR\tessdata,如圖

3.驗證Tesseract-OCR

  3.1 進入cmd 輸入下面的命令查看版本,正常運行則安裝成功:

    tesseract --version
  

    

  3.2 識別圖片

    tesseract 圖片路徑 輸出文件

    

      識別成功!!

4.進入cmd安裝python依賴

  4.1 安裝依賴

    

pip install pytesseract
pip install pillow

  4.2 編寫代碼

    識別下面的驗證碼

    代碼如下:

import pytesseract
from PIL import Image

image = Image.open("F:/imooc2.png")
text = pytesseract.image_to_string(image)
print(text)

結果為6067,識別成功

5.此時特別容易出現錯誤

解決方案1:仔細查看環境變量是否正確

解決方案2:在代碼中添加相關變量參數

#coding=utf-8
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'C://Program Files (x86)//Tesseract-OCR//tesseract.exe'
tessdata_dir_config = '--tessdata-dir "C://Program Files (x86)//Tesseract-OCR//tessdata"'
image = Image.open("F:/imooc2.png")
text = pytesseract.image_to_string(image, lang ='eng', config=tessdata_dir_config)
print(text)

  

 

 

 解決方案出處:https://www.cnblogs.com/chenshengkai/p/11318387.html

 

 

 

 

 

 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM