gpt4 book ai didi

从视频游戏截图中提取 Python 文本

转载 作者:行者123 更新时间:2023-12-04 15:06:52 27 4
gpt4 key购买 nike

我正在为视频游戏暗黑破坏神 2 使用 discord.py 构建一个不和谐机器人。其中一项功能要求机器人从暗黑破坏神 2 屏幕截图中提取项目的名称和属性。我目前正在为此使用 pytesseract,但我没有得到足够的结果。
示例截图:
enter image description here
我裁剪了项目的一部分(代码需要稍后自动执行此操作)并在预处理后得到它(参见下面的代码):
enter image description here
enter image description here
那是预处理和提取手动裁剪图像的代码:

def grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

def threshold(image):
return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

def dilate(image):
kernel = np.ones((5,5),np.uint8)
return cv2.dilate(image, kernel, iterations = 1)

image = cv2.imread('item.png')

scale = 10
w = int(image.shape[1] * scale)
h = int(image.shape[0] * scale)
dim = (w, h)
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)

image = grayscale(image)
image = threshold(image)
image = dilate(image)

custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%+'
print(pytesseract.image_to_string(image, config=custom_config))
它给了我这些结果:

sSPIRITWARD

WARD

ITamLavar66 Se a

Daryusa4if Oe

CHANCE Te BLece7a%

DURABILITY Blep5

REQUIREDSTRENGTHI76

RaguinaDLavat66

6%CHANCETSCASTLEVEL8FADEWHENSTRUCE

926%PFASTERBLeckRATE

29%IMCREASEDCHANCEOFBLeckiING2

Los t142%PNHANCEDDEFENSE a ALLRESISTANCES93i

Ay eYCeGlLOABSERE

ETHEREALCANNTBEREPAIRED


而且我不确定如何进行以获得更好的结果。 OCR 的字体和分辨率肯定是困难的(你可以在结果中看到 OCR 是如何在 5 和 6 上出现问题的)。以下是有关该问题的一些进一步说明:
  • 我有暗黑破坏神 2 字体(Exocet)所以我可以训练我自己的模型(?)
  • 我确实有所有可能的项目和属性的列表,以进一步将结果列入白名单(但是,我需要机器人功能的确切数字)
  • 我也尝试了另一个库(keras-ocr)但没有得到更好的结果
  • 最佳答案

    我有一个稍微改进的解决方案


  • 调整图像大小,使每个字符都可以清楚地看到


  • 每条线一一走




  • 预处理
    Tesseract 输出


    enter image description here
    oo : 精神病房 © (

    enter image description here
    _WARD '

    enter image description here
    等级:88

    enter image description here
    防御@:41°

    enter image description here
    CHANCE Te@BLeck:“73%

    enter image description here
    耐用性:51 @F 61

    enter image description here
    RE@UIRED 强度:176

    enter image description here
    需求等级:68 )

    enter image description here
    6% 几率 T® CAST LEVEL 8 被击中时褪色。

    enter image description here
    出块速度快 926%

    enter image description here
    增加 29% 的机会@F 阻塞

    enter image description here
    8h462% 增强)防御

    enter image description here
    所有电阻@3h

    enter image description here
    9 冷吸收

    enter image description here
    以太[无法@无法修复)


    比较


    当前结果
    OP的结果


    oo : 精神病房 © (
    sSPIRITWARD

    _WARD '
    病房

    等级:88
    ITamLavar66 Se a

    防御@:41°
    Daryusa4if Oe

    CHANCE Te@BLeck:“73%
    CHANCE Te BLece7a%

    耐用性:51 @F 61
    耐用性 Blep5

    RE@UIRED 强度:176
    所需强度76

    需求等级:68 )
    拉吉娜DLavat66

    6% 几率 T® CAST LEVEL 8 被击中时褪色。
    6% 机会城堡等级 8 褪色时

    出块速度快 926%
    926%PFASTERBLACKRATE

    增加 29% 的机会@F 阻塞
    29% 变黑的机会增加2

    8h462% 增强)防御
    损失 t142%PNHANCEDDEFENSE a ALLRESISTANCES93i

    所有电阻@3h
    Ay eyeGloabsere

    9 冷吸收
    ?

    以太[无法@无法修复)
    以太无法配对


    我在处理上做了一些细微的改变。
    代码:

    import cv2
    import pytesseract

    img = cv2.imread("YKEyQ.png")
    (h, w) = img.shape[:2]
    img = cv2.resize(img, (w*3, h*3))
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    (h, w) = gry.shape[:2]

    s_idx = 0
    e_idx = int(h/15)

    for i, _ in enumerate(range(0, 15)):
    gry_crp = gry[s_idx:e_idx, 0:w]
    thr = cv2.threshold(gry_crp, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    thr = cv2.dilate(thr, None, iterations=1)
    cv2.imwrite("/Users/ahx/Desktop/res{}.png".format(i), thr)
    txt = pytesseract.image_to_string(thr, config="--psm 6")
    print(txt)
    s_idx = e_idx
    e_idx = s_idx + int(h/15)
    cv2.imshow("thr", thr)
    cv2.waitKey(0)

    关于从视频游戏截图中提取 Python 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65979862/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com