diff --git a/.DS_Store b/.DS_Store index 7db90082..d3f49d4d 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/agent/.DS_Store b/agent/.DS_Store new file mode 100644 index 00000000..2227fda3 Binary files /dev/null and b/agent/.DS_Store differ diff --git a/agent/methods/__pycache__/genetic_algorithm.cpython-310.pyc b/agent/methods/__pycache__/genetic_algorithm.cpython-310.pyc new file mode 100644 index 00000000..28942204 Binary files /dev/null and b/agent/methods/__pycache__/genetic_algorithm.cpython-310.pyc differ diff --git a/agent/methods/__pycache__/graph_search.cpython-310.pyc b/agent/methods/__pycache__/graph_search.cpython-310.pyc index 2aa897a8..20a35630 100644 Binary files a/agent/methods/__pycache__/graph_search.cpython-310.pyc and b/agent/methods/__pycache__/graph_search.cpython-310.pyc differ diff --git a/agent/neural_network/.DS_Store b/agent/neural_network/.DS_Store index a1aa01f0..be074160 100644 Binary files a/agent/neural_network/.DS_Store and b/agent/neural_network/.DS_Store differ diff --git a/agent/neural_network/__pycache__/inference.cpython-310.pyc b/agent/neural_network/__pycache__/inference.cpython-310.pyc index abee7f33..4219b4b9 100644 Binary files a/agent/neural_network/__pycache__/inference.cpython-310.pyc and b/agent/neural_network/__pycache__/inference.cpython-310.pyc differ diff --git a/agent/neural_network/images/.DS_Store b/agent/neural_network/images/.DS_Store index d9261698..a123da48 100644 Binary files a/agent/neural_network/images/.DS_Store and b/agent/neural_network/images/.DS_Store differ diff --git a/agent/neural_network/images/test/.DS_Store b/agent/neural_network/images/test/.DS_Store index 597e1812..8ad19d66 100644 Binary files a/agent/neural_network/images/test/.DS_Store and b/agent/neural_network/images/test/.DS_Store differ diff --git a/agent/neural_network/inference.py b/agent/neural_network/inference.py index 5c6313c3..4e4095bc 100644 --- a/agent/neural_network/inference.py +++ b/agent/neural_network/inference.py @@ -41,7 +41,8 @@ def main(path): # read and preprocess the image image = cv2.imread(path) # get the ground truth class - gt_class = path.split('/')[-2] + #gt_class = path.split('/')[-2] + gt_class = path.split('/') orig_image = image.copy() # convert to RGB format image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) diff --git a/core/.DS_Store b/core/.DS_Store new file mode 100644 index 00000000..0fb6ae11 Binary files /dev/null and b/core/.DS_Store differ diff --git a/core/chicken/__pycache__/chicken.cpython-310.pyc b/core/chicken/__pycache__/chicken.cpython-310.pyc index 447acbec..cf14ee29 100644 Binary files a/core/chicken/__pycache__/chicken.cpython-310.pyc and b/core/chicken/__pycache__/chicken.cpython-310.pyc differ diff --git a/core/chicken/chicken.py b/core/chicken/chicken.py index 678ff476..142187df 100644 --- a/core/chicken/chicken.py +++ b/core/chicken/chicken.py @@ -27,7 +27,6 @@ class Chicken: self.step = 0 def move(self, direction): - print('MOVE') if direction == 'move': if self.angle == 0 and self.y != 0: self.y -= 1 @@ -56,9 +55,7 @@ class Chicken: self.image = self.left self.step = self.step + 1 - print(self.x, self.y) - print('step: ', self.step) - + print('The chicken is on: ', self.x, self.y) def water(self, xy , plant_list): for obj in plant_list: if obj.xy == xy: diff --git a/core/constants/__pycache__/strings.cpython-310.pyc b/core/constants/__pycache__/strings.cpython-310.pyc new file mode 100644 index 00000000..ae847d3d Binary files /dev/null and b/core/constants/__pycache__/strings.cpython-310.pyc differ diff --git a/core/constants/strings.py b/core/constants/strings.py new file mode 100644 index 00000000..d6a832cf --- /dev/null +++ b/core/constants/strings.py @@ -0,0 +1,24 @@ +#image path +#plants +class ConStrings: + flower = r'resources/plants/flower.png' + bush = r'resources/plants/bush.png' + ivy = r'resources/plants/ivy.png' + wheat_dead = r'resources/plants/wheat_dead.png' + wheat= r'resources/plants/wheat.png' + + strawberry = r'resources/fruits/6.png' + grapes = r'resources/fruits/8.png' + apple = r'resources/fruits/9.png' + banana = r'resources/fruits/10.png' + + eggplant = r'resources/vegies/1.png' + carrot = r'resources/vegies/12.png' + pumpkin = r'resources/vegies/11.png' + tomato = r'resources/vegies/7.png' + pepper = r'resources/vegies/14.png' + papaya = r'resources/vegies/2.png' + + + stone = r'resources/stones/stone.png' + aim = r'resources/aim.png' diff --git a/core/plants/__pycache__/plant.cpython-310.pyc b/core/plants/__pycache__/plant.cpython-310.pyc index 162c9e54..1df613f7 100644 Binary files a/core/plants/__pycache__/plant.cpython-310.pyc and b/core/plants/__pycache__/plant.cpython-310.pyc differ diff --git a/core/plants/__pycache__/plants_settings.cpython-310.pyc b/core/plants/__pycache__/plants_settings.cpython-310.pyc index a55eb2c9..3fe6939b 100644 Binary files a/core/plants/__pycache__/plants_settings.cpython-310.pyc and b/core/plants/__pycache__/plants_settings.cpython-310.pyc differ diff --git a/core/plants/plant.py b/core/plants/plant.py index 22bfadb0..c15b6d8e 100644 --- a/core/plants/plant.py +++ b/core/plants/plant.py @@ -1,10 +1,10 @@ class Plant: - def __init__(self, id, name, state, image_zero, image_one, x, y, empty): + def __init__(self, id, name, state, image_zero, image_one, image_path, x, y, empty): self.id = id self.name = name self.state = state self.image_state_zero = image_zero self.image_state_one = image_one + self.image_path = image_path self.xy = [x, y] - self.empty = empty - \ No newline at end of file + self.empty = empty \ No newline at end of file diff --git a/core/plants/plants_settings.py b/core/plants/plants_settings.py index a56e533c..a80f24b1 100644 --- a/core/plants/plants_settings.py +++ b/core/plants/plants_settings.py @@ -1,7 +1,9 @@ import pygame import random from core.plants import plant +from core.constants import strings +constant = strings.ConStrings class PlantsSettings: @@ -12,33 +14,43 @@ class PlantsSettings: self.all_plants = [] self.all_fruits = [] #plants - self.flower_image = pygame.image.load(r'resources/plants/flower.png').convert_alpha() + self.flower_image = pygame.image.load(constant.flower).convert_alpha() self.flower_image = pygame.transform.scale(self.flower_image, (self.cell_size, self.cell_size)) - self.bush_image = pygame.image.load(r'resources/plants/bush.png').convert_alpha() + self.bush_image = pygame.image.load(constant.bush).convert_alpha() self.bush_image = pygame.transform.scale(self.bush_image, (self.cell_size, self.cell_size)) - self.ivy_image = pygame.image.load(r'resources/plants/ivy.png').convert_alpha() + self.ivy_image = pygame.image.load(constant.ivy).convert_alpha() self.ivy_image = pygame.transform.scale(self.ivy_image, (self.cell_size, self.cell_size)) - self.wheat_dead_image = pygame.image.load(r'resources/plants/wheat_dead.png').convert_alpha() + self.wheat_dead_image = pygame.image.load(constant.wheat_dead).convert_alpha() self.wheat_dead_image = pygame.transform.scale(self.wheat_dead_image, (self.cell_size, self.cell_size)) - self.wheat_image = pygame.image.load(r'resources/plants/wheat.png').convert_alpha() + self.wheat_image = pygame.image.load(constant.wheat).convert_alpha() self.wheat_image = pygame.transform.scale(self.wheat_image, (self.cell_size, self.cell_size)) #vegies - self.eggplant_image = pygame.image.load(r'resources/vegies/1.png').convert_alpha() + self.eggplant_image = pygame.image.load(constant.eggplant).convert_alpha() self.eggplant_image = pygame.transform.scale(self.eggplant_image, (self.cell_size, self.cell_size)) + self.tomato_image = pygame.image.load(constant.tomato).convert_alpha() + self.tomato_image = pygame.transform.scale(self.tomato_image, (self.cell_size, self.cell_size)) + self.carrot_image = pygame.image.load(constant.carrot).convert_alpha() + self.carrot_image = pygame.transform.scale(self.carrot_image, (self.cell_size, self.cell_size)) + self.pumpkin_image = pygame.image.load(constant.pumpkin).convert_alpha() + self.pumpkin_image = pygame.transform.scale(self.pumpkin_image, (self.cell_size, self.cell_size)) + self.papaya_image = pygame.image.load(constant.papaya).convert_alpha() + self.papaya_image = pygame.transform.scale(self.papaya_image, (self.cell_size, self.cell_size)) + self.pepper_image = pygame.image.load(constant.pepper).convert_alpha() + self.pepper_image = pygame.transform.scale(self.pepper_image, (self.cell_size, self.cell_size)) #fruits - self.strawberrie_image = pygame.image.load(r'resources/fruits/6.png').convert_alpha() - self.strawberrie_image = pygame.transform.scale(self.strawberrie_image, (self.cell_size, self.cell_size)) - self.grapes_image = pygame.image.load(r'resources/fruits/8.png').convert_alpha() + self.strawberry_image = pygame.image.load(constant.strawberry).convert_alpha() + self.strawberry_image = pygame.transform.scale(self.strawberry_image, (self.cell_size, self.cell_size)) + self.grapes_image = pygame.image.load(constant.grapes).convert_alpha() self.grapes_image = pygame.transform.scale(self.grapes_image, (self.cell_size-12, self.cell_size)) - self.apple_image = pygame.image.load(r'resources/fruits/9.png').convert_alpha() + self.apple_image = pygame.image.load(constant.apple).convert_alpha() self.apple_image = pygame.transform.scale(self.apple_image, (self.cell_size, self.cell_size)) - self.banana_image = pygame.image.load(r'resources/fruits/10.png').convert_alpha() + self.banana_image = pygame.image.load(constant.banana).convert_alpha() self.banana_image = pygame.transform.scale(self.banana_image, (self.cell_size, self.cell_size)) - self.stone_image = pygame.image.load(r'resources/stones/stone.png').convert_alpha() + self.stone_image = pygame.image.load(constant.stone).convert_alpha() self.stone_image = pygame.transform.scale(self.stone_image, (self.cell_size, self.cell_size)) - self.aim_image = pygame.image.load(r'resources/aim.png').convert_alpha() + self.aim_image = pygame.image.load(constant.aim).convert_alpha() self.aim_image = pygame.transform.scale(self.aim_image, (self.cell_size, self.cell_size)) def locate_plant(self, field_list, name, num_of_blocks): # finds open space (coordinates) @@ -50,7 +62,7 @@ class PlantsSettings: self.all_plants.append([rand_x, rand_y]) if name == 'wheat': self.block = plant.Plant( - i, name, 0, self.wheat_dead_image, self.wheat_image, rand_x, rand_y, False + i, name, 0, self.wheat_dead_image, self.wheat_image, constant.wheat_dead, rand_x, rand_y, False ) if name == 'eggplant': self.block = plant.Plant( @@ -88,34 +100,75 @@ class PlantsSettings: self.all_fruits.append([rand_x, rand_y]) if name == 'wheat': self.block = plant.Plant( - i, name, 0, self.wheat_dead_image, self.wheat_image, rand_x, rand_y, False + i, name, 0, self.wheat_dead_image, self.wheat_image, constant.wheat_dead, rand_x, rand_y, False ) if name == 'strawberry': self.block = plant.Plant( - i, name, 1, self.strawberrie_image, self.strawberrie_image, rand_x, rand_y, False + i, name, 1, self.strawberry_image, self.strawberry_image, constant.strawberry, rand_x, rand_y, False ) if name == 'grapes': self.block = plant.Plant( - i, name, 1, self.grapes_image, self.grapes_image, rand_x, rand_y, False + i, name, 1, self.grapes_image, self.grapes_image,constant.grapes, rand_x, rand_y, False ) if name == 'apple': self.block = plant.Plant( - i, name, 1, self.apple_image, self.apple_image, rand_x, rand_y, False + i, name, 1, self.apple_image, self.apple_image,constant.apple, rand_x, rand_y, False ) if name == 'banana': self.block = plant.Plant( - i, name, 1, self.banana_image, self.banana_image, rand_x, rand_y, False + i, name, 1, self.banana_image, self.banana_image,constant.banana, rand_x, rand_y, False ) if name == 'stone': self.block = plant.Plant( - i, name, 1, self.stone_image, self.stone_image, rand_x, rand_y, False + i, name, 1, self.stone_image, self.stone_image, constant.stone, rand_x, rand_y, False ) field_list.append(self.block) break + def locate_veggies(self, field_list, name, num_of_blocks): # finds open space (coordinates) + for i in range(num_of_blocks): + while True: + rand_x = random.randint(0, self.cell_number - 1) # to check + rand_y = random.randint(0, self.cell_number - 1) + if [rand_x, rand_y] not in self.all_fruits: + self.all_fruits.append([rand_x, rand_y]) + if name == 'wheat': + self.block = plant.Plant( + i, name, 0, self.wheat_dead_image, self.wheat_image, constant.wheat_dead, rand_x, rand_y, False + ) + if name == 'pumpkin': + self.block = plant.Plant( + i, name, 1, self.pumpkin_image, self.pumpkin_image, constant.pumpkin, rand_x, rand_y, False + ) + if name == 'pepper': + self.block = plant.Plant( + i, name, 1, self.pepper_image, self.pepper_image, constant.pepper, rand_x, rand_y, False + ) + if name == 'papaya': + self.block = plant.Plant( + i, name, 1, self.papaya_image, self.papaya_image, constant.papaya, rand_x, rand_y, False + ) + if name == 'carrot': + self.block = plant.Plant( + i, name, 1, self.carrot_image, self.carrot_image, constant.carrot, rand_x, rand_y, False + ) + if name == 'tomato': + self.block = plant.Plant( + i, name, 1, self.tomato_image, self.tomato_image, constant.tomato, rand_x, rand_y, False + ) + if name == 'stone': + self.block = plant.Plant( + i, name, 1, self.stone_image, self.stone_image, constant.stone, rand_x, rand_y, False + ) + field_list.append(self.block) + break + + + + def locate_aim(self, field_list, x, y): self.block = plant.Plant( - 999, 'aim', 1, self.aim_image, self.aim_image, x, y, False + 999, 'aim', 1, self.aim_image, self.aim_image, constant.aim, x, y, False ) field_list.append(self.block) diff --git a/main.py b/main.py index 45fc6a88..28f705d3 100644 --- a/main.py +++ b/main.py @@ -32,7 +32,8 @@ class Game: self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number)) self.grass_list = [] # 1-level - self.plant_list = [] # 2-level + self.plant_list = [] # 2-level - test + self.veggies_list = [] # 2-level - first model agent self.fruits_list = [] # 2-level - second model agent self.stone_list = [] # 3-level self.aim_list = [] # 4-level @@ -48,18 +49,23 @@ class Game: # self.Plants.locate_plant(self.plant_list, 'flower', self.blocks_number) # self.Plants.locate_plant(self.plant_list, 'bush', self.blocks_number) - #fruits_list - self.Plants.locate_fruit(self.fruits_list, 'apple', self.blocks_number-5) - self.Plants.locate_fruit(self.fruits_list, 'banana', self.blocks_number-5) - self.Plants.locate_fruit(self.fruits_list, 'strawberry', self.blocks_number-5) - self.Plants.locate_fruit(self.fruits_list, 'grapes', self.blocks_number-5) - self.Plants.locate_fruit(self.plant_list, 'wheat', self.blocks_number) + # #fruits_list + # self.Plants.locate_fruit(self.fruits_list, 'apple', self.blocks_number-5) + # self.Plants.locate_fruit(self.fruits_list, 'banana', self.blocks_number-5) + # self.Plants.locate_fruit(self.fruits_list, 'strawberry', self.blocks_number-5) + # self.Plants.locate_fruit(self.fruits_list, 'grapes', self.blocks_number-5) + # self.Plants.locate_fruit(self.fruits_list, 'wheat', self.blocks_number) + #vegies_list + self.Plants.locate_veggies(self.veggies_list, 'pepper', self.blocks_number-5) + self.Plants.locate_veggies(self.veggies_list, 'carrot', self.blocks_number-5) + self.Plants.locate_veggies(self.veggies_list, 'pumpkin', self.blocks_number-5) + self.Plants.locate_veggies(self.veggies_list, 'wheat', self.blocks_number) self.Plants.locate_aim(self.aim_list, 0, 0) - self.Plants.locate_fruit(self.stone_list, 'stone', self.blocks_number) + self.Plants.locate_veggies(self.stone_list, 'stone', self.blocks_number) #self.image_wheat = self.Plants.wheat_watered() self.chicken = chick.Chicken(self.surface, self.cell_size, self.cell_number) @@ -69,14 +75,14 @@ class Game: running = True clock = pygame.time.Clock() move_chicken_event = pygame.USEREVENT + 1 - pygame.time.set_timer(move_chicken_event, 500) # chicken moves every 1000 ms + pygame.time.set_timer(move_chicken_event, 1000) # chicken moves every 1000 ms self.search_object = graph_search.Search(self.cell_size, self.cell_number) chicken_next_moves = [] veggies = dict() veggies_debug = dict() - wheat_list = [obj for obj in self.plant_list if obj.name == "wheat" and obj.state == 0] + wheat_list = [obj for obj in self.veggies_list if obj.name == "wheat" and obj.state == 0] new_list = [()] a = 1 @@ -107,7 +113,7 @@ class Game: if event.type == move_chicken_event: if len(chicken_next_moves) == 0: angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'} - closest_wheat = self.search_object.closest_point(self.chicken.x, self.chicken.y, 'wheat', self.plant_list) + closest_wheat = self.search_object.closest_point(self.chicken.x, self.chicken.y, 'wheat', self.veggies_list) # self.aim_list[0].xy[0] = closest_wheat[0] # self.aim_list[0].xy[1] = closest_wheat[1] self.aim_list[0].xy[0] = best_path[a][0] @@ -115,40 +121,50 @@ class Game: # a += 1 # target = wheat_list[a] # chicken_next_moves = self.search_object.astarsearch( - # [self.chicken.x, self.chicken.y, angles[self.chicken.angle]], [closest_wheat[0], closest_wheat[1]], self.stone_list, self.plant_list) + # [self.chicken.x, self.chicken.y, angles[self.chicken.angle]], [closest_wheat[0], closest_wheat[1]], self.stone_list, self.veggies_list) chicken_next_moves = self.search_object.astarsearch( - [self.chicken.x, self.chicken.y, angles[self.chicken.angle]], [best_path[a][0], best_path[a][1]], self.stone_list, self.plant_list) + [self.chicken.x, self.chicken.y, angles[self.chicken.angle]], [best_path[a][0], best_path[a][1]], self.stone_list, self.veggies_list) a += 1 - #neural_network - current_veggie = next(os.walk('./agent/neural_network/images/test'))[1][random.randint(0, len(next(os.walk('./agent/neural_network/images/test'))[1])-1)] - if(current_veggie in veggies_debug): - veggies_debug[current_veggie]+=1 - else: - veggies_debug[current_veggie] = 1 + # #neural_network + # current_veggie = next(os.walk('./agent/neural_network/images/test'))[1][random.randint(0, len(next(os.walk('./agent/neural_network/images/test'))[1])-1)] + # if(current_veggie in veggies_debug): + # veggies_debug[current_veggie]+=1 + # else: + # veggies_debug[current_veggie] = 1 - current_veggie_example = next(os.walk(f'./agent/neural_network/images/test/{current_veggie}'))[2][random.randint(0, len(next(os.walk(f'./agent/neural_network/images/test/{current_veggie}'))[2])-1)] - predicted_veggie = inference.main(f"./agent/neural_network/images/test/{current_veggie}/{current_veggie_example}") - if predicted_veggie in veggies: - veggies[predicted_veggie]+=1 - else: - veggies[predicted_veggie] = 1 - print("Debug veggies: ", veggies_debug, "Predicted veggies: ", veggies) + # current_veggie_example = next(os.walk(f'./agent/neural_network/images/test/{current_veggie}'))[2][random.randint(0, len(next(os.walk(f'./agent/neural_network/images/test/{current_veggie}'))[2])-1)] + # predicted_veggie = inference.main(f"./agent/neural_network/images/test/{current_veggie}/{current_veggie_example}") + # if predicted_veggie in veggies: + # veggies[predicted_veggie]+=1 + # else: + # veggies[predicted_veggie] = 1 + # print("Debug veggies: ", veggies_debug, "Predicted veggies: ", veggies) else: self.chicken.move(chicken_next_moves.pop(0)[0]) if len(chicken_next_moves) == 0: - self.chicken.water([self.aim_list[0].xy[0], self.aim_list[0].xy[1]], self.plant_list) + self.chicken.water([self.aim_list[0].xy[0], self.aim_list[0].xy[1]], self.veggies_list) print(self.chicken.x, self.chicken.y) - + current_block = '' + for obj in self.veggies_list: + if obj.xy == [self.chicken.x, self.chicken.y]: + if obj.name != 'wheat': + current_block = obj.image_path + if current_block == '': + print('the block is empty') + else: + veggies_images = inference.main(current_block) + print('Current veggie: ',veggies_images) elif event.type == QUIT: running = False self.surface.fill((123, 56, 51)) # background color self.Field.draw_grass(self.grass_list) - self.Plants.draw_plant(self.plant_list) - self.Plants.draw_plant(self.fruits_list) + #self.Plants.draw_plant(self.plant_list) + #self.Plants.draw_plant(self.fruits_list) self.Plants.draw_plant(self.stone_list) + self.Plants.draw_plant(self.veggies_list) self.Plants.draw_aim(self.aim_list) diff --git a/resources/.DS_Store b/resources/.DS_Store index 40bec9ab..d0c1cd6f 100644 Binary files a/resources/.DS_Store and b/resources/.DS_Store differ diff --git a/resources/vegies/.DS_Store b/resources/vegies/.DS_Store index a6a4fe55..8191434e 100644 Binary files a/resources/vegies/.DS_Store and b/resources/vegies/.DS_Store differ diff --git a/resources/vegies/11.png b/resources/vegies/11.png new file mode 100644 index 00000000..e4a38d37 Binary files /dev/null and b/resources/vegies/11.png differ diff --git a/resources/vegies/12.png b/resources/vegies/12.png new file mode 100644 index 00000000..93773e0f Binary files /dev/null and b/resources/vegies/12.png differ diff --git a/resources/vegies/14.png b/resources/vegies/14.png new file mode 100644 index 00000000..e251f71a Binary files /dev/null and b/resources/vegies/14.png differ diff --git a/resources/vegies/2.png b/resources/vegies/2.png new file mode 100644 index 00000000..7d2dd708 Binary files /dev/null and b/resources/vegies/2.png differ