Pytesseract OCR 错误文字识别
Pytesseract OCR wrong text recognition
当我使用Pytesseract识别这张图片中的文字时,Pytesseract returns 7A51k 但是这张图片中的文字是 7,451k.
如何使用代码解决此问题而不是提供更清晰的源图像?
我的代码
import pytesseract as pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'D:\App\Tesseract-OCR\tesseract'
img = Image.open("captured\amount.png")
string = pytesseract.image_to_string(image=img, config="--psm 10")
print(string)
如果调整大小后图像模糊,没有问题,您可以对其进行阈值处理,并按照AlexAlex建议的那样进行反转:
output: 7,451k
import numpy as np
import pytesseract
import cv2
# Read Image
gray = cv2.imread('2.png', 0)
# Resize
gray = cv2.resize(gray, (600,200))
# Inverting
gray = 255 - gray
emp = np.full_like(gray, 255)
emp -= gray
# Thresholding
emp[emp==0] = 255
emp[emp<100] = 0
text = pytesseract.image_to_string(emp, config='outputbase digits')
print(text)
我有一个两步解决方案
-
- 调整图片大小
-
- 应用阈值。
-
- 调整图像大小
- 输入的图像太小,无法识别数字、标点符号和字符。增加维度将启用准确的解决方案。
-
阈值处理将显示图像的特征。
当您应用阈值时,结果将是:
当您读取阈值图像时:
7,451k
代码:
import cv2
from pytesseract import image_to_string
img = cv2.imread("4ARXO.png")
(h, w) = img.shape[:2]
img = cv2.resize(img, (w*3, h*3))
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)
当我使用Pytesseract识别这张图片中的文字时,Pytesseract returns 7A51k 但是这张图片中的文字是 7,451k.
如何使用代码解决此问题而不是提供更清晰的源图像?
我的代码
import pytesseract as pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'D:\App\Tesseract-OCR\tesseract'
img = Image.open("captured\amount.png")
string = pytesseract.image_to_string(image=img, config="--psm 10")
print(string)
如果调整大小后图像模糊,没有问题,您可以对其进行阈值处理,并按照AlexAlex建议的那样进行反转:
output: 7,451k
import numpy as np
import pytesseract
import cv2
# Read Image
gray = cv2.imread('2.png', 0)
# Resize
gray = cv2.resize(gray, (600,200))
# Inverting
gray = 255 - gray
emp = np.full_like(gray, 255)
emp -= gray
# Thresholding
emp[emp==0] = 255
emp[emp<100] = 0
text = pytesseract.image_to_string(emp, config='outputbase digits')
print(text)
我有一个两步解决方案
-
- 调整图片大小
-
- 应用阈值。
-
- 调整图像大小
- 输入的图像太小,无法识别数字、标点符号和字符。增加维度将启用准确的解决方案。
-
阈值处理将显示图像的特征。
当您应用阈值时,结果将是:
当您读取阈值图像时:
7,451k
代码:
import cv2
from pytesseract import image_to_string
img = cv2.imread("4ARXO.png")
(h, w) = img.shape[:2]
img = cv2.resize(img, (w*3, h*3))
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)