提高Pytesseract识别率的步骤:
1)清理图像数组,只留下文本(字体生成,而不是手写)。字母的边缘应不变形。应用阈值(尝试不同的值)。同时应用一些平滑过滤器。我也推荐使用形态上的开/关-但这只是一个奖励。这是以数组形式进入pytesseract识别的夸张示例:https://i.ytimg.com/vi/1ns8tGgdpLY/maxresdefault.jpg
2)使用要识别的文本将图像大小调整为更高的分辨率
3)Pytesseract通常应该可以识别任何类型的字母,但是通过安装文本所用的字体,您可以极大地提高准确性。
如何在pytesseract中安装新字体:
1)以TIFF格式获取所需字体
3)将训练数据文件(.trained data)添加到此文件夹C:\程序文件(x86)\ Tesseract OCR\tessdata
4)将此字符串命令添加到pytesseract重新复制函数:假设您有两种经过训练的字体:font1.traineddata和font2.traineddata
要同时使用这两个命令
txt=pytesseract.image_to_字符串(img,lang='font1+font2')
这里有一个代码来测试您在网络图像上的识别:import cv2
import pytesseract
import cv2
import numpy as np
import urllib
import requests
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
TESSDATA_PREFIX = 'C:/Program Files (x86)/Tesseract-OCR'
from PIL import Image
def url_to_image(url):
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
url='http://jeroen.github.io/images/testocr.png'
img = url_to_image(url)
#img = cv2.GaussianBlur(img,(5,5),0)
img = cv2.medianBlur(img,5)
retval, img = cv2.threshold(img,150,255, cv2.THRESH_BINARY)
txt = pytesseract.image_to_string(img, lang='eng')
print('recognition:', txt)
>>> txt
'This ts a lot of 12 point text to test the\nocr code and see if it works on all types\nof file format\n\nThe quick brown dog jumped over the\nlazy fox The quick brown dog jumped\nover the lazy fox The quick brown dog\njumped over the lazy fox The quick\nbrown dog jumped over the lazy fox'