英文:
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论