提高 Pytesseract 阅读文本的可靠性
Improve Pytesseract reliability of reading text
我正在尝试从屏幕截图中读取相对清晰的数字,但我 运行 遇到了让 pytesseract 正确读取文本的问题。我有以下屏幕截图:
而且我知道比分 (2-0) 和时钟 (1:42) 将在完全相同的位置。
这是我目前用于读取时钟时间和橙色分数的代码:
lower_orange = np.array([0, 90, 200], dtype = "uint8")
upper_orange = np.array([70, 160, 255], dtype = "uint8")
#Isolate scoreboard location on a 1080p pic
clock = input[70:120, 920:1000]
scoreboard = input[70:150, 800:1120]
#greyscale
roi_gray = cv2.cvtColor(clock, cv2.COLOR_BGR2GRAY)
config = ("-l eng -c tessedit_char_whitelist=0123456789: --oem 1 --psm 8")
time = pytesseract.image_to_string(roi_gray, config=config)
print("time is " + time)
# find the colors within the specified boundaries and apply
# the mask
mask_orange = cv2.inRange(scoreboard, lower_orange, upper_orange)
# find contours in the thresholded image, then initialize the
# list of digit locations
cnts = cv2.findContours(mask_orange.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
locs = []
for (i, c) in enumerate(cnts):
# compute the bounding box of the contour, then use the
# bounding box coordinates to derive the aspect ratio
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)
# since score will be a fixed size of about 25 x 35, we'll set the area at about 300 to be safe
if w*h > 300:
orange_score_img = mask_orange[y-5:y+h+5, x-5:x+w+5]
orange_score_img = cv2.GaussianBlur(orange_score_img, (5, 5), 0)
config = ("-l eng -c tessedit_char_whitelist=012345 --oem 1 --psm 10")
orange_score = pytesseract.image_to_string(orange_score_img, config=config)
print("orange_score is " + orange_score)
这是输出:
time is 1:42
orange_score is
这是 orange_score_img,在我屏蔽掉上下橙色边界内的所有内容并应用高斯模糊之后。
然而在这一点上,即使我将 pytesseract 配置为搜索 1 个字符并限制白名单,我仍然无法正确读取它。我是否缺少一些额外的后处理来帮助 pytesseract 将此数字读取为 2?
根据@fmw42 的建议,我尝试了一些形态变化。增加数字似乎可以解决问题![=12=]
kernel = np.ones((5,5),np.uint8)
orange_score_img = cv2.dilate(orange_score_img,kernel,iterations=1)
编辑:我意识到,真正的答案是 pytesseract 在白色背景上使用黑色文本比在黑色背景上使用白色文本要好得多!当我反转颜色时,它看起来很完美:
orange_score_img = cv2.bitwise_not(orange_score_img)
我希望这对刚开始使用 pytesseract 的人有所帮助!试图调整图像以适应我的所有情况非常令人沮丧,并且知道白底黑字效果更好会节省我的时间...
我正在尝试从屏幕截图中读取相对清晰的数字,但我 运行 遇到了让 pytesseract 正确读取文本的问题。我有以下屏幕截图:
而且我知道比分 (2-0) 和时钟 (1:42) 将在完全相同的位置。
这是我目前用于读取时钟时间和橙色分数的代码:
lower_orange = np.array([0, 90, 200], dtype = "uint8")
upper_orange = np.array([70, 160, 255], dtype = "uint8")
#Isolate scoreboard location on a 1080p pic
clock = input[70:120, 920:1000]
scoreboard = input[70:150, 800:1120]
#greyscale
roi_gray = cv2.cvtColor(clock, cv2.COLOR_BGR2GRAY)
config = ("-l eng -c tessedit_char_whitelist=0123456789: --oem 1 --psm 8")
time = pytesseract.image_to_string(roi_gray, config=config)
print("time is " + time)
# find the colors within the specified boundaries and apply
# the mask
mask_orange = cv2.inRange(scoreboard, lower_orange, upper_orange)
# find contours in the thresholded image, then initialize the
# list of digit locations
cnts = cv2.findContours(mask_orange.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
locs = []
for (i, c) in enumerate(cnts):
# compute the bounding box of the contour, then use the
# bounding box coordinates to derive the aspect ratio
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)
# since score will be a fixed size of about 25 x 35, we'll set the area at about 300 to be safe
if w*h > 300:
orange_score_img = mask_orange[y-5:y+h+5, x-5:x+w+5]
orange_score_img = cv2.GaussianBlur(orange_score_img, (5, 5), 0)
config = ("-l eng -c tessedit_char_whitelist=012345 --oem 1 --psm 10")
orange_score = pytesseract.image_to_string(orange_score_img, config=config)
print("orange_score is " + orange_score)
这是输出:
time is 1:42
orange_score is
这是 orange_score_img,在我屏蔽掉上下橙色边界内的所有内容并应用高斯模糊之后。
然而在这一点上,即使我将 pytesseract 配置为搜索 1 个字符并限制白名单,我仍然无法正确读取它。我是否缺少一些额外的后处理来帮助 pytesseract 将此数字读取为 2?
根据@fmw42 的建议,我尝试了一些形态变化。增加数字似乎可以解决问题![=12=]
kernel = np.ones((5,5),np.uint8)
orange_score_img = cv2.dilate(orange_score_img,kernel,iterations=1)
编辑:我意识到,真正的答案是 pytesseract 在白色背景上使用黑色文本比在黑色背景上使用白色文本要好得多!当我反转颜色时,它看起来很完美:
orange_score_img = cv2.bitwise_not(orange_score_img)
我希望这对刚开始使用 pytesseract 的人有所帮助!试图调整图像以适应我的所有情况非常令人沮丧,并且知道白底黑字效果更好会节省我的时间...