【问题标题】:how to get an if statement to recognize a string of words如何让 if 语句识别一串单词
【发布时间】:2022-01-21 20:20:47
【问题描述】:

我有一个项目,我正在使用 tesseract 抓取屏幕以查找文本框以输入电子邮件地址。
Tessearact 找到该框,将名为 text 的变量返回为 Send copy to email
我可以在python中很好地打印那个字符串 但是当我尝试在 if 语句中使用它时,它不会匹配
这只是我与我制作的屏幕截图一起使用的一个测试脚本,它返回Send copy to email的保证值

我确定我在这里遗漏了一些简单的东西......非常感谢任何帮助

编辑* 文本 [to/by] 已修复

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')

【问题讨论】:

  • text 具有 by 但您要比较的字符串具有 to
  • 在 if text == str('Send copy to email') 行中:你说“to”,在 text = 'Send copy by email' 行中你说“by”,这样他们就会不匹配。 texttype() 是什么。

标签: python string if-statement


【解决方案1】:

所以在有人回复之前,我找到了一个对我有用的解决方案...... 我不知道这是正确的答案还是最好的方法......但它似乎很完美

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多