You are on page 1of 10

import cv2 as cv

import numpy as np
import pyautogui as au
import time as t
import tkinter as tk
import gtts as gt
from pygame import mixer
import os
from PyQt5 import QtWidgets, QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import threading
import concurrent.futures

#create GUI
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(427, 446)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Start = QtWidgets.QPushButton(self.centralwidget)
self.Start.setGeometry(QtCore.QRect(20, 30, 91, 81))
self.Start.setObjectName("Start")
self.Start.clicked.connect(self.start)
self.Stop = QtWidgets.QPushButton(self.centralwidget)
self.Stop.setGeometry(QtCore.QRect(130, 30, 91, 81))
self.Stop.setObjectName("Exit")
self.Stop.clicked.connect(self.stop)
self.Pause = QtWidgets.QPushButton(self.centralwidget)
self.Pause.setGeometry(QtCore.QRect(20, 120, 91, 81))
self.Pause.setObjectName("Pause")
self.Pause.clicked.connect(self.pause)
self.continue_2 = QtWidgets.QPushButton(self.centralwidget)
self.continue_2.setGeometry(QtCore.QRect(130, 120, 91, 81))
self.continue_2.setObjectName("continue_2")
self.continue_2.clicked.connect(self.con)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(240, 30, 151, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(240, 10, 171, 16))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 427, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):


