简体   繁体   中英

Why tesseract wont find this easy text in image?

I have been trying for hours to resize and change color of this image but nothing is consistently getting correct letters. Please see image below. This is a test image I'm using. The goal is to use this for automation purposes. Thanks! 在此处输入图像描述 Larger sample在此处输入图像描述

import numpy as np
import pytesseract
from PIL import ImageGrab
import win32gui
import time

toplist, winlist = [], []

#time.sleep(3)

def enum_cb(hwnd, results):
    if 'FPS:' in win32gui.GetWindowText(hwnd):
        print(hex(hwnd), win32gui.GetWindowText(hwnd))
        winlist.append(hwnd)


win32gui.EnumWindows(enum_cb, None)

win32gui.SetForegroundWindow(winlist[0])
bbox = win32gui.GetWindowRect(winlist[0])
print(bbox)
img = np.array(ImageGrab.grab(bbox=(130, 810, 800, 1080)))


#percent by which the image is resized
scale_percent = 400

#calculate the 50 percent of original dimensions
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)

# dsize
dsize = (width, height)

# resize image
output = cv2.resize(img, dsize)

#img.show()

pytesseract.pytesseract.tesseract_cmd = r"L:\Program Files\Tesseract-OCR\tesseract.exe"

# img = cv2.imread(r"L:\MGO2PC\MGO2 UNOFFICIAL PC\RPCS3 EMU\screenshots\screenshot-2021_04_06_17_13_03.png", 0)
#crop_img = img[800:900, 260:800]
#cv2.imshow("cropped", crop_img)
#cv2.waitKey(0)

i = cv2.imwrite("test.png",output)
text = pytesseract.image_to_string(output, lang='eng')
print(text)

Ultimately found that I can isolate the text color and tesseract had no problem reading after that. 在此处输入图像描述

def cv2_from_screen(self):
   boundaries = [
        ([0, 179, 105], [38, 255, 167])  # BGR
    ]

    pytesseract.pytesseract.tesseract_cmd = r"L:\Program Files\Tesseract- 
    OCR\tesseract.exe"
    def enum_cb(hwnd, results):
        if 'FPS:' in win32gui.GetWindowText(hwnd):
            print(hex(hwnd), win32gui.GetWindowText(hwnd))
            self.winlist.append(hwnd)

    win32gui.EnumWindows(enum_cb, None)
    win32gui.EnumWindows(enum_cb, None)
    win32gui.SetForegroundWindow(self.winlist[0])

    image = pyautogui.screenshot()
    image = cv2.cvtColor(np.array(image.crop(box=[0, 800, 1000, 1080])), cv2.COLOR_RGB2BGR) #COLOR_RGB2BGR and COLOR_BGR2GRAY

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