简体   繁体   English

Tesseract:无法从像素化字体中读取数字

[英]Tesseract: cannot read digits from pixelated font

I would like to let my computer learn to play a game in a virtual machine, using reinforcement learning.我想让我的电脑学习在虚拟机中玩游戏,使用强化学习。 Unfortunately I cannot read the score, which should be used for positive rewards.不幸的是,我无法阅读分数,这应该用于积极的奖励。 The font is kinda strange as well, which is probably the reason.字体也有点奇怪,这可能是原因。 This is my code:这是我的代码:

def show(img):
    plt.imshow(img, cmap="gray")
    plt.show()

image = cv2.imread('screenshot.png',0)
crop_img = image[100:140, 38:280]


ret, thresh = cv2.threshold(crop_img, 127, 255, cv2.THRESH_BINARY) 
kernel = np.ones((3,3),np.uint8)
img = cv2.erode(thresh,kernel,iterations = 1)

data = pytesseract.image_to_string(img, lang='eng',config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
show(img)
print(data)

I tried to extract just the score from the screenshot, which worked out, but it doesn't seem te recognise a single character.我试图从屏幕截图中提取分数,结果成功了,但它似乎无法识别单个字符。

分数

The amount of lives, which I would like to use for negative rewards do seem to be recognised.我想用于负奖励的生命数量似乎得到了认可。 Those are kind of strange objects, which tesseract seems to think those are Euro signs, so I could count the amount of Euro signs to determine the amount of lives...那些是一种奇怪的物体,tesseract 似乎认为那些是欧元符号,所以我可以计算欧元符号的数量来确定生命的数量......

But any tips for the score?但是对于分数有什么提示吗?

Is quite challenging to detect all the digit in the same ROI.检测同一 ROI 中的所有数字非常具有挑战性。 It would be best to detect in multiple ROI.最好在多个 ROI 中检测。 Below is what i tried.以下是我尝试过的。

  1. Resize the image smaller.将图像调整为更小。

  2. Blur Out the digit as possible.尽可能模糊数字。

     barroi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) scale_percent = 50 # percent of original size width = int(barroi.shape[1] * scale_percent / 100) height = int(barroi.shape[0] * scale_percent / 100) dim = (width, height) barroi = cv2.resize(barroi, dim, interpolation = cv2.INTER_AREA) barroi = cv2.GaussianBlur(barroi,(5,5),0) barroi = cv2.medianBlur(barroi, 5) barroi = cv2.GaussianBlur(barroi,(5,5),0) barroi = cv2.medianBlur(barroi, 5) barroi = cv2.GaussianBlur(barroi,(5,5),0) barroi = cv2.medianBlur(barroi, 5) barroi = cv2.GaussianBlur(barroi,(5,5),0) barroi = cv2.medianBlur(barroi, 5) kernel = np.ones((3,3),np.uint8) barroi = cv2.erode(barroi,kernel,iterations = 1) (thresh, barroi) = cv2.threshold(barroi, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY) cv2.imwrite("testing.tif", barroi) text = pytesseract.image_to_string(barroi, lang='eng', config='-- psm 10 --oem 3 -c tessedit_char_whitelist=0123456789') print(str(ROIRegion[region])+" "+str(text)) imageName = "Region"+str(region)+".tif" cv2.imwrite(imageName, roi) cv2.putText(img, "Result: "+str(text), ROIRegion[region][0], cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0), 2) imageName = "Result.tif" cv2.imwrite(imageName, img) cv2.namedWindow('Result') cv2.imshow('Result',img)

在此处输入图像描述

Result结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM