import pygame import time import random import pandas as pd import joblib from area.constants import WIDTH, HEIGHT, TILE_SIZE from area.field import drawWindow from area.tractor import Tractor, do_actions, display_work_results from area.field import tiles, fieldX, fieldY from area.field import get_tile_coordinates, get_tile_index from ground import Dirt from plant import Plant from bfs import graphsearch, Istate, succ_astar, succ from astar import a_star from NN.neural_network import load_model, load_image, guess_image, display_image, display_result, clear_text_area from PIL import Image from genetic import genetic_algorithm from area.field import createTiles pygame.init() WIN_WIDTH = WIDTH + 300 WIN = pygame.display.set_mode((WIN_WIDTH, HEIGHT)) pygame.display.set_caption('Intelligent tractor') def main(): run = True window = drawWindow(WIN) pygame.display.update() #Tractor initialization: tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None) tractor.rect.x += fieldX tractor.rect.y += fieldY tractor.tractor_start = ((170, 100)) istate = Istate(170, 100, 2) #initial state #main loop: while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False time.sleep(1) #getting coordinates of our "goal tile": tile_index = get_tile_index() tile_x, tile_y = get_tile_coordinates(tile_index) if tile_x is not None and tile_y is not None: print(f"Coordinates of tile {tile_index} are: ({tile_x}, {tile_y})") else: print("Such tile does not exist") #mark the goal tile: tiles[tile_index].image = "resources/images/sampling_goal.png" image = pygame.image.load(tiles[tile_index].image).convert() image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE)) WIN.blit(image, (tiles[tile_index].x, tiles[tile_index].y)) pygame.display.flip() tractor.tractor_end = ((tile_x, tile_y)) goaltest = [] #final state (consists of x and y because direction doesnt matter) goaltest.append(tile_x) goaltest.append(tile_y) goaltest[0] = tile_x goaltest[1]=tile_y #moves = (graphsearch(istate, succ, goaltest, tractor)) #<-------BFS moves = (a_star(istate, succ_astar, goaltest)) print(moves) # movement based on route-planning: tractor.draw_tractor(WIN) time.sleep(1) if moves != False: do_actions(tractor, WIN, moves) #guessing the image under the tile: goalTile = tiles[tile_index] image_path = goalTile.photo display_image(WIN, goalTile.photo, (WIDTH-20 , 300)) #displays photo next to the field pygame.display.update() image_tensor = load_image(image_path) prediction = guess_image(load_model(), image_tensor) clear_text_area(WIN, WIDTH - 50, 600, 400, 50) display_result(WIN, (WIDTH - 50 , 600), prediction) #display text under the photo pygame.display.update() print(f"The predicted image is: {prediction}") p1 = Plant('wheat', 'cereal', random.randint(1,100), random.randint(1,100), random.randint(1,100)) goalTile.plant = p1 d1 = Dirt(random.randint(1, 100), random.randint(1,100)) d1.pests_and_weeds() goalTile.ground=d1 #getting the name and type of the recognized plant: p1.update_name(prediction) #decission tree test: if d1.pest: pe = 1 else: pe = 0 if d1.weed: we = 1 else: we = 0 if p1.plant_type == 'cereal': t1 = True t2 = False t3 = False t4 = False else: t1 = False if p1.plant_type == 'fruit': t2 = True t3 = False t4 = False else: t2 = False if p1.plant_type == 'vegetable': t4 = True t3 = False else: t3 = True t4 = False weather_n = random.randint(1, 4) if weather_n == 1: h1 = True h2 = False h3 = False h4 = False else: h1 = False if weather_n == 2: h2 = True h3 = False h4 = False else: h2 = False if weather_n == 3: h3 = True h4 = False else: h3 = False h4 = True season_n = random.randint(1,4) if season_n == 1: s1 = True s2 = False s3 = False s4 = False temp_n = random.randint(0,22) else: s1 = False if season_n == 2: s2 = True s3 = False s4 = False temp_n = random.randint(0,22) else: s2 = False if season_n == 3: s3 = True s4 = False temp_n = random.randint(20,39) else: s3 = False s4 = True temp_n = random.randint(-20, 10) anomaly_n = random.randint(1, 10) if anomaly_n == 1: a1 = True else: a1 = False dane = { 'anomalies': [a1], 'temp': [temp_n], 'water': [d1.water_level], 'nutri': [d1.nutrients_level], 'pests': [pe], 'weeds': [we], 'ripeness': [p1.growth_level], 'season_autumn': [s1], 'season_spring': [s2], 'season_summer': [s3], 'season_winter': [s4], 'weather_heavyCloudy': [h1], 'weather_partCloudy': [h2], 'weather_precipitation': [h3], 'weather_sunny': [h4], 'type_cereal': [t1], 'type_fruit': [t2], 'type_none': [t3], 'type_vegetable': [t4] } df = pd.DataFrame(dane) df.to_csv('model_data.csv', index=False) model = joblib.load('model.pkl') nowe_dane = pd.read_csv('model_data.csv') predykcje = model.predict(nowe_dane) print(predykcje) #work on field: if predykcje == 'work': tractor.work_on_field(WIN, goalTile, d1, p1) #update the initial state for the next target: istate = Istate(tile_x, tile_y, tractor.direction) time.sleep(2) print("\n") main()