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

Tesseract: cannot read digits from pixelated font

我想让我的电脑在虚拟机中学习玩游戏,使用强化学习。不幸的是我看不懂分数,这应该用于积极的奖励。字体也有点奇怪,这可能是原因。这是我的代码:

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)

我试图只从屏幕截图中提取乐谱,结果成功了,但它似乎无法识别单个字符。

我想用于负面奖励的生命数量似乎得到了认可。那些东西有点奇怪,tesseract 似乎认为那些是欧元符号,所以我可以通过计算欧元符号的数量来确定生命的数量...

但是对分数有什么提示吗?

检测同一 ROI 中的所有数字非常具有挑战性。最好在多个 ROI 中进行检测。以下是我尝试过的。

  1. 缩小图片。

  2. 尽可能模糊数字。

     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)
    

结果