forked from arpit-kalla/AI_Gaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI_Haxball.py
263 lines (206 loc) · 7.68 KB
/
AI_Haxball.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import threading
import time
import cv2
import numpy as np
from mss import mss
import math
import pyautogui
import random
player_center,player_radius, player_speed,player_acc = [],0,[],0.0
ball_center,ball_radius,ball_speed,player_acc = [],0,[],0.0
delta_time = 1
# Shows an Image in a new window
def show_image(image):
cv2.imshow(str(random.randint(1,101)), image)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
# Finds the distance between two points
def find_dist(pos1,pos2):
x1,y1 = pos1[0],pos1[1]
x2,y2 = pos2[0],pos2[1]
return math.sqrt((x2-x1)**2+(y2-y1)**2)
# Enter a RGB color and the funtion returns the HSV equivalent
def rgb2hsv(rgb):
r,g,b = rgb
r_prime = r/255
g_prime = g/255
b_prime = b/255
Cmax = max(r_prime,g_prime,b_prime)
Cmin = min(r_prime,g_prime,b_prime)
delta = Cmax-Cmin
if delta == 0:
h = 0
elif Cmax == r_prime:
h = 60*(((g_prime-b_prime)/delta)%6)
elif Cmax == g_prime:
h = 60*(((b_prime-r_prime)/delta)+2)
elif Cmax == b_prime:
h = 60*(((r_prime-g_prime)/delta)+4)
if Cmax == 0:
s = 0
else:
s = delta/Cmax
v = Cmax
return h,int(s*255),int(v*255)
#Converts the coordinates to equivalent slope and intercept
def get_slope_bias(coords):
x1 =coords[0]
y1 =coords[1]
x2 =coords[2]
y2 =coords[3]
isVertical = False
if not -(1e-4)< x2-x1 < 1e-4 :
m = (y2-y1)/(x2-x1)
b = y1-m*x1
else:
isVertical = True
m = 0
b = x1
return m,b,isVertical
#Enter a image and color and it filters the objects of that color in that image
def color_filter(image,color,pure):
hsvImg = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsv_color = np.array(rgb2hsv(color))
if pure == False:
delta = np.array([10,100,100])
else:
delta = np.array([10,10,10])
lower = hsv_color - delta
upper = hsv_color + delta
mask = cv2.inRange(hsvImg, lower, upper)
res = cv2.bitwise_and(image,image, mask= mask)
return res
# Get information of the Player
def get_player(image):
try:
f_img = color_filter(image,[255,0,0],False)
f_img = cv2.cvtColor(f_img, cv2.COLOR_BGR2GRAY)
im2 ,contours,hierarchy = cv2.findContours(f_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
c = contours[0]
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
(x,y),radius = cv2.minEnclosingCircle(c)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(image, center, radius, (255, 0, 0), 2)
player_center.append(center)
player_radius = radius
except:
pass
# Get information of the Ball
def get_ball(image):
try:
f_img = color_filter(image,[255,255,255],True)
f_img = cv2.cvtColor(f_img, cv2.COLOR_BGR2GRAY)
im2 ,contours,hierarchy = cv2.findContours(f_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
if cv2.contourArea(c)>60:
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
(x,y),radius = cv2.minEnclosingCircle(c)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(image, center, radius, (255, 0, 0), 2)
ball_center.append(center)
ball_radius = radius
except:
pass
# Prints text on the Image
def show_data(image):
if len(ball_center)>1:
cv2.putText(image, "Ball Center: {},{}".format(ball_center[-1][0],ball_center[-1][1]), (200, 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,0,0), 1)
ball_speed.append(find_dist(ball_center[-1],ball_center[-2])/delta_time)
cv2.putText(image, "Ball Speed: {}".format(ball_speed[-1]), (200, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,0,0), 1)
if len(player_center)>1:
pX2,pY2 = player_center[-1][0],player_center[-1][1]
pX1, pY1 = player_center[-2][0], player_center[-2][1]
cv2.putText(image, "Player Center: {},{}".format(pX2,pY2), (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,0,0), 1)
player_speed.append([(pX2-pX1)/delta_time, (pY2-pY1)/delta_time])
print("Player Speed: <{},{}>".format(player_speed[-1][0],player_speed[-1][1]))
cv2.putText(image, "Player Speed: <{},{}>".format(player_speed[-1][0],player_speed[-1][1]), (15, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,0,0), 1)
# if len(player_speed)>1:
# player_acc = abs(player_speed[-1]-player_speed[-2])/delta_time
# cv2.putText(image, "Player Acc: {}".format(player_acc), (15, 100),
# cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,0,0), 1)
# if len(ball_speed)>1:
# ball_acc = abs(ball_speed[-1]-ball_speed[-2])/delta_time
# cv2.putText(image, "Ball Acc: {}".format(ball_acc), (200, 100),
# cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,0,0), 1)
#All Processing of the image is done here
def process_image(image):
get_player(image)
get_ball(image)
show_data(image)
# Grab Screenshots of the Screen
def get_screen():
sct = mss()
while 1:
last_time = time.time()
monitor = {'top': 230, 'left': 230, 'width': 820, 'height': 400}
img = np.array(sct.grab(monitor))
img = cv2.resize(img, (int(monitor['width']/3),int(monitor['height']/3)))
processed_img = process_image(img)
delta_time = time.time()-last_time
# print('{} FPS'.format(1/delta_time))
last_time = time.time()
cv2.imshow('image', img)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
def emulate():
epsilon = 1e-5
startTime = time.time()
x,y,buffer = 276,132,40
pressKeys = {"w": 0.0, "a": 0.0, "s": 0.0, "d": 0.0}
while True:
if len(player_center)>0 and len(ball_center)>0 and len(player_speed)>0:
pX,pY,pR = player_center[-1][0], player_center[-1][1], player_radius
pVx,pVy = abs(player_speed[-1][0]),abs(player_speed[-1][1])
bX,bY,bR = ball_center[-1][0], ball_center[-1][1], ball_radius
if pX-x>buffer:
print("a")
pressKeys["a"] = (pX-x)/(pVx+epsilon)
elif x-pX>buffer:
print("d")
pressKeys["d"] = (x - pX) / (pVx + epsilon)
else:
pressKeys["a"] = 0.0
pressKeys["d"] = 0.0
if pY-y>buffer:
print("w")
pressKeys["w"] = (pY - y) / (pVy + epsilon)
elif y-pY>buffer:
print("s")
pressKeys["s"] = (y - pY) / (pVy + epsilon)
else:
pressKeys["w"] = 0.0
pressKeys["s"] = 0.0
currTime = time.time()
if currTime-startTime < pressKeys["w"]:
pyautogui.keyDown("w")
else:
pyautogui.keyUp("w")
if currTime-startTime < pressKeys["s"]:
pyautogui.keyDown("s")
else:
pyautogui.keyUp("s")
if currTime-startTime < pressKeys["a"]:
pyautogui.keyDown("a")
else:
pyautogui.keyUp("a")
if currTime-startTime < pressKeys["d"]:
pyautogui.keyDown("d")
else:
pyautogui.keyUp("d")
for i in range(3)[-1:]:
print(i)
time.sleep(1)
emulator = threading.Thread(target = emulate)
emulator.daemon = True
emulator.start()
get_screen()