pytesseract 无法识别数字 1
pytesseract can't recognice number 1
我是 运行 一个脚本,它可以返回数字小键盘中杂乱无章的数字和数字位置。但是当谈到识别 1 时,它给了我 71 或 7。
这是我的剧本运行
numero.save(r'C:\imagenes\numeros\numero.png')
image = Image.open(r'C:\imagenes\numeros\numero.png')
inverted_image = PIL.ImageOps.invert(image)
inverted_image.save(r'C:\imagenes\numeros\numero.png')
image = cv2.imread(r'C:\imagenes\numeros\numero.png')
numero = int(pytesseract.image_to_string(image, lang='spa', config='--psm 6 digits'))
print("numero :", numero)
if numero == 7 or numero not in numeros:
numero_1_eng = int(pytesseract.image_to_string(image, lang='eng', config='--psm 6 digits'))
if numero_eng != 7:
numero = 1
else:
numero = numero_eng
print("numero:", numero)
vector = 930, 425, numero
vector_de_vectores.append(vector)
解决方案
1- 应用 adaptive-thresholding
2- 将 tesseract 配置设置为 --psm 7
(因为您正在尝试识别单个文本行。查看全部 )
adaptive-thresholding
的结果:
当您阅读时:
txt = pytesseract.image_to_string(thr, config="--psm 7")
print(txt)
结果:
1
代码:
import cv2
import pytesseract
img = cv2.imread("tUh0U.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 252, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 31, 61)
txt = pytesseract.image_to_string(thr, config="--psm 7")
print(txt)
我是 运行 一个脚本,它可以返回数字小键盘中杂乱无章的数字和数字位置。但是当谈到识别 1 时,它给了我 71 或 7。
这是我的剧本运行
numero.save(r'C:\imagenes\numeros\numero.png')
image = Image.open(r'C:\imagenes\numeros\numero.png')
inverted_image = PIL.ImageOps.invert(image)
inverted_image.save(r'C:\imagenes\numeros\numero.png')
image = cv2.imread(r'C:\imagenes\numeros\numero.png')
numero = int(pytesseract.image_to_string(image, lang='spa', config='--psm 6 digits'))
print("numero :", numero)
if numero == 7 or numero not in numeros:
numero_1_eng = int(pytesseract.image_to_string(image, lang='eng', config='--psm 6 digits'))
if numero_eng != 7:
numero = 1
else:
numero = numero_eng
print("numero:", numero)
vector = 930, 425, numero
vector_de_vectores.append(vector)
解决方案
1- 应用 adaptive-thresholding
2- 将 tesseract 配置设置为 --psm 7
(因为您正在尝试识别单个文本行。查看全部
adaptive-thresholding
的结果:
当您阅读时:
txt = pytesseract.image_to_string(thr, config="--psm 7")
print(txt)
结果:
1
代码:
import cv2
import pytesseract
img = cv2.imread("tUh0U.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 252, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 31, 61)
txt = pytesseract.image_to_string(thr, config="--psm 7")
print(txt)