_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("Map_Helper", "Map_Helper"))
self.Start.setText(_translate("MainWindow", "Start"))
self.Stop.setText(_translate("MainWindow", "Exit"))
self.Pause.setText(_translate("MainWindow", "Pause"))
self.continue_2.setText(_translate("MainWindow", "Continue"))
self.comboBox.setItemText(0, _translate("MainWindow", "Aatrox"))
self.comboBox.setItemText(1, _translate("MainWindow", "Akali"))
self.comboBox.setItemText(2, _translate("MainWindow", "Amumu"))
self.comboBox.setItemText(3, _translate("MainWindow", "Ashe"))
self.comboBox.setItemText(4, _translate("MainWindow", "Blitzcrank"))
self.comboBox.setItemText(5, _translate("MainWindow", "Brand"))
self.comboBox.setItemText(6, _translate("MainWindow", "Diana"))
self.comboBox.setItemText(7, _translate("MainWindow", "Dr._Mundo"))
self.comboBox.setItemText(8, _translate("MainWindow", "Ekko"))
self.comboBox.setItemText(9, _translate("MainWindow", "Elise"))
self.comboBox.setItemText(10, _translate("MainWindow", "Evelynn"))
self.comboBox.setItemText(11, _translate("MainWindow", "Fiddlesticks"))
self.comboBox.setItemText(12, _translate("MainWindow", "Garen"))
self.comboBox.setItemText(13, _translate("MainWindow", "Gragas"))
self.comboBox.setItemText(14, _translate("MainWindow", "Graves"))
self.comboBox.setItemText(15, _translate("MainWindow", "Gwen"))
self.comboBox.setItemText(16, _translate("MainWindow", "Hecarim"))
self.comboBox.setItemText(17, _translate("MainWindow", "ChoGath"))
self.comboBox.setItemText(18, _translate("MainWindow", "Ivern"))
self.comboBox.setItemText(19, _translate("MainWindow", "Jarvan"))
self.comboBox.setItemText(20, _translate("MainWindow", "Jax"))
self.comboBox.setItemText(21, _translate("MainWindow", "Karthus"))
self.comboBox.setItemText(22, _translate("MainWindow", "Kayn"))
self.comboBox.setItemText(23, _translate("MainWindow", "KhaZix"))
self.comboBox.setItemText(24, _translate("MainWindow", "Kindred"))
self.comboBox.setItemText(25, _translate("MainWindow", "Lee_Sin"))
self.comboBox.setItemText(26, _translate("MainWindow", "Lillia"))
self.comboBox.setItemText(27, _translate("MainWindow", "Malphite"))
self.comboBox.setItemText(28, _translate("MainWindow", "Malzahar"))
self.comboBox.setItemText(29, _translate("MainWindow", "Maokai"))
self.comboBox.setItemText(30, _translate("MainWindow", "Master_Yi"))
self.comboBox.setItemText(31, _translate("MainWindow", "Mordekaiser"))
self.comboBox.setItemText(32, _translate("MainWindow", "Morgana"))
self.comboBox.setItemText(33, _translate("MainWindow", "Nasus"))
self.comboBox.setItemText(34, _translate("MainWindow", "Nautilus"))
self.comboBox.setItemText(35, _translate("MainWindow", "Nidalee"))
self.comboBox.setItemText(36, _translate("MainWindow", "Nocturne"))
self.comboBox.setItemText(37, _translate("MainWindow", "Nunu"))
self.comboBox.setItemText(38, _translate("MainWindow", "Olaf"))
self.comboBox.setItemText(39, _translate("MainWindow", "Pantheon"))
self.comboBox.setItemText(40, _translate("MainWindow", "Poppy"))
self.comboBox.setItemText(41, _translate("MainWindow", "Rammus"))
self.comboBox.setItemText(42, _translate("MainWindow", "RekSai"))
self.comboBox.setItemText(43, _translate("MainWindow", "Rengar"))
self.comboBox.setItemText(44, _translate("MainWindow", "Rumble"))
self.comboBox.setItemText(45, _translate("MainWindow", "Sejuani"))
self.comboBox.setItemText(46, _translate("MainWindow", "Sett"))
self.comboBox.setItemText(47, _translate("MainWindow", "Shaco"))
self.comboBox.setItemText(48, _translate("MainWindow", "Shen"))
self.comboBox.setItemText(49, _translate("MainWindow", "Shyvana"))
self.comboBox.setItemText(50, _translate("MainWindow", "Singed"))
self.comboBox.setItemText(51, _translate("MainWindow", "Sion"))
self.comboBox.setItemText(52, _translate("MainWindow", "Skarner"))
self.comboBox.setItemText(53, _translate("MainWindow", "Taliyah"))
self.comboBox.setItemText(54, _translate("MainWindow", "Teemo"))
self.comboBox.setItemText(55, _translate("MainWindow", "Trundle"))
self.comboBox.setItemText(56, _translate("MainWindow", "Tryndamere"))
self.comboBox.setItemText(57, _translate("MainWindow", "Twitch"))
self.comboBox.setItemText(58, _translate("MainWindow", "Udyr"))
self.comboBox.setItemText(59, _translate("MainWindow", "Urgot"))
self.comboBox.setItemText(60, _translate("MainWindow", "Vi"))
self.comboBox.setItemText(61, _translate("MainWindow", "Viego"))
self.comboBox.setItemText(62, _translate("MainWindow", "Volibear"))
self.comboBox.setItemText(63, _translate("MainWindow", "Warwick"))
self.comboBox.setItemText(64, _translate("MainWindow", "Wukong"))
self.comboBox.setItemText(65, _translate("MainWindow", "Xin_Zhao"))
self.comboBox.setItemText(66, _translate("MainWindow", "Zac"))
self.comboBox.setItemText(67, _translate("MainWindow", "Zed"))
self.comboBox.setItemText(68, _translate("MainWindow", "Zyra"))
self.label.setText(_translate("MainWindow", "Chose enemy Jungler"))

def start(self):
self._running = True
self.Start.setText("Restart")
self.Start.update()
size = get_screen_size()
#print(size)
tmp = find_map()
cords = (tmp.left,tmp.top)
index = self.comboBox.currentIndex()
tmp = self.comboBox.itemText(index)
print(tmp)
create_overlay(cords,0.1,size)
left_map,screen,map_size,char_name,limit,visible,champ_movement,now,start =
search_for_char_init(tuple(cords), size, str(tmp))
resize_var_x,resize_var_y =
get_minimap_overlay_size_difference(tuple(cords),size,map_size)
global i
i = 0
while self._running:
i += 1

left_map,screen,map_size,char_name,limit,visible,champ_movement,now,start =
search_for_char_loop(left_map,screen,map_size,char_name,limit,visible,champ_movemen
t,now,start,resize_var_x,resize_var_y)
if i == 20:
Window.lift()
Window.attributes('-topmost', True)
i = 0

def stop(self):
exit()
def pause(self):
self._running = False

def con(self):
self.start()

