You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tesseract OCR无法识别表单灰底框文本的解决方法

灰底输入框文本OCR识别失败问题说明

我正在学习AI/ML相关知识,尝试从示例表单中自动提取文本,当前编写的基于OpenCV与pytesseract的识别代码如下:

配图:示例表单

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\Users\Pranav\AppData\Local\Programs\Tesseract-OCR\tesseract.exe'

image = cv2.imread('image2.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)

x,y,w,h = 393, 531, 837, 80
firstROI = blur[y:y+h,x:x+w]
firstname = pytesseract.image_to_string(firstROI, lang='eng', config='--psm 6')
print(firstname)
firstname = re.sub(r'[^\w]', '', firstname)

cv2.imshow('image', firstROI)
cv2.waitKey()
cv2.destroyAllWindows()

运行上述代码后,可正常识别表单白色背景区域的普通印刷文本,但无法正确识别灰色背景输入框内的文本——例如名字输入框的实际内容为“Andrew”,最终仅能识别得到“oe”。
配图:ROI识别区域效果

我根据评论区的相关建议,参考公开的Tesseract OCR实践教程调整实现方案,改写为基于tesserocr的识别代码,但调整后仍无法得到正确识别结果:

from tesserocr import PyTessBaseAPI, PSM, OEM
api = PyTessBaseAPI(psm=PSM.AUTO_OSD, lang='eng', path=r'C:\Users\Pranav\tessdata-master')
images = ['andrew1.png', 'andrew2.png', 'test1.png']

for img in images:
    api.SetImageFile(img)
    print (api.GetUTF8Text())
    print (api.AllWordConfidences())

本次测试使用的三张示例图片如下:

  • 配图:andrew1示例图
  • 配图:andrew2示例图
  • 配图:test1示例图

当前代码仅能正确识别第三张图中的“Demographics”文本,无法识别两张灰底输入框图片中的“Andrew”等文本,恳请提供可正确识别这类灰底图像中文本的实现方法。

内容的提问来源于stack exchange,提问作者Arun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.26 02:45:00