forked from s444426/AIProjekt
200 lines
7.1 KiB
Python
200 lines
7.1 KiB
Python
import pygame, sys
|
|
from traktor import Traktor
|
|
import dijkstra as di
|
|
import decisiontree as dt
|
|
from ID3 import predict_data
|
|
|
|
import matplotlib.pyplot as plt
|
|
from tensorflow.keras.preprocessing import image
|
|
from tensorflow.keras.models import load_model
|
|
from tensorflow.keras.applications import MobileNetV2
|
|
from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
|
|
import numpy as np
|
|
import os, random
|
|
#u mnie blad z biblioteka
|
|
os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
|
|
|
class Game(object):
|
|
|
|
def __init__(self):
|
|
|
|
#lokalizacje odpowiednich punktow na planszy
|
|
lokalizacje = [[0,0],
|
|
[100, 0],
|
|
[200, 0],
|
|
[300, 0],
|
|
[400, 0],
|
|
[400, 100],
|
|
[300, 100],
|
|
[200, 100],
|
|
[100, 100],
|
|
[0, 100],
|
|
[0, 200],
|
|
[100, 200],
|
|
[200, 200],
|
|
[300, 200],
|
|
[400, 200],
|
|
[400, 300],
|
|
[300, 300],
|
|
[200, 300],
|
|
[100, 300],
|
|
[0, 300],
|
|
[0, 400],
|
|
[100, 400],
|
|
[200, 400],
|
|
[300, 400],
|
|
[400, 400]]
|
|
|
|
#ładowanie tablicy ze zdjeciami
|
|
imgs = []
|
|
data_plant = []
|
|
data_soil = []
|
|
img_dir = './imgs'
|
|
for _ in lokalizacje:
|
|
data_plant.append([random.randrange(5, 90),random.randrange(0, 120),random.randrange(5, 90)])
|
|
data_soil.append([random.randrange(44, 75)/10, random.randrange(0, 100)/100])
|
|
imgs.append(img_dir + '/' + random.choice(os.listdir(img_dir)))
|
|
|
|
#model do rozpoznawania
|
|
model = load_model('data/moj_model.h5')
|
|
|
|
#inicjalizacja
|
|
pygame.init()
|
|
self.pole = pygame.display.set_mode((501,501))
|
|
|
|
self.player = Traktor(self)
|
|
|
|
sterowanie = int(input("Podaj czy chcesz sam sterowac traktorem - 1, czy chcesz podawac punkty - 0: "))
|
|
if sterowanie == 1:
|
|
sterowanie = True
|
|
else:
|
|
sterowanie = False
|
|
|
|
while True:
|
|
run_classifier = False
|
|
|
|
#obsługa zdarzń
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
pygame.quit()
|
|
sys.exit(0)
|
|
#sterowanie traktorem
|
|
if sterowanie:
|
|
if event.type == pygame.KEYDOWN :
|
|
if event.key == pygame.K_UP:
|
|
self.player.y -= 100
|
|
if self.player.y < 0:
|
|
self.player.y = 0
|
|
if event.key == pygame.K_DOWN:
|
|
self.player.y += 100
|
|
if self.player.y > 400:
|
|
self.player.y = 400
|
|
if event.key == pygame.K_RIGHT:
|
|
self.player.x += 100
|
|
if self.player.x > 400:
|
|
self.player.x = 400
|
|
if event.key == pygame.K_LEFT:
|
|
self.player.x -= 100
|
|
if self.player.x < 0:
|
|
self.player.x = 0
|
|
|
|
#okreslanie w ktorym punkcie jesteśmy
|
|
wsp = [self.player.x, self.player.y]
|
|
self.player.punkt = lokalizacje.index(wsp)
|
|
run_classifier = True
|
|
|
|
else:
|
|
#po spacji wpisujemy punkty
|
|
if event.type == pygame.KEYDOWN :
|
|
if event.key == pygame.K_SPACE:
|
|
pt = 0
|
|
punkt1 = int(input("Podaj pierwszy punkt (liczba od 0 do 24): "))
|
|
punkt2 = int(input("Podaj drugi punkt (liczba od 0 do 24): "))
|
|
dist = di.dijkstra(self.player.punkt)
|
|
|
|
if dist[punkt1] < dist[punkt2]:
|
|
print("Wybralem pierwszy punkt")
|
|
pt = punkt1
|
|
else:
|
|
print("Wybralem drugi punkt")
|
|
pt = punkt2
|
|
|
|
self.player.x = lokalizacje[pt][0]
|
|
self.player.y = lokalizacje[pt][1]
|
|
self.player.punkt = pt
|
|
run_classifier = True
|
|
|
|
|
|
pygame.display.update()
|
|
|
|
#rysowanie
|
|
self.pole.fill((157, 128, 48))
|
|
self.krata()
|
|
self.rysowanie()
|
|
pygame.display.flip()
|
|
|
|
if run_classifier:
|
|
#wybieranie zdjecia
|
|
pt = self.player.punkt
|
|
img_path = imgs[pt]
|
|
img = image.load_img(img_path, target_size=(224, 224))
|
|
x = image.img_to_array(img)
|
|
x = np.expand_dims(x, axis=0)
|
|
x = preprocess_input(x)
|
|
|
|
#detektor roslin, 5 rezultatow
|
|
plt.imshow(img)
|
|
preds = model.predict(x)
|
|
list_of_preds = preds.tolist()[0]
|
|
index = list_of_preds.index(max(list_of_preds))
|
|
#print(index)
|
|
|
|
names = ['cabbage', 'cauliflower', 'mushroom', 'pumpkin']
|
|
|
|
# decyzja o posadzeniu nowej rośliny następuje wtedy, gdy jest ona dojrzała przynajmniej w 90%
|
|
if(data_plant[pt][1] > 90):
|
|
|
|
new_plant = predict_data([names[index], data_soil[pt][0], data_soil[pt][1]])
|
|
if(new_plant != 'none'):
|
|
index = names.index(new_plant)
|
|
imgs[pt] = img_dir + '/' + (os.listdir(img_dir))[index]
|
|
data_plant[pt] = [0, 0, 50]
|
|
else:
|
|
print("Planted: none")
|
|
|
|
if index == 0:
|
|
dt.decision(4, data_plant[pt][0],data_plant[pt][1],data_plant[pt][2])
|
|
if index == 1:
|
|
dt.decision(1, data_plant[pt][0],data_plant[pt][1],data_plant[pt][2])
|
|
if index == 2:
|
|
dt.decision(2, data_plant[pt][0],data_plant[pt][1],data_plant[pt][2])
|
|
if index == 3:
|
|
dt.decision(3, data_plant[pt][0],data_plant[pt][1],data_plant[pt][2])
|
|
|
|
print("kapusta, kalafior, grzyb, dynia\n {}\n".format(preds) )
|
|
plt.show()
|
|
|
|
def krata(self):
|
|
#wymiary
|
|
w= 500
|
|
rows = 5
|
|
sizeBtw = w // rows
|
|
|
|
x = 0
|
|
y = 0
|
|
for r in range(rows):
|
|
x = x + sizeBtw
|
|
y = y + sizeBtw
|
|
|
|
#rysownie karaty
|
|
pygame.draw.line(self.pole, (255,255,255), (x,0), (x,w))
|
|
pygame.draw.line(self.pole, (255,255,255), (0,y), (w,y))
|
|
|
|
|
|
def rysowanie(self):
|
|
#rysowanie agenta
|
|
self.player.rysowanie()
|
|
|
|
|
|
if __name__=="__main__":
|
|
Game() |