def window():
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

def overlay_to_background(event):
i = 0
print("I m called")
Window.attributes('-topmost', False)
Window.lower()

def
get_minimap_overlay_size_difference(overlay_top_left,overlay_bottom_right,map_size)
:
overlay_x_size = overlay_bottom_right[0]-overlay_top_left[0]
overlay_y_size = overlay_bottom_right[1]-overlay_top_left[1]
x_const = overlay_x_size/map_size
y_const = overlay_y_size/map_size
return x_const, y_const

def overlay_dot(top_left,resize_var_x,resize_var_y):
overlay.create_oval(top_left[0]*resize_var_x-2,top_left[1]*resize_var_y-2,
top_left[0]*resize_var_x+2,top_left[1]*resize_var_y+2,
fill = "red",outline = "red",tags = "dots")

# creates a dot folowing the found characters current movement


def dot(img, cord):
if not cord:
return img
else:
for pixel in cord:
cv.circle(img, pixel, radius=2, color=(0, 0, 255), thickness=3)
return img

#plays a warning message


def speak(now,before,between_calls):
if (now - before) > between_calls:
mixer.music.play()

#decides if the champion has been found or not tbd...


def champ_found(confidence,limit):
global times_found
if confidence > limit:
if confidence < 1:
times_found += 1
else:
times_found = 0
return False
if times_found >1:
return True
else:
return False
else:
times_found = 0
return False

#gets screensize of user tbd...


def get_screen_size():
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.destroy()
return screen_width, screen_height

#finds users minimap by scaling the top left corner image untill found
def find_map():
left_map = None
scale_and_map_finder = cv.imread('find_map.png')
cv.imwrite('find_map_tmp.png', scale_and_map_finder)
index = 0
width = 50
height = 50
# print(width,height)
while left_map is None:
width += 5
height += 5 # have to make sure I make a square finder
left_map = au.locateOnScreen('find_map_tmp.png', confidence=0.55)
tmp = cv.resize(scale_and_map_finder, (width, height))
cv.imwrite('find_map_tmp.png', tmp)
# print(left_map)
index += 1
print(index)
if index > 55: # reset and go for smaller
width = 25
height = 25
index = 0
cv.imwrite('find_map_tmp.png', scale_and_map_finder)
# scale = width / (int(scale_and_map_finder.shape[1]))

return left_map

# cv2 stuff getting necesary images to use also matching the template
def get_processed_minimap(map_size,char_name,img):
img_rgb = np.array(img)
img_rgb = cv.cvtColor(img_rgb,cv.COLOR_RGB2BGR)
img_rgb = cv.resize(img_rgb, (map_size, map_size))
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('champs 40x40/' + str(char_name) + '_OriginalCircle.png',
0)
res = cv.matchTemplate(img_gray, template, cv.TM_CCOEFF_NORMED, None, template)
return img_rgb,img_gray,template,res

def update_stopwatch(seconds):
time_counter = np.zeros((200, 200, 3), np.uint8)
cv.putText(time_counter, "Last seen :" + str(seconds),
(20, 100),
cv.FONT_HERSHEY_SIMPLEX,
0.7,
(255, 255, 255),
2)
cv.imshow("stopwatch", time_counter)
cv.waitKey(1)

def take_screenshot_of_map(left_map,screen):
try:
im = au.screenshot(region=(left_map[0], left_map[1], screen[0] -
left_map[0], screen[1] - left_map[1]))
except:
im = False
print("SC failed")
pass
return im

#this so that the map window apears on startup


def init_map(left_map,screen,map_size,char_name):
im = take_screenshot_of_map(left_map, screen)
img_rgb, img_gray, template, res = get_processed_minimap(map_size, char_name,
im)
cv.imshow("minimap", img_rgb)

#returns a best match found when no enemy is visble to use as a limit


def get_limit(left_map,screen,map_size,char_name):
arr = []
t.sleep(2)
limit = False
for i in range(10):
im = take_screenshot_of_map(left_map, screen)
img_rgb, img_gray, template, res = get_processed_minimap(map_size,
char_name, im)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
arr.append(max_val)
limit = max(arr)
if limit < 0.75 and limit < 1.1:
limit = limit + 0.1
if limit < 0.6:
limit = 0.6
elif limit > 1.1:
limit = 0.75
for i in range(100):
print("found inf as a limit!!! chosing 0.75 as tmp limit")
else:
limit = limit + 0.05
for z in range(100):
print("limit too high for acurate results... : ", limit)
return limit

