pyautogui 置信度问题
def find_and_click(self, image,delay_before_click=0.5):"""查找屏幕上的图像并点击其中心点。:param image: 要查找的图像文件名。:param confidence: 匹配置信度,范围为0到1。:param delay_before_click: 点击前等待的时间(秒)。"""time.sleep(delay_before_click) # 在点击前等待position = pyautogui.locateOnScreen(image,confidence=0.7)if position:center = pyautogui.center(position)pyautogui.click(center)else:print(f"图像 '{image}' 未找到。")
这是我的初始代码运行没问题
def find_and_click(self, image,confidence=0.7,delay_before_click=0.5):"""查找屏幕上的图像并点击其中心点。:param image: 要查找的图像文件名。:param confidence: 匹配置信度,范围为0到1。:param delay_before_click: 点击前等待的时间(秒)。"""time.sleep(delay_before_click) # 在点击前等待position = pyautogui.locateOnScreen(image,confidence)if position:center = pyautogui.center(position)pyautogui.click(center)else:print(f"图像 '{image}' 未找到。")
后续调整了一下 结果报错
Traceback (most recent call last):
File "C:\Users\zheng\anaconda3\envs\taobao\Lib\site-packages\pyautogui\__init__.py", line 172, in wrapper
return wrappedFunction(*args, **kwargs)
File "C:\Users\zheng\anaconda3\envs\taobao\Lib\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
return pyscreeze.locateOnScreen(*args, **kwargs)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "C:\Users\zheng\anaconda3\envs\taobao\Lib\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
retVal = locate(image, screenshotIm, **kwargs)
File "C:\Users\zheng\anaconda3\envs\taobao\Lib\site-packages\pyscreeze\__init__.py", line 383, in locate
points = tuple(locateAll(needleImage, haystackImage, **kwargs))
File "C:\Users\zheng\anaconda3\envs\taobao\Lib\site-packages\pyscreeze\__init__.py", line 257, in _locateAll_opencv
raise ImageNotFoundException('Could not locate the image (highest confidence = %.3f)' % result.max())
pyscreeze.ImageNotFoundException: Could not locate the image (highest confidence = 0.985)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\zheng\Desktop\淘宝自动上架\自选颜色型号.py", line 53, in <module>
auto_clicker.find_and_click("image4test/colorpicture.png") # 点击颜色图片按钮
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\zheng\Desktop\淘宝自动上架\selectBrand.py", line 19, in find_and_click
position = pyautogui.locateOnScreen(image,confidence)
File "C:\Users\zheng\anaconda3\envs\taobao\Lib\site-packages\pyautogui\__init__.py", line 174, in wrapper
raise ImageNotFoundException # Raise PyAutoGUI's ImageNotFoundException.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyautogui.ImageNotFoundException
Python 会把 confidences
当作第二个位置参数 传递给 locateOnScreen()
函数。
但 locateOnScreen()
的第二个参数是 minSearchTime
,不是一个浮点数类型的置信度参数!
def find_and_click(self, image,confidences=0.7,delay_before_click=0.5):"""查找屏幕上的图像并点击其中心点。:param image: 要查找的图像文件名。:param confidence: 匹配置信度,范围为0到1。:param delay_before_click: 点击前等待的时间(秒)。"""time.sleep(delay_before_click) # 在点击前等待position = pyautogui.locateOnScreen(image,confidence=confidences)if position:center = pyautogui.center(position)pyautogui.click(center)else:print(f"图像 '{image}' 未找到。")
正确写法如上图所示