IntelligentTractor/main.py

143 lines
4.9 KiB
Python
Raw Permalink Normal View History

2023-03-07 12:28:13 +01:00
import pygame
import sys
import random
from settings import SIZE, directions, draw_lines_on_window, matrix_plants_type
2023-06-03 22:42:07 +02:00
from src.map import drawRoads, seedForFirstTime, return_fields_list, WORLD_MATRIX, get_type_by_position
from src.Tractor import Tractor
from src.bfs import Astar
2023-05-28 10:23:30 +02:00
from src.Plant import Plant
2023-05-25 18:11:06 +02:00
from src.Field import Field
2023-05-25 20:17:48 +02:00
import pickle
import os
2023-06-06 18:52:04 +02:00
from src.ID3 import action
2023-06-03 11:22:42 +02:00
import torch
2023-06-03 22:42:07 +02:00
import src.neural_networks as neural_networks
2023-06-03 11:22:42 +02:00
2023-06-03 22:42:07 +02:00
def show_plant_img(img):
image = pygame.image.load(img)
image = pygame.transform.scale(image, (360, 360))
screen.blit(image, (972, 288))
pygame.display.update()
pygame.time.delay(1000)
# neural_networks.learn()
def recognize_plants(fields, destination):
checkpoint = torch.load(f'plants2.model')
2023-06-03 22:11:48 +02:00
model = neural_networks.Net(num_classes=3)
2023-06-03 11:24:47 +02:00
model.load_state_dict(checkpoint)
model.eval()
img = ''
2023-06-03 22:42:07 +02:00
if get_type_by_position(fields, destination[0], destination[1]) == 'carrot':
img = 'assets/learning/test/carrot/' + str(random.randint(1, 200)) + '.jpg'
pred = neural_networks.predict(img, model)
show_plant_img(img)
elif get_type_by_position(fields, destination[0], destination[1]) == 'potato':
img = 'assets/learning/test/potato/' + str(random.randint(1, 200)) + '.jpg'
pred = neural_networks.predict(img, model)
show_plant_img(img)
elif get_type_by_position(fields, destination[0], destination[1]) == 'wheat':
img = 'assets/learning/test/wheat/' + str(random.randint(1, 200)) + '.jpg'
pred = neural_networks.predict(img, model)
show_plant_img(img)
else:
pred = 'none'
print(pred)
2023-06-06 18:52:04 +02:00
return pred
2023-03-07 12:28:13 +01:00
# pygame initialization
2023-03-07 12:28:13 +01:00
pygame.init()
clock = pygame.time.Clock()
2023-06-06 18:52:04 +02:00
# GAME SCREEN
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Traktor_interaktor")
background = pygame.image.load("assets/farmland.jpg")
background = pygame.transform.scale(background,SIZE)
screen.fill((90,50,20))
background.fill((90,50,20))
background = drawRoads(background)
2023-03-07 12:28:13 +01:00
2023-06-06 18:52:04 +02:00
draw_lines_on_window(background)
#TRACTOR
tractor = Tractor('oil','manual', 'fuel', 'fertilizer1', 20)
tractor_group = pygame.sprite.Group()
tractor_group.add(tractor)
2023-05-28 10:23:30 +02:00
tractor.setCapacity(90)
tractor.setFuel(100)
#PLANTS
plant_group = pygame.sprite.Group()
plant_group = seedForFirstTime()
fields = return_fields_list()
2023-03-07 12:28:13 +01:00
#
tractor_move = pygame.USEREVENT + 1
2023-06-03 22:42:07 +02:00
pygame.time.set_timer(tractor_move, 200)
moves = []
goal_astar = Astar()
2023-06-06 18:52:04 +02:00
mx = random.randrange(0, 936, 36)
my = random.randrange(0, 936, 36)
2023-05-25 18:11:06 +02:00
destination = (mx, my)
print("Destination: ", destination)
2023-06-06 18:52:04 +02:00
mx = int((mx+18)/36)
my = int((my+18)/36)
print("Destination: ", mx, my)
2023-05-28 10:23:30 +02:00
2023-05-28 12:20:54 +02:00
#ID3 TREE LOADING
2023-05-28 10:23:30 +02:00
dtree = pickle.load(open(os.path.join('src','tree.plk'),'rb'))
# pobierz dane o polu field i czy ma na sobie roslinke, zadecyduj czy zebrac
this_field = WORLD_MATRIX[mx][my]
this_contain = Field.getContain(this_field)
2023-06-03 22:42:07 +02:00
2023-05-25 18:11:06 +02:00
moves = goal_astar.search(
[tractor.rect.x, tractor.rect.y, directions[tractor.rotation]], destination)
2023-06-03 22:42:07 +02:00
expected_plant = get_type_by_position(fields, destination[0], destination[1])
if __name__ == "__main__":
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
tractor.collect(plant_group)
2023-06-06 18:52:04 +02:00
# recognize_plants(fields, destination)
if event.key == pygame.K_ESCAPE:
running = False
if event.type == tractor_move:
if len(moves) != 0:
moves_list = list(moves) # convert to list
step = moves_list.pop() # pop the last element
moves = tuple(moves_list) # convert back to tuple
tractor.movement(step[0])
2023-06-06 18:52:04 +02:00
# checks if tractor is in destiantion field and make decision if it's ready to collect
if tractor.rect.x == destination[0] and tractor.rect.y == destination[1] and action(this_contain, Plant, tractor, dtree) == 1:
# show what should be in this field
2023-06-03 22:42:07 +02:00
print('expected:', expected_plant)
2023-06-06 18:52:04 +02:00
# check if program correctly recognize plant
if recognize_plants(fields, destination) == expected_plant:
# if correctly recognized than plant can be collected
2023-06-03 22:42:07 +02:00
tractor.collect(plant_group)
2023-06-06 18:52:04 +02:00
else:
print('wrong recognition')
Tractor.movement_using_keys(tractor)
screen.blit(background,(0,0))
plant_group.draw(screen)
tractor_group.draw((screen))
tractor_group.update()
pygame.display.flip()
clock.tick(60)