欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識(shí)別的方法

 更新時(shí)間:2020年02月26日 11:38:52   作者:天涯泛孤舟  
這篇文章主要介紹了python3使用Pillow、tesseract-ocr與pytesseract模塊的圖片識(shí)別的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.安裝Pillow

pip install Pillow

2.安裝tesseract-ocr

github地址: https://github.com/tesseract-ocr/tesseract

或本地下載地址:http://www.dbjr.com.cn/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] 系統(tǒng)找不到指定的文件

解決辦法:

方法1[推薦]: 將tesseract.exe添加到環(huán)境變量PATH中,

例如: D:\Tesseract-OCR,默認(rèn)路徑為C:\Program Files (x86)\Tesseract-OCR

注意: 為了使環(huán)境變量生效,需要關(guān)閉cmd窗口或是關(guān)閉pycharm等ide重新啟動(dòng)

方法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:  在實(shí)際運(yùn)行代碼中指定

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目錄的上級(jí)目錄所在路徑(默認(rèn)為tesseract-ocr安裝目錄)添加至TESSDATA_PREFIX環(huán)境變量中

例如: 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') #定位驗(yàn)證碼 
location = imgelement.location #獲取驗(yàn)證碼x,y軸坐標(biāo) 
size=imgelement.size #獲取驗(yàn)證碼的長寬 
rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #寫成我們需要截取的位置坐標(biāo) 
name="code.jpg"  
driver.find_element_by_id("codeImg").click() 
driver.save_screenshot(name) #截取當(dāng)前網(wǎng)頁,該網(wǎng)頁有我們需要的驗(yàn)證碼 
aa=Image.open(name) #打開截圖 
frame4=aa.crop(rangle) #使用Image的crop函數(shù),從截圖中再次截取我們需要的區(qū)域 
frame4.save(name) 
im = Image.open(name)
#轉(zhuǎn)化到灰度圖
imgry = im.convert('L')
#保存圖像
imgry.save('g'+name)
#二值化,采用閾值分割法,threshold為分割點(diǎn)
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)
#識(shí)別
text = pytesseract.image_to_string(out)
#識(shí)別對(duì)嗎
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模塊的圖片識(shí)別的方法的詳細(xì)內(nèi)容,更多關(guān)于python3 圖片識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python使用Pandas讀寫Excel實(shí)例解析

    Python使用Pandas讀寫Excel實(shí)例解析

    這篇文章主要介紹了Python使用Pandas讀寫Excel實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Ubuntu18.04安裝 PyCharm并使用 Anaconda 管理的Python環(huán)境

    Ubuntu18.04安裝 PyCharm并使用 Anaconda 管理的Python環(huán)境

    這篇文章主要介紹了Ubuntu18.04安裝 PyCharm并使用 Anaconda 管理的Python環(huán)境的教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python實(shí)現(xiàn)靜態(tài)服務(wù)器

    python實(shí)現(xiàn)靜態(tài)服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)靜態(tài)服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 改變 Python 中線程執(zhí)行順序的方法

    改變 Python 中線程執(zhí)行順序的方法

    這篇文章主要介紹了改變 Python 中線程的執(zhí)行順序的方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • 使用Python實(shí)現(xiàn)tail的示例代碼

    使用Python實(shí)現(xiàn)tail的示例代碼

    tail是一個(gè)常用的Linux命令, 它可以打印文件的后面n行數(shù)據(jù), 也能實(shí)時(shí)輸出文件的追加數(shù)據(jù)。本文就來用Python實(shí)現(xiàn)tail,感興趣的可以了解一下
    2023-03-03
  • django 使用 PIL 壓縮圖片的例子

    django 使用 PIL 壓縮圖片的例子

    今天小編就為大家分享一篇django 使用 PIL 壓縮圖片的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 淺析Python中的heapq優(yōu)先隊(duì)列

    淺析Python中的heapq優(yōu)先隊(duì)列

    在Python中,heapq模塊提供了實(shí)現(xiàn)最小堆算法的數(shù)據(jù)結(jié)構(gòu),能夠用作優(yōu)先隊(duì)列,本文將詳細(xì)介紹heapq模塊,包括堆的基本概念、heapq的功能和示例代碼,需要的可以參考下
    2023-12-12
  • python 使用cycle構(gòu)造無限循環(huán)迭代器

    python 使用cycle構(gòu)造無限循環(huán)迭代器

    這篇文章主要介紹了python 使用cycle構(gòu)造無限循環(huán)迭代器的方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-12-12
  • 使用python實(shí)現(xiàn)鏈表操作

    使用python實(shí)現(xiàn)鏈表操作

    鏈表是計(jì)算機(jī)科學(xué)里面應(yīng)用最廣泛的數(shù)據(jù)結(jié)構(gòu)之一。這篇文章主要介紹了使用python實(shí)現(xiàn)鏈表操作,需要的朋友可以參考下
    2018-01-01
  • python3 numpy中數(shù)組相乘np.dot(a,b)運(yùn)算的規(guī)則說明

    python3 numpy中數(shù)組相乘np.dot(a,b)運(yùn)算的規(guī)則說明

    這篇文章主要介紹了python3 numpy中數(shù)組相乘np.dot(a,b)運(yùn)算的規(guī)則說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評(píng)論