繁体   English   中英

字符识别问题 pytesseract/tesseract-OCR/ 无法识别

[英]Problem With Char Recognition pytesseract/tesseract-OCR/ Won't Recognize

如标题中所述,我无法识别字符,正方体无法识别任何数字。 我如何解决这个问题?

图片:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

代码:

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\\Tesseract-OCR\tesseract.exe'

img = cv2.imread('img002.png')
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 100, 128, cv2.THRESH_BINARY_INV)[1]
cv2.imshow('Show', thr)
cv2.waitKey(0)
cmd = pytesseract.image_to_string(thr)
xx = pytesseract.image_to_boxes(thr)
print(cmd)
print(xx) 

我的 Output

ann

a 8 0 34 0 0
n 50 0 75 0 0
n 92 0 118 0 0

对于第一张和第三张图像,您可以找到包括OTSU 阈值的数字。

thresh = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
图片 结果
在此处输入图像描述 10
4
20
在此处输入图像描述 10
3
5

该技术对第二个图像没有用:

在此处输入图像描述

我们需要改变阈值方法

thresh = cv2.threshold(crop, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

如果我们逐行

当前数字 结果
在此处输入图像描述 6
在此处输入图像描述 4
在此处输入图像描述

不幸的是, 8未被识别。 您需要应用不同的组合来查找8

代码:

import cv2
import pytesseract

names = ["C2GJJ.png", "GKH7k.png", "neIWN.png"]

for name in names:
    image = cv2.imread(name)
    (height, width) = image.shape[:2]
    image = cv2.resize(image, (width*4, height*4))
    grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    text = pytesseract.image_to_string(thresh, config="--psm 6 digits")
    print(text)
    cv2.imshow("result", thresh)
    cv2.waitKey(0)

second_image = cv2.imread("GKH7k.png")
(height, width) = second_image.shape[:2]
second_image = cv2.resize(second_image, (width * 4, height * 4))
second_grey = cv2.cvtColor(second_image, cv2.COLOR_BGR2GRAY)
start = 0
end = int(height/2) + 20

for _ in range(0, 3):
    crop = second_grey[start:end, 0:width]
    (height_crop, width_crop) = crop.shape[:2]
    crop = cv2.resize(crop, (width_crop*2, height_crop*2))
    second_thresh = cv2.threshold(crop, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    second_text = pytesseract.image_to_string(second_thresh,
                                              config="--psm 6 digits")
    print(second_text)
    start = end
    end = start + int(height/2) + 40
    cv2.imshow("second_thresh", second_thresh)
    cv2.waitKey(0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM