亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python3光學(xué)字符識(shí)別模塊tesserocr與pytesseract的使用詳解

 更新時(shí)間:2020年02月26日 09:06:37   作者:Py.qi  
這篇文章主要介紹了python3光學(xué)字符識(shí)別模塊tesserocr與pytesseract的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

OCR,即Optical Character Recognition,光學(xué)字符識(shí)別,是指通過掃描字符,然后通過其形狀將其翻譯成電子文本的過程,對(duì)應(yīng)圖形驗(yàn)證碼來說,它們都是一些不規(guī)則的字符,這些字符是由字符稍加扭曲變換得到的內(nèi)容,我們可以使用OCR技術(shù)來講其轉(zhuǎn)化為電子文本,然后將結(jié)果提取交給服務(wù)器,便可以達(dá)到自動(dòng)識(shí)別驗(yàn)證碼的過程

tesserocr與pytesseract是Python的一個(gè)OCR識(shí)別庫,但其實(shí)是對(duì)tesseract做的一層Python API封裝,pytesseract是Google的Tesseract-OCR引擎包裝器;所以它們的核心是tesseract,因此在安裝tesserocr之前,我們需要先安裝tesseract

1、安裝tesseract、tesserocr、pytesseract

(1)windows下的安裝

下載tesseract:https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-v4.0.0-beta.1.20180414.exe

或者本地下載:http://chabaoo.cn/softs/538925.html

然后雙擊程序安裝即可,可以勾選Additional language data(download)選項(xiàng)來安裝OCR識(shí)別支持的語言包,但下載語言包實(shí)在是慢,我們可以直接從https://github.com/tesseract-ocr/tessdata下載zip的語言包壓縮文件,解壓后將tessdata-master中的文件復(fù)制到Tesseract的安裝目錄C:\Program Files (x86)\Tesseract-OCR\tessdata目錄下,最后我們配置下環(huán)境變量,我們將C:\Program Files (x86)\Tesseract-OCR添加到環(huán)境變量中

在測(cè)試之前先了解下tesseract的命令程序格式:

tesseract imagename outputbase [-l lang]

imagename指定圖片名稱,outputbase指定輸出文件名,-l指定識(shí)別的語言

#顯示安裝的語言包
tesseract --list-langs

#顯示幫助
tesseract --help
tesseract --help-extra
tesseract --version

進(jìn)行測(cè)試:

#統(tǒng)計(jì)安裝的語言包,安裝了168個(gè)語言包
C:\Users\Administrator.DESKTOP-6JT7D2H>tesseract --list-langs | find /c /v ""
168

#使用一張圖片測(cè)試,成功識(shí)別字符串
tesseract image.png result -l eng |type result.txt
Python3WebSpider

由于tesserocr在windows環(huán)境下會(huì)出現(xiàn)各種不兼容問題,并且與pycharm虛擬環(huán)境不兼容等問題,所以在windows系統(tǒng)環(huán)境下,選擇pytesseract模塊進(jìn)行安裝,如果實(shí)在要安裝請(qǐng)使用whl文件安裝或者使用conda安裝

pip install pytesseract

如果在pytesseract運(yùn)行是找不到tesseract解釋器,這種情況一般是在虛擬環(huán)境下會(huì)發(fā)生,我們需要將tesseract-OCR的執(zhí)行文件tesseract.ext配置到windows系統(tǒng)中的PATH環(huán)境中,或者修改pytesseract.py文件,將其中的“tesseract_cmd”字段指定為tesseract.exe的完整路徑即可

測(cè)試識(shí)別功能:

import pytesseract
from PIL import Image

im=Image.open('image.png')
print(pytesseract.image_to_string(im))

(2)linux下的安裝
在Ubuntu、Debian、Deepin系統(tǒng)中,安裝命令如下:

#安裝tesseract
sudo apt-get install -y tesseract-ocr libtesseract-dev libleptonica-dev

