How can I make this while True loop run faster and work properly? It detects the presence of 3 unwanted items on screen, when gone, alert triggered

huangapple go评论76阅读模式
英文:

How can I make this while True loop run faster and work properly? It detects the presence of 3 unwanted items on screen, when gone, alert triggered

问题

以下是您提供的代码的翻译部分:

我有一个 while True 循环来搜索三种不同图像的存在如果检测到其中任何一种图像将按下空格以搜索另一个应用程序如果找到一个应用程序但没有其中任何一种图像循环将中断并触发一个警报通知用户还有工作要做

我遇到的问题是我认为 pyautogui 在按下 Enter 键后立即检测到图像已消失而下一个应用程序正在我的浏览器中加载加载时什么也没有是一个空白页面加载下一个应用程序只需要不到一秒钟的时间当下一个应用程序加载时往往情况下三种图像中的一种已经存在但循环已经过早地中断

以下是循环的代码

while True:
    pyautogui.press("space")
    img = pyautogui.locateOnScreen("ffd1.PNG", confidence=0.7)
    img = pyautogui.locateOnScreen("ffd2.PNG", confidence=0.7)
    img = pyautogui.locateOnScreen("ffd3.PNG", confidence=0.7)
    if img == None:
        ctypes.windll.user32.MessageBoxW(0, "还有工作要做!!", "还有工作要做!!", 0x1000)
        break

是否有更好的方法来执行这个操作我希望它能快速按下空格并同时搜索我的图像的缺失我尝试在其中添加一个 time.sleep(.5)但一点帮助都没有循环仍然在加载屏幕之间中断:|

有人指出我只在搜索一个图像我已经修复了这个问题...但仍然非常慢

while True:
    img = pyautogui.locateOnScreen("ffd1.PNG", confidence=0.7)
    if img == None and img2 == None and img3 == None:
        ctypes.windll.user32.MessageBoxW(0, "还有工作要做!!", "还有工作要做!!", 0x1000)
        break
    img2 = pyautogui.locateOnScreen("ffd2.PNG", confidence=0.7)
    if img == None and img2 == None and img3 == None:
        ctypes.windll.user32.MessageBoxW(0, "还有工作要做!!", "还有工作要做!!", 0x1000)
        break
    img3 = pyautogui.locateOnScreen("ffd3.PNG", confidence=0.7)
    if img == None and img2 == None and img3 == None:
        ctypes.windll.user32.MessageBoxW(0, "还有工作要做!!", "还有工作要做!!", 0x1000)
        break
    pyautogui.press("space")

尝试使用opencv 重写时它似乎经常无缘无故崩溃

img = cv2.imread('ffd1.PNG')
image_detected = False

while True:
    screenshot = pyautogui.screenshot()
    screenshot = np.array(screenshot)
    screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
    
    res = cv2.matchTemplate(screenshot, img, cv2.TM_CCOEFF_NORMED)
    threshold = 0.8
    loc = np.where(res >= threshold)
    
    if np.any(loc) and not image_detected:
        print("在屏幕上检测到图像!按下空格键。")
        image_detected = True
        pyautogui.press('空格')
        time.sleep(1)
        
    elif not np.any(loc) and image_detected:
        print("在屏幕上不再检测到图像。退出...")
        break

如果您需要更多帮助或有其他问题,请随时提出。

英文:

I have a while True loop searching for the presence three different images. If any of the three are detected, 'space' is pressed, to search for another application. If an application is found without any of the three images, the loop is broken, and an alert is triggered notifying the user that there's work to be done.

The problem I'm having is, I think pyautogui detects that the image is gone immediately after enter is pressed, and the next application is loading in my browser, as it loads, nothing is there, it's a blank page. It only takes a fraction of a second to load the next application. When the next one loads in, often times, 1 of the 3 images are present but the loop already broke prematurely.

Here's the loop:

while True:
pyautogui.press("space")
img = pyautogui.locateOnScreen("ffd1.PNG", confidence=0.7)
img = pyautogui.locateOnScreen("ffd2.PNG", confidence=0.7)
img = pyautogui.locateOnScreen("ffd3.PNG", confidence=0.7)
if img == None:
ctypes.windll.user32.MessageBoxW(0, "There's work to be done!!", "There's work to be done!!", 0x1000)
break

Is there a better way to do this? I want it to press 'space' quickly, and search for the lack of my images at the same time. I tried adding a time.sleep(.5) in there, and it didn't help at all. The loop still breaks between loading screens. How can I make this while True loop run faster and work properly? It detects the presence of 3 unwanted items on screen, when gone, alert triggered

somebody pointed out that I'm only searching for one image. I fixed that here... It's still very slow.

while True:
img = pyautogui.locateOnScreen("ffd1.PNG", confidence=0.7)
if img == None and img2 == None and img3 == None:
ctypes.windll.user32.MessageBoxW(0, "There's work to be done!!", "There's work to be done!!", 0x1000)
break
img2 = pyautogui.locateOnScreen("ffd2.PNG", confidence=0.7)
if img == None and img2 == None and img3 == None:
ctypes.windll.user32.MessageBoxW(0, "There's work to be done!!", "There's work to be done!!", 0x1000)
break
img3 = pyautogui.locateOnScreen("ffd3.PNG", confidence=0.7)
if img == None and img2 == None and img3 == None:
ctypes.windll.user32.MessageBoxW(0, "There's work to be done!!", "There's work to be done!!", 0x1000)
break
pyautogui.press("space")

tried rewriting using opencv and the thing just crashes all the time for no reason

img = cv2.imread('ffd1.PNG')
image_detected = False
while True:
screenshot = pyautogui.screenshot()
screenshot = np.array(screenshot)
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
res = cv2.matchTemplate(screenshot, img, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
if np.any(loc) and not image_detected:
print("Image detected on the screen! Pressed space key.")
image_detected = True
pyautogui.press('space')
time.sleep(1)
elif not np.any(loc) and image_detected:
print("Image is no longer detected on the screen. Exiting...")
break

答案1

得分: 0

pyautogui在屏幕捕获方面以缓慢著称,openCV和mss是可以探索的替代方案。尽管如此,通常仍需要多次循环来设置img的所有值以进行测试。您的opencv代码与我的预期最接近。

如果只使用pyautogui,请尝试以下代码:

while any(map(lambda f: pyautogui.locateOnScreen(f, confidence=0.7), ["ffd1.PNG", "ffd2.PNG", "ffd3.PNG"])):
    print("屏幕上检测到图像!按下空格键。")
    pyautogui.press('space')
    time.sleep(1)

ctypes.windll.user32.MessageBoxW(0, "有工作要做!", "有工作要做!", 0x1000)

我不预期会有显著的改进,尤其是如果您有较大的显示器。

英文:

pyautogui is notoriously slow at screen capture and openCV and mss is/are alternatives to explore. That said, you are still often looping several times to set all values for img in order to do the test. Your opencv code is the closest to what I might expect.

For just pyautogui, try this:

while any(map(lambda f: pyautogui.locateOnScreen(f, confidence=0.7), ["ffd1.PNG", "ffd2.PNG", "ffd3.PNG"])):
    print("Image detected on the screen! Pressed space key.")
    pyautogui.press('space')
    time.sleep(1)

ctypes.windll.user32.MessageBoxW(0, "There's work to be done!!", "There's work to be done!!", 0x1000)

I'm not anticipating a huge improvement particularly if you have a larger monitor.

huangapple
  • 本文由 发表于 2023年2月19日 06:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定