使用 tesseract 提高 OCR 准确性? (Python)
我正在尝试构建一些简单的脚本来在游戏中自动农场。
基本上,在游戏中,我会进行训练,直到达到屏幕上始终显示的称为“身体疲劳”的统计数据的一定百分比。我想要发生的是检测何时达到 60% 并将该数字存储在变量中,以便我可以使用它。我尝试使用 tesseract 的 image_to_string 函数和片段,但由于文本的字体很奇怪,它显示了错误的信息。我也会将图片放在这里,任何信息都将不胜感激!
我尝试将图片重新缩放 x2 并且它似乎正在进步,但它仍然犯着严重的错误,可能会毁了这件事,比如把 5 当作 $ 或 6 当作 0。 以下代码应该在由 reg 元组确定的区域中查找名为 BodyFatigue.png 的已保存图像片段。它将坐标(顶部、左侧、宽度、高度)返回到 reg2,然后保存 reg2 区域的 .PNG。
我本来应该提取百分比数字并在一些进一步的代码中使用它,但我在超正方体的文本识别方面遇到困难,我认为这是因为奇怪的字体。
import pyautogui as pag
import pytesseract as pyt
from PIL import Image
pyt.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
reg = 0, 197, 841, 146
reg2 = pag.locateOnScreen('resources/BodyFatigue.png', region = reg, confidence = 0.4, grayscale = True)
bdftg = pag.screenshot('resources/BFNew.png', region = reg2)
new_size = tuple(2*x for x in bdftg.size)
bdftg = bdftg.resize(new_size, Image.ANTIALIAS)
output = pyt.image_to_string(bdftg)
print(output)
Im trying to build some simple script to auto-farm on a game.
Basically in-game I train until I reach a certain percentage of a stat called Body Fatigue thats always on the screen. What I would like to happen is detect when it reaches 60% and store that number in a variable so I can work with it. I tried using tesseract's image_to_string function with a snippet taken but it displays the wrong info because of the weird font of the text. I'll put the picture here as well and any info would be appreciated!
I tried rescaling the picture x2 and it seems to be improving but its still doing critical mistakes that could ruin the thing ex taking 5 as a $ or 6 as 0.
The following code is supposed to find a saved image snippet called BodyFatigue.png in the region determined by the reg tuple. It returns the coordinates (top, left, width, height) to reg2 then saves a .PNG of the reg2 region.
I was supposed to extract the percentage number and use it in some further code but I'm having difficulties with tesseract's text recognition, I think its because of the weird font.
import pyautogui as pag
import pytesseract as pyt
from PIL import Image
pyt.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
reg = 0, 197, 841, 146
reg2 = pag.locateOnScreen('resources/BodyFatigue.png', region = reg, confidence = 0.4, grayscale = True)
bdftg = pag.screenshot('resources/BFNew.png', region = reg2)
new_size = tuple(2*x for x in bdftg.size)
bdftg = bdftg.resize(new_size, Image.ANTIALIAS)
output = pyt.image_to_string(bdftg)
print(output)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论