Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.4 kB
1
Indexable
Never
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 23 00:09:06 2023

@author: salihcanturan
"""

from pytesseract import pytesseract
import threading
import pygetwindow
from pynput.keyboard import Key, Controller
import win32api, win32con
from win32gui import GetWindowText, GetForegroundWindow
from PIL import ImageFont
import pyautogui

import mss
import numpy as np
import cv2       
import time
import pyuac
import keyboard
import pickle
import uuid
from unidecode import unidecode
from DatasetLoader import getItemIconDictionary
import re

path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
pytesseract.tesseract_cmd = path_to_tesseract

print('Is user admin?', pyuac.isUserAdmin())

WINDOW_NAME = 'Harbi2 | shaula666'

titles = pygetwindow.getAllTitles()

stopApp = False
screenshot = None
dogrulama_screenshot = None
active_window = None

imageDataset = getItemIconDictionary()

controller = Controller()

def click(x, y):
    old = pyautogui.position()
    win32api.SetCursorPos((x,y))
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
    win32api.SetCursorPos(old)

def string_benzerligi(str1, str2):
    str1 = str1[:-4]
    str1 = str1.lower()
    str2 = str2.lower()
    
    str1 = re.sub('[^A-Za-z]+', '', unidecode(str1))
    str2 = re.sub('[^A-Za-z]+', '', unidecode(str2))
    
    toplam_benzerlik = 0
    for karakter in set(str1 + str2):
        toplam_benzerlik += min(str1.count(karakter), str2.count(karakter))
    return 100.0 * toplam_benzerlik / (len(str1) + len(str2)) + 1

def mse_benzerligi(img1, img2):
    return np.mean((img1[:,:,:3].astype("float") - img2.astype("float")) ** 2)

metinThreadDone = True
def clickMetinThread(region, ratio):
    global metinThreadDone
    metinThreadDone = False
    
    found = False
    x = None
    y = None
    # Metin tespit kısa kodu tüm piksellere bakar.
    for i in range(100, len(screenshot)-50, 2):
        for j in range(0, len(screenshot[0]), 2):
            blue = screenshot[i, j][0]
            green = screenshot[i, j][1]
            red = screenshot[i, j][2]
            if abs(150 - blue) < 40 and abs(46 - green) < 20 and abs(95 - red) < 20:
                found = True
                x = int(region['left']+j*(1/ratio))
                y = int(region['top']+i*(1/ratio))
                break
        if found:
            break
    
    if found:
        click(x, y)
        
    controller.press('q')
    time.sleep(1.5)
    controller.release('q')
    
    if found:
        time.sleep(10)
    metinThreadDone = True
    
        
def updateScreenshotThread():
    global dogrulama_screenshot, screenshot
    
    sct = mss.mss()
    window = pygetwindow.getWindowsWithTitle(WINDOW_NAME)
    if len(window) == 0:
        return
    window = window[0]
    left, top = window.topleft
    right, bottom = window.bottomright
    
    region = {'top': top, 'left': left, 'width': right-left, 'height': bottom-top}
    
    raw_screenshot = sct.grab(region)
    raw_screenshot = np.array(raw_screenshot)[:,:,:3]
    
    ratio = 1000 / raw_screenshot.shape[0]
    
    width = int(raw_screenshot.shape[1] * ratio)
    height = int(raw_screenshot.shape[0] * ratio + 0.001)
    
    raw_screenshot = cv2.resize(raw_screenshot, (width, height), interpolation=cv2.INTER_AREA)
    
    screenshot = raw_screenshot.copy()
    dogrulama_screenshot = raw_screenshot[203:446, 405:884, :]
    
    return ratio, region, np.sum(dogrulama_screenshot[20:30, 0:100]) / \
                          np.prod(dogrulama_screenshot[20:30, 0:100].shape) < 10


def mainThread():
    global stopApp, screenshot, active_window, metinThreadDone
    
    i = 0
    while True:
        pack = updateScreenshotThread()
        if pack is None:
            time.sleep(0.5)
            continue
        
        ratio, region, validation_came = pack
        
        active_window = GetWindowText(GetForegroundWindow())
        
        if not validation_came:
            if metinThreadDone and active_window == WINDOW_NAME:
                metinThread = threading.Thread(target=clickMetinThread, args=(region, ratio,)).start()
                time.sleep(0.1)
            continue
        
        if screenshot is None:
            time.sleep(0.1)
            continue
        
        if active_window != WINDOW_NAME:
            print(GetWindowText(GetForegroundWindow()))
            cv2.imshow("DOGRULAMA TESPIT", resized)
            if cv2.waitKey(1) == ord('x'):
                stopApp=True
                cv2.destroyAllWindows()
                break 
            continue
        
        text_img = dogrulama_screenshot[5:40, 50:400, :]
        text = pytesseract.image_to_string(text_img, lang="tur")
        
        max_val = 0
        max_key = None
        for key in imageDataset:
            if max_val < string_benzerligi(key, text):
                max_val = string_benzerligi(key, text)
                max_key = key
        
        image_to_compare = imageDataset[max_key]

        resized = cv2.resize(dogrulama_screenshot, (dogrulama_screenshot.shape[1]*2, dogrulama_screenshot.shape[0]*2), interpolation=cv2.INTER_AREA)
        resized = cv2.rectangle(resized, (100, 10), ((800, 80)), (0, 255, 0), 1)
        
        slots = []
        for j in range(9):
            slots.append(cv2.resize(dogrulama_screenshot[45:165,10+j*52:50+j*52], (4, 12), interpolation=cv2.INTER_AREA))
        
        min_fark = 9999999
        match_idx = 0
        for idx, slot in enumerate(slots):
            if min_fark > mse_benzerligi(image_to_compare, slot):
                match_idx = idx
                min_fark = mse_benzerligi(image_to_compare, slot)
        
        resized = cv2.rectangle(resized, (124, 90), ((204, 330)), (0, 255, 0), -1)
        
        for j in range(9):
            resized[90:330, 20+104*j:100+104*j] = cv2.resize(slots[j], (80, 240), interpolation=cv2.INTER_AREA)
            
        for j in range(9):
            if j == match_idx:
                print(text)
                print(max_key)
                click(int(region['left'] + (405 + 30+j*52) * (1/ratio)),
                      int(region['top'] + (203 + 105) * (1/ratio))
                      )
                time.sleep(0.1)
                
                cv2.rectangle(resized, (20+j*104, 90), ((100+j*104, 330)), (0, 255, 0), 5)
            else:
                cv2.rectangle(resized, (20+j*104, 90), ((100+j*104, 330)), (255, 0, 0), 1)
                
        font = cv2.FONT_HERSHEY_DUPLEX
        fontScale = 2
        thickness = 2
        cv2.putText(resized, re.sub('[^A-Za-z]+', '', unidecode(text)), (200, 70), font, 
                    fontScale, (0,255,0), thickness, cv2.LINE_AA)
        
        cv2.putText(resized, unidecode(max_key), (100, 400), font, 
                    fontScale, (0,255,0), thickness, cv2.LINE_AA)
        
        cv2.imshow("DOGRULAMA TESPIT", resized)

        if cv2.waitKey(1) == ord('x'):
            cv2.destroyAllWindows()
            stopApp=True
            break
        i += 1

mainThread()