added color preference to crop bot

This commit is contained in:
2022-10-14 16:22:59 +02:00
parent 5c6abc0cda
commit c89f1aa3c6
9 changed files with 101 additions and 26 deletions

View File

@@ -7,11 +7,12 @@ from vision import Vision
from config_file import UserConfigs from config_file import UserConfigs
from utils import mse from utils import mse
from utils import get_click_point from utils import get_click_point
from hsvfilter import HsvFilter
GREEN = 1 GREEN = 1
YELLOW = 2 YELLOW = 2
BLUE = 3 RED = 3
RED = 4 BLUE = 4
PURPLE = 5 PURPLE = 5
RAINBOW = 6 RAINBOW = 6
BIGBOMB = 7 BIGBOMB = 7
@@ -41,6 +42,10 @@ class Field:
screenshot = [] screenshot = []
next_level = cv.imread("next_level.jpg", cv.IMREAD_COLOR) next_level = cv.imread("next_level.jpg", cv.IMREAD_COLOR)
next_level_x = cv.imread("next_level_x.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 = []
explosives.append(RAINBOW) explosives.append(RAINBOW)
#explosives.append(ARROW_RIGHT) #explosives.append(ARROW_RIGHT)
@@ -50,15 +55,15 @@ class Field:
colors = [] colors = []
colors.append(GREEN) colors.append(GREEN)
colors.append(YELLOW) colors.append(YELLOW)
colors.append(BLUE)
colors.append(RED) colors.append(RED)
colors.append(BLUE)
colors.append(PURPLE) colors.append(PURPLE)
# colors.append(MAGINENT) colors.append(MAGINENT)
# colors.append(CHEMTRANT) colors.append(CHEMTRANT)
# colors.append(TENESENT) colors.append(TENESENT)
# colors.append(CIBUTRANT) colors.append(CIBUTRANT)
# colors.append(ARTISENT) colors.append(ARTISENT)
def __init__(self): def __init__(self):
self.data_value_grid = np.zeros((8, 14), dtype=int) 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), self.needles = {GREEN: cv.imread("green.jpg", cv.IMREAD_COLOR),
YELLOW: cv.imread("yellow.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), 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), RAINBOW: cv.imread("rainbow.jpg", cv.IMREAD_COLOR),
#BIGBOMB: cv.imread("bigbomb.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), # BURGER: cv.imread("burger.jpg", cv.IMREAD_COLOR),
# GOLDBAR: cv.imread("burger.jpg", cv.IMREAD_COLOR), # GOLDBAR: cv.imread("burger.jpg", cv.IMREAD_COLOR),
# PAB1: cv.imread("pab1.jpg", cv.IMREAD_COLOR), # PAB1: cv.imread("pab1.jpg", cv.IMREAD_COLOR),
# MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR), MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR),
# CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR), CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR),
# TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR), TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR),
# CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR), CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR),
# ARTISENT: cv.imread("artisent.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): def reset(self):
self.observation = [] 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): def assess_playfield_and_make_move(self):
new_observation, new_screenshot = self.get_current_board_state() new_observation, new_screenshot = self.get_current_board_state()
@@ -116,7 +172,23 @@ class Field:
# last board state is same as actual # last board state is same as actual
if mse(new_observation, self.observation) == 0.0: if mse(new_observation, self.observation) == 0.0:
# no movement detected -> blow explosives or reset # 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) cv.waitKey(500)
self.find_patterns_and_valid_moves(new_observation) self.find_patterns_and_valid_moves(new_observation)
@@ -147,9 +219,7 @@ class Field:
return e, i return e, i
return None, None return None, None
def check_for_next_level(self, screen, needle): def check_for_button_and_execute(self, screen, needle, offset_left=0, offset_down=0):
offset_left = 230
offset_down = 58
rectangles = self.vision_stun.find(screen, needle, 0.70, 1) rectangles = self.vision_stun.find(screen, needle, 0.70, 1)
if len(rectangles) == 0: if len(rectangles) == 0:
return False return False
@@ -160,18 +230,19 @@ class Field:
def get_current_board_state(self): def get_current_board_state(self):
try: try:
# get an updated image of the game # get an updated image of the game
screenshot = self.capture_window.get_screenshot() #screenshot = self.capture_window.get_screenshot()
#screenshot = cv.imread("playfield.jpg") screenshot = cv.imread("playfield.jpg")
screenshot = screenshot[58:1134, 230:2113] # 1883,1076 screenshot = screenshot[58:1134, 230:2113] # 1883,1076
self.screenshot = screenshot self.screenshot = screenshot
# gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) # gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY)
# thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] # thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1]
offset_left = 230
if self.check_for_next_level(screenshot, self.next_level): offset_down = 58
if self.check_for_button_and_execute(screenshot, self.next_level, offset_left, offset_down):
cv.waitKey(500) cv.waitKey(500)
screenshot = self.capture_window.get_screenshot() screenshot = self.capture_window.get_screenshot()
screenshot = screenshot[58:1134, 230:2113] 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) cv.waitKey(500)
screenshot = self.capture_window.get_screenshot() screenshot = self.capture_window.get_screenshot()
screenshot = screenshot[58:1134, 230:2113] screenshot = screenshot[58:1134, 230:2113]
@@ -186,10 +257,14 @@ class Field:
# continue # continue
data_coords = np.zeros((8, 14), dtype=object) data_coords = np.zeros((8, 14), dtype=object)
# field = Field() # field = Field()
for needle_key in self.needles.keys(): for needle_key in self.needles.keys():
# gray_needle = cv.cvtColor(self.needles[needle_key], cv.COLOR_BGR2GRAY) # 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] # 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: if len(rectangles) == 0:
continue continue
points = self.vision_stun.get_click_points(rectangles) points = self.vision_stun.get_click_points(rectangles)
@@ -477,7 +552,7 @@ class Field:
score = 0 score = 0
color = state[current_x, current_y] color = state[current_x, current_y]
if color <= 0 or color >= 6: if color not in self.colors:
return score return score
for direction in directions: for direction in directions:
score = score + self.is_direction_in_bounce_and_same_color(state, direction, color) score = score + self.is_direction_in_bounce_and_same_color(state, direction, color)

BIN
crop/artisent.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
crop/chemtrant.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
crop/cibutrant.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

BIN
crop/hsh_base.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1013 KiB

BIN
crop/maginent.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
crop/reset_button.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
crop/reset_confirm.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

BIN
crop/tenesent.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB