-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
204 lines (165 loc) · 9.9 KB
/
main.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
from pyautogui import *
from time import time
import time
import cv2 as cv
import torch
import util
from angle_detection import get_arrow_angle
from choose_action import ChooseAction
from rect_detection import detect_rectangle, get_rectangle
from util import list_window_names, cv2_to_pil, pil_to_cv2, trim, compare_and_resize_images, match_template, \
is_game_started, is_handle_found, delete_image, is_players_turn, draw_soccer_ball_rectangle, get_soccer_ball_click_point, calculate_target_point, perform_drag_action
from window_capture import WindowCapture
from object_detection import ObjectDetection
from save_element_screenshot import save_element_screenshot
from environment import Environment
from soccer_ball_detection import get_soccer_ball_position
class GameAnalyzer:
def __init__(self, window_name, soccer_ball_model, arrow_model):
self.methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR', 'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
self.method = self.methods[1]
self.window_name = window_name
self.player_image_path = "images/player.jpg"
self.opponent_image_path = 'images/opponent.jpg'
self.player_handle_image_path = "images/player_handle.jpg" # "images/paris.jpg"
self.window_capture = None
self.obj_detc_player = None
self.obj_detc_opponent = None
self.obj_detc_opponent_goal = None
self.obj_detc_player_goal = None
# self.obj_detc_ball = None
self.player_goal_rectangle = None
self.opponent_goal_rectangle = None
self.player_goal_position = None
self.opponent_goal_position = None
self.red_rgb_code = (102, 102, 255)
self.orange_rgb_code = (102, 178, 255)
self.yellow_rgb_code = (102, 255, 255)
self.green_rgb_code = (102, 255, 178)
self.blue_rgb_code = (255, 178, 102)
self.purple_rgb_code = (255, 102, 178)
self.pink_rgb_code = (178, 102, 255)
# self.player_turn_conf = (self.player_handle_image_path, 0.2, cv.TM_SQDIFF_NORMED, True)
self.player_turn_conf = (0, 0, None, None)
self.playground = (0, 0, 0, 0)
self.get_state = True
self.game_state = None
self.opponent_radius = 0
self.player_radius = 0
self.ball_radius = 0
self.radius = 0
self.soccer_ball_model = soccer_ball_model
self.arrow_model = arrow_model
self.parameters = None
def initialize(self):
while True:
if is_game_started():
break
else:
print("Game has not started yet.")
self.window_capture = WindowCapture(self.window_name)
sc = self.window_capture.get_screenshot()
x, y, w, h = get_rectangle(sc)
self.playground = (x, y, w, h)
self.obj_detc_player_goal = ObjectDetection('images/player_goal.jpg', self.method, self.purple_rgb_code)
self.obj_detc_opponent_goal = ObjectDetection('images/opponent_goal.jpg', self.method, self.blue_rgb_code)
self.player_goal_rectangle = self.obj_detc_player_goal.find_objects(sc, 0.7)
self.opponent_goal_rectangle = self.obj_detc_opponent_goal.find_objects(sc, 0.7)
self.player_goal_position = self.player_goal_rectangle[0]
self.opponent_goal_position = self.opponent_goal_rectangle[0]
self.init_players()
self.obj_detc_player = ObjectDetection('images/player.jpg', self.method, self.green_rgb_code)
self.obj_detc_opponent = ObjectDetection('images/opponent.jpg', self.method, self.red_rgb_code)
self.parameters = util.get_environment_parameters(3)
def init_players(self):
sc = self.window_capture.get_screenshot()
pil_sc = cv2_to_pil(sc)
trimmed_sc = trim(pil_sc)
sc = pil_to_cv2(trimmed_sc)
height, width, _ = sc.shape
self.opponent_radius = save_element_screenshot(sc, "opponent", self.playground[0] + self.playground[2] // 2, self.playground[1], self.playground[2] // 2, self.playground[3])
self.player_radius = save_element_screenshot(sc, "player", self.playground[0], self.playground[1], self.playground[2] // 2, self.playground[3])
self.ball_radius = save_element_screenshot(sc, "ball", self.playground[0] + self.playground[2] // 2 - (self.playground[2] // 26), self.playground[1], self.playground[2] // 13, self.playground[3])
self.radius = min(self.opponent_radius, self.player_radius)
compare_and_resize_images(self.player_image_path, self.opponent_image_path)
def run(self):
self.initialize()
count = 0
while True:
sc = self.window_capture.get_screenshot()
# checking whether it is our player's turn or not
height, width, _ = sc.shape
area_x = self.playground[0]
area_y = 0
area_width = self.playground[2] // 3
area_height = height // 4
self.player_turn_conf = (area_x, area_y, area_width, area_height)
if is_players_turn(sc, self.player_turn_conf):
if self.get_state is True:
self.get_state = False
count += 1
print("getting game state ...")
player_rectangles = self.obj_detc_player.find_objects(sc, 0.7)
opponent_rectangles = self.obj_detc_opponent.find_objects(sc, 0.7)
ball_rectangle = get_soccer_ball_position(self.soccer_ball_model, sc)
players_position = ObjectDetection.get_click_points(player_rectangles)
opponents_position = ObjectDetection.get_click_points(opponent_rectangles)
# ball_position = ObjectDetection.get_click_points(ball_rectangle)
ball_position = get_soccer_ball_click_point(ball_rectangle)
self.game_state = (players_position, opponents_position, ball_position, tuple(self.player_goal_position), tuple(self.opponent_goal_position), tuple(self.playground))
player_radius, player_mass, player_elasticity, ball_radius, ball_mass, ball_elasticity, walls_thickness, walls_elasticity, max_force, _, _ = self.parameters
# Setup Environment
env = Environment(self.game_state, player_radius, player_mass, player_elasticity, ball_radius, ball_mass, ball_elasticity, walls_thickness, walls_elasticity, max_force)
env.simulate()
# w, h = env.playground[2] + 2 * env.playground[0], env.playground[3] + 2 * env.playground[1]
Environment.capture_screenshot(env.space, width, height, f"images/env_{count}.png")
Environment.capture_screenshot(env.space, width, height, f"images/before.jpg")
# # Choose Best Action
# ca = ChooseAction(50, 50, 0.9, 0.5, 10, 10000, self.game_state, self.parameters)
# ca.search()
# ca.save_action(count)
# # Perform Action
# player_id, angle, force = ca.best_action.action
# start_x, start_y = players_position[player_id - 1]
# target_x, target_y = calculate_target_point(start_x, start_y, angle, -force/60)
# perform_drag_action(start_x, start_y, target_x, target_y)
output_image = self.obj_detc_player.draw_rectangles(sc, player_rectangles)
output_image = self.obj_detc_opponent.draw_rectangles(output_image, opponent_rectangles)
# output_image = self.obj_detc_ball.draw_rectangles(output_image, ball_rectangle)
output_image = draw_soccer_ball_rectangle(output_image, ball_rectangle, self.yellow_rgb_code)
output_image = self.obj_detc_player_goal.draw_rectangles(output_image, self.player_goal_rectangle)
output_image = self.obj_detc_opponent_goal.draw_rectangles(output_image, self.opponent_goal_rectangle)
output_image = detect_rectangle(output_image)
# ball_location, output_image = detect_ball(output_image)
angle, length, force, tail = get_arrow_angle(self.arrow_model, cv2_to_pil(sc), count)
action_result_image = cv.imread(f"images/before.jpg")
if angle is None:
print("Did not detect arrow.")
else:
player_radius, player_mass, player_elasticity, ball_radius, ball_mass, ball_elasticity, walls_thickness, walls_elasticity, max_force, _, _ = self.parameters
env = Environment(self.game_state, player_radius, player_mass, player_elasticity, ball_radius,
ball_mass, ball_elasticity, walls_thickness, walls_elasticity, max_force)
env.simulate()
target_shape = env.find_closest_shape(tail)
Environment.shoot(target_shape, angle, force)
for _ in range(500):
env.space.step(1 / 120)
Environment.capture_screenshot(env.space, width, height, f"images/after.jpg")
action_result_image = cv.imread(f"images/after.jpg")
cv.imshow("Action Result", action_result_image)
if cv.waitKey(1) == ord('q'):
cv.destroyAllWindows()
break
else:
output_image = sc
self.get_state = True
# cv.imshow('Matches', output_image)
# if cv.waitKey(1) == ord('q'):
# cv.destroyAllWindows()
# break
print('Done.')
# list_window_names()
SOCCER_BALL_MODEL = torch.hub.load('yolov5', 'custom', path='YOLO Model/soccer_ball/best.pt', source='local')
ARROW_MODEL = torch.hub.load('yolov5', 'custom', path='YOLO Model/arrow/best.pt', source='local')
game = GameAnalyzer("BlueStacks App Player", SOCCER_BALL_MODEL, ARROW_MODEL)
game.run()