#安裝語言包
git clone https://github.com/tesseract-ocr/tessdata.git
sudo mv tessdata/* /usr/share/tesseract-ocr/tessdata

#安裝tesserocr
pip3 install tesserocr

#安裝pytesseract
pip3 install pytesseract

在CentOS、Red Hat系統(tǒng)下,安裝命令如下:

#安裝tesseract
yum install -y tesseract

#安裝語言包
git clone https://github.com/tesseract-ocr/tessdata.git
mv tessdata/* /usr/share/tesseract/tessdata

#安裝tesserocr
pip3 install tesserocr

#安裝pytesseract
pip3 install pytesseract

測(cè)試安裝環(huán)境:

In [1]: import tesserocr
In [2]: from PIL import Image
In [3]: im=Image.open('image.png')
In [4]: tesserocr.image_to_text(im)
Out[4]: 'Python3WebSpider\n\n'

tesserocr安裝參考鏈接:https://github.com/sirfz/tesserocr

pytesseract安裝參考鏈接:https://github.com/madmaze/pytesseract

tesseract安裝參考鏈接:https://github.com/tesseract-ocr/tesseract/wiki

2、tesserocr與pytesseract模塊的使用

(1)tesserocr的使用

#從文件識(shí)別圖像字符
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ù)識(shí)別圖像字符
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  返回包含已識(shí)別字符及其框邊界的結(jié)果
  • image_to_data  返回包含框邊界,置信度和其他信息的結(jié)果。需要Tesseract 3.05+。有關(guān)更多信息,請(qǐng)查看Tesseract TSV文檔
  • image_to_osd  返回包含有關(guān)方向和腳本檢測(cè)的信息的結(jié)果。

參數(shù):

image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)

  • image object  圖像對(duì)象
  • lang String,Tesseract  語言代碼字符串
  • config String  任何其他配置為字符串,例如:config='--psm 6'
  • nice Integer  修改Tesseract運(yùn)行的處理器優(yōu)先級(jí)。Windows不支持。尼斯調(diào)整了類似unix的流程的優(yōu)點(diǎn)。
  • output_type  類屬性,指定輸出的類型,默認(rèn)為string。有關(guān)所有支持類型的完整列表,請(qǐng)檢查pytesseract.Output類的定義。
from PIL import Image
import pytesseract

#如果PATH中沒有tesseract可執(zhí)行文件,請(qǐng)指定tesseract路徑
pytesseract.pytesseract.tesseract_cmd='C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe'

#打印識(shí)別的圖像的字符串
print(pytesseract.image_to_string(Image.open('test.png')))

#指定語言識(shí)別圖像字符串,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')))

#獲取方向和腳本檢測(cè)
print(pytesseract.image_to_osd(Image.open('test.png'))

3、圖像識(shí)別簡(jiǎn)單應(yīng)用

 一般圖像處理驗(yàn)證,需要通過對(duì)圖像進(jìn)行灰度處理、二值化后增加圖像文字的辨識(shí)度,下面是一個(gè)簡(jiǎn)單的對(duì)圖像驗(yàn)證碼識(shí)別處理,如遇到復(fù)雜點(diǎn)的圖像驗(yàn)證碼如中間帶多條同等大小劃線的驗(yàn)證碼需要對(duì)文字進(jìn)行喬正切割等操作,但它的識(shí)別度也只有百分之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)識(shí)別驗(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)

#識(shí)別驗(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()

參考鏈接:

tesserocr GitHub:https://github.com/sirfz/tesserocr

tesserocr PyPI:https://pypi.python.org/pypi/tesserocr

pytesserocr GitHub:https://github.com/madmaze/pytesseract

pytesserocr PyPI:https://pypi.org/project/pytesseract/

tesseract下載地址:http://digi.bib.uni-mannheim.de/tesseract

tesseract GitHub:https://github.com/tesseract-ocr/tesseract

tesseract 語言包:https://github.com/tesseract-ocr/tessdata

tesseract文檔:https://github.com/tesseract-ocr/tesseract/wiki/Documentation

到此這篇關(guān)于python3光學(xué)字符識(shí)別模塊tesserocr與pytesseract的使用詳解的文章就介紹到這了,更多相關(guān)python3 tesserocr pytesseract內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 詳解python學(xué)習(xí)筆記之解釋器

    詳解python學(xué)習(xí)筆記之解釋器

    這篇文章主要為大家詳細(xì)介紹了python學(xué)習(xí)筆記之解釋器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Python中用PIL庫批量給圖片加上序號(hào)的教程

    Python中用PIL庫批量給圖片加上序號(hào)的教程

    這篇文章主要介紹了Python中用PIL庫批量給圖片加上序號(hào)的教程,PIL庫是Python中一個(gè)非常強(qiáng)大的處理圖片的庫,需要的朋友可以參考下
    2015-05-05
  • 關(guān)于Python錯(cuò)誤重試方法總結(jié)

    關(guān)于Python錯(cuò)誤重試方法總結(jié)

    在本篇文章里小編給網(wǎng)友們分享一篇關(guān)于關(guān)于Python錯(cuò)誤重試方法總結(jié)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。
    2021-01-01
  • python 隨機(jī)數(shù)生成的代碼的詳細(xì)分析

    python 隨機(jī)數(shù)生成的代碼的詳細(xì)分析

    如果你對(duì)python隨機(jī)數(shù)生成的實(shí)際應(yīng)用有不解之處,你就可以通過以下的內(nèi)容對(duì)其進(jìn)行了解,以下十九相關(guān)內(nèi)容的介紹
    2011-05-05
  • python入門之語句(if語句、while語句、for語句)

    python入門之語句(if語句、while語句、for語句)

    這篇文章主要介紹了python入門之語句,主要包括if語句、while語句、for語句的使用,需要的朋友可以參考下
    2015-01-01
  • Python3實(shí)現(xiàn)旋轉(zhuǎn)數(shù)組的3種算法小結(jié)

    Python3實(shí)現(xiàn)旋轉(zhuǎn)數(shù)組的3種算法小結(jié)

    旋轉(zhuǎn)數(shù)組是一種常見的數(shù)據(jù)結(jié)構(gòu)問題,通常是指一個(gè)有序數(shù)組經(jīng)過旋轉(zhuǎn)后,使得所有元素逆序排列,本文主要介紹了Python3實(shí)現(xiàn)旋轉(zhuǎn)數(shù)組的3種算法小結(jié),感興趣的可以了解一下
    2023-12-12
  • Pytest測(cè)試報(bào)告工具Allure用法介紹

    Pytest測(cè)試報(bào)告工具Allure用法介紹

    這篇文章介紹了Pytest測(cè)試報(bào)告工具Allure的用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • python使用openpyxl庫讀取Excel文件數(shù)據(jù)

    python使用openpyxl庫讀取Excel文件數(shù)據(jù)

    openpyxl是一個(gè)功能強(qiáng)大的庫,可以輕松地實(shí)現(xiàn)Excel文件的讀寫操作,本文將介紹如何使用openpyxl庫讀取Excel文件中的數(shù)據(jù),感興趣的小伙伴可以了解下
    2023-11-11
  • python的metaclass使用小結(jié)

    python的metaclass使用小結(jié)

    python中的metaclass可謂熟悉而又陌生,自己開發(fā)時(shí)很少用,閱讀源碼時(shí)卻經(jīng)常遇到,那么到底什么是metaclass呢?何時(shí)使用metaclass呢?這篇文章主要介紹了python的metaclass,需要的朋友可以參考下
    2024-01-01
  • 如何使用python socket模塊實(shí)現(xiàn)簡(jiǎn)單的文件下載

    如何使用python socket模塊實(shí)現(xiàn)簡(jiǎn)單的文件下載

    這篇文章主要介紹了如何使用python socket模塊實(shí)現(xiàn)簡(jiǎn)單的文件下載,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09

最新評(píng)論