opencv 创建一个机器人,它可以识别在Python中按下'E'的正确时刻

5us2dqdw  于 2023-10-24  发布在  Python
关注(0)|答案(1)|浏览(107)

What needs to be identifiedRight time to press 'E'我试图使机器人按下'E'自动钓鱼游戏,在照片中,我显示它看起来像什么时候,你可以按下它(它是红色的),我想知道如何识别这种颜色变化,使机器人按下'E'自动。
我试着这样做,但它不能识别颜色。下面是我测试的代码:

import cv2
import pyautogui
from time import sleep
from PIL import Image

fishing_image_rgb = fishing_image.convert("RGB")
pixel_fishing_image = fishing_image_rgb.getpixel((10,20))

print(pixel_fishing_image)

# Color in RGB
color = (89, 42, 42)

while True:
    ps = pyautogui.screenshot(region=(1267, 833, 300, 100))
    ps.save('PS.png')
    ps.show()

    # Look for the color in the image
    if ps is not None:
        if color in ps.getcolors():
            print("Color found")
            pyautogui.press('e')
        else:
            print("Couldn't find the color")
            sleep(0.1) 
    else:
        print("Unable to capture screen.")

我尝试了这个代码,但它不工作。我尝试了这个代码,但它不工作。我测试了确切的颜色,打印屏幕正在工作,但它不识别颜色。

v09wglhw

v09wglhw1#

你可以这样做:

# x and y are the coordinates of the pixel in the screenshot you want to know the position of.
color = ps.getpixel((x,y))
if color[0] == 89 and color[1] == 42 and color[2] == 42:
    print("Color found!")
    pyautogui.press('e')

如果它不起作用,可能是你的颜色不准确,所以试着这样修改if语句:

if color[0] > 84 and color[0] < 94:
    if color[1] > 37 and color[1] < 47:
        if color[2] > 37 and color[2] < 47:
            pyautogui.press('e')

相关问题