姓名:林世余
學(xué)號(hào):19021210863
轉(zhuǎn)載:https://blog.csdn.net/fyfugoyfa/article/details/108160915
一厌杜、環(huán)境配置
1喘落、需要 pillow 和 pytesseract 這兩個(gè)庫瘪板,pip install 安裝就好了。
pip install pillow -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install pytesseract -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
2妄迁、安裝好Tesseract-OCR.exe
3、pytesseract 庫的配置:搜索找到pytesseract.py,打開該.py文件嗓化,找到 tesseract_cmd,改變它的值為剛才安裝 tesseract.exe 的路徑谬哀。
二刺覆、驗(yàn)證碼識(shí)別
識(shí)別驗(yàn)證碼,需要先對(duì)圖像進(jìn)行預(yù)處理玻粪,去除會(huì)影響識(shí)別準(zhǔn)確度的線條或噪點(diǎn)隅津,提高識(shí)別準(zhǔn)確度。
實(shí)例1
import cv2 ascv
importpytesseract
from PIL importImage
def recognize_text(image):
??? # 邊緣保留濾波? 去噪
??? dst= cv.pyrMeanShiftFiltering(image, sp=10, sr=150)
??? # 灰度圖像
??? gray= cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
??? # 二值化
??? ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
??? # 形態(tài)學(xué)操作?? 腐蝕? 膨脹
??? erode= cv.erode(binary, None, iterations=2)
??? dilate= cv.dilate(erode, None, iterations=1)
??? cv.imshow('dilate', dilate)
??? # 邏輯運(yùn)算? 讓背景為白色? 字體為黑? 便于識(shí)別
??? cv.bitwise_not(dilate, dilate)
??? cv.imshow('binary-image', dilate)
??? # 識(shí)別
??? test_message= Image.fromarray(dilate)
??? text= pytesseract.image_to_string(test_message)
??? print(f'識(shí)別結(jié)果:{text}')
src = cv.imread(r'./test/044.png')
cv.imshow('input image', src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()
運(yùn)行效果如下:
識(shí)別結(jié)果:3n3D
Process finished with exit code 0
實(shí)例2
import cv2 ascv
importpytesseract
from PIL importImage
def recognize_text(image):
??? # 邊緣保留濾波? 去噪
??? blur=cv.pyrMeanShiftFiltering(image, sp=8, sr=60)
??? cv.imshow('dst', blur)
? ??# 灰度圖像
??? gray= cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
??? # 二值化
??? ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
??? print(f'二值化自適應(yīng)閾值:{ret}')
??? cv.imshow('binary', binary)
??? # 形態(tài)學(xué)操作? 獲取結(jié)構(gòu)元素? 開操作
??? kernel= cv.getStructuringElement(cv.MORPH_RECT, (3, 2))
??? bin1= cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
??? cv.imshow('bin1', bin1)
??? kernel= cv.getStructuringElement(cv.MORPH_OPEN, (2, 3))
??? bin2= cv.morphologyEx(bin1, cv.MORPH_OPEN, kernel)
??? cv.imshow('bin2', bin2)
??? # 邏輯運(yùn)算? 讓背景為白色? 字體為黑? 便于識(shí)別
??? cv.bitwise_not(bin2, bin2)
??? cv.imshow('binary-image', bin2)
??? # 識(shí)別
??? test_message= Image.fromarray(bin2)
??? text= pytesseract.image_to_string(test_message)
??? print(f'識(shí)別結(jié)果:{text}')
src = cv.imread(r'./test/045.png')
cv.imshow('input image', src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()
運(yùn)行效果如下:
二值化自適應(yīng)閾值:181.0
識(shí)別結(jié)果:8A62N1
Process finished with exit code 0
實(shí)例3
import cv2 ascv
importpytesseract
from PIL importImage
def recognize_text(image):
??? # 邊緣保留濾波? 去噪
??? blur= cv.pyrMeanShiftFiltering(image, sp=8, sr=60)
??? cv.imshow('dst', blur)
??? # 灰度圖像
??? gray= cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
??? # 二值化? 設(shè)置閾值? 自適應(yīng)閾值的話 黃色的4會(huì)提取不出來
??? ret, binary = cv.threshold(gray, 185, 255, cv.THRESH_BINARY_INV)
??? print(f'二值化設(shè)置的閾值:{ret}')
??? cv.imshow('binary', binary)
??? # 邏輯運(yùn)算? 讓背景為白色? 字體為黑? 便于識(shí)別
??? cv.bitwise_not(binary, binary)
??? cv.imshow('bg_image', binary)
??? # 識(shí)別
??? test_message= Image.fromarray(binary)
??? text= pytesseract.image_to_string(test_message)
??? print(f'識(shí)別結(jié)果:{text}')
src = cv.imread(r'./test/045.jpg')
cv.imshow('input image', src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()
運(yùn)行效果如下:
二值化設(shè)置的閾值:185.0
識(shí)別結(jié)果:7364
Process finished with exit code 0