tesserocr與pytesseract模塊的使用方法解析
1.tesserocr的使用
#從文件識別圖像字符
In [7]: tesserocr.file_to_text('image.png')
Out[7]: 'Python3WebSpider\n\n'
#查看tesseract已安裝的語言包
In [8]: tesserocr.get_languages()
Out[8]: ('/usr/share/tesseract/tessdata/', ['eng'])
#從圖片數(shù)據(jù)識別圖像字符
In [9]: tesserocr.image_to_text(im)
Out[9]: 'Python3WebSpider\n\n'
#查看版本信息
In [10]: tesserocr.tesseract_version()
Out[10]: 'tesseract 3.04.00\n leptonica-1.72\n libgif 4.1.6(?) : libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libwebp 0.3.0\n'
2.pytesseract使用
功能:
- get_tesseract_version 返回系統(tǒng)中安裝的Tesseract版本。
- image_to_string 將圖像上的Tesseract OCR運(yùn)行結(jié)果返回到字符串
- image_to_boxes 返回包含已識別字符及其框邊界的結(jié)果
- image_to_data 返回包含框邊界,置信度和其他信息的結(jié)果。需要Tesseract 3.05+。有關(guān)更多信息,請查看Tesseract TSV文檔
- image_to_osd 返回包含有關(guān)方向和腳本檢測的信息的結(jié)果。
參數(shù):
image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)
- image object 圖像對象
- lang String,Tesseract 語言代碼字符串
- config String 任何其他配置為字符串,例如:config='--psm 6'
- nice Integer 修改Tesseract運(yùn)行的處理器優(yōu)先級。Windows不支持。尼斯調(diào)整了類似unix的流程的優(yōu)點(diǎn)。
- output_type 類屬性,指定輸出的類型,默認(rèn)為string。有關(guān)所有支持類型的完整列表,請檢查pytesseract.Output類的定義。
from PIL import Image
import pytesseract
#如果PATH中沒有tesseract可執(zhí)行文件,請指定tesseract路徑
pytesseract.pytesseract.tesseract_cmd='C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe'
#打印識別的圖像的字符串
print(pytesseract.image_to_string(Image.open('test.png')))
#指定語言識別圖像字符串,eng為英語
print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='eng'))
#獲取圖像邊界框
print(pytesseract.image_to_boxes(Image.open('test.png')))
#獲取包含邊界框,置信度,行和頁碼的詳細(xì)數(shù)據(jù)
print(pytesseract.image_to_data(Image.open('test.png')))
#獲取方向和腳本檢測
print(pytesseract.image_to_osd(Image.open('test.png'))
圖像識別簡單應(yīng)用
一般圖像處理驗(yàn)證,需要通過對圖像進(jìn)行灰度處理、二值化后增加圖像文字的辨識度,下面是一個(gè)簡單的對圖像驗(yàn)證碼識別處理,如遇到復(fù)雜點(diǎn)的圖像驗(yàn)證碼如中間帶多條同等大小劃線的驗(yàn)證碼需要對文字進(jìn)行喬正切割等操作,但它的識別度也只有百分之30左右,所以得另外想別的辦法來繞過驗(yàn)證
from PIL import Image
import pytesseract
im = Image.open('66.png')
#二值化圖像傳入圖像和閾值
def erzhihua(image,threshold):
''':type image:Image.Image'''
image=image.convert('L')
table=[]
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return image.point(table,'1')
image=erzhihua(im,127)
image.show()
result=pytesseract.image_to_string(image,lang='eng')
print(result)
模擬自動(dòng)識別驗(yàn)證碼登陸:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/13 8:58
# @Author : Py.qi
# @File : login.py
# @Software: PyCharm
from selenium import webdriver
from selenium.common.exceptions import TimeoutException,WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.remote.webelement import WebElement
from io import BytesIO
from PIL import Image
import pytesseract
import time
user='zhang'
password='123'
url='http://10.0.0.200'
driver=webdriver.Chrome()
wait=WebDriverWait(driver,10)
#識別驗(yàn)證碼
def acker(content):
im_erzhihua=erzhihua(content,127)
result=pytesseract.image_to_string(im_erzhihua,lang='eng')
return result
#驗(yàn)證碼二值化
def erzhihua(image,threshold):
''':type image:Image.Image'''
image=image.convert('L')
table=[]
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return image.point(table,'1')
#自動(dòng)登陸
def login():
try:
driver.get(url)
#獲取用戶輸入框
input=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#loginname'))) #type:WebElement
input.clear()
#發(fā)送用戶名
input.send_keys(user)
#獲取密碼框
inpass=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#password'))) #type:WebElement
inpass.clear()
#發(fā)送密碼
inpass.send_keys(password)
#獲取驗(yàn)證輸入框
yanzheng=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#code'))) #type:WebElement
#獲取驗(yàn)證碼在畫布中的位置
codeimg=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#codeImg'))) #type:WebElement
image_location = codeimg.location
#截取頁面圖像并截取掩碼碼區(qū)域圖像
image=driver.get_screenshot_as_png()
im=Image.open(BytesIO(image))
imag_code=im.crop((image_location['x'],image_location['y'],488,473))
#輸入驗(yàn)證碼并登陸
yanzheng.clear()
yanzheng.send_keys(acker(imag_code))
time.sleep(2)
yanzheng.send_keys(Keys.ENTER)
except TimeoutException as e:
print('timeout:',e)
except WebDriverException as e:
print('webdriver error:',e)
if __name__ == '__main__':
login()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
最新解決'nvidia-smi' 不是內(nèi)部或外部命令也不是可運(yùn)行的程序
使用cmd查看電腦顯卡的信息,調(diào)用nvidia-smi查看顯卡使用情況報(bào)錯(cuò),提示'nvidia-smi' 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序,本文給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧2023-01-01
Win10系統(tǒng)下安裝編輯器之神(The?God?of?Editor)Vim并且構(gòu)建Python生態(tài)開發(fā)環(huán)境過程(2
這篇文章主要介紹了Win10系統(tǒng)下安裝編輯器之神(The?God?of?Editor)Vim并且構(gòu)建Python生態(tài)開發(fā)環(huán)境(2020年最新攻略),本次我們在Win10平臺(tái)構(gòu)建一套以Vim為核心的Python開發(fā)環(huán)境,需要的朋友可以參考下2023-01-01
使用Python實(shí)現(xiàn)屏幕截圖的兩種方法
Python作為一種高效的編程語言,可以通過一些庫來實(shí)現(xiàn)對屏幕的截圖操作,本文主要介紹了使用Python實(shí)現(xiàn)屏幕截圖的兩種方法,具有一定的 參考價(jià)值,感興趣的可以了解一下2023-12-12
flask + pymysql操作Mysql數(shù)據(jù)庫的實(shí)例
下面小編就為大家?guī)硪黄猣lask + pymysql操作Mysql數(shù)據(jù)庫的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
這可能是最好玩的python GUI入門實(shí)例(推薦)
這篇文章主要介紹了這可能是最好玩的python GUI入門實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Django?logging日志模塊實(shí)例詳解(日志記錄模板配置)
Django使用python自帶的logging作為日志打印工具,下面這篇文章主要給大家介紹了Django?logging日志模塊(日志記錄模板配置)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

