1.安裝Pillow
pip install Pillow
2.安裝tesseract-ocr
github地址: https://github.com/tesseract-ocr/tesseract
或本地下載地址:https://www.jb51.net/softs/538925.html
windows:
The latest installer can be downloaded here: tesseract-ocr-setup-3.05.01.exe and tesseract-ocr-setup-4.00.00dev.exe (experimental).
ubuntu:
sudo apt-get install tesseract-ocr traineddata文件路徑: /usr/share/tesseract-ocr/tessdata/
3.安裝pytesseract
pip install pytesseract
如不能使用pip直接安裝可取搜索模塊文件直接安裝
遇到問題及解決:
1.FileNotFoundError: [WinError 2] 系統找不到指定的文件
解決辦法:
方法1[推薦]: 將tesseract.exe添加到環境變量PATH中,
例如: D:\Tesseract-OCR,默認路徑為C:\Program Files (x86)\Tesseract-OCR
注意: 為了使環境變量生效,需要關閉cmd窗口或是關閉pycharm等ide重新啟動
方法2: 修改pytesseract.py文件,指定tesseract.exe安裝路徑
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe‘
方法3: 在實際運行代碼中指定
pytesseract.pytesseract.tesseract_cmd = 'D:\\Tesseract-OCR\\tesseract.exe'
2.pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Tesseract-OCR\\tessdata/eng.traineddata')
解決方法:
方法1[推薦]:
將tessdata目錄的上級目錄所在路徑(默認為tesseract-ocr安裝目錄)添加至TESSDATA_PREFIX環境變量中
例如: C:\Program Files (x86)\Tesseract-OCR
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.
方法2: 在.py文件配置中指定tessdata-dir
tessdata_dir_config = '--tessdata-dir "D:\\Tesseract-OCR\\tessdata"' # tessdata_dir_config = '--tessdata-dir "'C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"' pytesseract.image_to_string(image, config=tessdata_dir_config)
trainedata下載地址: the latest from github.com
示例:
# -*-coding:utf-8-*- from PIL import Image import sys import os import pytesseract from selenium import webdriver sys.path.append('C:\Python27\Lib\site-packages\pytesser') import pytesser url='http://192.168.24.189/system/code?0.6824490785056669' driver = webdriver.Firefox() driver.maximize_window() #將瀏覽器最大化 driver.get(url) imgelement = driver.find_element_by_id('codeImg') #定位驗證碼 location = imgelement.location #獲取驗證碼x,y軸坐標 size=imgelement.size #獲取驗證碼的長寬 rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #寫成我們需要截取的位置坐標 name="code.jpg" driver.find_element_by_id("codeImg").click() driver.save_screenshot(name) #截取當前網頁,該網頁有我們需要的驗證碼 aa=Image.open(name) #打開截圖 frame4=aa.crop(rangle) #使用Image的crop函數,從截圖中再次截取我們需要的區域 frame4.save(name) im = Image.open(name) #轉化到灰度圖 imgry = im.convert('L') #保存圖像 imgry.save('g'+name) #二值化,采用閾值分割法,threshold為分割點 threshold = 140 table = [] for j in range(256): if j < threshold: table.append(0) else: table.append(1) out = imgry.point(table, '1') out.save('b'+name) #識別 text = pytesseract.image_to_string(out) #識別對嗎 text = text.strip() text = text.upper(); print (text) text = pytesseract.image_to_string(Image.open('code.png'), lang="eng") print(text)
以上就是python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識別的方法的詳細內容,更多關于python3 圖片識別的資料請關注億速云其它相關文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。