Genting Legends Assistant Highlighting

I. Introduction

This article provides a simple demo for highlighting the target lineup to avoid pig brain overload or slow thinking. The basic process consists of two parts. One is to use tesseract to identify the name list of the card pool and compare it with the target lineup to obtain the required card coordinates; the other is to highlight the cards based on the above coordinates

2. Environment settings

The blogger runs on windows and uses it with Pycharm. The following settings are required to use this tutorial:

  1. Set the League of Legends client language to English
  2. Set the resolution in the game to 1920*1080 , borderless mode
  3. Windows desktop, right click -> display settings -> zoom and layout -> change the size of text, applications and other items -> 100%
  4. Install tesseract and configure it to system environment variables
  5. run pycharm as administrator

3. Card recognition

3.1 Candidate box interception

import pyscreenshot                 # Screenshot tool, PIL friendly
from ocr import get_text_from_image
from pynput import keyboard


def takeScreenshotROI(coordinates):
    # Take a screenshot of the champion zone only
    return pyscreenshot.grab(coordinates)


def get_champions():

    width, height = (1920, 1080)
    left, top, right, bottom = (int(width * 0.25), int(height * 0.96), int(width * 0.77), int(height * 0.99))
    _width = (right-left)/5

    champions_list = []
    for idx in range(5):

        coordinates = (int(left + idx*_width)+10, top, int(left + (idx+1)*_width)-55, bottom)

        roi_example = takeScreenshotROI(coordinates)
        # 用于测试 正式运行时将 以下两行代码 注释
        # roi_example = Image.open('examples/ (9).png')
        # roi_example = roi_example.crop(coordinates)

        champion_name = get_text_from_image(roi_example)
        champions_list.append(champion_name)

    return champions_list

Calculate the upper left and lower right coordinates coordinates , and then capture the screen according to the coordinates. Since there are five cards in the card pool, it needs to be cycled five times

3.2 ocr recognition

"""
Contains all code related to turning a screenshot into a string
"""

from typing import Any
import cv2
import numpy as np
from PIL import ImageGrab
import pytesseract

def image_grayscale(image: ImageGrab.Image) -> Any:
    """Converts an image to grayscale so OCR has an easier time deciphering characters"""
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


def image_thresholding(image: ImageGrab.Image) -> Any:
    """Applies thresholding to the image https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html"""
    return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]


def image_array(image: ImageGrab.Image) -> Any:
    """Turns the image into an array"""
    image = np.array(image)
    image = image[..., :3]
    return image


def image_resize(image: int, scale: int) -> Any:
    """Resizes the image using the scale passed in argument two"""
    (width, height) = (image.width * scale, image.height * scale)
    return image.resize((width, height))


def get_text_from_image(image: ImageGrab.Image, whitelist: str = "") -> str:
    """Takes an image and returns the text"""
    resize = image_resize(image, 3)
    array = image_array(resize)
    grayscale = image_grayscale(array)
    thresholding = image_thresholding(grayscale)
    return pytesseract.image_to_string(thresholding,
                                       config=f'--psm 7 -c tessedit_char_whitelist={
      
      whitelist}').strip()

The image intercepted by the candidate frame in 3.1 will be used as a parameter, passed to the function get_text_from_image for identification, and the card name will be obtained

4. Highlight

Based on the function in Chapter 3, you will get the name list of the current card pool, compare the list with the target lineup, get the coordinates of the position to be highlighted, and then use Pyqt5 to display

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import sys
from PyQt5 import QtCore

class Window(QMainWindow):
    def __init__(self, display_list):
        super(Window, self).__init__(None, QtCore.Qt.WindowStaysOnTopHint)

        # this will hide the title bar
        self.setWindowFlag(Qt.FramelessWindowHint)

        # set the title
        self.setWindowTitle("no title")
        self.setGeometry(500, 940, 950, 30)

        for idx in display_list:
            self.label = QLabel('recommend', self)
            self.label.setStyleSheet("background-color: lightgreen; border: 3px solid green")
            self.label.setGeometry(200*idx, 0, 100, 30)

        # show all the widgets
        self.show()


    def keyPressEvent(self, e):
        if e.key() == Qt.Key_D:
            self.close()


App = QApplication(sys.argv)
# recommend i th champion
win1 = Window([0, 1, 3])
App.exec()

Effect preview
insert image description here

References

[1]TFT-OCR-BOT
[2]TFT-Auto-Buy
[3]TFT_zynp

Guess you like

Origin blog.csdn.net/qq_42811827/article/details/128714645