Skip to content

Instantly share code, notes, and snippets.

@Michael-Evergreen
Created December 9, 2020 09:56
Show Gist options
  • Save Michael-Evergreen/a054ea77f248b7790d823843e62bdf2a to your computer and use it in GitHub Desktop.
Save Michael-Evergreen/a054ea77f248b7790d823843e62bdf2a to your computer and use it in GitHub Desktop.
def Scan(self):
try:
dota = gw.getWindowsWithTitle("Dota 2")[0]
dota.activate()
except IndexError as error:
pass
time.sleep(0.2)
pyautogui.click(x=347, y=798)
pyautogui.click(x=348, y=798)
pyautogui.click(x=349, y=798)
self.progressbar.setText("Occupying mouse for taking pictures, please don't use it...")
self.progressbar.setVisible(True)
os.chdir("C:/autochess_data")
screenshot = pyautogui.screenshot()
screenshot = np.asarray(screenshot)
player_status_list = [screenshot[438:449, 1705:1754], screenshot[525:536, 1705:1754],
screenshot[613:624, 1705:1754], screenshot[701:712, 1705:1754],
screenshot[788:799, 1705:1754],
screenshot[875:886, 1705:1754]]
failed_icon = cv2.imread("C:/autochess_data/failed_icon.png")
transparent_failed_icon = cv2.imread("C:/autochess_data/transparent_failed_icon.png")
num_of_lost_players = 0
for i in range(0, len(player_status_list)):
if max(cv2.matchTemplate(player_status_list[i], failed_icon, cv2.TM_CCOEFF_NORMED)) > 0.8 or max(
cv2.matchTemplate(player_status_list[i], transparent_failed_icon, cv2.TM_CCOEFF_NORMED)) > 0.8:
num_of_lost_players = 6 - i
break
keyboard.press_and_release("f3")
time.sleep(0.5)
on_stage_heroes = pyautogui.screenshot()
keyboard.press_and_release("f3")
on_stage_heroes.save("on_stage_heroes.png")
time.sleep(0.3)
count = 0
pyautogui.click(x=1602, y=233)
pyautogui.click(x=1602, y=233)
time.sleep(TIMESLEEP)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
pyautogui.click(x=1602, y=320)
pyautogui.click(x=1602, y=320)
time.sleep(TIMESLEEP)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
if num_of_lost_players < 6:
pyautogui.click(x=1602, y=409)
pyautogui.click(x=1602, y=409)
time.sleep(TIMESLEEP)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
else:
img = cv2.imread("C:/autochess_data/Lost.png")
count += 1
cv2.imwrite(f"{count}.jpg", img)
if num_of_lost_players < 5:
pyautogui.click(x=1602, y=497)
pyautogui.click(x=1602, y=497)
time.sleep(TIMESLEEP)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
else:
img = cv2.imread("C:/autochess_data/Lost.png")
count += 1
cv2.imwrite(f"{count}.jpg", img)
if num_of_lost_players < 4:
pyautogui.click(x=1602, y=586)
pyautogui.click(x=1602, y=586)
time.sleep(TIMESLEEP)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
else:
img = cv2.imread("C:/autochess_data/Lost.png")
count += 1
cv2.imwrite(f"{count}.jpg", img)
if num_of_lost_players < 3:
pyautogui.click(x=1602, y=670)
pyautogui.click(x=1602, y=670)
pyautogui.moveTo(x=1546, y=620)
time.sleep(0.6)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
else:
img = cv2.imread("C:/autochess_data/Lost.png")
count += 1
cv2.imwrite(f"{count}.jpg", img)
if num_of_lost_players < 2:
pyautogui.click(x=1602, y=758)
pyautogui.click(x=1602, y=758)
pyautogui.moveTo(x=1546, y=708)
time.sleep(0.6)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
else:
img = cv2.imread("C:/autochess_data/Lost.png")
count += 1
cv2.imwrite(f"{count}.jpg", img)
if num_of_lost_players < 1:
pyautogui.click(x=1602, y=844)
pyautogui.click(x=1602, y=844)
pyautogui.moveTo(x=1546, y=794)
time.sleep(0.6)
img = pyautogui.screenshot(region=(527, 619, 848, 212))
count += 1
img.save(f"{count}.jpg")
else:
img = cv2.imread("C:/autochess_data/Lost.png")
count += 1
cv2.imwrite(f"{count}.jpg", img)
self.progressbar.setText("Feeding pictures to the neural network...")
os.chdir("G:/darknet/")
os.system(
"G:\darknet/darknet.exe detector test G:\darknet/obj.data G:/darknet/thresh_cfg.cfg G:\darknetsave/yolov4-obj_last_25.weights -ext_output -dont_show -out result.json < data/train.txt")
f = open("G:/darknet/result.json")
data = json.load(f)
all_chess_pieces_list = ([], [], [], [], [], [], [], [])
for i in range(8):
for hero in data[i]["objects"]:
all_chess_pieces_list[i].append(hero["name"])
on_stage_heroes = cv2.imread("C:/autochess_data/on_stage_heroes.png")
on_stage_heroes_grayed = cv2.cvtColor(on_stage_heroes, cv2.COLOR_BGR2GRAY)
temp_on_stage_list = ([], [], [], [], [], [], [], [])
os.chdir("C:/autochess_data/")
count3 = 0
num_of_lost_players = 0
self.progressbar.setText("Template matching icons...")
for i in range(0, 80 - num_of_lost_players * 10):
if i % 10 == 0:
count3 = 0 + int(i / 10)
hero_level = ""
level_icon_template = on_stage_heroes_grayed[
Y_COORDINATES_END[count3] + 1:Y_COORDINATES_END[count3] + 1 + LEVEL_ICON_HEIGHT,
X_COORDINATES_START[i % 10]:X_COORDINATES_END[i % 10]]
for level in level_dict:
res = cv2.matchTemplate(level_dict[level], level_icon_template, cv2.TM_CCOEFF_NORMED)
if max(res) > 0.7:
hero_level = level
break
template = on_stage_heroes[Y_COORDINATES_START[count3]:Y_COORDINATES_END[count3],
X_COORDINATES_START[i % 10]:X_COORDINATES_END[i % 10]]
all_matches = []
for hero_name in namelist:
cv2.waitKey(0)
res = cv2.matchTemplate(hero_icon_dict[hero_name], template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.65)
if loc[0].size > 0:
all_matches.append((hero_name, max(res)))
all_matches = sorted(all_matches, key=lambda match: match[1])
if len(all_matches) > 0:
all_chess_pieces_list[count3].append(all_matches[-1][0] + hero_level)
temp_on_stage_list[count3].append((all_matches[-1][0] + hero_level, Y_COORDINATES_START[count3],
Y_COORDINATES_END[count3] + 1 + LEVEL_ICON_HEIGHT,
X_COORDINATES_START[i % 10], X_COORDINATES_END[i % 10]))
self.progressbar.setText("Tabulating the results...")
flat_list = [item for sublist in all_chess_pieces_list for item in sublist]
Dict = copy.deepcopy(RSCP_Dict)
for hero in flat_list:
if hero.endswith("2"):
Dict[hero[:-1]][4] -= 3
elif hero.endswith("3"):
Dict[hero[:-1]][4] -= 9
else:
Dict[hero][4] -= 1
colors = [Qt.white, QtGui.QColor(170, 170, 255), QtGui.QColor(90, 90, 255), QtGui.QColor(249, 28, 249),
QtGui.QColor(255, 151, 36)]
row = 0
for hero in Dict:
self.tableWidget.setItem(row, 4, QTableWidgetItem(str(Dict[hero][4])))
self.tableWidget.item(row, 4).setForeground(colors[int(Dict[hero][0][0]) - 1])
row += 1
CAS_dict_copy = copy.deepcopy(CAS_dict)
on_stage_CAS_icons = on_stage_heroes[
on_stage_CAS_y_coors[7 - num_of_lost_players][0]:
on_stage_CAS_y_coors[7 - num_of_lost_players][
1],
on_stage_CAS_x_coors[0]:on_stage_CAS_x_coors[1]]
on_stage_CAS_icons_grayed = cv2.cvtColor(on_stage_CAS_icons, cv2.COLOR_BGR2GRAY)
for CAS in CAS_names_and_icons:
result = cv2.matchTemplate(on_stage_CAS_icons, CAS[1], cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= 0.9)
if loc[0].size == 0:
continue
else:
CAS_dict_copy[CAS[0]][0] = loc[0].size
for i in range(loc[0].size):
number_icon = on_stage_CAS_icons_grayed[loc[0][i] + 25:loc[0][i] + 37, loc[1][i] + 2:loc[1][i] + 19]
result_list = []
for icon in CAS_numbers_and_icons:
if cv2.matchTemplate(number_icon, icon[1], cv2.TM_CCOEFF_NORMED) > 0.85:
result_list.append((icon[0], cv2.matchTemplate(number_icon, icon[1], cv2.TM_CCOEFF_NORMED)))
result_list = sorted(result_list, key=lambda icon: icon[1], reverse=True)
CAS_dict_copy[CAS[0]][1] += result_list[0][0]
row = 0
for CAS in CAS_dict_copy:
self.CAS_tableWidget.setItem(row, 0, QTableWidgetItem(CAS))
self.CAS_tableWidget.setItem(row, 1, QTableWidgetItem(str(CAS_dict_copy[CAS][0])))
self.CAS_tableWidget.setItem(row, 2, QTableWidgetItem(str(CAS_dict_copy[CAS][1])))
self.CAS_tableWidget.item(row, 0).setForeground(Qt.white)
self.CAS_tableWidget.item(row, 1).setTextAlignment(Qt.AlignHCenter)
self.CAS_tableWidget.item(row, 1).setForeground(Qt.white)
self.CAS_tableWidget.item(row, 2).setTextAlignment(Qt.AlignHCenter)
self.CAS_tableWidget.item(row, 2).setForeground(Qt.white)
row += 1
global to_show_list
to_show_list = all_chess_pieces_list
global on_stage_list
on_stage_list = temp_on_stage_list
print(temp_on_stage_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment