Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

this is my code: def findandclickbuttoncsgoroll(team1, team2, buttonstring):…

  1. User

    this is my code: def find_and_click_button_csgoroll(team_1, team_2, button_string): button_string_options = [button_string.lower(), button_string.replace('.', '').lower()] csgoroll_logo = cv2.imread("csgoroll_logo.png", cv2.IMREAD_UNCHANGED) csgoroll_logo.astype(np.uint8) screenshot = take_screenshot(csgoroll_logo) logo_side = cv2.matchTemplate(screenshot, csgoroll_logo, cv2.TM_CCOEFF_NORMED) _, _, _, max_loc_item = cv2.minMaxLoc(logo_side) x_loc, y_loc = max_loc_item side = "right" if x_loc > screen_width / 2 else "left" print(side)

    while not keyboard.is_pressed('q'):
        if side == "left":
            pyautogui.moveTo(screen_width * 0.25, screen_height // 2)
        else:
            pyautogui.moveTo(screen_width * 0.75, screen_height // 2)
    
        if side == "left":
            screenshot = ImageGrab.grab(bbox=(0, 0, screen_width // 2, screen_height))
        else:
            screenshot = ImageGrab.grab(bbox=(screen_width // 2, 0, screen_width, screen_height))
    
        screenshot_np = np.array(screenshot)
        gray = cv2.cvtColor(screenshot_np, cv2.COLOR_BGR2GRAY)
        text = image_to_string(gray).lower()
        team_1 = team_1.lower()
        team_2 = team_2.lower()
        if "team" in team_1:
            team_1 = team_1.replace("team", "")
        if "team" in team_2:
            team_2 = team_2.replace("team", "")
        print("-" * 20)
        print(text)
        print("-" * 20)
    
        team_1_similar = any(string_similar(team_1, word) >= 0.6 for word in text.split())
        team_2_similar = any(string_similar(team_2, word) >= 0.6 for word in text.split())
        button_string_position = next((text.find(option) for option in button_string_options if option in text), -1)
        print("team_1_similar:", team_1_similar)
        print("team_2_similar:", team_2_similar)
    
        if (team_1_similar and team_2_similar) and button_string_position != -1:
            print("*" * 20)
            print("Button found!")
    
            contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
            found_button = False
            button_string_coordinates = None
    
            for contour in contours:
                x, y, w, h = cv2.boundingRect(contour)
                text_roi = gray[y:y + h, x:x + w]
                roi_text = image_to_string(text_roi).lower()
    
                if any(option in roi_text for option in button_string_options):
                    if side == "left":
                        button_string_coordinates = (x + w // 2, y + h // 2)
                    else:
                        button_string_coordinates = (x + x_loc + w // 2, y + h // 2)
    
                    print("Button string coordinates:", button_string_coordinates)
                    found_button = True
                    break
    
            print("*" * 20)
            break
    
        else:
            print("Button not found.")
    
        time.sleep(0.5)
    

    make a code that do the same job but with easy ocr

  2. Assistant

    Here's a version of the code that uses easyOCR for text recognition:

    import cv2
    import numpy as np
    import pyautogui
    from time import sleep
    from PIL import ImageGrab
    import easyocr
    import keyboard
    
    def string_similar(a, b):
        return SequenceMatcher(None, a, b).ratio()
    
    def take_screenshot(csgoroll_logo):
        screenshot = np.array(pyautogui.screenshot())
        return screenshot
    
    screen_width, screen_height = pyautogui.size()
    
    # Initialize easyOCR reader
    reader = easyocr.Reader(['en'])
    
    
    def find_and_click_button_csgoroll(team_1, team_2, button_string):
        button_string_options = [button_string.lower(), button_string.replace('.', '').lower()]
        csgoroll_logo = cv2.imread("csgoroll_logo.png", cv2.IMREAD_UNCHANGED)
        csgoroll_logo.astype(np.uint8)
        screenshot = take_screenshot(csgoroll_logo)
        logo_side = cv2.matchTemplate(screenshot, csgoroll_logo, cv2.TM_CCOEFF_NORMED)
        _, _, _, max_loc_item = cv2.minMaxLoc(logo_side)
        x_loc, y_loc = max_loc_item
        side = "right" if x_loc > screen_width / 2 else "left"
        print(side)
    
        while not keyboard.is_pressed('q'):
            if side == "left":
                pyautogui.moveTo(screen_width * 0.25, screen_height // 2)
            else:
                pyautogui.moveTo(screen_width * 0.75, screen_height // 2)
    
            if side == "left":
                screenshot = ImageGrab.grab(bbox=(0, 0, screen_width // 2, screen_height))
            else:
                screenshot = ImageGrab.grab(bbox=(screen_width // 2, 0, screen_width, screen_height))
    
            screenshot_np = np.array(screenshot)
            gray = cv2.cvtColor(screenshot_np, cv2.COLOR_BGR2GRAY)
    
            text_list = reader.readtext(gray, detail=0, min_size=1)
            text = ' '.join(text_list).lower()
    
            team_1 = team_1.lower()
            team_2 = team_2.lower()
    
            if "team" in team_1:
                team_1 = team_1.replace("team", "")
            if "team" in team_2:
                team_2 = team_2.replace("team", "")
    
            print("-" * 20)
            print(text)
            print("-" * 20)
    
            team_1_similar = any(string_similar(team_1, word) >= 0.6 for word in text_list)
            team_2_similar = any(string_similar(team_2, word) >= 0.6 for word in text_list)
            button_string_position = next((text.find(option) for option in button_string_options if option in text), -1)
            print("team_1_similar:", team_1_similar)
            print("team_2_similar:", team_2_similar)
    
            if (team_1_similar and team_2_similar) and button_string_position != -1:
                print("*" * 20)
                print("Button found!")
    
                contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
                found_button = False
                button_string_coordinates = None
    
                for contour in contours:
                    x, y, w, h = cv2.boundingRect(contour)
                    text_roi = gray[y:y + h, x:x + w]
                    roi_text_list = reader.readtext(text_roi, detail=0, min_size=1)
                    roi_text = ' '.join(roi_text_list).lower()
    
                    if any(option in roi_text for option in button_string_options):
                        if side == "left":
                            button_string_coordinates = (x + w // 2, y + h // 2)
                        else:
                            button_string_coordinates = (x + x_loc + w // 2, y + h // 2)
    
                        print("Button string coordinates:", button_string_coordinates)
                        found_button = True
                        break
    
                print("*" * 20)
                break
    
            else:
                print("Button not found.")
            sleep(0.5)
    

    The primary difference between the original code and the modified version is the usage of the easyOCR library for text recognition. To accomplish this, I have initialized the easyOCR reader and replaced the calls to the pytesseract image_to_string function with the easyOCR reader.readtext function.

  3. User

    I need a function that does the same porpuse but selenium based def find_and_click_button_csgoroll(drive, team_1, team_2, button_string)

  4. Assistant

    Here's a function that uses Selenium to find and click the specified button:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    def find_and_click_button_csgoroll(driver, team_1, team_2, button_string):
        # Convert input to lowercase as we will search with case-insensitive xpath
        team_1 = team_1.lower()
        team_2 = team_2.lower()
        button_string = button_string.lower()
    
        # Search for elements containing team_1 and team_2
        team_elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located(
            (By.XPATH, f"//*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{team_1}') and contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{team_2}')]")))
    
        if not team_elements:
            print("No elements containing both teams found")
            return False
    
        # Search for sibling buttons containing button_string
        button_elements = []
        for team_element in team_elements:
            button_elements.extend(team_element.find_elements_by_xpath(f"following-sibling::*//button[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '{button_string}')]"))
    
        if not button_elements:
            print("No buttons with the specified text found")
            return False
    
        # Click the first button found
        button_elements[0].click()
        print("Button clicked")
        return True
    

    This function takes a Selenium WebDriver instance and the two team names and button_string as input. It uses XPath to find elements containing both team names and sibling buttons containing the specified button_string. Once it finds the first button matching the criteria, it clicks it. It returns True if a button was clicked, False otherwise.