从这种类型的图像中读取文本是否可行?如果是这样,我将如何处理呢?

Is reading the text from this type of image doable? If so, how would I approach doing it?

我认为大多数OCR工具都是用来阅读文档的。 我正在尝试制作一个程序来读取游戏的 post-result 屏幕。 我想知道是否可以使用某种解决方法(我是 OCR 工具的新手)。

图像的 example

我尝试使用来自互联网的一个简单程序:

import cv2
import pytesseract

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

img = cv2.imread('test2.png')

text = pytesseract.image_to_string(img)
print(text)

然后我尝试了不同的阈值,并尝试了灰度,但它并没有改变 result 太多。

然后我考虑制作一个函数,首先将图像裁剪成 table,然后从列的单元格中读取值?我不知道这是否会使 OCR 工具更容易。

类似于 this。 然后我会将图像中的数据放入电子表格中(我认为我可以做到)

我的问题是,我将如何阅读非文档且难以阅读的图像。 (我当前的问题是阅读图片本身的文字)。

在调用 image_to_string 之前尝试:

 img=cv2.bitwise_not(img)

类似这个问题

您可以试试这个代码。它可以输出你想要的大部分内容。要真正满足您的需求,您必须过滤掉不必要的图标、图像等。

import cv2
import pytesseract
from pytesseract import Output
import pandas as pd

img = cv2.imread("sxFRauD.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
gauss = cv2.GaussianBlur(thresh, (3, 3), 0)

custom_config = r'-l eng --oem 3 --psm 6 -c preserve_interword_spaces=1 -c tessedit_char_whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-:. "'
d = pytesseract.image_to_data(gauss, config=custom_config, output_type=Output.DICT)
df = pd.DataFrame(d)

# clean up blanks
df1 = df[(df.conf != '-1') & (df.text != ' ') & (df.text != '')]
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

# sort blocks vertically
sorted_blocks = df1.groupby('block_num').first().sort_values('top').index.tolist()
for block in sorted_blocks:
    curr = df1[df1['block_num'] == block]
    sel = curr[curr.text.str.len() > 3]
    # sel = curr
    char_w = (sel.width / sel.text.str.len()).mean()
    prev_par, prev_line, prev_left = 0, 0, 0
    text = ''
    for ix, ln in curr.iterrows():
        # add new line when necessary
        if prev_par != ln['par_num']:
            text += '\n'
            prev_par = ln['par_num']
            prev_line = ln['line_num']
            prev_left = 0
        elif prev_line != ln['line_num']:
            text += '\n'
            prev_line = ln['line_num']
            prev_left = 0

        added = 0  # num of spaces that should be added
        if ln['left'] / char_w > prev_left + 1:
            added = int((ln['left']) / char_w) - prev_left
            text += ' ' * added
        text += ln['text'] + ' '
        prev_left += len(ln['text']) + added + 1
    text += '\n'
    print(text)

cv2.waitKey(0)
cv2.destroyAllWindows()
           POST-ACTION REPORT 
           CUSTOM GAME                                             0:04                                                            e 
           MATCH     SCOREBOARD 
                   DEFEAT                                                             Y        Oo      j8     kK     al 
                      MM      Zz  ezswims.ENMU                               a       2188       6      0      8      52 
                               a  Shiro.ENMU                                 8       2040       4      4      8      52 
                               SH RubbaDucky.ENMU                       t            1721       3      1      9      44 
                                  ICEDrgon29.ENMU                            a       1710       3      0      8      69 
                               3  Gongshow.ENMU                              a       1375       1      1      8      56 
                               2  Nemesis.ERAU                                       4930       9      3      2      41 
                               2   Phantom.ERAU                                      4895       9      3      4      50 
                      7        2  Reggie.ERAU                                        4630       6      o      2      80 
                                2  D4NG3RZON3.ERAU                                   4510       6      1      5      53