我正在使用python制作一个OCR系统,该系统可以从ID卡读取并提供图像的准确结果,但是由于tesseract读取的错误字符过多,因此无法提供正确的答案。我该如何训练tesseract,使其能够完美地读取ID卡并为我们提供正确和准确的详细信息,此外,我如何才能进入.tiff文件并使tesseract用于我的项目。
改善Pytesseract识别的步骤:
1)清洁图像阵列,以便仅包含文本(生成字体,而不是手写字体)。字母的边缘应无扭曲。应用阈值(尝试不同的值)。同时应用一些平滑过滤器。我还建议使用Morfholofical开/关-但这仅是一个奖励。这是应该以数组形式输入pytesseract识别的夸张示例:https ://i.ytimg.com/vi/1ns8tGgdpLY/maxresdefault.jpg
2)使用您要识别的文字将图像调整为更高的分辨率
3)Pytesseract通常应该识别任何类型的字母,但是通过安装用于书写文本的字体,可以极大地提高准确性。
如何在pytesseract中安装新字体:
1)以TIFF格式获取所需字体
2)将其上传到http://trainyourtesseract.com/并将经过培训的数据接收到您的电子邮件中
3)将训练后的数据文件(* .traineddata)添加到此文件夹C:\ Program Files(x86)\ Tesseract-OCR \ tessdata
4)将此字符串命令添加到pytesseract重构函数中:
假设您有2种经过训练的字体:font1.traineddata和font2.traineddata
要同时使用这两个命令
txt = pytesseract.image_to_string(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'
Run Code Online (Sandbox Code Playgroud)