当前位置: 首页 > news >正文

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}' 未找到。")

正确写法如上图所示

http://www.dtcms.com/a/296510.html

相关文章:

  • 拖拽同时支持Y轴滚动
  • 开立医疗2026年校园招聘
  • openbmc 日志系统继续分析
  • 行为型模式-协作与交互机制
  • 华为仓颉编程语言的表达式及其特点
  • mac llama_index agent算术式子计算示例
  • 力扣刷题(第九十七天)
  • 强化学习入门三(SARSA)
  • 专题:2025微短剧行业生态构建与跨界融合研究报告|附100+份报告PDF汇总下载
  • LeetCode 1695.删除子数组的最大得分:滑动窗口(哈希表)
  • 07 RK3568 Debian11 网络优先级
  • “抓了个寂寞”:一次实时信息采集的意外和修复
  • 网络基础19--OSPF路由协议(上)
  • 基于QT(C++)实现(图形界面)通讯录系统
  • JavaJSP
  • 【SpringAI实战】FunctionCalling实现企业级自定义智能客服
  • Qt 调用ocx的详细步骤
  • 单片机学习课程
  • 数据推荐丨海天瑞声7月数据集上新啦!
  • 海外红人营销的下一站:APP出海如何布局虚拟网红与UGC生态?
  • idea监控本地堆栈
  • Redis分布式锁的学习(八)
  • 无源域自适应综合研究【2】
  • Qt连接MySql数据库
  • SAP B1 DTW成功登录后点击下一步提示没有权限读取清单
  • QML 模型
  • 阿里云SLS未开启索引时无法查询日志内容
  • 11.事务
  • 【GoLang#1】:Go 语言概述(背景 | 环境配置 | 特点 | 学习)
  • Redis单线程模型(含面试题)