integration plate recognition with the game, add popup

This commit is contained in:
s450026 2020-05-24 23:29:17 +02:00
parent 4dd12ebf10
commit f7f3e5de84
23 changed files with 51 additions and 15 deletions

18
main.py
View File

@ -1,8 +1,10 @@
import secrets
import sys
from random import randrange
from src.decisionTree import *
#from src.SubprojectMaksymilianKierski.PlateRecognition import use_model_to_predict
from src.SubprojectMaksymilianKierski.PlateRecognition import use_model_to_predict, text_speech
if __name__ == "__main__":
# SETUP
@ -18,6 +20,7 @@ if __name__ == "__main__":
goal = None
path = ''
while True:
for event in pygame.event.get():
@ -43,11 +46,12 @@ if __name__ == "__main__":
tree.print()
if event.key == pygame.K_m:
tabPos = [[1, 2], [1, 4], [1, 5], [1, 7], [1, 8], [1, 10], [5, 4], [5, 8]]
print(tabPos[0][0], tabPos[0][1])
if not [waiter.X, waiter.Y] in tabPos:
#save_dataset()
# save_dataset()
x = tabPos[0][0]
y = tabPos[0][1]
@ -55,7 +59,15 @@ if __name__ == "__main__":
path = waiter.findPath(goal)
path = waiter.translatePath(path)
else:
print('hello')
predict = use_model_to_predict('test-{}'.format(randrange(20)))
if predict == 1:
predict = 'This plate is empty'
else:
predict = 'This plate has food'
text_speech('arialnarrow.ttf', 30, predict, (255, 255, 255), (0, 128, 0),
(waiter.X*100), (waiter.Y*50), False, False, screen=graphics.screen)
pygame.display.flip()
if event.key == pygame.K_r:
temp = False

View File

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

View File

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View File

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

View File

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 41 KiB

View File

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

View File

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 106 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 37 KiB

View File

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

View File

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

View File

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 74 KiB

View File

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 76 KiB

View File

Before

Width:  |  Height:  |  Size: 213 KiB

After

Width:  |  Height:  |  Size: 213 KiB

View File

Before

Width:  |  Height:  |  Size: 4.0 KiB

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

View File

@ -16,6 +16,8 @@ from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D,
from tensorflow.keras.callbacks import TensorBoard # tensorboard to analysing our models in period of time
import time
import pygame
current_path = os.path.dirname(__file__)
relative_path = os.path.join(current_path, 'Data/')
@ -28,6 +30,7 @@ training_data = []
X = [] # feature set
y = [] # label set
def create_training_data():
for category in CATEGORIES:
path = os.path.join(DATADIR, category) # path to plate images dir
@ -60,20 +63,20 @@ def pack_dataset():
def save_dataset():
create_training_data()
X = pack_dataset()
pickle_out = open(relative_path+'SavedData/X.pickle', 'wb')
pickle_out = open(relative_path + 'SavedData/X.pickle', 'wb')
pickle.dump(X, pickle_out)
pickle_out.close()
pickle_out = open(relative_path+'SavedData/y.pickle', 'wb')
pickle_out = open(relative_path + 'SavedData/y.pickle', 'wb')
pickle.dump(y, pickle_out)
pickle_out.close()
def load_dataset():
pickle_in = open(relative_path+'SavedData/X.pickle', 'rb')
pickle_in = open(relative_path + 'SavedData/X.pickle', 'rb')
X = pickle.load(pickle_in)
pickle_in = open(relative_path+'SavedData/y.pickle', 'rb')
pickle_in = open(relative_path + 'SavedData/y.pickle', 'rb')
y = pickle.load(pickle_in)
return X, y
@ -112,7 +115,7 @@ def creat_model():
model.fit(X, y, batch_size=32, epochs=10, validation_split=0.1, callbacks=[tenserboard])
model.save(relative_path+'SavedModels/plate-64x2-cnn.model')
model.save(relative_path + 'SavedModels/plate-64x2-cnn.model')
def use_model_to_predict(name):
@ -121,16 +124,37 @@ def use_model_to_predict(name):
def prepare(filepath):
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # The same which in model
plt.imshow(new_array, cmap=plt.cm.binary)
plt.imshow(img_array, cmap=plt.cm.binary)
plt.show()
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
model = tf.keras.models.load_model(relative_path+'SavedModels/plate-64x2-cnn.model')
prediction = model.predict([prepare(relative_path+'TestData/' + name + '.jpg')])
model = tf.keras.models.load_model(relative_path + 'SavedModels/plate-64x2-cnn.model')
prediction = model.predict([prepare(relative_path + 'TestData/' + name + '.jpg')])
print(CATEGORIES[int(prediction[0][0])])
print(prediction)
print(CATEGORIES[int(prediction[0][0])])
return int(prediction[0][0])
#save_dataset()
use_model_to_predict('test-full-5')
#creat_model()
def text_speech(font: str, size: int, text: str, color, background, x, y, bold: bool, bubble: bool, screen):
global textRect
font = pygame.font.SysFont(None, 40)
# font = pygame.font.Font(font,size)
font.set_bold(bold)
textSurf = font.render(text, True, color).convert_alpha()
if bubble:
textSize = textSurf.get_size()
bubbleSurf = pygame.Surface((textSize[0] * 2., textSize[1] * 2))
textRect = bubbleSurf.get_rect()
bubbleSurf.fill(background)
bubbleSurf.blit(textSurf, textSurf.get_rect(center=textRect.center))
textRect.center = (x, y)
screen.blit(bubbleSurf, textRect)
else:
textRect = textSurf.get_rect()
textRect.center = (x, y)
screen.blit(textSurf, textRect)
# save_dataset()
# use_model_to_predict('test-full-5')
# creat_model()