tractor moves in loop now
This commit is contained in:
parent
43bfb278d0
commit
21681b7ef1
Binary file not shown.
Binary file not shown.
@ -7,6 +7,7 @@ import matplotlib.pyplot as plt
|
||||
from NN.model import *
|
||||
from PIL import Image
|
||||
import pygame
|
||||
from area.constants import GREY
|
||||
|
||||
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)
|
||||
return testImage
|
||||
|
||||
#display the image for prediction next to the field
|
||||
def display_image(screen, image_path, position):
|
||||
image = pygame.image.load(image_path)
|
||||
image = pygame.transform.scale(image, (250, 250))
|
||||
screen.blit(image, position)
|
||||
|
||||
#display result of the guessed image (text under the image)
|
||||
def display_result(screen, position, predicted_class):
|
||||
font = pygame.font.Font(None, 30)
|
||||
displayed_text = font.render("The predicted image is: "+str(predicted_class), 1, (255,255,255))
|
||||
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):
|
||||
with torch.no_grad():
|
||||
testOutput = model(image_tensor)
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
source/__pycache__/genetic.cpython-311.pyc
Normal file
BIN
source/__pycache__/genetic.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -156,6 +156,6 @@ def do_actions(tractor, WIN, move_list):
|
||||
tractor.rotate_to_left()
|
||||
tractor.draw_tractor(WIN)
|
||||
pygame.display.update()
|
||||
time.sleep(1)
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
|
@ -12,7 +12,7 @@ from ground import Dirt
|
||||
from plant import Plant
|
||||
from bfs import graphsearch, Istate, succ
|
||||
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 genetic import genetic_algorithm
|
||||
|
||||
@ -30,6 +30,24 @@ def main():
|
||||
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)
|
||||
@ -45,34 +63,20 @@ def main():
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
#graphsearch activation:
|
||||
istate = Istate(170, 100, 2) #initial state
|
||||
|
||||
goaltest = []
|
||||
goaltest.append(tile_x) #final state (consists of x and y because direction doesnt matter)
|
||||
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
|
||||
|
||||
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
|
||||
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 = (graphsearch(istate, succ, goaltest, tractor)) #<-------BFS
|
||||
moves = (a_star(istate, succ, goaltest, tractor))
|
||||
print(moves)
|
||||
|
||||
|
||||
|
||||
#main loop:
|
||||
while run:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
time.sleep(1)
|
||||
|
||||
# movement based on route-planning (test):
|
||||
# movement based on route-planning:
|
||||
|
||||
tractor.draw_tractor(WIN)
|
||||
time.sleep(1)
|
||||
@ -89,6 +93,7 @@ def main():
|
||||
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}")
|
||||
@ -155,7 +160,10 @@ def main():
|
||||
#work on field:
|
||||
if predykcje == 'work':
|
||||
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")
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user