python - 如何使用 Tesseract 识别图像上两个数字之间的斜线?
问题描述
我有一些图像,其中两个数字/
非常接近。Tesseract 根本无法识别该破折号,或者1
在大多数情况下都可以识别它(对于少数图像它有效)。
我的正方体代码:
pytesseract.image_to_string(img,lang='eng',config='--psm 7 --oem 3 -c tessedit_char_whitelist=/0123456789').strip()
我已经尝试过其他psm
和oem
配置。我一直在玩图像很多,例如cv2.threshold
调整cv2.cvtColor
大小。
编辑:
后
img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY_INV)[1]` <br>
img = cv2.resize(img,(0,0), fx=1.5, fy=1.5)`
大多数图像返回良好的值,但其中一些5
随机添加(转换后的图像):
很少有案例仍然无法识别斜线。
解决方案
灰度,甚至更大,不同的阈值在我的机器上完成了这项工作:
import cv2
import pytesseract
def extract_stats(img_filepath):
img = cv2.imread(img_filepath, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (0, 0), None, 4.0, 4.0)
img = cv2.threshold(img, 160, 255, cv2.THRESH_BINARY)[1]
config = '--psm 6 -c tessedit_char_whitelist="0123456789/"'
text = pytesseract.image_to_string(img, config=config)
print(text.replace('\n', '').replace('\f', ''))
for filepath in ['Bzh3j.png', 't9gAh.png', 'BBy2P.png']:
extract_stats(filepath)
# 4319/6149
# 943/7114
# 103/6149
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19042-SP0
Python: 3.9.6
PyCharm: 2021.2
OpenCV: 4.5.3
pytesseract: 5.0.0-alpha.20201127
----------------------------------------
推荐阅读
- junit - 使用 JUnitCore 运行时,TestWatcher 不会捕获失败的测试
- c - 找不到如何正确使用 ptrace()
- python - 无法在 Windows 上使用键盘 Ctrl+C 中断线程化 Python 控制台应用程序
- java - 通过 selenium 禁用 chrome 中的文本自动填充
- enums - 解构枚举时是否可以向上转换为特征?
- python - 如何使用 POST GET 传递 access_token
- r - 使用 dplyr 有条件地将一列中的值替换为另一列中的值
- json - 尝试使用文件加载器和 webpack 加载大型 json 文件
- css - 如何在 Bootstrap 4 中居中对齐文本
- javascript - 在Javascript-Jquery中的点击类中获取一个类