pytesseract 识别率低提升方法

pytesseract 识别率低提升方法

一.跟换识别语言包

下载地址https://github.com/tesseract-ocr/tessdata

二.修改图片的灰度

from PIL import Image
from PIL import ImageEnhance
import pytesseract
img = Image.open('sanyecao.jpg')
img = img.convert('RGB')  #这里也可以尝试使用L
enhancer = ImageEnhance.Color(img)
enhancer = enhancer.enhance(0)
enhancer = ImageEnhance.Brightness(enhancer)
enhancer = enhancer.enhance(2)
enhancer = ImageEnhance.Contrast(enhancer)
enhancer = enhancer.enhance(8)
enhancer = ImageEnhance.Sharpness(enhancer)
img = enhancer.enhance(20)
text=pytesseract.image_to_string(img)

三.结合cv2,np对于图片处理后在进行读取

这个情况有很多种,也不说了,可以自己去尝试,简单写个调整图片亮度

#调整亮度
filename = "sanyecao.jpg"
img = cv2.imread(filename, 0)
print(np.shape(img))
kernel = np.ones((1,1), np.uint8)
dilate = cv2.dilate(img, kernel, iterations=1)
cv2.imwrite('new_dilate.jpg', dilate)

#还有些常用的方法
cv2.Canny
cv2.erode
cv2.rectangle


original_img = cv2.imread("qingwen.png", 0)

# canny(): 边缘检测
img1 = cv2.GaussianBlur(original_img,(3,3),0)
canny = cv2.Canny(img1, 50, 150)

# 形态学:边缘检测
_,Thr_img = cv2.threshold(original_img,210,255,cv2.THRESH_BINARY)#设定红色通道阈值210(阈值影响梯度运算效果)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))         #定义矩形结构元素
gradient = cv2.morphologyEx(Thr_img, cv2.MORPH_GRADIENT, kernel) #梯度

cv2.imshow("original_img", original_img) 
cv2.imshow("gradient", gradient) 
cv2.imshow('Canny', canny)

你可能感兴趣的:(pytesseract 识别率低提升方法)