├── connect_data.py ├── grabscreen.py ├── train_data.py ├── image_grab.py ├── getkeys.py ├── test_train.py ├── data_test.py ├── README.md ├── Alexnet.py └── directkeys.py /connect_data.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Apr 13 10:04:12 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import os 9 | import numpy as np 10 | import pandas as pd 11 | from collections import Counter 12 | from random import shuffle 13 | import cv2 14 | 15 | file_name_1 = 'training_data_1_v2_c.npy' 16 | if os.path.isfile(file_name_1): 17 | print("file exists , loading previous data") 18 | training_data_1 = list(np.load(file_name_1,allow_pickle=True)) 19 | 20 | file_name_2 = 'training_data_2_v2_c.npy' 21 | if os.path.isfile(file_name_2): 22 | print("file exists , loading previous data") 23 | training_data_2 = list(np.load(file_name_2,allow_pickle=True)) 24 | 25 | final_data = training_data_1+training_data_2 26 | shuffle(final_data) 27 | print(len(final_data)) 28 | np.save('training_data_2_v2.npy',final_data) 29 | 30 | 31 | ''' 32 | for data in training_data: 33 | img = data[0] 34 | choice = data[1] 35 | cv2.imshow('test',img) 36 | print(choice) 37 | if cv2.waitKey(20) & 0xFF == ord('q'): 38 | break 39 | cv2.waitKey()# 视频结束后,按任意键退出 40 | cv2.destroyAllWindows() 41 | ''' 42 | 43 | df = pd.DataFrame(final_data) 44 | print(df.head()) 45 | print(Counter(df[1].apply(str))) -------------------------------------------------------------------------------- /grabscreen.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Apr 8 12:14:29 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import cv2 9 | import numpy as np 10 | import win32gui, win32ui, win32con, win32api 11 | 12 | def grab_screen(region=None): 13 | 14 | hwin = win32gui.GetDesktopWindow() 15 | 16 | if region: 17 | left,top,x2,y2 = region 18 | width = x2 - left + 1 19 | height = y2 - top + 1 20 | else: 21 | width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) 22 | height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) 23 | left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN) 24 | top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) 25 | 26 | hwindc = win32gui.GetWindowDC(hwin) 27 | srcdc = win32ui.CreateDCFromHandle(hwindc) 28 | memdc = srcdc.CreateCompatibleDC() 29 | bmp = win32ui.CreateBitmap() 30 | bmp.CreateCompatibleBitmap(srcdc, width, height) 31 | memdc.SelectObject(bmp) 32 | memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY) 33 | 34 | signedIntsArray = bmp.GetBitmapBits(True) 35 | img = np.fromstring(signedIntsArray, dtype='uint8') 36 | img.shape = (height,width,4) 37 | 38 | srcdc.DeleteDC() 39 | memdc.DeleteDC() 40 | win32gui.ReleaseDC(hwin, hwindc) 41 | win32gui.DeleteObject(bmp.GetHandle()) 42 | 43 | return img -------------------------------------------------------------------------------- /train_data.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Apr 8 23:12:12 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import numpy as np 9 | from Alexnet import alexnet2 10 | from random import shuffle 11 | import pandas as pd 12 | 13 | # what to start at 14 | START_NUMBER = 60 15 | 16 | # what to end at 17 | hm_data = 111 18 | 19 | # use a previous model to begin? 20 | START_FRESH = False 21 | 22 | WIDTH = 96 23 | HEIGHT = 86 24 | LR = 1e-3 25 | EPOCHS = 20 26 | MODEL_NAME = 'model_sekiro_1/py-sekiro-{}-{}-{}-epochs.model'.format(LR,'alexnetv2',EPOCHS) 27 | EXISTING_MODEL_NAME = 'model_sekiro_1/py-sekiro-{}-{}-{}-epochs.model'.format(LR,'alexnetv2',EPOCHS) 28 | file_name = 'training_data_2_v2.npy' 29 | 30 | model = alexnet2(WIDTH, HEIGHT, LR) 31 | 32 | 33 | if not START_FRESH: 34 | model.load(EXISTING_MODEL_NAME) 35 | 36 | for i in range(EPOCHS): 37 | ''' 38 | data_order = [i for i in range(START_NUMBER,hm_data+1)] 39 | shuffle(data_order) 40 | for i in data_order: 41 | train_data = np.load(file_name,allow_pickle=True) 42 | 43 | df = pd.DataFrame(train_data) 44 | df = df.iloc[np.random.permutation(len(df))] 45 | train_data = df.values.tolist() 46 | ''' 47 | train_data = np.load(file_name,allow_pickle=True) 48 | train = train_data[:-3000] 49 | test = train_data[-3000:] 50 | 51 | X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,1) 52 | Y = [i[1] for i in train] 53 | 54 | test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,1) 55 | test_y = [i[1] for i in test] 56 | 57 | model.fit({'input': X}, {'targets': Y}, n_epoch=1, validation_set=({'input': test_x}, {'targets': test_y}), 58 | snapshot_step=2500, show_metric=True, run_id=MODEL_NAME) 59 | 60 | model.save(MODEL_NAME) 61 | 62 | # tensorboard --logdir=foo:C:/Users/H/Desktop/ai-gaming-phase5/log -------------------------------------------------------------------------------- /image_grab.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Apr 8 09:45:04 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import numpy as np 9 | from PIL import ImageGrab 10 | import cv2 11 | import time 12 | import directkeys 13 | import grabscreen 14 | import getkeys 15 | import os 16 | 17 | wait_time = 5 18 | L_t = 3 19 | file_name = 'training_data_2_1.npy' 20 | window_size = (320,104,704,448)#384,344 192,172 96,86 21 | 22 | if os.path.isfile(file_name): 23 | print("file exists , loading previous data") 24 | training_data = list(np.load(file_name,allow_pickle=True)) 25 | else: 26 | print("file don't exists , create new one") 27 | training_data = [] 28 | 29 | for i in list(range(wait_time))[::-1]: 30 | print(i+1) 31 | time.sleep(1) 32 | 33 | last_time = time.time() 34 | while(True): 35 | output_key = getkeys.get_key(getkeys.key_check())#按键收集 36 | if output_key == [1,1,1,1,1,1]: 37 | print(len(training_data)) 38 | np.save(file_name,training_data) 39 | break 40 | 41 | #printscreen = np.array(ImageGrab.grab(bbox=(window_size))) 42 | #printscreen_numpy = np.array(printscreen_pil.getdata(),dtype='uint8')\ 43 | #.reshape((printscreen_pil.size[1],printscreen_pil.size[0],3)) 44 | #pil格式耗时太长 45 | 46 | screen_gray = cv2.cvtColor(grabscreen.grab_screen(window_size),cv2.COLOR_BGR2GRAY)#灰度图像收集 47 | screen_reshape = cv2.resize(screen_gray,(96,86)) 48 | 49 | training_data.append([screen_reshape,output_key]) 50 | 51 | if len(training_data) % 500 == 0: 52 | print(len(training_data)) 53 | np.save(file_name,training_data) 54 | 55 | cv2.imshow('window1',screen_gray) 56 | #cv2.imshow('window3',printscreen) 57 | #cv2.imshow('window2',screen_reshape) 58 | 59 | #测试时间用 60 | print('loop took {} seconds'.format(time.time()-last_time)) 61 | last_time = time.time() 62 | 63 | 64 | if cv2.waitKey(5) & 0xFF == ord('q'): 65 | break 66 | cv2.waitKey()# 视频结束后,按任意键退出 67 | cv2.destroyAllWindows() 68 | -------------------------------------------------------------------------------- /getkeys.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Apr 8 12:03:44 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import win32api as wapi 9 | import time 10 | 11 | keyList = ["\b"] 12 | for char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789,.'£$/\\": 13 | keyList.append(char) 14 | 15 | def key_check(): 16 | keys = [] 17 | for key in keyList: 18 | if wapi.GetAsyncKeyState(ord(key)): 19 | keys.append(key) 20 | return keys 21 | 22 | def get_key(keys): 23 | ''' 24 | #W,A,S,D,V,J,M,K 25 | output = [0,0,0,0,0,0,0,0] 26 | if 'W' in keys: 27 | output[0] = 1 28 | elif 'A' in keys: 29 | output[1] = 1 30 | elif 'S' in keys: 31 | output[2] = 1 32 | elif 'D' in keys: 33 | output[3] = 1 34 | elif 'V' in keys: 35 | output[4] = 1 36 | elif 'J' in keys: 37 | output[5] = 1 38 | elif 'M' in keys: 39 | output[6] = 1 40 | elif 'K' in keys: 41 | output[7] = 1 42 | elif '8' in keys:#停止记录操作 43 | output = [1,1,1,1,1,1,1,1] 44 | ''' 45 | ''' 46 | #W,J,M,K,none 47 | output = [0,0,0,0,0] 48 | if 'W' in keys: 49 | output[0] = 1 50 | elif 'J' in keys: 51 | output[1] = 1 52 | elif 'M' in keys: 53 | output[2] = 1 54 | elif 'K' in keys: 55 | output[3] = 1 56 | elif '8' in keys:#停止记录操作 57 | output = [1,1,1,1] 58 | ''' 59 | 60 | #W,J,M,K,R,none R代替左shift,识破 61 | output = [0,0,0,0,0,0] 62 | if 'W' in keys: 63 | output[0] = 1 64 | elif 'J' in keys: 65 | output[1] = 1 66 | elif 'M' in keys: 67 | output[2] = 1 68 | elif 'K' in keys: 69 | output[3] = 1 70 | elif 'R' in keys: 71 | output[4] = 1 72 | elif '8' in keys:#停止记录操作 73 | output = [1,1,1,1,1,1] 74 | else: 75 | output[5] = 1 76 | 77 | return output 78 | 79 | if __name__ == '__main__': 80 | while True: 81 | if get_key(key_check()) != [1,1,1,1,1,1]: 82 | print(key_check()) 83 | else: 84 | break -------------------------------------------------------------------------------- /test_train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Apr 11 15:29:20 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import numpy as np 9 | from grabscreen import grab_screen 10 | import cv2 11 | import time 12 | import directkeys 13 | from Alexnet import alexnet2 14 | from getkeys import key_check 15 | import random 16 | 17 | WIDTH = 96 18 | HEIGHT = 86 19 | LR = 1e-3 20 | EPOCHS = 20 21 | MODEL_NAME = 'model_sekiro_1/py-sekiro-{}-{}-{}-epochs.model'.format(LR,'alexnetv2',EPOCHS) 22 | 23 | #w j m k none 24 | 25 | w = [1,0,0,0,0,0] 26 | j = [0,1,0,0,0,0] 27 | m = [0,0,1,0,0,0] 28 | k = [0,0,0,1,0,0] 29 | r = [0,0,0,0,1,0] 30 | n_choise = [0,0,0,0,0,1] 31 | 32 | model = alexnet2(WIDTH, HEIGHT, LR, output = 6) 33 | model.load(MODEL_NAME) 34 | 35 | window_size = (320,104,704,448)#384,224 192,112 96,86 36 | 37 | def main(): 38 | last_time = time.time() 39 | for i in list(range(5))[::-1]: 40 | print(i+1) 41 | time.sleep(1) 42 | 43 | paused = False 44 | while(True): 45 | 46 | if not paused: 47 | # 800x600 windowed mode 48 | screen = grab_screen(region=(window_size)) 49 | print('loop took {} seconds'.format(time.time()-last_time)) 50 | last_time = time.time() 51 | screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) 52 | screen = cv2.resize(screen, (WIDTH,HEIGHT)) 53 | 54 | prediction = model.predict([screen.reshape(WIDTH,HEIGHT,1)])[0] 55 | print(prediction) 56 | 57 | print("np后的预测:",np.argmax(prediction)) 58 | 59 | if np.argmax(prediction) == np.argmax(w): 60 | print("前") 61 | directkeys.go_forward() 62 | elif np.argmax(prediction) == np.argmax(j): 63 | print("攻击") 64 | directkeys.attack() 65 | elif np.argmax(prediction) == np.argmax(m): 66 | print("防御") 67 | directkeys.define() 68 | elif np.argmax(prediction) == np.argmax(k): 69 | print("跳") 70 | directkeys.jump() 71 | elif np.argmax(prediction) == np.argmax(r): 72 | print("识破") 73 | directkeys.dodge() 74 | elif np.argmax(prediction) == np.argmax(n_choise): 75 | print("不动") 76 | 77 | keys = key_check() 78 | 79 | # p pauses game and can get annoying. 80 | if 'T' in keys: 81 | if paused: 82 | paused = False 83 | time.sleep(1) 84 | else: 85 | paused = True 86 | ''' 87 | ReleaseKey(A) 88 | ReleaseKey(W) 89 | ReleaseKey(D) 90 | ''' 91 | time.sleep(1) 92 | if 'Y' in keys: 93 | if paused: 94 | paused = False 95 | time.sleep(1) 96 | else: 97 | paused = True 98 | ''' 99 | directkeys.ReleaseKey(J) 100 | directkeys.ReleaseKey(W) 101 | directkeys.ReleaseKey(M) 102 | directkeys.ReleaseKey(K) 103 | ''' 104 | time.sleep(1) 105 | break 106 | 107 | main() -------------------------------------------------------------------------------- /data_test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Apr 8 13:28:11 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import os 9 | import numpy as np 10 | import pandas as pd 11 | from collections import Counter 12 | from random import shuffle 13 | import cv2 14 | 15 | file_name = 'training_data.npy' 16 | if os.path.isfile(file_name): 17 | print("file exists , loading previous data") 18 | training_data = list(np.load(file_name,allow_pickle=True)) 19 | 20 | ''' 21 | w=[] 22 | a=[] 23 | s=[] 24 | d=[] 25 | v=[] 26 | j=[] 27 | m=[] 28 | k=[] 29 | n_choise=[] 30 | ''' 31 | 32 | w=[] 33 | j=[] 34 | m=[] 35 | k=[] 36 | r=[] 37 | n_choise=[] 38 | 39 | df = pd.DataFrame(training_data) 40 | print(df.head()) 41 | print(Counter(df[1].apply(str))) 42 | 43 | for data in training_data: #[img,choice] chose=[1,0,0,0,0,0] 44 | img = data[0] 45 | choise = data[1] 46 | ''' 47 | if choise[0] == 1: 48 | w.append([img,choise]) 49 | elif choise[1] == 1: 50 | a.append([img,choise]) 51 | elif choise[2] == 1: 52 | s.append([img,choise]) 53 | elif choise[3] == 1: 54 | d.append([img,choise]) 55 | elif choise[4] == 1: 56 | v.append([img,choise]) 57 | elif choise[5] == 1: 58 | j.append([img,choise]) 59 | elif choise[6] == 1: 60 | m.append([img,choise]) 61 | elif choise[7] == 1: 62 | k.append([img,choise]) 63 | elif choise == [0,0,0,0,0,0,0,0]: 64 | n_choise.append([img,choise]) 65 | ''' 66 | ''' 67 | if choise[0] == 1: 68 | w.append([img,choise]) 69 | elif choise[1] == 1: 70 | j.append([img,choise]) 71 | elif choise[2] == 1: 72 | m.append([img,choise]) 73 | elif choise[3] == 1: 74 | k.append([img,choise]) 75 | elif choise == [0,0,0,0]: 76 | n_choise.append([img,choise]) 77 | ''' 78 | 79 | if choise[0] == 1: 80 | w.append([img,choise]) 81 | elif choise[1] == 1: 82 | j.append([img,choise]) 83 | elif choise[2] == 1: 84 | m.append([img,choise]) 85 | elif choise[3] == 1: 86 | k.append([img,choise]) 87 | elif choise[4] == 1: 88 | r.append([img,choise]) 89 | elif choise[5] == 1: 90 | n_choise.append([img,choise]) 91 | 92 | length=len(m) 93 | #数据量按防御次数 94 | ''' 95 | w=w[:length] 96 | a=a[:length] 97 | s=s[:length] 98 | d=d[:length] 99 | v=v[:length] 100 | j=j[:length] 101 | m=m[:length] 102 | k=k[:length] 103 | n_choise=n_choise[:length] 104 | 105 | 106 | final_data = w+a+s+d+v+j+m+k+n_choise 107 | ''' 108 | 109 | w=w[:length] 110 | j=j[:length] 111 | m=m[:length] 112 | k=k[:length] 113 | r=r[:length] 114 | n_choise=n_choise[:length] 115 | 116 | 117 | final_data = w+j+m+k+r+n_choise 118 | shuffle(final_data) 119 | print(len(final_data)) 120 | np.save('training_data_2_v2_2.npy',final_data) 121 | 122 | 123 | ''' 124 | for data in training_data: 125 | img = data[0] 126 | choice = data[1] 127 | cv2.imshow('test',img) 128 | print(choice) 129 | if cv2.waitKey(20) & 0xFF == ord('q'): 130 | break 131 | cv2.waitKey()# 视频结束后,按任意键退出 132 | cv2.destroyAllWindows() 133 | ''' 134 | 135 | df = pd.DataFrame(final_data) 136 | print(df.head()) 137 | print(Counter(df[1].apply(str))) 138 | 139 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sekiro_tensorflow (English Version) 2 | This is the code of using machine learning to play Sekiro . 3 | 4 | Hello everyone , this is analoganddigital . I use this code to complete an interesting porgram of using machine learning to play Sekiro . 5 | You can see the final presentation in https://www.bilibili.com/video/BV1wC4y1s7oa/ . 6 | I am a junior student in university , which means I can't spend too much time on this program . What a shame ! On the other hand , many audiences hope me share this code . Thus , I eventually put it on the GitHub . 7 | This is an interesting program , and I hope everyone can enjoy it. In addition , I really welcome you to improve this program , to make this AI more smart ! 8 | There still something you need to konw: 9 | * The window size I set is 96*86 , you can change it by yourselves . 10 | * I finally collected 300M training data , if you want better result , maybe you need to collect more data . 11 | * I use Alexnet to finish the training . This program is depend on Supervised learning. 12 | * I have no idea about using Reinforcement learning yet , so I will really appreciate it if someone can help me to overcome this difficulty. 13 | * See the tutorial video for specific code usage , link : https://www.bilibili.com/video/BV1bz4y1R7kB 14 | 15 | Reference : 16 | https://github.com/Sentdex/pygta5/blob/master/LICENSE 17 | 18 | 19 | # Update for using DQN to play sekiro 2021.2.2 20 | I am very glad to tell that I have writen the codes of using DQN to play Sekiro . 21 | As is known to all , Supervised learning can only learn skills from the data we provide for it . However , this time by using Reinforcement Learning , we can see a more clever agent playing Sekiro . 22 | 23 | Reinforcement Learning can update its network by itself , using the reward feedback , which means we no longer need to collect our own data sets this time . All the data sets come from the real-time interaction between DQN network and the game. 24 | By using this DQN network (see below for a link) , you can fight any boss you want in the game . 25 | Code link for using DQN to play Sekiro : https://github.com/analoganddigital/DQN_play_sekiro 26 | 27 | There still something you need to know : 28 | * In order to shorten the time of restart the game , we need game modifier . 29 | * The link for game modifier : https://patch.ali213.net/showpatch/118405.html 30 | * See the tutorial video for specific code usage , link : https://www.bilibili.com/video/BV1by4y1n7pe/ 31 | 32 | Have fun ! 33 | 34 | # 用机器学习打只狼 (中文说明) 35 | 这就是b站上面用机器学习打只狼的代码。 36 | 37 | 各位观众大家好,我GitHub用户名是analoganddigital。我用这个程序完成了机器学习打只狼这个项目。 38 | 最终效果视频可以看b站https://www.bilibili.com/video/BV1wC4y1s7oa/ 。 39 | 我是一个大三学生,真的非常抱歉没能长时间更新这个项目,所以我把它放到了GitHub上面,之前很多观众也是私信我想要代码。 40 | 总之我还是希望大家能喜欢这个小项目吧。当然,我非常希望大家能帮忙完善这个程序,万分感激,大家共同讨论我们会获益更多,这其实就是开源的意义。现在由于代码比较基础,所以训练效果不太好。我相信大家会有更多的点子,如果能更新一点算法,我们将会看到一个更机智的AI。我很感谢大家对之前视频的支持(受宠若惊),也十分期待大家有趣的优化,就算没有优化直接用也可以。 41 | 还有一些细节我这声明一下: 42 | * 我截取的图像大小是96*86的,各位可以根据自身情况选择。 43 | * 我最终只收集了300M的数据,如果你想训练效果更好的话,可能要收集更多。 44 | * 我用的神经网络是Alexnet,基于监督学习完成的。 45 | * 由于我能力有限,我还没想好如何用强化学习优化算法,所以如果有大佬能分享一下自己的才华,那将十分感谢。 46 | * 具体代码使用方法请见我在b站上发布的机器学习打只狼的教程视频,链接: https://www.bilibili.com/video/BV1bz4y1R7kB 47 | 48 | 部分参考代码: 49 | https://github.com/Sentdex/pygta5/blob/master/LICENSE 50 | 51 | # 更新——强化学习DQN打只狼 2021.2.2 52 | 我非常高兴地告诉大家,我最近又开发出了用DQN强化学习打只狼的代码。 53 | 众所周知,监督学习只能学习到我们所提供的数据集的相关技能,但是利用强化学习,我们将看到一个完全不一样的只狼。 54 | 55 | 强化学习会根据reward奖励进行判断并且自己学习一种打斗方法。更重要的是,我们这次不再需要自己收集数据集了,所有更新数据均来自于DQN网络与游戏的实时交互。 56 | 利用这个DQN代码(链接见下方),你可以挑战只狼中任何一个boss,只要boss的血条位置不变即可(因为我采用的是图像抓取的方式获取只狼的血量与boss的血量进行reward判断)。 57 | DQN打只狼代码链接:https://github.com/analoganddigital/DQN_play_sekiro 58 | 59 | 然后还有一些注意事项: 60 | * 为了缩短只狼复活周期,在这个项目训练中,我们需要采用只狼的24项修改器,让只狼能够原地复活继续训练。 61 | * 修改器下载地址:https://patch.ali213.net/showpatch/118405.html 62 | * 具体代码使用方法请见我在b站上发布的DQN打只狼的教程视频,链接:https://www.bilibili.com/video/BV1by4y1n7pe/ 63 | 64 | 祝各位玩得愉快! 65 | -------------------------------------------------------------------------------- /Alexnet.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Apr 8 23:11:05 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | # alexnet.py 9 | 10 | import tflearn 11 | from tflearn.layers.conv import conv_2d, max_pool_2d 12 | from tflearn.layers.core import input_data, dropout, fully_connected 13 | from tflearn.layers.estimator import regression 14 | from tflearn.layers.normalization import local_response_normalization 15 | 16 | def alexnet(width, height, lr, output=6): 17 | network = input_data(shape=[None, width, height, 1], name='input') 18 | network = conv_2d(network, 96, 11, strides=4, activation='relu') 19 | network = max_pool_2d(network, 3, strides=2) 20 | network = local_response_normalization(network) 21 | network = conv_2d(network, 256, 5, activation='relu') 22 | network = max_pool_2d(network, 3, strides=2) 23 | network = local_response_normalization(network) 24 | network = conv_2d(network, 384, 3, activation='relu') 25 | network = conv_2d(network, 384, 3, activation='relu') 26 | network = conv_2d(network, 256, 3, activation='relu') 27 | network = max_pool_2d(network, 3, strides=2) 28 | network = local_response_normalization(network) 29 | network = fully_connected(network, 4096, activation='tanh') 30 | network = dropout(network, 0.5) 31 | network = fully_connected(network, 4096, activation='tanh') 32 | network = dropout(network, 0.5) 33 | network = fully_connected(network, output, activation='softmax') 34 | network = regression(network, optimizer='momentum', 35 | loss='categorical_crossentropy', 36 | learning_rate=lr, name='targets') 37 | 38 | model = tflearn.DNN(network, checkpoint_path='model_alexnet', 39 | max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir='log1') 40 | 41 | return model 42 | 43 | 44 | def alexnet2(width, height, lr, output=6): 45 | network = input_data(shape=[None, width, height, 1], name='input') 46 | network = conv_2d(network, 96, 11, strides=4, activation='relu') 47 | network = max_pool_2d(network, 3, strides=2) 48 | network = local_response_normalization(network) 49 | network = conv_2d(network, 256, 5, activation='relu') 50 | network = max_pool_2d(network, 3, strides=2) 51 | network = local_response_normalization(network) 52 | network = conv_2d(network, 384, 3, activation='relu') 53 | network = conv_2d(network, 384, 3, activation='relu') 54 | network = conv_2d(network, 256, 3, activation='relu') 55 | network = max_pool_2d(network, 3, strides=2) 56 | network = conv_2d(network, 256, 5, activation='relu') 57 | network = max_pool_2d(network, 3, strides=2) 58 | network = local_response_normalization(network) 59 | network = conv_2d(network, 384, 3, activation='relu') 60 | network = conv_2d(network, 384, 3, activation='relu') 61 | network = conv_2d(network, 256, 3, activation='relu') 62 | network = max_pool_2d(network, 3, strides=2) 63 | network = local_response_normalization(network) 64 | network = fully_connected(network, 4096, activation='tanh') 65 | network = dropout(network, 0.5) 66 | network = fully_connected(network, 4096, activation='tanh') 67 | network = dropout(network, 0.5) 68 | network = fully_connected(network, 4096, activation='tanh') 69 | network = dropout(network, 0.5) 70 | network = fully_connected(network, 4096, activation='tanh') 71 | network = dropout(network, 0.5) 72 | network = fully_connected(network, output, activation='softmax') 73 | network = regression(network, optimizer='momentum', 74 | loss='categorical_crossentropy', 75 | learning_rate=lr, name='targets') 76 | 77 | model = tflearn.DNN(network, checkpoint_path='model_alexnet/', 78 | max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir='log1') 79 | 80 | return model -------------------------------------------------------------------------------- /directkeys.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Apr 8 10:37:50 2020 4 | 5 | @author: analoganddigital ( GitHub ) 6 | """ 7 | 8 | import ctypes 9 | import time 10 | 11 | SendInput = ctypes.windll.user32.SendInput 12 | 13 | 14 | W = 0x11 15 | A = 0x1E 16 | S = 0x1F 17 | D = 0x20 18 | 19 | M = 0x32 20 | J = 0x24 21 | K = 0x25 22 | LSHIFT = 0x2A 23 | R = 0x13#用R代替识破 24 | V = 0x2F 25 | 26 | Q = 0x10 27 | I = 0x17 28 | O = 0x18 29 | P = 0x19 30 | C = 0x2E 31 | 32 | # C struct redefinitions 33 | PUL = ctypes.POINTER(ctypes.c_ulong) 34 | class KeyBdInput(ctypes.Structure): 35 | _fields_ = [("wVk", ctypes.c_ushort), 36 | ("wScan", ctypes.c_ushort), 37 | ("dwFlags", ctypes.c_ulong), 38 | ("time", ctypes.c_ulong), 39 | ("dwExtraInfo", PUL)] 40 | 41 | class HardwareInput(ctypes.Structure): 42 | _fields_ = [("uMsg", ctypes.c_ulong), 43 | ("wParamL", ctypes.c_short), 44 | ("wParamH", ctypes.c_ushort)] 45 | 46 | class MouseInput(ctypes.Structure): 47 | _fields_ = [("dx", ctypes.c_long), 48 | ("dy", ctypes.c_long), 49 | ("mouseData", ctypes.c_ulong), 50 | ("dwFlags", ctypes.c_ulong), 51 | ("time",ctypes.c_ulong), 52 | ("dwExtraInfo", PUL)] 53 | 54 | class Input_I(ctypes.Union): 55 | _fields_ = [("ki", KeyBdInput), 56 | ("mi", MouseInput), 57 | ("hi", HardwareInput)] 58 | 59 | class Input(ctypes.Structure): 60 | _fields_ = [("type", ctypes.c_ulong), 61 | ("ii", Input_I)] 62 | 63 | # Actuals Functions 64 | 65 | def PressKey(hexKeyCode): 66 | extra = ctypes.c_ulong(0) 67 | ii_ = Input_I() 68 | ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) ) 69 | x = Input( ctypes.c_ulong(1), ii_ ) 70 | ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x)) 71 | 72 | def ReleaseKey(hexKeyCode): 73 | extra = ctypes.c_ulong(0) 74 | ii_ = Input_I() 75 | ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) ) 76 | x = Input( ctypes.c_ulong(1), ii_ ) 77 | ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x)) 78 | 79 | 80 | def define(): 81 | PressKey(M) 82 | time.sleep(0.05) 83 | ReleaseKey(M) 84 | #time.sleep(0.1) 85 | 86 | def attack(): 87 | PressKey(J) 88 | time.sleep(0.05) 89 | ReleaseKey(J) 90 | #time.sleep(0.1) 91 | 92 | def go_forward(): 93 | PressKey(W) 94 | time.sleep(0.3) 95 | ReleaseKey(W) 96 | 97 | def go_back(): 98 | PressKey(S) 99 | time.sleep(0.1) 100 | ReleaseKey(S) 101 | 102 | def go_left(): 103 | PressKey(A) 104 | time.sleep(0.1) 105 | ReleaseKey(A) 106 | 107 | def go_right(): 108 | PressKey(D) 109 | time.sleep(0.1) 110 | ReleaseKey(D) 111 | 112 | def jump(): 113 | PressKey(K) 114 | time.sleep(0.1) 115 | ReleaseKey(K) 116 | #time.sleep(0.1) 117 | 118 | def dodge():#闪避 119 | PressKey(R) 120 | time.sleep(0.1) 121 | ReleaseKey(R) 122 | #time.sleep(0.1) 123 | 124 | def lock_vision(): 125 | PressKey(V) 126 | time.sleep(0.1) 127 | ReleaseKey(V) 128 | time.sleep(0.1) 129 | 130 | if __name__ == '__main__': 131 | time.sleep(5) 132 | time1 = time.time() 133 | while(True): 134 | if abs(time.time()-time1) > 5: 135 | break 136 | else: 137 | PressKey(M) 138 | time.sleep(0.1) 139 | ReleaseKey(M) 140 | time.sleep(0.2) 141 | 142 | 143 | PressKey(W) 144 | time.sleep(0.4) 145 | ReleaseKey(W) 146 | time.sleep(1) 147 | 148 | PressKey(J) 149 | time.sleep(0.1) 150 | ReleaseKey(J) 151 | time.sleep(1) --------------------------------------------------------------------------------