-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.py
133 lines (105 loc) · 3.59 KB
/
snake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import cv2
import torch
from torchvision.models.efficientnet import efficientnet_v2_s
import keyboard
from PIL import Image, ImageGrab
import numpy as np
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize, InterpolationMode
from tqdm import tqdm
model = efficientnet_v2_s()
model.classifier = torch.nn.Sequential(torch.nn.Dropout(p=0.2, inplace=True), torch.nn.Linear(in_features = 1280, out_features = 5))
model.load_state_dict(torch.load("models/06_07_2023-22_25_07_effnet_v2_s_0.0007_35.pth"))
model.to("cuda")
model.eval()
label_keys= {
0: "",
1 :"left",
2: "up",
3: "right",
4: "down"
}
transformer = Compose([
Resize([64,64], interpolation = InterpolationMode.BILINEAR),
CenterCrop(64),
Normalize(mean =[0.485, 0.456, 0.406], std =[0.229, 0.224, 0.225])
])
def generator():
while(not keyboard.is_pressed("esc")):
yield
for _ in tqdm(generator()):
image = ImageGrab.grab(bbox = (685,350,1235,840))
image = ToTensor()(image)
image = image.to("cuda")
image = transformer(image)
outputs = model(image[None , ...])
preds = torch.softmax(outputs, dim=1).argmax(dim = 1)
# print(label_keys[preds.item()])
if preds.item() != 0:
# print(label_keys[preds.item()])
keyboard.press_and_release(label_keys[preds.item()])
# import threading
# import time
# c = threading.Condition()
# flag = 0 #shared between Thread_A and Thread_B
# image:any
# class Thread_A(threading.Thread):
# def __init__(self, name):
# threading.Thread.__init__(self)
# self.name = name
# def run(self):
# global flag
# global image #made global here
# while True:
# c.acquire()
# if flag == 0:
# flag = 1
# image = ImageGrab.grab(bbox = (685,350,1235,840))
# c.notify_all()
# else:
# c.wait()
# c.release()
# class Thread_B(threading.Thread):
# def __init__(self, name):
# threading.Thread.__init__(self)
# self.name = name
# def run(self):
# global flag
# global image #made global here
# while True:
# c.acquire()
# if flag == 1:
# flag = 0
# image = ToTensor()(image)
# image = image.to("cuda")
# image = transformer(image)
# outputs = model(image[None , ...])
# preds = torch.softmax(outputs, dim=1).argmax(dim = 1)
# if preds.item() != 0:
# # print(label_keys[preds.item()])
# keyboard.press_and_release(label_keys[preds.item()])
# c.notify_all()
# else:
# c.wait()
# c.release()
# a = Thread_A("myThread_name_A")
# b = Thread_B("myThread_name_B")
# for _ in tqdm(generator()):
# b.start()
# a.start()
# a.join()
# b.join()
# from threading import Thread
# def modify_image(image):
# image = ToTensor()(image)
# image = image.to("cuda")
# image = transformer(image)
# outputs = model(image[None , ...])
# preds = torch.softmax(outputs, dim=1).argmax(dim = 1)
# if preds.item() != 0:
# # print(label_keys[preds.item()])
# keyboard.press_and_release(label_keys[preds.item()])
# for _ in tqdm(generator()):
# image = ImageGrab.grab(bbox = (685,350,1235,840))
# t = Thread(target=modify_image, args=(image, ))
# t.start()
# t.join()