forked from jaspreetbhamra/Agent-GPY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g.py
393 lines (297 loc) · 13 KB
/
g.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import numpy as np
# from PIL import ImageGrab
import argparse
import cv2
import pyvjoy
import sys
import recognizer
import grabscreen
import threading
import pyautogui as gui
import directInput
from time import sleep
###################################################################### Constants
WINDOW_START_X = 0
WINDOW_START_Y = 0
WINDOW_WIDTH = 1920
WINDOW_HEIGHT = 1080
MAX_VJOY = 32767
MID_VJOY = 16383
###################################################################### Constants
ap = argparse.ArgumentParser()
ap.add_argument("-t", "--team", required=True, help="team to deploy GPY in")
args = vars(ap.parse_args())
team = args["team"]
###################################################################### Screen Capture
def process_img(image):
original_image = image
# convert to gray
processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# edge detection
processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300)
return processed_img
def capture():
while True:
screen = np.array(ImageGrab.grab(bbox=(WINDOW_START_X, WINDOW_START_Y, WINDOW_START_X+WINDOW_WIDTH, WINDOW_START_Y+WINDOW_HEIGHT)))
new_screen = process_img(screen)
cv2.imshow('window', new_screen)
cv2.imshow('window', cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
###################################################################### Screen Capture
###################################################################### Controller
controller = pyvjoy.VJoyDevice(1)
def square_in(startX, startY, endX, endY, windowCenterX, windowCenterY):
midTargetX = (startX + endX) / 2
midTargetY = (startY + endY) / 2
diffX = midTargetX - windowCenterX
diffY = midTargetY - windowCenterY
# print("diffX = ", diffX)
# print("diffY = ", diffY)
# return
if diffY < 0:
verticalBias = -1
tempDiffY = -diffY
else:
verticalBias = 1
tempDiffY = diffY
if diffX < 0:
horizontalBias = -1
tempDiffX = -diffX
else:
horizontalBias = 1
tempDiffX = diffX
if tempDiffX == 0 and tempDiffY == 0:
controller.data.wAxisX = MID_VJOY
controller.data.wAxisY = MID_VJOY
controller.update()
return
if tempDiffX > tempDiffY:
axisRatio = tempDiffY / tempDiffX
controller.data.wAxisX = MID_VJOY + int(MID_VJOY * horizontalBias)
controller.data.wAxisY = MID_VJOY + int((MID_VJOY * axisRatio) * verticalBias)
controller.update()
else:
axisRatio = tempDiffX / tempDiffY
controller.data.wAxisX = MID_VJOY + int((MID_VJOY * axisRatio) * horizontalBias)
controller.data.wAxisY = MID_VJOY + int(MID_VJOY * verticalBias)
controller.update()
print("controller.data.wAxisX = {:.0f}%".format((controller.data.wAxisX - MID_VJOY) / MAX_VJOY * 200))
print("controller.data.wAxisY = {:.0f}%".format((controller.data.wAxisY - MID_VJOY) / MAX_VJOY * 200))
###################################################################### Controller
###################################################################### flick_movement
def flick_movement(startX, startY, endX, endY):
# distance = (startX+endX)
update_time = gui.MINIMUM_DURATION
positionAdjustment = 30
positionX = (startX+endX)/2
positionY = startY + 0.4*(endY - startY)
print ('Moving to target location: ', positionX, positionY)
sourceX = gui.position()[0]
sourceY = gui.position()[1]
# gui.moveTo((startX+endX)/2, (startY+endY)/2, duration=0.0)
# gui.click(x=positionX, y=positionY, interval=update_time)
# gui.moveTo(positionX, positionY, gui.MINIMUM_DURATION, gui.easeOutQuad)
gui.moveTo(positionX, positionY, 0.0)
# gui.moveTo(positionX, positionY, 0.0)
# sleep(1)
# if (startX>sourceX and startY<sourceY):
# gui.moveTo(positionX-positionAdjustment, positionY+positionAdjustment, update_time, gui.easeOutQuad)
# print ('1')
# elif (startX>sourceX and startY>sourceY):
# gui.moveTo(positionX-positionAdjustment, positionY-positionAdjustment, update_time, gui.easeOutQuad)
# print ('2')
# elif (startX<sourceX and startY>sourceY):
# gui.moveTo(positionX+positionAdjustment, positionY-positionAdjustment, update_time, gui.easeOutQuad)
# print ('3')
# else:
# gui.moveTo(positionX+positionAdjustment, positionY+positionAdjustment, update_time, gui.easeOutQuad)
# print ('4')
print("Intermediate mouse pointer location", gui.position())
# gui.moveTo(positionX, positionY, update_time)
gui.click()
gui.click()
gui.click()
gui.click()
gui.click()
# print("Current mouse pointer location", gui.position())
###################################################################### flick_movement
###################################################################### Methods for Navigation
def draw_lines(img, lines):
for line in lines:
coords = line[0]
cv2.line(img, (coords[0], coords[1]), (coords[2], coords[3]), 255, 3)
cv2.circle(img, (coords[0], coords[1]), 10, 255, 2)
coords = lines[0][0]
cv2.line(img, (coords[0], coords[1]), (coords[2], coords[3]), 255, 20)
def draw_processed_lines(img, procLines):
for line in procLines:
# print("{}".format(line))
cv2.line(img, (line[0],line[1]), (line[2],line[3]), 255, 3)
cv2.circle(img, (line[0], line[1]), 10, 255, 2)
def roi(img, vertices):
mask = np.zeros_like(img)
cv2.fillPoly(mask, vertices, 255)
masked = cv2.bitwise_and(img, mask)
return masked
def processLines(lines):
M_TRES = 0.5
C_TRES = 0.5
MAX_LINES = 8
metaLines = []
newLines = []
i_newLines = -1
try:
# Store lines first
for line in lines:
x1,y1,x2,y2 = line[0]
# print('//////////\n\n{} {} {} {}'.format(x1,y1,x2,y2))
if x2-x1 == 0:
m = 999999
else:
m = (y2-y1) / (x2-x1)
c = y2 - m*x2
if m == 0:
if x1 < x2:
metaLines.append([x1,y1,x2,y2,m,c,False])
else:
metaLines.append([x2,y2,x1,y1,m,c,False])
else:
if y1 < y2:
metaLines.append([x2,y2,x1,y1,m,c,False])
else:
metaLines.append([x1,y1,x2,y2,m,c,False])
# print("{}".format(metaLines))
for i in range(MAX_LINES):
breaker = True
for metaLine in metaLines:
if not metaLine[6]:
breaker = False
newLines.append(metaLine)
i_newLines += 1
metaLine[6] = True
break
if breaker:
break
for metaLine in metaLines:
if not metaLine[6]:
if (
metaLine[4] < newLines[i_newLines][4]*(1+M_TRES) and
metaLine[4] > newLines[i_newLines][4]*(1-M_TRES) and
metaLine[5] < newLines[i_newLines][5]*(1+C_TRES) and
metaLine[5] > newLines[i_newLines][5]*(1-C_TRES)
):
# Visited
metaLine[6] = True
# if not vertical
if metaLine[2] - metaLine[0] != 0:
if newLines[i_newLines][1] < metaLine[1]:
newLines[i_newLines][1] = metaLine[1]
newLines[i_newLines][0] = metaLine[0]
if newLines[i_newLines][3] > metaLine[3]:
newLines[i_newLines][3] = metaLine[3]
newLines[i_newLines][2] = metaLine[2]
return newLines
except TypeError:
print("No lines found!")
return None
def findLineLength(line):
diffx = line[2] - line[0]
diffy = line[3] - line[1]
return np.sqrt(diffx*diffx + diffy*diffy)
###################################################################### Methods for Navigation
###################################################################### Main
# scannerThread = threading.Thread(target=ObjectDetectionDeepLearning.deep_learning_object_detection.runDeepLearningObjectDetection)
# scannerThread.start()
myVar = 0
tres1 = 160
tres_gap = 10
minLineLength = 40
maxLineGap = 5
threshold = 1
while True:
myVar = myVar +1
screen = cv2.cvtColor(np.array(grabscreen.grab_screen(region=(0, 30, 800, 540))), cv2.COLOR_BGR2RGB)
startX, startY, endX, endY = recognizer.recognize(screen, args["team"])
if startX != -1 and startY != -1:
# for objectCoord in objectArray:
# print(objectCoord[1], objectCoord[2], objectCoord[3], objectCoord[4])
# square_in(objectCoord[1], objectCoord[2], objectCoord[3], objectCoord[4], WINDOW_START_X+(WINDOW_WIDTH/2), WINDOW_START_Y+(WINDOW_HEIGHT/2))
# flickMovementThread = threading.Thread(target=flick_movement, args=[startX, startY, endX, endY])
# flickMovementThread.start()
print ("Shoot him!!!!!!!!!!")
flick_movement(startX, startY, endX, endY)
# flick_movement(objectCoord[1], objectCoord[2], objectCoord[3], objectCoord[4])
# break
# print('-------------------------------------------------- ', myVar)
else:
processedScreen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
processedScreen = cv2.equalizeHist(processedScreen)
bg_img = cv2.dilate(processedScreen, np.ones((7,7), np.uint8))
processedScreen = 255 - cv2.absdiff(processedScreen, bg_img)
norm_img = processedScreen.copy()
cv2.normalize(processedScreen, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
tempTres = 200
processedScreen = cv2.Canny(processedScreen, tempTres, tempTres+1, L2gradient=False)
# vertices = np.array([
# [0,320],
# [0,670],
# [780,670],
# [780,460],
# [860,460],
# [1140,640],
# [1270,640],
# [1270,320]])
vertices = np.array([
[0,266],
[0,558],
[491,558],
[491,350],
[542,350],
[718,520],
[800,520],
[800,266]])
processedScreen = roi(processedScreen, [vertices])
lines = cv2.HoughLinesP(processedScreen, 1, np.pi/180, 100, 100, minLineLength, maxLineGap)
# if startX != -1 and startY != -1:
# # for objectCoord in objectArray:
# # print(objectCoord[1], objectCoord[2], objectCoord[3], objectCoord[4])
# # square_in(objectCoord[1], objectCoord[2], objectCoord[3], objectCoord[4], WINDOW_START_X+(WINDOW_WIDTH/2), WINDOW_START_Y+(WINDOW_HEIGHT/2))
# # flickMovementThread = threading.Thread(target=flick_movement, args=[startX, startY, endX, endY])
# # flickMovementThread.start()
# print ("Shoot him!!!!!!!!!!")
# flick_movement(startX, startY, endX, endY)
# # flick_movement(objectCoord[1], objectCoord[2], objectCoord[3], objectCoord[4])
# # break
# # print('-------------------------------------------------- ', myVar)
if not (lines is None):
# draw_lines(processedScreen, lines)
newLines = processLines(lines)
draw_processed_lines(processedScreen, newLines)
for newLine in newLines:
if findLineLength(newLine) > 400:
ALMOST_FLAT_SLOPE = 0.05
m_pos = gui.position()
# print(newLine)
if newLine[4] > 0:
if newLine[4] < ALMOST_FLAT_SLOPE:
# print("//////////////////////////////\nSlope > 0!")
gui.moveTo(m_pos[0]-100)
elif newLine[4] < 0:
if newLine[4] > -ALMOST_FLAT_SLOPE:
# print("//////////////////////////////\nSlope < 0!")
gui.moveTo(m_pos[0]+100)
# if newLine[0] < 635 and newLine[1] > 356 and newLine[4] < 0:
if newLine[0] < 529 and newLine[1] > 297 and newLine[4] < 0:
directInput.HoldKey(directInput.W, 0.2)
break
# # if newLine[0] > 653 and newLine[1] > 356 and newLine[4] > 0:
if newLine[0] > 529 and newLine[1] > 297 and newLine[4] > 0:
directInput.HoldKey(directInput.W, 0.2)
break
else:
gui.moveTo(500)
cv2.imshow("TestPyWindow", processedScreen)
if cv2.waitKey(1) and 0xFF == ord('q'):
cv2.destroyAllWindows()