【发布时间】:2021-11-04 15:25:41
【问题描述】:
我的变量points 中存储了两个坐标:[(100, 50)]
我正在尝试使用 pyautogui.moveTo(points) 移动鼠标,但出现错误:
pyautogui.PyAutoGUIException: The supplied sequence must have exactly 2 or exactly 4 elements (0 were received).
我假设这意味着我传递的是单个列表对象而不是坐标。
表达式 [(100, 50)] 是什么意思,我如何将 x 和 y 转换为两个元素。
我从中获得points 的源代码:
import cv2 as cv
import numpy as np
class Vision:
# properties
needle_img = None
needle_w = 0
needle_h = 0
method = None
# constructor
def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
# Save the dimensions of the needle image
self.needle_w = self.needle_img.shape[1]
self.needle_h = self.needle_img.shape[0]
self.method = method
def find(self, haystack_img, threshold=0.5, debug_mode=None):
# run the OpenCV algorithm
result = cv.matchTemplate(haystack_img, self.needle_img, self.method)
# Get the all the positions from the match result that exceed our threshold
locations = np.where(result >= threshold)
locations = list(zip(*locations[::-1]))
for loc in locations:
rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h]
# Add every box to the list twice in order to retain single (non-overlapping) boxes
rectangles.append(rect)
rectangles.append(rect)
# Apply group rectangles
rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5)
points = []
if len(rectangles):
line_color = (0, 255, 0)
line_type = cv.LINE_4
marker_color = (255, 0, 255)
marker_type = cv.MARKER_CROSS
# Loop over all the rectangles
for (x, y, w, h) in rectangles:
# Determine the center position
center_x = x + int(w/2)
center_y = y + int(h/2)
# Save the points
points.append((center_x, center_y))
if debug_mode == 'rectangles':
# Determine the box position
top_left = (x, y)
bottom_right = (x + w, y + h)
# Draw the box
cv.rectangle(haystack_img, top_left, bottom_right, color=line_color,
lineType=line_type, thickness=2)
elif debug_mode == 'points':
# Draw the center point
cv.drawMarker(haystack_img, (center_x, center_y),
color=marker_color, markerType=marker_type,
markerSize=40, thickness=2)
if debug_mode:
cv.imshow('Matches', haystack_img)
return points
【问题讨论】:
-
“我知道 [ ] 用于列表或可变数据等可能更改的内容,而 ( ) 用于函数或元组,即不会更改的内容。”不是真的,可变性并不真正相关
-
请在问题中包含您的源代码 - 谢谢。
-
无论如何,在这种情况下,
[(100, 50)]是一个包含一个元组的列表...这是一个具有单个元素的序列(列表),(100, 50)对(作为一个元组,它本身就是一个序列) -
“我正在尝试使用 pyautogui.moveTo(points) 移动鼠标,但出现错误” - 是您在使用
pyautogui时关于此错误的问题,还是关于语法[(100, 50)]的含义?请将您的问题集中在您想要回答的一件具体事情上;如果是关于[(100, 50)]的含义,那么您的实际问题与pyautogui无关,并且您提到此错误只会使事情变得混乱。 -
抱歉,两者都是。我想了解括号的含义,以帮助我回答我正在努力解决的问题。