python - OpenCV tesseract 无法检测图像中的单个数字
问题描述
我正在将 tesseract 与 python 一起使用。它几乎可以识别我所有带有 2 个或更多数字或字符的图像
我不想用“只有数字”来训练 tesseract,因为我也在识别字符。
但是无法从 tessearact 识别附加的图像
解决方案
我认为问题是由粗边框引起的。删除后,数字被正确识别。
如果您有兴趣,这里是代码:
import cv2
import numpy as np
import pytesseract
def discard(image):
image = np.uint8(image)
_, im_label, stts, _ = cv2.connectedComponentsWithStats(image, connectivity=4)
msk1 = np.isin(im_label, np.where(stts[:, cv2.CC_STAT_WIDTH] > 100)[0])
msk2 = np.isin(im_label, np.where(stts[:, cv2.CC_STAT_HEIGHT] > 100)[0])
image[(msk1 | msk2)] = 0
return image
img = cv2.imread("check_img.jpg", 0)
# Binarization
thresh = 255 - img
ret, thresh = cv2.threshold(thresh, 5, 255, cv2.THRESH_BINARY)
# removing long connected-components
thresh = discard(thresh)
# remove noise
thresh = cv2.medianBlur(thresh, 3)
# invert again
thresh = 255 - thresh
# showing the image
cv2.imshow("img", thresh)
# Using Tesseract OCR
custom_config = r'--oem 3 --psm 6'
text = pytesseract.image_to_string(thresh, config=custom_config)
print(text)
cv2.waitKey(0)
推荐阅读
- node.js - 是否可以通过节点 js 中的服务帐户连接托管在谷歌云平台中的 mongo db?如果是,该怎么做?
- excel - 如何使用excel函数查找产品的最新类别变化
- vue.js - Vue - 在 Chrome 开发工具中显示错误行
- python - Python:ImportError:无法为root用户导入模块
- java - java.lang.NoClassDefFoundError: java/lang/ModuleLayer
- powershell - 混淆 PowerShell 脚本
- javascript - 如何将方法添加到由构造函数创建的数组对象
- node.js - Mongoose findOneAndUpdate 不会更新我的数据库
- java - Spring Security 标头过滤器和状态码响应
- mysql - Mysql存储过程创建与TRANSACTION问题