如何解决使用 PyAutoGui locateCenter 函数尝试/排除循环
由于双增量,这段代码应该通过 for 循环的 6 次迭代/技术上的 3 次迭代来执行。相反,我收到了一个错误。
对我的代码的任何批评也表示赞赏,任何建议都会有所帮助。
我的代码:
import pyautogui
import time
import random
import keyboard
import sys
def findClickShaftFunction():
count = 1
v = 1.0
movementTime = random.uniform(0.24,1.88)
timetoSleep = random.uniform(25.32,62.5)
for count in range (0,5):
try:
print("Try:",count," Sample:"," Image:",count)
pictureOfShaftsX,pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/pyautogui_ImageRecognition/shafts{}.png'.format(count),confidence=v)
except TypeError:
continue
print("Try:",count+1,count+1)
pictureOfShaftsX,pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/pyautogui_ImageRecognition/shafts{}.png'.format(count+1),confidence=v - 0.10)
#else:
#count = count + 1
#print("Unable to trace " + count)
time.sleep(timetoSleep)
pictureOfShaftsX = pictureOfShaftsX - (random.randint(2,11))
pictureOfShaftsY = pictureOfShaftsY - (random.randint(2,11))
pyautogui.moveto(pictureOfShaftsX,pictureOfShaftsY,movementTime)
pyautogui.click()
完整的错误追溯:
Would you like to begin? Enter y to continue. y
Try: 0 Sample: 0 Image: 0
Try: 1 Sample: 1 Image: 1
Traceback (most recent call last):
File "C:\Users\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial.py",line 17,in findClickShaftFunction
pictureOfShaftsX,confidence=v)
TypeError: cannot unpack non-iterable nonetype object
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "C:\Users\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial.py",line 78,in <module>
main()
File "C:\Users\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial.py",line 56,in main
findClickShaftFunction()
File "C:\Users\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial\pyautogui_Shaft_Trial.py",line 20,confidence=v - 0.10)
TypeError: cannot unpack non-iterable nonetype object
Press any key to continue . . .
更改后
except TypeError:
print("Try:",confidence=v - 0.10)
到
except TypeError:
continue
它在实际定位图像之前遍历整个循环。
Would you like to begin? Enter y to continue. y
Try: 0 Sample: 0 Image: 0
Try: 1 Sample: 1 Image: 1
Try: 2 Sample: 2 Image: 2
Try: 3 Sample: 3 Image: 3
Try: 4 Sample: 4 Image: 4
Try: 5 Sample: 5 Image: 5
Try: 0 Sample: 0 Image: 0
Try: 1 Sample: 1 Image: 1
Try: 2 Sample: 2 Image: 2
Try: 3 Sample: 3 Image: 3
Try: 4 Sample: 4 Image: 4
Try: 5 Sample: 5 Image: 5
Try: 0 Sample: 0 Image: 0
Try: 1 Sample: 1 Image: 1
Try: 2 Sample: 2 Image: 2
Try: 3 Sample: 3 Image: 3
Try: 4 Sample: 4 Image: 4
Try: 5 Sample: 5 Image: 5
Try: 0 Sample: 0 Image: 0
Try: 1 Sample: 1 Image: 1
Try: 2 Sample: 2 Image: 2
Try: 3 Sample: 3 Image: 3
Try: 4 Sample: 4 Image: 4
Try: 5 Sample: 5 Image: 5
Try: 0 Sample: 0 Image: 0
Try: 1 Sample: 1 Image: 1
Try: 2 Sample: 2 Image: 2
Try: 3 Sample: 3 Image: 3
Try: 4 Sample: 4 Image: 4
Try: 5 Sample: 5 Image: 5
Try: 0 Sample: 0 Image: 0
Try: 1 Sample: 1 Image: 1
Try: 2 Sample: 2 Image: 2
Try: 3 Sample: 3 Image: 3
Try: 4 Sample: 4 Image: 4
Try: 5 Sample: 5 Image: 5
我打算让循环执行的示例(没有打印语句)
try:
pictureOfShaftsX,pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/pyautogui_ImageRecognition/shafts0.png',confidence=0.7)
except TypeError:
pictureOfShaftsX,pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/pyautogui_ImageRecognition/shafts1.png',confidence=0.6)
try:
pictureOfShaftsX,pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/pyautogui_ImageRecognition/shafts2.png',confidence=0.55)
except TyperError:
pictureOfShaftsX,pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/pyautogui_ImageRecognition/shafts3.png',confidence=0.50)
解决方法
据我所知,pyautogui.locateCenterOnScreen
正在返回 None
,这就是您得到 TypeError
的原因。与其在 except
块中调用相同的方法,您可能只想continue
到循环的下一次迭代,如下所示:
for count in range(6):
try:
print("Try:",count," Sample:"," Image:",count)
pictureOfShaftsX,pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count),confidence=v)
except TypeError:
continue
另外,请注意 range(1,6)
从 1-5 包含在内 - 您可能希望 range(6)
为 0-5 包含(总共 6 次迭代)。
我认为这是对我的问题的合理回答。它似乎按预期迭代 - 检查每个图像。现在,我开始意识到 TensorFlow 可能更适合我的应用程序。
如有任何进一步的建议,我们将不胜感激。
count = 0;
v = 0.55
movementTime = random.uniform(0.24,1.88)
timeToSleep = random.uniform(25.32,62.5)
success = False
while not success:
try:
print("Try:",count)
pictureOfShaftsX,confidence=v)
success = True
except TypeError:
count = count + 1
pass
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。