diff --git a/crop/Field_Representation_crop.py b/crop/Field_Representation_crop.py index cc30aa9..95cba0d 100644 --- a/crop/Field_Representation_crop.py +++ b/crop/Field_Representation_crop.py @@ -7,11 +7,12 @@ from vision import Vision from config_file import UserConfigs from utils import mse from utils import get_click_point +from hsvfilter import HsvFilter GREEN = 1 YELLOW = 2 -BLUE = 3 -RED = 4 +RED = 3 +BLUE = 4 PURPLE = 5 RAINBOW = 6 BIGBOMB = 7 @@ -41,6 +42,10 @@ class Field: screenshot = [] next_level = cv.imread("next_level.jpg", cv.IMREAD_COLOR) next_level_x = cv.imread("next_level_x.jpg", cv.IMREAD_COLOR) + reset_board = cv.imread("reset_button.jpg", cv.IMREAD_COLOR) + reset_confirm = cv.imread("reset_confirm.jpg", cv.IMREAD_COLOR) + reset_counter = 0 + colors_at_standard = True explosives = [] explosives.append(RAINBOW) #explosives.append(ARROW_RIGHT) @@ -50,15 +55,15 @@ class Field: colors = [] colors.append(GREEN) colors.append(YELLOW) - colors.append(BLUE) colors.append(RED) + colors.append(BLUE) colors.append(PURPLE) - # colors.append(MAGINENT) - # colors.append(CHEMTRANT) - # colors.append(TENESENT) - # colors.append(CIBUTRANT) - # colors.append(ARTISENT) + colors.append(MAGINENT) + colors.append(CHEMTRANT) + colors.append(TENESENT) + colors.append(CIBUTRANT) + colors.append(ARTISENT) def __init__(self): self.data_value_grid = np.zeros((8, 14), dtype=int) @@ -83,9 +88,9 @@ class Field: self.needles = {GREEN: cv.imread("green.jpg", cv.IMREAD_COLOR), YELLOW: cv.imread("yellow.jpg", cv.IMREAD_COLOR), - PURPLE: cv.imread("purple.jpg", cv.IMREAD_COLOR), - BLUE: cv.imread("blue.jpg", cv.IMREAD_COLOR), RED: cv.imread("red.jpg", cv.IMREAD_COLOR), + BLUE: cv.imread("blue.jpg", cv.IMREAD_COLOR), + PURPLE: cv.imread("purple.jpg", cv.IMREAD_COLOR), RAINBOW: cv.imread("rainbow.jpg", cv.IMREAD_COLOR), #BIGBOMB: cv.imread("bigbomb.jpg", cv.IMREAD_COLOR), @@ -98,16 +103,67 @@ class Field: # BURGER: cv.imread("burger.jpg", cv.IMREAD_COLOR), # GOLDBAR: cv.imread("burger.jpg", cv.IMREAD_COLOR), # PAB1: cv.imread("pab1.jpg", cv.IMREAD_COLOR), - # MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR), - # CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR), - # TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR), - # CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR), - # ARTISENT: cv.imread("artisent.jpg", cv.IMREAD_COLOR) + MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR), + CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR), + TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR), + CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR), + ARTISENT: cv.imread("artisent.jpg", cv.IMREAD_COLOR) } + self.hsh_needles = {GREEN: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + YELLOW: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + PURPLE: HsvFilter(120, 45, 0, 170, 255, 255, 0, 0, 0, 0), + BLUE: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + RED: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + RAINBOW: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + MAGINENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + CHEMTRANT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + TENESENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + CIBUTRANT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + ARTISENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0) + } def reset(self): self.observation = [] + def set_color_order(self, order): + self.needles.clear() + self.needles = {order[0]: cv.imread("green.jpg", cv.IMREAD_COLOR), + order[1]: cv.imread("yellow.jpg", cv.IMREAD_COLOR), + order[2]: cv.imread("red.jpg", cv.IMREAD_COLOR), + order[3]: cv.imread("blue.jpg", cv.IMREAD_COLOR), + order[4]: cv.imread("purple.jpg", cv.IMREAD_COLOR), + + RAINBOW: cv.imread("rainbow.jpg", cv.IMREAD_COLOR), + #BIGBOMB: cv.imread("bigbomb.jpg", cv.IMREAD_COLOR), + #BOMB: cv.imread("bomb.jpg", cv.IMREAD_COLOR), + #ARROW_DOWN: cv.imread("arrow_down.jpg", cv.IMREAD_COLOR), + #ARROW_RIGHT: cv.imread("arrow_right.jpg", cv.IMREAD_COLOR) + # ROCK_1: cv.imread("rock1.jpg", cv.IMREAD_COLOR), + # ROCK_2: cv.imread("rock2.jpg", cv.IMREAD_COLOR), + # ROCK_3: cv.imread("rock3.jpg", cv.IMREAD_COLOR), + # BURGER: cv.imread("burger.jpg", cv.IMREAD_COLOR), + # GOLDBAR: cv.imread("burger.jpg", cv.IMREAD_COLOR), + # PAB1: cv.imread("pab1.jpg", cv.IMREAD_COLOR), + MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR), + CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR), + TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR), + CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR), + ARTISENT: cv.imread("artisent.jpg", cv.IMREAD_COLOR) + } + self.hsh_needles.clear() + self.hsh_needles = {order[0]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + order[1]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + order[2]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + order[3]: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + order[4]: HsvFilter(120, 45, 0, 170, 255, 255, 0, 0, 0, 0), + RAINBOW: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + MAGINENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + CHEMTRANT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + TENESENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + CIBUTRANT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), + ARTISENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0) + } + def assess_playfield_and_make_move(self): new_observation, new_screenshot = self.get_current_board_state() @@ -116,7 +172,23 @@ class Field: # last board state is same as actual if mse(new_observation, self.observation) == 0.0: # no movement detected -> blow explosives or reset - #self.detonate_explosive_when_stuck(new_observation) + self.reset_counter += 1 + if self.reset_counter >= 3: + screenshot = self.capture_window.get_screenshot() + if self.check_for_button_and_execute(screenshot, self.reset_board): + cv.waitKey(500) + screenshot = self.capture_window.get_screenshot() + if self.check_for_button_and_execute(screenshot, self.reset_confirm): + cv.waitKey(500) + + self.set_color_order((PURPLE, BLUE, RED, YELLOW, GREEN)) + self.reset_counter = 0 + self.colors_at_standard = False + return + else: + if not self.colors_at_standard: + self.set_color_order((GREEN, YELLOW, RED, BLUE, PURPLE)) + self.colors_at_standard = True cv.waitKey(500) self.find_patterns_and_valid_moves(new_observation) @@ -147,9 +219,7 @@ class Field: return e, i return None, None - def check_for_next_level(self, screen, needle): - offset_left = 230 - offset_down = 58 + def check_for_button_and_execute(self, screen, needle, offset_left=0, offset_down=0): rectangles = self.vision_stun.find(screen, needle, 0.70, 1) if len(rectangles) == 0: return False @@ -160,18 +230,19 @@ class Field: def get_current_board_state(self): try: # get an updated image of the game - screenshot = self.capture_window.get_screenshot() - #screenshot = cv.imread("playfield.jpg") + #screenshot = self.capture_window.get_screenshot() + screenshot = cv.imread("playfield.jpg") screenshot = screenshot[58:1134, 230:2113] # 1883,1076 self.screenshot = screenshot # gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) # thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] - - if self.check_for_next_level(screenshot, self.next_level): + offset_left = 230 + offset_down = 58 + if self.check_for_button_and_execute(screenshot, self.next_level, offset_left, offset_down): cv.waitKey(500) screenshot = self.capture_window.get_screenshot() screenshot = screenshot[58:1134, 230:2113] - if self.check_for_next_level(screenshot, self.next_level_x): + if self.check_for_button_and_execute(screenshot, self.next_level_x, offset_left, offset_down): cv.waitKey(500) screenshot = self.capture_window.get_screenshot() screenshot = screenshot[58:1134, 230:2113] @@ -186,10 +257,14 @@ class Field: # continue data_coords = np.zeros((8, 14), dtype=object) # field = Field() + for needle_key in self.needles.keys(): # gray_needle = cv.cvtColor(self.needles[needle_key], cv.COLOR_BGR2GRAY) # thresh_needle = cv.threshold(gray_needle, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] - rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.70, 56) + processed_screenshot = self.vision_stun.apply_hsv_filter(screenshot, self.hsh_needles[needle_key]) + processed_needle = self.vision_stun.apply_hsv_filter(self.needles[needle_key], self.hsh_needles[needle_key]) + rectangles = self.vision_stun.find(processed_screenshot, processed_needle, 0.70, 56) + #rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.70, 56) if len(rectangles) == 0: continue points = self.vision_stun.get_click_points(rectangles) @@ -477,7 +552,7 @@ class Field: score = 0 color = state[current_x, current_y] - if color <= 0 or color >= 6: + if color not in self.colors: return score for direction in directions: score = score + self.is_direction_in_bounce_and_same_color(state, direction, color) diff --git a/crop/artisent.jpg b/crop/artisent.jpg new file mode 100644 index 0000000..c6e40d5 Binary files /dev/null and b/crop/artisent.jpg differ diff --git a/crop/chemtrant.jpg b/crop/chemtrant.jpg new file mode 100644 index 0000000..6dd84b7 Binary files /dev/null and b/crop/chemtrant.jpg differ diff --git a/crop/cibutrant.jpg b/crop/cibutrant.jpg new file mode 100644 index 0000000..82e619b Binary files /dev/null and b/crop/cibutrant.jpg differ diff --git a/crop/hsh_base.jpg b/crop/hsh_base.jpg new file mode 100644 index 0000000..5de6bd6 Binary files /dev/null and b/crop/hsh_base.jpg differ diff --git a/crop/maginent.jpg b/crop/maginent.jpg new file mode 100644 index 0000000..ab83d91 Binary files /dev/null and b/crop/maginent.jpg differ diff --git a/crop/reset_button.jpg b/crop/reset_button.jpg new file mode 100644 index 0000000..99e09da Binary files /dev/null and b/crop/reset_button.jpg differ diff --git a/crop/reset_confirm.jpg b/crop/reset_confirm.jpg new file mode 100644 index 0000000..695afdd Binary files /dev/null and b/crop/reset_confirm.jpg differ diff --git a/crop/tenesent.jpg b/crop/tenesent.jpg new file mode 100644 index 0000000..adf9923 Binary files /dev/null and b/crop/tenesent.jpg differ