使用tessaract和opencv从图像中提取文本

问题描述 投票:0回答:1

截图.png:

enter image description here

modified_image.png:enter image description here

我正在尝试从图像中提取文本,但似乎我这样做了,尽管我认为我已经将图像处理为非常好的格式,但 tessaract 给了我一些随机值。我只关注白色文本,想忽略红色文本。

import cv2 as cv
import pytesseract
from PIL import Image

image = cv.imread("screenshot.png", cv.IMREAD_GRAYSCALE)

ret, modified_image = cv.threshold(image, 120, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)

modified_image= cv.resize(modified_image, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)

#cv.imshow("image", image)
#cv.imshow("modified_image", modified_image)
cv.imwrite("modified_image.png", modified_image)

pytesseract.pytesseract.tesseract_cmd = r'C:\Tesseract-OCR\tesseract.exe'

text = pytesseract.image_to_string(Image.open('modified_image.png'), config="--psm 6 --oem 3", lang="eng")

print(f'Text: {text}')

这将错误地打印“CWS-1Y”而不是“CW9-1Y”。据我了解,使用的字体是 Shentox,但从我在网上找到的信息来看,在其上训练 tessaract 似乎是一项艰巨的任务

python opencv ocr tesseract image-preprocessing
1个回答
0
投票

我发现 Tesseract 过去的性能相当不可靠。

尝试一种更新的开源模型可能会更好,例如 Huggingface:https://huggingface.co/docs/transformers/en/model_doc/trocr

他们也有笔记本向您展示如何使用它:https://github.com/NielsRogge/Transformers-Tutorials/tree/master/TrOCR

如果您愿意支付少量费用,Cloud Vision API 中的 Google OCR 也能很好地工作。

© www.soinside.com 2019 - 2024. All rights reserved.