如何使locateCenterOnScreen更准确-PYTHON-,-WINDOWS-

2023-12-31

您好,我的目标是能够让我的机器人在任何屏幕尺寸上单击我选择的内容,因为我认为这是主要问题。我尝试降低置信度,但最终只是点击了具有相同一般颜色的其他内容。我用精确的图像对其进行了测试,它点击了正确的位置,因此它不像坐标已关闭或任何其他东西,它只是图像识别。

这些是要经过的图像

(X1, NextLesson, Arrow) X1 NextLesson Arrow

from pyautogui import *
import pyautogui
import time
import keyboard
import random

def NextLesson():
    keepGoing = True
    while keepGoing == True:
        counter = 0
        nl_coordinates = pyautogui.locateOnScreen('images/nextLesson.png', confidence=0.4)
        print(nl_coordinates)  # This will print out where it is
        if nl_coordinates:
            print(f"I can see it at {nl_coordinates}")
            pyautogui.click(nl_coordinates)
            keepGoing = False
        else:
            print("I cannot see it.")

def Arrow():
    keepGoing = True
    while keepGoing == True:
        counter = 0
        arrow_coordinates = pyautogui.locateOnScreen('images/arrow.png', confidence=0.4)
        print(arrow_coordinates)  # This will print out where it is
        if arrow_coordinates:
            print(f"I can see it at {arrow_coordinates}")
            pyautogui.click(arrow_coordinates)
            keepGoing = False
        else:
            print("I cannot see it.")

def X1():
    keepGoing = True
    while keepGoing == True:
        counter = 0
        x1_coordinates = pyautogui.locateOnScreen('images/x1.png', confidence=0.4)
        print(x1_coordinates)  # This will print out where it is
        if x1_coordinates:
            print(f"I can see it at {x1_coordinates}")
            pyautogui.click(x1_coordinates)
            keepGoing = False
        else:
            print("I cannot see it.")

while True:
    counter = 0
    counter2 = 0
    true = True

    time.sleep(2)
    X1()#
    time.sleep(8)
    NextLesson()#
    time.sleep(10)
    Arrow()#
    print("calibration complete ")


    time.sleep(5)
    cords = pyautogui.position()

    while counter != 1800:
        time.sleep(60)
        pyautogui.click(cords) #clicking where ouse is at
        print("clicked")
        counter += 60
        print(counter)
    if counter == 1800:
        time.sleep(5) #stops code for 5 secs
        X1() #clicks mouse to x button
        print("clicked x")

        time.sleep(5) #stops code for 5 secs
        NextLesson() #clicks mouse to the assignment button
        print("clicked assignemnt")

        time.sleep(15) #stops code for 2 secs
        Arrow() #clicks mouse to the second assignment button
        print("clicked 2nd assignment button ")

        time.sleep(5) #waits 5secs to put cursor at position
        cords = pyautogui.position() #grabs position
        print("grabbed position")

