diff --git a/.gitignore b/.gitignore index 4932e41..b25a44d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ __pycache__ #PyCharm -.idea/ \ No newline at end of file +.idea/ +AI_brain/model.h5 diff --git a/AI_brain/image_recognition.py b/AI_brain/image_recognition.py index 47f766f..99bf249 100644 --- a/AI_brain/image_recognition.py +++ b/AI_brain/image_recognition.py @@ -5,18 +5,19 @@ from tensorflow import keras import cv2 import random -#You can download model from https://uam-my.sharepoint.com/:f:/g/personal/pavbia_st_amu_edu_pl/EmBHjnETuk5LiCZS6xk7AnIBNsnffR3Sygf8EX2bhR1w4A -#Change the path to model + to datasets (string 12 + strings 35,41,47,53) +# You can download model from https://uam-my.sharepoint.com/:f:/g/personal/pavbia_st_amu_edu_pl/EmBHjnETuk5LiCZS6xk7AnIBNsnffR3Sygf8EX2bhR1w4A +# Change the path to model + to datasets (string 12 + strings 35,41,47,53) + class VacuumRecognizer: - model = keras.models.load_model('AI_brain\model.h5') #Neuron model path + model = keras.models.load_model("AI_brain\model.h5") # Neuron model path def recognize(self, image_path) -> str: - class_names = ['Banana', 'Cat', 'Earings', 'Plant'] + class_names = ["Banana", "Cat", "Earings", "Plant"] img = cv2.imread(image_path, flags=cv2.IMREAD_GRAYSCALE) cv2.waitKey(0) - img = (np.expand_dims(img, 0)) + img = np.expand_dims(img, 0) predictions = self.model.predict(img)[0].tolist() @@ -31,31 +32,43 @@ class VacuumRecognizer: return class_names[predictions.index(max(predictions))] def get_random_dir(self, type) -> str: - if type == 'Plant': - plant_image_paths = 'C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Plant' #Plant dataset path + if type == "Plant": + plant_image_paths = "C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Plant" # Plant dataset path plant_dirs = os.listdir(plant_image_paths) - full_path = plant_image_paths + '\\' + plant_dirs[random.randint(0, len(plant_dirs)-1)] + full_path = ( + plant_image_paths + + "\\" + + plant_dirs[random.randint(0, len(plant_dirs) - 1)] + ) print(full_path) return full_path - elif type == 'Earings': - earnings_image_paths = 'C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Earings' #Earings dataset path + elif type == "Earings": + earnings_image_paths = "C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Earings" # Earings dataset path earning_dirs = os.listdir(earnings_image_paths) - full_path = earnings_image_paths + '\\' + earning_dirs[random.randint(0, len(earning_dirs)-1)] + full_path = ( + earnings_image_paths + + "\\" + + earning_dirs[random.randint(0, len(earning_dirs) - 1)] + ) print(full_path) return full_path - elif type == 'Banana': - banana_image_paths = 'C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Banana' #Banana dataset path + elif type == "Banana": + banana_image_paths = "C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Banana" # Banana dataset path banana_dirs = os.listdir(banana_image_paths) - full_path = banana_image_paths + '\\' + banana_dirs[random.randint(0, len(banana_dirs)-1)] + full_path = ( + banana_image_paths + + "\\" + + banana_dirs[random.randint(0, len(banana_dirs) - 1)] + ) print(full_path) return full_path - elif type == 'Cat': - cat_image_paths = 'C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Cat' #Cat dataset path + elif type == "Cat": + cat_image_paths = "C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Cat" # Cat dataset path cat_dir = os.listdir(cat_image_paths) -#For testing the neuron model -'''image_paths = [] +# For testing the neuron model +"""image_paths = [] image_paths.append('C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Banana') image_paths.append('C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Cat') image_paths.append('C:\\Users\\Pavel\\Desktop\\AI\\Machine_learning_2023\\AI_brain\\Image_datasetJPGnewBnW\\check\\Earings') @@ -65,4 +78,4 @@ uio = VacuumRecognizer() for image_path in image_paths: dirs = os.listdir(image_path) for i in range(3): - print(uio.recognize(image_path + '\\' + dirs[random.randint(0, len(dirs)-1)]))''' \ No newline at end of file + print(uio.recognize(image_path + '\\' + dirs[random.randint(0, len(dirs)-1)]))""" diff --git a/config.ini b/config.ini index 42cac05..b2eab07 100644 --- a/config.ini +++ b/config.ini @@ -9,4 +9,4 @@ NumberOfEarrings = 3 NumberOfPlants = 5 [NEURAL_NETWORK] -is_nural_network_off = True \ No newline at end of file +is_neural_network_off = True \ No newline at end of file diff --git a/main.py b/main.py index f94b1df..0ec3034 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,9 @@ from random import randint import pygame import configparser +config = configparser.ConfigParser() +config.read("config.ini") + from domain.commands.random_cat_move_command import RandomCatMoveCommand from domain.commands.vacuum_move_command import VacuumMoveCommand from domain.entities.cat import Cat @@ -13,17 +16,15 @@ from domain.entities.earring import Earring from domain.entities.docking_station import Doc_Station from domain.world import World from view.renderer import Renderer -from AI_brain.image_recognition import VacuumRecognizer + +if not config.getboolean("NEURAL_NETWORK", "is_neural_network_off"): + from AI_brain.image_recognition import VacuumRecognizer # from AI_brain.movement import GoAnyDirectionBFS, State # from AI_brain.rotate_and_go_bfs import RotateAndGoBFS, State from AI_brain.rotate_and_go_aStar import RotateAndGoAStar, State -config = configparser.ConfigParser() -config.read("config.ini") - - class Main: def __init__(self): tiles_x = 10 @@ -145,7 +146,7 @@ class Main: def generate_world(tiles_x: int, tiles_y: int) -> World: - if config.getboolean("NEURAL_NETWORK", "is_nural_network_off"): + if config.getboolean("NEURAL_NETWORK", "is_neural_network_off"): world = World(tiles_x, tiles_y) for _ in range(config.getint("CONSTANT", "NumberOfBananas")): temp_x = randint(0, tiles_x - 1) @@ -165,15 +166,16 @@ def generate_world(tiles_x: int, tiles_y: int) -> World: world.add_entity(Earring(5, 5)) world.add_entity(Earring(4, 6)) else: - def world_adder(x,y,object,style=None): + + def world_adder(x, y, object, style=None): print(object) - if object == 'Plant': + if object == "Plant": world.add_entity(Entity(x, y, f"PLANT{randint(1, 3)}")) - if object == 'Earings': + if object == "Earings": world.add_entity(Earring(x, y)) - if object == 'Banana': + if object == "Banana": world.add_entity(Garbage(temp_x, temp_y)) - if object == 'Cat' and config.getboolean("APP", "cat"): + if object == "Cat" and config.getboolean("APP", "cat"): world.add_entity(Cat(x, y)) neural_network = VacuumRecognizer() @@ -188,22 +190,21 @@ def generate_world(tiles_x: int, tiles_y: int) -> World: for _ in range(config.getint("CONSTANT", "NumberOfPlants")): temp_x = randint(0, tiles_x - 1) temp_y = randint(0, tiles_y - 1) - path = VacuumRecognizer.get_random_dir(neural_network,'Plant') + path = VacuumRecognizer.get_random_dir(neural_network, "Plant") world_adder(temp_x, temp_y, neural_network.recognize(path)) for _ in range(config.getint("CONSTANT", "NumberOfEarrings")): temp_x = randint(0, tiles_x - 1) temp_y = randint(0, tiles_y - 1) - path = VacuumRecognizer.get_random_dir(neural_network,'Earings') + path = VacuumRecognizer.get_random_dir(neural_network, "Earings") world_adder(temp_x, temp_y, neural_network.recognize(path)) for _ in range(config.getint("CONSTANT", "NumberOfBananas")): temp_x = randint(0, tiles_x - 1) temp_y = randint(0, tiles_y - 1) - path = VacuumRecognizer.get_random_dir(neural_network,'Banana') + path = VacuumRecognizer.get_random_dir(neural_network, "Banana") world_adder(temp_x, temp_y, neural_network.recognize(path)) - for x in range(world.width): for y in range(world.height): if world.is_garbage_at(x, y):