簡體   English   中英

為什么 tesseract 不會在圖像中找到這個簡單的文本?

[英]Why tesseract wont find this easy text in image?

我已經嘗試了幾個小時來調整這個圖像的大小和顏色,但沒有什么能始終如一地得到正確的字母。 請看下圖。 這是我正在使用的測試圖像。 目標是將其用於自動化目的。 謝謝! 在此處輸入圖像描述 更大的樣本在此處輸入圖像描述

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)

最終發現我可以隔離文本顏色,之后tesseract閱讀沒有問題。 在此處輸入圖像描述

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM