圖形驗證碼的識別
阿新 • • 發佈:2019-04-01
sso div rda har poi The tps pan 掃描
OCR 技術:
(1) 在爬蟲過程中,難免會遇到各種各樣的驗證碼,而大多數驗證碼還是罔形驗證碼,這時候我們可以直接用 OCR 來識別
(2) OCR ,即 Optical Character Recognition ,光學字符識別, 是指通過掃描字符,然後通過其形狀將其翻譯成電子文本的過程
(3) tesserocr 是 Python 的一個OCR 識別庫,但其實是對 tesseract 做的一層 Python API 封裝,所以它的核心是 tesseract。因此,在安裝 tesserocr 之前,我們需要先安裝 tesseract
Windows 下安裝 tessorocr:
1. 先安裝 tessoract,下載地址:https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe 2. 再安裝 tessorocr,使用 pip3 安裝即可:pip3 install tesserocr pillow
Linux 下安裝 tessorocr:
yum install -y tesseract
git clone https://github.com/tesseract-ocr/tessdata.git
sudo mv tessdata/* /usr/share/tesseract/tessdata
pip3 install tesserocr pillow
Python 識別圖片驗證碼:
import tesserocr from PIL importImage image = Image.open(‘1.png‘) # Opens and identifies the given image file result = tesserocr.image_to_text(image) # Recognize OCR text from an image object print(result)
Python 識別有幹擾的圖片驗證碼:
import tesserocr from PIL import Image image = Image.open(‘2.png‘) image= image.convert(‘L‘) threshold = 127 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) image = image.point(table, ‘1‘) result = tesserocr.image_to_text(image) print(result)
圖形驗證碼的識別