diff --git a/Readme.md b/Readme.md index e97d34d..9a9c54f 100644 --- a/Readme.md +++ b/Readme.md @@ -2,7 +2,7 @@ ### 1. Requirements python version 3.9 or higher ```bash -python3 -v +python3 --version ``` ### 2. Create virtual environments and install libs ```bash @@ -35,14 +35,14 @@ VERTICAL_NUM_OF_FIELDS = 3 HORIZONTAL_NUM_OF_FIELDS = 3 ``` \ -####4.1 Save generated map: +#### 4.1 Save generated map: ```bash python main.py --save-map ``` Map will be saved in maps directory. Generated filename: map-uuid -####4.2 Load map +#### 4.2 Load map ```bash python main.py --load-map=name_of_map ``` diff --git a/app/neural_network.py b/app/neural_network.py index f2e517e..d3db826 100644 --- a/app/neural_network.py +++ b/app/neural_network.py @@ -28,9 +28,9 @@ class NeuralNetwork: self.input_shape = (self.img_width, self.img_height, self.img_num_channels) # labels - self.labels = ["cabbage", "carrot", "corn", "lettuce", "paprika", "potato", "sunflower" , "tomato"] + self.labels = ["cabbage", "carrot", "corn", "lettuce", "paprika", "potato", "sunflower", "tomato"] - def init_model(self): + def init_model(self) -> None: if not self.model_dir_is_empty(): # Load the model self.model = load_model( @@ -60,9 +60,9 @@ class NeuralNetwork: shuffle=False) # Display a model summary - #self.model.summary() + # self.model.summary() - def load_images(self): + def load_images(self) -> None: # Create a generator self.train_datagen = ImageDataGenerator( rescale=1. / 255 diff --git a/app/tractor.py b/app/tractor.py index a5f6442..9e14138 100644 --- a/app/tractor.py +++ b/app/tractor.py @@ -210,7 +210,7 @@ class Tractor(BaseField): time.sleep(TIME_OF_MOVING) is_running.clear() - def move_or_rotate(self, movement: str): + def move_or_rotate(self, movement: str) -> None: print(f"Move {movement}") if movement == M_GO_FORWARD: self.move() @@ -267,14 +267,25 @@ class Tractor(BaseField): board.get_fields()[x][y] = obj() return obj() - def harvest_checked_fields_handler(self, is_running: threading.Event): + def harvest_checked_fields_handler(self, is_running: threading.Event) -> None: thread = threading.Thread(target=self.harvest_checked_fields, args=(is_running,), daemon=True) thread.start() - def harvest_checked_fields(self, is_running: threading.Event): - moves = [M_GO_FORWARD, M_ROTATE_LEFT, M_ROTATE_RIGHT] - distribution=[0.6,0.2,0.2] + def go_forward_is_legal_move(self) -> bool: + flag = False + if (self.__direction == D_EAST and self.__pos_y - self.__move >= 0) or \ + (self.__direction == D_NORTH and self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT) or \ + (self.__direction == D_WEST and self.__pos_x - self.__move >= 0) or \ + (self.__direction == D_SOUTH and self.__pos_x + self.__move + FIELD_SIZE <= WIDTH): + flag = True + + return flag + + def harvest_checked_fields(self, is_running: threading.Event) -> None: while True: + moves = [M_GO_FORWARD, M_ROTATE_LEFT, M_ROTATE_RIGHT] + distribution = [0.6, 0.2, 0.2] + field = self.get_field_from_board() self.__neural_network = NeuralNetwork() @@ -283,8 +294,12 @@ class Tractor(BaseField): if prediction.capitalize() in CROPS: self.harvest() break - chosen_move = random.choices(moves,distribution) + if not self.go_forward_is_legal_move(): + moves = moves[1:] + distribution = distribution[1:] + + chosen_move = random.choices(moves, distribution) self.move_or_rotate(chosen_move[0]) time.sleep(1)