AIProjekt/main.py
2020-05-12 18:25:47 +02:00

169 lines
5.7 KiB
Python

import pygame, sys
from traktor import Traktor
import dijkstra as di
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing import image
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 = []
img_dir = './imgs'
for _ in lokalizacje:
imgs.append(img_dir + '/' + random.choice(os.listdir(img_dir)))
#model do rozpoznawania
model = MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights="imagenet", input_tensor=None, pooling=None, classes=1000)
#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((0,0,0))
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)
decoded = decode_predictions(preds, top=1)
print(decoded)
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()