我们可以使用opencv-python来执行多尺度模板匹配。这个想法是缩放模板图像并尝试在屏幕截图中找到调整大小的模板。下面的代码(改编自here https://www.pyimagesearch.com/2015/01/26/multi-scale-template-matching-using-python-opencv/) 循环超过 [0.25,2] 范围内的 50 个缩放参数,并选择提供最佳匹配的参数。您可能希望减少缩放参数的范围或数量以提高效率。最好保存正确的缩放参数并将其重新用于多个图像。

import cv2
import pyscreeze
import numpy as np
import imutils
import pyautogui

def template_match_with_scaling(image,gs=True,confidence=0.8):
"""
Locate an image and return a pyscreeze box surrounding it. 
Template matching is done by default in grayscale (gs=True)
Detect image if normalized correlation coefficient is > confidence (0.8 is default)
"""
    templateim = pyscreeze._load_cv2(image,grayscale=gs) # template image
    (tH, tW)   = templateim.shape[:2]

    screenim_color = pyautogui.screenshot() # screenshot of image
    screenim_color = cv2.cvtColor(np.array(screenim_color),cv2.COLOR_RGB2BGR)
    if gs is True:
       screenim = cv2.cvtColor(np.array(screenim_color),cv2.COLOR_BGR2GRAY)
    else:
       screenim = screenim_color

    #try different scaling parameters and see which one matches best
    found = None #bookeeping variable for the maximum correlation coefficient, position and scale
    scalingrange = np.linspace(0.25,2,num=50)
    for scale in scalingrange:
        resizedtemplate = imutils.resize(templateim,  width = int(templateim.shape[1]*scale) ) # resizing with  imutils maintains the aspect ratio
        r = float(resizedtemplate.shape[1])/templateim.shape[1] # recompute scaling factor
        result = cv2.matchTemplate(screenim, resizedtemplate, cv2.TM_CCOEFF_NORMED) # template matching using the correlation coefficient
        (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result) #returns a 4-tuple which includes the minimum correlation value, the maximum correlation value, the (x, y)-coordinate of the minimum value, and the (x, y)-coordinate of the maximum value
        if found is None or maxVal > found[0]:
           found = (maxVal, maxLoc, r)

    (maxVal, maxLoc, r) = found
    if maxVal > confidence:
       box = pyscreeze.Box(int(maxLoc[0]), int(maxLoc[1]), int(tW*r), int(tH*r) )
       return box
    else:
       return None

def locate_center_with_scaling(image,gs=True):
    loc = template_match_with_scaling(image,gs=gs) 
    if loc:
       return pyautogui.center(loc)
    else:
       raise Exception("Image not found")

#sample usage 
coords = locate_center_with_scaling('images/arrow.png')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使locateCenterOnScreen更准确-PYTHON-,-WINDOWS- 的相关文章

  • Python 中的哈希映射

    我想用Python实现HashMap 我想请求用户输入 根据他的输入 我从 HashMap 中检索一些信息 如果用户输入HashMap的某个键 我想检索相应的值 如何在 Python 中实现此功能 HashMap
  • Python getstatusoutput 替换不返回完整输出

    我发现了这个很棒的替代品getstatusoutput Python 2 中的函数在 Unix 和 Windows 上同样有效 不过我觉得这个方法有问题output被构建 它只返回输出的最后一行 但我不明白为什么 任何帮助都是极好的 def
  • 需要在python中找到print或printf的源代码[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我正在做一些我不能完全谈论的事情 我
  • 使用 kivy textinput 的 'input_type' 属性的问题

    您好 我在使用 kivy 的文本输入小部件的 input type 属性时遇到问题 问题是我制作了两个自定义文本输入 其中一个称为 StrText 其中设置了 input type text 然后是第二个文本输入 名为 NumText 其
  • 使用Python请求登录Google帐户

    在多个登录页面上 需要谷歌登录才能继续 我想用requestspython 中的库以便让我自己登录 通常这很容易使用requests库 但是我无法让它工作 我不确定这是否是由于 Google 做出的一些限制 也许我需要使用他们的 API 或
  • 您可以格式化 pandas 整数以进行显示,例如浮点数的“pd.options.display.float_format”?

    我见过this https stackoverflow com questions 18404946 py pandas formatdataframe and this https stackoverflow com questions
  • Node.js 升级在 Windows 中仍然显示旧版本

    我已使用 msi 安装程序下载并安装了新版本的 nodejs 4 1 2 之后我跑了node v 但它仍然显示旧版本 0 12 2 我尝试重新启动Windows 甚至卸载nodejs并重新安装它 但仍然显示相同的内容 为什么会发生这种情况
  • YOLOv8获取预测边界框

    我想将 OpenCV 与 YOLOv8 集成ultralytics 所以我想从模型预测中获取边界框坐标 我该怎么做呢 from ultralytics import YOLO import cv2 model YOLO yolov8n pt
  • 使用 xlrd 打开 BytesIO (xlsx)

    我正在使用 Django 需要读取上传的 xlsx 文件的工作表和单元格 使用 xlrd 应该可以 但因为文件必须保留在内存中并且可能不会保存到我不知道如何继续的位置 本例中的起点是一个带有上传输入和提交按钮的网页 提交后 文件被捕获req
  • 为什么 PyYAML 花费这么多时间来解析 YAML 文件?

    我正在解析一个大约 6500 行的 YAML 文件 格式如下 foo1 bar1 blah name john age 123 metadata whatever1 whatever whatever2 whatever stuff thi
  • 从Python中的字典列表中查找特定值

    我的字典列表中有以下数据 data I versicolor 0 Sepal Length 7 9 I setosa 0 I virginica 1 I versicolor 0 I setosa 1 I virginica 0 Sepal
  • 如何使用python在一个文件中写入多行

    如果我知道要写多少行 我就知道如何将多行写入一个文件 但是 当我想写多行时 问题就出现了 但是 我不知道它们会是多少 我正在开发一个应用程序 它从网站上抓取并将结果的链接存储在文本文件中 但是 我们不知道它会回复多少行 我的代码现在如下 r
  • Docker 中的 Python 日志记录

    我正在 Ubuntu Web 服务器上的 Docker 容器中测试运行 python 脚本 我正在尝试查找由 Python Logger 模块生成的日志文件 下面是我的Python脚本 import time import logging
  • 如何使用 pybrain 黑盒优化训练神经网络来处理监督数据集?

    我玩了一下 pybrain 了解如何生成具有自定义架构的神经网络 并使用反向传播算法将它们训练为监督数据集 然而 我对优化算法以及任务 学习代理和环境的概念感到困惑 例如 我将如何实现一个神经网络 例如 1 以使用 pybrain 遗传算法
  • pip 列出活动 virtualenv 中的全局包

    将 pip 从 1 4 x 升级到 1 5 后pip freeze输出我的全局安装 系统 软件包的列表 而不是我的 virtualenv 中安装的软件包的列表 我尝试再次降级到 1 4 但这并不能解决我的问题 这有点类似于这个问题 http
  • 从 NumPy ndarray 中选择行

    我只想从 a 中选择某些行NumPy http en wikipedia org wiki NumPy基于第二列中的值的数组 例如 此测试数组的第二列包含从 1 到 10 的整数 gt gt gt test numpy array nump
  • import matplotlib.pyplot 给出 AttributeError: 'NoneType' 对象没有属性 'is_interactive'

    我尝试在 Pycharm 控制台中导入 matplotlib pyplt import matplotlib pyplot as plt 然后作为回报我得到 Traceback most recent call last File D Pr
  • 使用特定颜色和抖动在箱形图上绘制数据点

    我有一个plotly graph objects Box图 我显示了箱形 图中的所有点 我需要根据数据的属性为标记着色 如下所示 我还想抖动这些点 下面未显示 Using Box我可以绘制点并抖动它们 但我不认为我可以给它们着色 fig a
  • Python ImportError:无法导入名称 __init__.py

    我收到此错误 ImportError cannot import name life table from cdc life tables C Users tony OneDrive Documents Retirement retirem
  • Pandas 每周计算重复值

    我有一个Dataframe包含按周分组的日期和 ID df date id 2022 02 07 1 3 5 4 2022 02 14 2 1 3 2022 02 21 9 10 1 2022 05 16 我想计算每周有多少 id 与上周重

随机推荐

  • np.power 还做了哪些额外的工作?

    我意识到np power a b 慢于np exp b np log a import numpy as np a b np random random 2 100000 timeit np power a b best of 3 4 16
  • jQuery 1.3 只选择第一个元素

    我不确定这是否是一个错误 或者只是 jQuery 1 3 中我不知道的一些疯狂的新事物 或者我只是疯了 我有一个表 其中有 11 个复选框 我无法使用 jQuery 1 3 将它们全部选中 jQuery 1 2 6 myTable find
  • 如何使用官方 10gen C# 驱动程序设置地理值的序列化选项?

    考虑这个类 public class Location public Coordinates Geo get set public Location Geo new Coordinates public class Coordinates
  • jquery删除新行然后用块元素包装文本节点

    我有一些这样的段落 这是第一段 r r n n 这 是第二个有很多新行的 在 n n n n n n 和最后一段之后 n r r 我想删除新行并将每个段落用 p 标签 我期望输出如下 p p This is the first para p
  • Windows 头文件中的“#ifdef _MAC”是什么意思?

    我正在浏览 Windows Platform SDK 头文件 真是太棒了 对吧 我注意到很多地方都包含对预处理器符号的引用 MAC 例如 WinUser h line 1568 Message structure typedef struc
  • 控制器策略/垃圾收集(销毁)

    试图找出我的应用程序关于 MVC 的 ember 最佳实践 另外供参考 我正在使用 ember data ember layout 和 ember route manager 我将以用户为例 我觉得我想做的是从数据库中获取用户模型 然后将其
  • 如何用Python做华夫饼图? (方形饼图)

    Something like this 有一个很好的包在 R 中做到这一点 https github com hrbrmstr waffle 在Python中 我能想到的最好的办法就是使用squarify包 灵感来自关于如何制作树形图的帖子
  • 如何在 C# 3.5 中对泛型方法施加接口约束?

    我想在 C 3 5 中实现这样的目标 public void Register
  • Elasticsearch 访问日志

    我正在尝试追踪谁向 ElasticSearch 集群发出查询 Elastic 似乎没有访问日志 有没有地方可以找出哪个 IP 正在访问集群 Elasticsearch 不提供任何开箱即用的安全性 即有目的和有意设计 所以你有几个解决方案 不
  • 如何使用 pdfbox 生成可下载的 PDF(损坏的 PDF)?

    如何使 PDF 文件可通过链接下载 我正在使用 JSF 构建一个 Web 应用程序 当用户单击 另存为 PDF 链接时 应该可以下载 PDF 到目前为止 我有一个生成 PDF 文件的工作代码 但该文件保存在我的桌面上 我想要做的是 当用户单
  • 检测 iPhone 应用程序的 iPad 2x 按钮

    有没有办法检测您的 iPhone 应用程序在 iPad 上运行 2x 1x 我需要能够检测我的应用程序每英寸点数的差异 检查scale财产 UIScreen mainScreen scale 这是一个方便的功能 BOOL screenIs2
  • 将 *.lib 文件与 MinGW 链接

    是否可以链接 lib与 MinGW 一起使用文件 我将其与 Eclipse 一起使用 我正在与 libcurl OpenSSL 作斗争 我不明白我的错误是否是因为我尝试使用 lib MinGW 中的文件或其他错误 lib libeay32
  • 将 Phoenix 项目拆分为应用程序

    Mix 提供了一个伞式项目的功能 其中独立的功能 应用程序 可以一起运行 但可以松散耦合并单独开发 在我的 phoenix 应用程序中 我想将身份验证逻辑移动到一个单独的应用程序中 以便稍后重用它 然而 对于我的身份验证应用程序 模块 我需
  • 如何找到 Hive 中表的上次修改时间戳?

    我正在尝试获取 Hive 中表的最后修改时间戳 请使用以下命令 show TBLPROPERTIES table name transient lastDdlTime
  • 跟踪以查看 AngularJS 中视图何时发生变化

    有谁知道如何在视图改变时使角度射击成为事件 或者在请求并下载视图时 我正在尝试添加页面更改时的加载动画 看一眼这个线程 https groups google com d topic angular OroP1DBE6AA discussi
  • 设置 DataContext 后 PropertyChanged 事件为 null

    我将视图构造函数中视图的 DataContext 设置为 ViewModel 的实例 只是标准的东西 此后不久 一个UPDATE RECENT DOCUMENTS LIST事件从事件聚合器触发 我的 ViewModel 正确捕获了该事件 更
  • 在某些情况下,SVG 在 Safari 中过滤模糊

    我有一个带有交互式 SVG 的页面 它在除 Safari 之外的所有浏览器 Firefox Chrome 甚至 IE Edge 上看起来都很好 其中受 SVG 过滤器之一影响的所有内容都会变成模糊的糊状 看起来像是渲染在低分辨率上的东西 使
  • JQuerymobile 和 Bartender Tabbar - 单独的图标

    我对 JQM 和 CSS 很陌生 所以如果这是一个简单的问题 我很抱歉 我一直在使用 Bartender Tabbar http www stokkers mobi valuables bartender html 和 JQM Barten
  • 都在同一个视图 MVC3 中创建和列出

    所以我想做的是在同一视图中同时创建和列出一个视图 所以我读到我可以为此使用 ViewModel 所以我创建了我的视图模型 Evento ViewModel public class EventoViewModel public Evento
  • 如何使locateCenterOnScreen更准确-PYTHON-,-WINDOWS-

    您好 我的目标是能够让我的机器人在任何屏幕尺寸上单击我选择的内容 因为我认为这是主要问题 我尝试降低置信度 但最终只是点击了具有相同一般颜色的其他内容 我用精确的图像对其进行了测试 它点击了正确的位置 因此它不像坐标已关闭或任何其他东西 它