Home > Net >  how to get an if statement to recognize a string of words
how to get an if statement to recognize a string of words

Time:01-24

I have a project where I am using tesseract to take a screen grab to look for a text box to enter an email address.
Tessearact finds the box, returns a variable called text as Send copy to email
and I can print that string just fine in python but when I try to use it in an if statement, it wont match
this is just a test script I was using with the screen shot I made that returns the guaranteed value of Send copy to email

I'm Sure I'm Missing something simple here.... Any help is Greatly appreciated

EDIT* Text [to/by] fixed

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

img = cv2.imread('C:\\screeny\\emailscreentest.png')

text = pytesseract.image_to_string(img)

print(text)

#As a Test I simply added this line to guarantee the string I wanted
text = 'Send copy by email'

number = 0

for number in range(1000):
    if text == str('Send copy by email') :
        break    # break here

    print('Number is '   str(number))
    img = cv2.imread('C:\\screeny\\emailscreentest.png')
    text = pytesseract.image_to_string(img)


print('Out of loop')

CodePudding user response:

so I found a solution that worked for me before somebody had replied.... I dont know if its the right answer or the best way to do this... but it seems to work perfect

import cv
import pyautogui as pg
import pytesseract
import re

#initial Screenshot-------
pg.screenshot('emailscreentest.png', region=(763,441, 125, 19))
img = cv2.imread('C:\\screeny\\emailscreentest.png')
text = str(pytesseract.image_to_string(img))
match= re.search("Send copy by email",text)


#Loop till screenshot match---------

number = 0
for number in range(1000):
    if match :
        break    # break here

    #print('Number is '   str(number))
    pg.screenshot('emailscreentest.png', region=(763,441, 125, 19))
    img = cv2.imread('C:\\screeny\emailscreentest.png')
    text = pytesseract.image_to_string(img)
    match= re.search("Send copy by email",text)
  •  Tags:  
  • Related