def create_overlay(top_left,transparency,bottom_right):
global overlay
global Window
Window = tk.Tk()
size1 = bottom_right[0] - top_left[0]
size2 = bottom_right[1] - top_left[1]
text = "{}x{}+{}+{}".format(size1,size2,top_left[0],top_left[1])
Window.overrideredirect(True)
Window.title('Transparent window')
Window.geometry(str(text))

#Window.attributes('-alpha', transparency)
Window.wm_attributes('-transparentcolor', Window['bg'])
Window.attributes('-topmost', True)
overlay = tk.Canvas(Window,width = size1,height = size2)
#overlay.bind("<Enter>", overlay_to_background)
overlay.pack()
overlay.update()

def overlay_draw_rectangle( top_left, bottom_right,resize_x,resize_y):


try:
#print("overlay: ",top_left,"x",bottom_right)
x1 = top_left[0] * resize_x
y1 = top_left[1] * resize_y
x2 = bottom_right[0] *resize_x
y2 = bottom_right[1] *resize_y
overlay.create_rectangle(int(x1),int(y1),int(x2),int(y2),outline =
"red",width = 4,tags = "always_delete")
#overlay.create_rectangle(200,200,250,250,outline = "red",width = 4)
overlay.update()
except:
print("cant draw")
pass
#draws a rectangle around an enemy champion we are looking for
def draw_rectangle(img_rgb, top_left, bottom_right):
try:
#print("draw: ", top_left, "x", bottom_right)
cv.rectangle(img_rgb, top_left, bottom_right, (0, 0, 255), 2)
# print(top_left)
cv.imshow("minimap", img_rgb)
cv.waitKey(1)
except:
pass

def
search_for_char_loop(left_map,screen,map_size,char_name,limit,visible,champ_movemen
t,now,start,resize_x,resize_y):
# overide:
limit = 0.75
overlay.update()
im = take_screenshot_of_map(left_map, screen)
img_rgb, img_gray, template, res = get_processed_minimap(map_size, char_name,
im)
w = template.shape[1]
h = template.shape[0]
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
#print("max_val :", max_val)
#print("limit :", limit)
# print("size = ",map_size)
last = visible
visible = champ_found(max_val, limit)
if visible:
champ_movement.append(top_left)
img_rgb = dot(img_rgb, champ_movement)
draw_rectangle(img_rgb, top_left, bottom_right)
overlay.delete("always_delete")
overlay_draw_rectangle( top_left, bottom_right, resize_x,resize_y)
overlay_dot(top_left,resize_x,resize_y)
if last is False:
overlay.delete("dots")
before = now
now = t.perf_counter()
speak(now, before, 10)
update_stopwatch(0)
if not visible:
present = t.perf_counter()
last_seen_seconds = int(present - start)
update_stopwatch(last_seen_seconds)
if last:
start = t.perf_counter()
if last_seen_seconds >= 5:
champ_movement = []
return
left_map,screen,map_size,char_name,limit,visible,champ_movement,now,start

def search_for_char_init(left_map, screen, char_name):


global times_found
visible = False
last = False
map_size = 545
now = 0
champ_movement = []
start = t.perf_counter()
time_counter = np.zeros((200,200,3), np.uint8)
cv.imshow("stopwatch", time_counter)
init_map(left_map,screen,map_size,char_name)
limit = get_limit(left_map,screen,map_size,char_name)
return
left_map,screen,map_size,char_name,limit,visible,champ_movement,now,start
#cv.imwrite('res.png', img_rgb)

# https://docs.opencv.org/4.5.2/d4/dc6/tutorial_py_template_matching.html

#config:
times_found = 0
# endx
print(sys.argv)
ping = gt.gTTS(text="care", lang="en", slow=False)
ping.save("ping.mp3")
mixer.init()
mixer.music.load("ping.mp3")
window()
input("press enter to exit")

You might also like