如何使用python从该图像中准确提取一行数据

2024-04-27 12:07:18 发布

您现在位置:Python中文网/ 问答频道 /正文

a snippet of a war thunder stats screen

我只想说一句话。所以我希望当我运行pytesseract时,我能得到有用的数据

相反,我得到的字符串是'Ce eet il ae oe a on) os\n\nooo eo oo oo oom om om om)\n\n[OO COCO ORR OW OR PRP ODWWG\n\neyo fe) Fee ote) = = - = = eo me-e-)\n\n(Ss: oo ~7~oO 0 0\n\neB\n\n© te O fa ©\n\nOORFONONWR OW DFW NN\n\nVaso\nVES -5)\n1866\nnny\n1625\n1368\nLt\n1070\n898\n838\nwhey)\nom\na\nRie)\n15\n\nny,\n\n=ARAM= gksvlrwOlf\nDarth_Zipzap\naE a\njohnny478423\n=CNAPG _920831993\nOLOLUCTIIN AG\nRivDecartes\nfleadog406\nFormula13\n\nxL LongDubber\nDankdudledan\n_Trix_1740\n\nLUT engl)\n\n=MOPB= JP_Akatonbo\nPlutoh71689\nMakinHerSquirt\n\x0c'

我试着用灰色缩放它,但没有用。我想如果这里有离散的列,我就可以在空格和换行符上拆分字符串,但是……不行

任何指向正确方向的指示都将不胜感激

在以前的实验中,我遇到了一些问题,因为像小控制器图标这样的图像,我只能在将图像传递给tesseract之前检测并屏蔽这些图像。但在这幅图中,tesseract无法非常一致地识别列中的数字


Tags: 数据字符串图像oneoiloeoo
1条回答
网友
1楼 · 发布于 2024-04-27 12:07:18
  • 我试着用灰色缩放它,但没有用

将图像转换为灰度将加快计算速度,因为您正在从3通道减少到1通道。如果你的意思是“我已经应用了预处理,但是没有用”,那么你应该看看下面的techniques。转换为灰度不是一种预处理,而是一种计算优势

  • 我想如果这里有离散的列,我就可以在空格和换行符上拆分字符串,但是……不行

你试过不同的page-segmentation-modes吗?有时识别输入文本的默认值不准确。因此,您应该尝试其他模式

  • 任何指向正确方向的指示都将不胜感激

输入图像的第一个事实是,您不需要第二部分。如果当前图像大小为HW,则需要H/2W

第二个事实是我们需要binarize图像。结果将是:

enter image description here

如果您读取结果图像,假设一个统一的文本块:

1 0 3 0 0 7 Whey. =ARAM= qksvlrwolf
3 0 3 0 0 7 2389 Darth_Zipzap
4° 0 6 0 0 3 1866 KILLAIRE
4 1 1 0 8 1 ARs johnny478423
3 0 1 0 0 6 1625 =CNAPC= _920831993
3 0 1 0 0 3 1368 ole] NCAT LG
2 0 0 0 13 0 1291 RN Bstecl ates)
4° 0 3 0 0 1 1070 fleadog406
1 0 0 0 0 3 eds imelaial etch}
2 01 0 1 2 CRS xL LongDubber
1 0 1 0 11 0 rh Dankdudledan
0 0 0 0 0 2 611 _Trix_1740
2 1 0 0 0 #0 Zs Illinois_Fats
10000 1 309 =MOPB= JP_Akatonbo
2 0 0 0 0 90 15 Plutoh71689
1 00 0 0 0 vy MakinHerSquirt

与之前的尝试相比,您将获得更准确的结果。然而,并不是每个单词都能被准确识别。您可以执行以下操作:

    1. 您可以一行一行地获取和识别
    1. 您可以将border添加到图像中。将图像居中可以提高精度

代码:

# Load the libraries
import cv2
import pytesseract

# Load the image in BGR format
img = cv2.imread("NwEsC.png")

# Get first-half of the image
(h, w) = img.shape[:2]
img = img[0:int(h/2), 0:w]

# Convert to the gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Threshold
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# OCR
txt = pytesseract.image_to_string(thr, config=" psm 6")
print(txt)

相关问题 更多 >