tractor moves in loop now

This commit is contained in:
MarRac 2024-06-09 16:37:41 +02:00
parent 43bfb278d0
commit 21681b7ef1
16 changed files with 47 additions and 32 deletions

View File

@ -7,6 +7,7 @@ import matplotlib.pyplot as plt
from NN.model import * from NN.model import *
from PIL import Image from PIL import Image
import pygame import pygame
from area.constants import GREY
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
@ -84,16 +85,22 @@ def load_image(image_path):
testImage = testImage.unsqueeze(0) testImage = testImage.unsqueeze(0)
return testImage return testImage
#display the image for prediction next to the field
def display_image(screen, image_path, position): def display_image(screen, image_path, position):
image = pygame.image.load(image_path) image = pygame.image.load(image_path)
image = pygame.transform.scale(image, (250, 250)) image = pygame.transform.scale(image, (250, 250))
screen.blit(image, position) screen.blit(image, position)
#display result of the guessed image (text under the image)
def display_result(screen, position, predicted_class): def display_result(screen, position, predicted_class):
font = pygame.font.Font(None, 30) font = pygame.font.Font(None, 30)
displayed_text = font.render("The predicted image is: "+str(predicted_class), 1, (255,255,255)) displayed_text = font.render("The predicted image is: "+str(predicted_class), 1, (255,255,255))
screen.blit(displayed_text, position) screen.blit(displayed_text, position)
def clear_text_area(win, x, y, width, height, color=GREY):
pygame.draw.rect(win, color, (x, y, width, height))
pygame.display.update()
def guess_image(model, image_tensor): def guess_image(model, image_tensor):
with torch.no_grad(): with torch.no_grad():
testOutput = model(image_tensor) testOutput = model(image_tensor)

Binary file not shown.

View File

@ -156,6 +156,6 @@ def do_actions(tractor, WIN, move_list):
tractor.rotate_to_left() tractor.rotate_to_left()
tractor.draw_tractor(WIN) tractor.draw_tractor(WIN)
pygame.display.update() pygame.display.update()
time.sleep(1) time.sleep(0.5)

View File

@ -12,7 +12,7 @@ from ground import Dirt
from plant import Plant from plant import Plant
from bfs import graphsearch, Istate, succ from bfs import graphsearch, Istate, succ
from astar import a_star from astar import a_star
from NN.neural_network import load_model, load_image, guess_image, display_image, display_result from NN.neural_network import load_model, load_image, guess_image, display_image, display_result, clear_text_area
from PIL import Image from PIL import Image
from genetic import genetic_algorithm from genetic import genetic_algorithm
@ -30,7 +30,25 @@ def main():
window = drawWindow(WIN) window = drawWindow(WIN)
pygame.display.update() pygame.display.update()
#getting coordinates of our "goal tile":
#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_index = get_tile_index()
tile_x, tile_y = get_tile_coordinates(tile_index) tile_x, tile_y = get_tile_coordinates(tile_index)
if tile_x is not None and tile_y is not None: if tile_x is not None and tile_y is not None:
@ -45,34 +63,20 @@ def main():
pygame.display.flip() pygame.display.flip()
#graphsearch activation: tractor.tractor_end = ((tile_x, tile_y))
istate = Istate(170, 100, 2) #initial state goaltest = [] #final state (consists of x and y because direction doesnt matter)
goaltest.append(tile_x)
goaltest = []
goaltest.append(tile_x) #final state (consists of x and y because direction doesnt matter)
goaltest.append(tile_y) goaltest.append(tile_y)
goaltest[0] = tile_x
goaltest[1]=tile_y
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None) #moves = (graphsearch(istate, succ, goaltest, tractor)) #<-------BFS
tractor.rect.x += fieldX
tractor.rect.y += fieldY
tractor.tractor_start = ((istate.get_x(), istate.get_y()))
#tractor.tractor_start = ((istate.get_x(), istate.get_y(), istate.get_direction))
tractor.tractor_end = ((goaltest[0], goaltest[1]))
#moves = (graphsearch(istate, succ, goaltest, tractor))
moves = (a_star(istate, succ, goaltest, tractor)) moves = (a_star(istate, succ, goaltest, tractor))
print(moves) print(moves)
#main loop: # movement based on route-planning:
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
time.sleep(1)
# movement based on route-planning (test):
tractor.draw_tractor(WIN) tractor.draw_tractor(WIN)
time.sleep(1) time.sleep(1)
@ -89,6 +93,7 @@ def main():
image_tensor = load_image(image_path) image_tensor = load_image(image_path)
prediction = guess_image(load_model(), image_tensor) 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 display_result(WIN, (WIDTH - 50 , 600), prediction) #display text under the photo
pygame.display.update() pygame.display.update()
print(f"The predicted image is: {prediction}") print(f"The predicted image is: {prediction}")
@ -155,7 +160,10 @@ def main():
#work on field: #work on field:
if predykcje == 'work': if predykcje == 'work':
tractor.work_on_field(goalTile, d1, p1) tractor.work_on_field(goalTile, d1, p1)
time.sleep(50)
#update the initial state for the next target:
istate = Istate(tile_x, tile_y, tractor.direction)
time.sleep(5)
print("\n") print("\n")