How to find border around specific detected text?

201 Views Asked by At

I have a batch of screenshots where I'm printing the coordinates of buttons with certain text (in this case, buttons with the word "exit"). Finding the coordinates of the text was pretty straightforward with EasyOCR. My problem is that I'm not sure how to find the coordinates of the border around the text.

import cv2
import easyocr

img = cv2.imread('C:/Users/xyz/PycharmProjects/findTextImage_v0/testBank/sampleScreen.png')

text = easyocr.Reader(['en'], gpu = True)
result = text.readtext(img)

for item in result:
    if "EXIT" in item:
        print(item[0], item[1])
    else:
        print("False")

A sample screenshot is below for context. Unfortunately, the buttons are not consistent between screenshots. Sometimes, the margins are bigger than the one below, the button is in a different place, the button's background color is a lighter gray, etc.

Previously, I've tackled this by using template matching to find the button, but that's not always accurate and involves a fair bit of manual tweaking per project and multiple image templates and functions.

Once I find the specific text I'm looking for in an image, how can I go about finding its nearest borders and its coordinates?

Thanks for any help!

enter image description here

0

There are 0 best solutions below