[Python] object is not supported when using opencv

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I tried to make a small color recognition script to recognize squares or outlines inside a game and click on the center of them. I know very little about python and OpenCV, and when I run the script it shows me an unsupported format error. I can't find the error.

    The idea is to create a kind of main file where I can reuse detect_objects(image, the color in HSV) in other files, so I don't do it all over again.

    from PIL import Image
    import cv2
    import numpy as np
    import pyautogui
    import win32gui

    def detectar_objetos(imagen, colores_hsv):
    hsv = cv2.cvtColor(imagen, cv2.COLOR_BGR2HSV)

    mascaras = []
    for color in colores_hsv:
    lower = np.array(color)
    upper = np.array(color)
    mask = cv2.inRange(hsv, lower, upper)
    mascaras.append(mask)

    contornos = []
    for mask in mascaras:
    cnts, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contornos.extend(cnts)

    for cnt in contornos:
    M = cv2.moments(cnt)
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])

    pyautogui.moveTo(cx, cy, duration=0.25)
    pyautogui.click()

    # print("Objeto encontrado en:", cx, cy)

    def encontrar_ventana_runelite():
    for win in pyautogui.getAllWindows():
    if "Runelite" in win.title:
    return win
    return None

    def capturar_pantalla_runelite(ventana):
    if ventana is None:
    return None
    win32gui.SetForegroundWindow(ventana.handle)
    x, y, w, h = win32gui.GetWindowRect(ventana.handle)
    return pyautogui.screenshot(region=(x, y, w, h))

    ventana_runelite = encontrar_ventana_runelite()
    imagen = capturar_pantalla_runelite(ventana_runelite)
    imagen = np.array(imagen)
    imagen = cv2.cvtColor(imagen, cv2.COLOR_RGB2BGR)

    detectar_objetos(imagen, (60, 100, 100))


    Console error

    imagen = cv2.cvtColor(imagen, cv2.COLOR_RGB2BGR)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cv2.error: OpenCV(4.10.0) :-1: error: (-5:Bad argument) in function 'cvtColor'
    > Overload resolution failed:
    > - src data type = object is not supported
    > - Expected Ptr<cv::UMat> for argument 'src'


    when running the script, it should check if the game window is in focus, and then take a screenshot to recognize what is being asked for detect_objects(image, color in HSV) and if TRUE, it directs the mouse to those coordinates and performs a click.

    Continue reading...

Compartilhe esta Página