为什么 pyautogui.click() return 会出现类型错误?
Why does pyautogui.click() return a Type Error?
我正在尝试制作一个可以检测眼睛是睁开还是闭上的脚本。我使用下面的代码来做到这一点。
https://github.com/balajisrinivas/Color-Detection-OpenCV
感谢 balajisrinivas 提供代码!
使用此代码,我可以添加实时视频捕获并创建在 while 循环中每 2 秒刷新一次的单个图像,而不是单个图像(colorpic.jpg 用作示例)。另外,我这样做是为了如果颜色是棕色或 RGB 接近棕色,计算机会发出哔哔声,但如果颜色是白色、黑色或虹膜的颜色,那么它就知道你的眼睛没有闭上。有一个问题。问题是我必须单击图像才能获得结果,但我希望它自动发生。因此,我在 while True:
循环中使用 if pyautogui.locateOnScreen(‘needle.jpg’, confidence=0.8)
而不是 if clicked
。图片‘needle..jpg’是我眼睛的照片。之后我添加了 pyautogui.click(‘needle.jpg’)
,它会点击图片的中心,并在没有手动点击的情况下给我睁眼或闭眼的结果。
我卡在哪里:
while True:
cv2.imshow("image", img)
if pyautogui.locateOnScreen('needle.jpg', confidence=0.8):
pyautogui.click('needle.jpg')
# cv2.rectangle(image, start point, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)
# Creating text string to display( Color name and RGB values )
text = get_color_name(r, g, b) + ' R=' + str(r) + \
' G=' + str(g) + ' B=' + str(b)
之前的代码是:
while True:
cv2.imshow("image", img)
if clicked:
# cv2.rectangle(image, start point, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)
# Creating text string to display( Color name and RGB values )
text = get_color_name(r, g, b) + ' R=' + str(r) + \
' G=' + str(g) + ' B=' + str(b)
唯一的问题是出现类型错误。
这是错误:
Traceback (most recent call last):
File "C:\Users\user1\Desktop\Color-Detection-OpenCV\hello.py", line 89, in <module>
pyautogui.click('needle.jpg')
line 598, in wrapper
returnVal = wrappedFunction(*args, **kwargs)
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\pyautogui\__init__.py", line 980, in click
x, y = _normalizeXYArgs(x, y)
TypeError: cannot unpack non-iterable NoneType object
只是想让你知道我是python的新手,我是一名在校学生。如果您知道我收到此错误的原因,请帮助我。谢谢!
看起来这只是 pyautogui 的故意疏忽。因为我们已经知道图像在您的 locateOnScreen
调用中的位置,所以为什么我们在点击时再次搜索。
来自他们的源代码:
def _normalizeXYArgs(firstArg, secondArg):
# skip
elif isinstance(firstArg, str):
# If x is a string, we assume it's an image filename to locate on the screen:
try:
location = locateOnScreen(firstArg)
# The following code only runs if pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION is not set to True, meaning that
# locateOnScreen() returns None if the image can't be found.
if location is not None:
return center(location)
else:
return None
# skipping lines past this
最后看到评论说locateOnScreen
return None if image is not found.
对于 locateOnScreen
的调用,您通过了 confidence=0.8
但 默认置信度为 0.999。
所以有信心找到 0.8 图像,但没有找到 0.999。因此它 returned None
到 location
然后它导致 if 块到 return None
- 这是不可解压的。
因为我目前在移动设备上,所以我无法测试自己,但我打赌 locateOnScreen
returns x, y coord when matched.
所以尝试将 if block 改成这样。
try:
x, y = pyautogui.locateOnScreen( ~~~ )
except TypeError:
# value unpack failed, so we got None. Therefore no image matched.
# therefore no x exist.
# Put things you want to do when there's NO image matched.
pass
else:
# Put things you want to do when image matched.
pyautogui.click(x, y)
强烈建议阅读Error handling part的官方文档。
我正在尝试制作一个可以检测眼睛是睁开还是闭上的脚本。我使用下面的代码来做到这一点。
https://github.com/balajisrinivas/Color-Detection-OpenCV
感谢 balajisrinivas 提供代码!
使用此代码,我可以添加实时视频捕获并创建在 while 循环中每 2 秒刷新一次的单个图像,而不是单个图像(colorpic.jpg 用作示例)。另外,我这样做是为了如果颜色是棕色或 RGB 接近棕色,计算机会发出哔哔声,但如果颜色是白色、黑色或虹膜的颜色,那么它就知道你的眼睛没有闭上。有一个问题。问题是我必须单击图像才能获得结果,但我希望它自动发生。因此,我在 while True:
循环中使用 if pyautogui.locateOnScreen(‘needle.jpg’, confidence=0.8)
而不是 if clicked
。图片‘needle..jpg’是我眼睛的照片。之后我添加了 pyautogui.click(‘needle.jpg’)
,它会点击图片的中心,并在没有手动点击的情况下给我睁眼或闭眼的结果。
我卡在哪里:
while True:
cv2.imshow("image", img)
if pyautogui.locateOnScreen('needle.jpg', confidence=0.8):
pyautogui.click('needle.jpg')
# cv2.rectangle(image, start point, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)
# Creating text string to display( Color name and RGB values )
text = get_color_name(r, g, b) + ' R=' + str(r) + \
' G=' + str(g) + ' B=' + str(b)
之前的代码是:
while True:
cv2.imshow("image", img)
if clicked:
# cv2.rectangle(image, start point, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)
# Creating text string to display( Color name and RGB values )
text = get_color_name(r, g, b) + ' R=' + str(r) + \
' G=' + str(g) + ' B=' + str(b)
唯一的问题是出现类型错误。
这是错误:
Traceback (most recent call last):
File "C:\Users\user1\Desktop\Color-Detection-OpenCV\hello.py", line 89, in <module>
pyautogui.click('needle.jpg')
line 598, in wrapper
returnVal = wrappedFunction(*args, **kwargs)
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\pyautogui\__init__.py", line 980, in click
x, y = _normalizeXYArgs(x, y)
TypeError: cannot unpack non-iterable NoneType object
只是想让你知道我是python的新手,我是一名在校学生。如果您知道我收到此错误的原因,请帮助我。谢谢!
看起来这只是 pyautogui 的故意疏忽。因为我们已经知道图像在您的 locateOnScreen
调用中的位置,所以为什么我们在点击时再次搜索。
来自他们的源代码:
def _normalizeXYArgs(firstArg, secondArg):
# skip
elif isinstance(firstArg, str):
# If x is a string, we assume it's an image filename to locate on the screen:
try:
location = locateOnScreen(firstArg)
# The following code only runs if pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION is not set to True, meaning that
# locateOnScreen() returns None if the image can't be found.
if location is not None:
return center(location)
else:
return None
# skipping lines past this
最后看到评论说locateOnScreen
return None if image is not found.
对于 locateOnScreen
的调用,您通过了 confidence=0.8
但 默认置信度为 0.999。
所以有信心找到 0.8 图像,但没有找到 0.999。因此它 returned None
到 location
然后它导致 if 块到 return None
- 这是不可解压的。
因为我目前在移动设备上,所以我无法测试自己,但我打赌 locateOnScreen
returns x, y coord when matched.
所以尝试将 if block 改成这样。
try:
x, y = pyautogui.locateOnScreen( ~~~ )
except TypeError:
# value unpack failed, so we got None. Therefore no image matched.
# therefore no x exist.
# Put things you want to do when there's NO image matched.
pass
else:
# Put things you want to do when image matched.
pyautogui.click(x, y)
强烈建议阅读Error handling part的官方文档。