简体   繁体   中英

Python Tesseract is not detecting my number

I wanted to take a screenshot of My Valorant Game and give out the remaining Time in the Image.

It all works fine but its not detecting a number in the Image.

time.sleep(2)
myScreenshot = pyautogui.screenshot()
myScreenshot.save(r'Path\screenshot.png')
raw = cv2.imread("Path/screenshot.png")
y=10
x=1160
h=100
w=200
cropped = raw[y:y+h, x:x+w]

cv2.imwrite("Path/Time.png", cropped)

Time = cv2.imread("Path/Time.png")

string = pytesseract.image_to_string(Time, config='--psm 13')

print(string)

Example Image of "Time.png"

I tryed different psm setting they didnt help.

You need to preprocess the input image before OCR (eg remove background/noise). Something like this should work for image you provided:

import numpy as np
import pyautogui
import pytesseract
from PIL import Image

y = 10
x = 1160
h = 100
w = 200
    
cropped = pyautogui.screenshot(region=(x, y, h, w))
data = np.array(cropped)
color = (255, 255, 255)
mask_cv = np.any(data == [255,255,255], axis = -1)
ocr_area = Image.fromarray(np.invert(mask_cv))
string = pytesseract.image_to_string(ocr_area)

print(string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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