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

问题描述

我想让我的计算机通过强化学习来学习如何在虚拟机中玩游戏。不幸的是,我看不懂分数,该分数应被用作积极的奖励。字体也有点奇怪,这可能是原因。这是我的代码:

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)

我试图从屏幕截图中仅提取分数,结果很不错,但是似乎无法识别单个字符。

the score

我想用于消极报酬的生命似乎确实得到了认可。这些都是奇怪的物体,特塞斯特拉特似乎认为这些是欧元符号,因此我可以计算欧元符号的数量来确定生命的数量...

但是分数有什么提示吗?

解决方法

要检测相同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)
     kernel = np.ones((3,3),np.uint8)
     barroi = cv2.erode(barroi,kernel,iterations = 1)
    
     (thresh,barroi) = cv2.threshold(barroi,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),2)
     imageName =  "Result.tif"
     cv2.imwrite(imageName,img)
     cv2.namedWindow('Result')
     cv2.imshow('Result',img)
    

enter image description here

结果

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...