Wyświetlanie zdj pizzy po wejściu na górny prawy róg

This commit is contained in:
Łukasz Kinder 2022-05-26 15:01:10 +02:00
parent e0254f8d80
commit 5f198cde38

108
tiles.py
View File

@ -18,7 +18,6 @@ from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D from keras.layers import Conv2D, MaxPooling2D
import math import math
pygame.init() pygame.init()
display = pygame.display.set_mode((640, 640)) display = pygame.display.set_mode((640, 640))
@ -31,6 +30,7 @@ waiterImgR = pygame.image.load("waiterR.png")
tableImg = pygame.image.load('table.png') tableImg = pygame.image.load('table.png')
chairImg = pygame.image.load('chair.png') chairImg = pygame.image.load('chair.png')
clientImg = pygame.image.load('client.png') clientImg = pygame.image.load('client.png')
image = pygame.image.load('test/0.jpg')
class Waiter: class Waiter:
def __init__(self, loc): def __init__(self, loc):
@ -62,6 +62,7 @@ class Waiter:
def right(self): def right(self):
self.loc[0] += 32 self.loc[0] += 32
class Client: class Client:
def __init__(self, loc): def __init__(self, loc):
self.loc = loc self.loc = loc
@ -69,6 +70,7 @@ class Client:
def render(self, surface): def render(self, surface):
surface.blit(clientImg, (self.loc[0], self.loc[1])) surface.blit(clientImg, (self.loc[0], self.loc[1]))
def generate_client(): def generate_client():
loc_for_client = [] loc_for_client = []
for i in chairs: for i in chairs:
@ -78,6 +80,7 @@ def generate_client():
client_coordinates = (loc_for_client[loc]) client_coordinates = (loc_for_client[loc])
return client_coordinates return client_coordinates
class Table: class Table:
def __init__(self, loc, num): def __init__(self, loc, num):
self.loc = loc self.loc = loc
@ -93,7 +96,8 @@ class Table:
def get_number_of_chairs(self): def get_number_of_chairs(self):
return self.num_of_chairs return self.num_of_chairs
def check_collision_with_table(x,y):
def check_collision_with_table(x, y):
answer = False answer = False
for i in tables_coordinates: for i in tables_coordinates:
if i[0] <= x <= i[0] + 32 and i[1] <= y <= i[1] + 32: if i[0] <= x <= i[0] + 32 and i[1] <= y <= i[1] + 32:
@ -110,6 +114,7 @@ class Chair:
def render(self, surface): def render(self, surface):
surface.blit(chairImg, (self.loc[0], self.loc[1])) surface.blit(chairImg, (self.loc[0], self.loc[1]))
def pos_of_chair(table_cor, k): def pos_of_chair(table_cor, k):
pos = ((table_cor[0], table_cor[1] - 32), pos = ((table_cor[0], table_cor[1] - 32),
(table_cor[0], table_cor[1] + 32), (table_cor[0], table_cor[1] + 32),
@ -117,7 +122,8 @@ def pos_of_chair(table_cor, k):
(table_cor[0] + 32, table_cor[1])) (table_cor[0] + 32, table_cor[1]))
return pos[k] return pos[k]
def check_collision_with_chair(x,y):
def check_collision_with_chair(x, y):
answer = False answer = False
for i in chairs: for i in chairs:
for j in i: for j in i:
@ -143,6 +149,7 @@ class Food:
def get_food(self): def get_food(self):
return self.name, self.price return self.name, self.price
class Menu: class Menu:
def __init__(self, card={}): def __init__(self, card={}):
self.card = card self.card = card
@ -153,6 +160,7 @@ class Menu:
def add_to_card(self, dish): def add_to_card(self, dish):
self.card[str(len(self.card) + 1)] = dish self.card[str(len(self.card) + 1)] = dish
class Order(Table): class Order(Table):
def __init__(self, status=False, table=0, dishes=[]): def __init__(self, status=False, table=0, dishes=[]):
self.table = table self.table = table
@ -166,6 +174,7 @@ class Order(Table):
def deliver(self): def deliver(self):
self.status = True self.status = True
class Graph: class Graph:
def __init__(self, num_of_nodes, directed=True): def __init__(self, num_of_nodes, directed=True):
self.m_num_of_nodes = num_of_nodes self.m_num_of_nodes = num_of_nodes
@ -209,6 +218,7 @@ class Graph:
path.reverse() path.reverse()
return path return path
def pathFromTo(start, dest): def pathFromTo(start, dest):
tab2 = [42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57] tab2 = [42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
tab4 = [41, 58] tab4 = [41, 58]
@ -250,6 +260,7 @@ def pathFromTo(start, dest):
path = graph.bfs(start, dest) path = graph.bfs(start, dest)
return path return path
def numToX(num): def numToX(num):
digits = [int(a) for a in str(num)] digits = [int(a) for a in str(num)]
if num >= 100: if num >= 100:
@ -265,14 +276,17 @@ def numToX(num):
x = x * 32 x = x * 32
return x return x
def numToY(num): def numToY(num):
y = (math.floor(num / 20)) * 32 y = (math.floor(num / 20)) * 32
return y return y
def coordsToNum(coords): def coordsToNum(coords):
num = ((coords[1] // 32) * 20) + (coords[0] // 32) num = ((coords[1] // 32) * 20) + (coords[0] // 32)
return int(num) return int(num)
def waiterGo(dest): def waiterGo(dest):
print("Path: ", (pathFromTo(coordsToNum(waiter.loc), dest))) print("Path: ", (pathFromTo(coordsToNum(waiter.loc), dest)))
go = [] go = []
@ -305,6 +319,7 @@ def waiterGo(dest):
pygame.display.update() pygame.display.update()
time.sleep(0.2) time.sleep(0.2)
def mouseToNum(): def mouseToNum():
x = pygame.mouse.get_pos()[0] x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1] y = pygame.mouse.get_pos()[1]
@ -312,6 +327,7 @@ def mouseToNum():
squareNum = coordsToNum(cordTab) squareNum = coordsToNum(cordTab)
return squareNum return squareNum
class Map: class Map:
def __init__(self): def __init__(self):
self.arr = np.zeros((20, 20)) self.arr = np.zeros((20, 20))
@ -326,6 +342,7 @@ class Map:
def get_arr(self): def get_arr(self):
return self.arr.transpose() return self.arr.transpose()
class Tile(): class Tile():
def __init__(self, parent=None, loc=None): def __init__(self, parent=None, loc=None):
self.parent = parent self.parent = parent
@ -337,6 +354,7 @@ class Tile():
def __eq__(self, other): def __eq__(self, other):
return self.position == other.position return self.position == other.position
def astar(map, start, end): def astar(map, start, end):
start_tile = Tile(None, start) start_tile = Tile(None, start)
start_tile.g = start_tile.h = start_tile.f = 0 start_tile.g = start_tile.h = start_tile.f = 0
@ -379,7 +397,8 @@ def astar(map, start, end):
skip = True skip = True
if skip is False: if skip is False:
child.g = current_tile.g + map[child.position[0]][child.position[1]] child.g = current_tile.g + map[child.position[0]][child.position[1]]
child.h = np.absolute(child.position[0] - end_tile.position[0]) + np.absolute(child.position[1] - end_tile.position[1]) child.h = np.absolute(child.position[0] - end_tile.position[0]) + np.absolute(
child.position[1] - end_tile.position[1])
child.f = child.g + child.h child.f = child.g + child.h
for open_node in open_list: for open_node in open_list:
if child == open_node and child.g > open_node.g: if child == open_node and child.g > open_node.g:
@ -388,7 +407,7 @@ def astar(map, start, end):
def tell_preferences(): def tell_preferences():
possibilities = [[30, 40 ,50, None], possibilities = [[30, 40, 50, None],
[True, False, None], [True, False, None],
[True, False, None], [True, False, None],
["high", "low", None], ["high", "low", None],
@ -401,6 +420,7 @@ def tell_preferences():
return choices return choices
def evaluate_preferences(preferences): def evaluate_preferences(preferences):
data = [] data = []
if preferences[0] == 30: if preferences[0] == 30:
@ -499,11 +519,13 @@ def choose_pizza(prefernce):
return ans return ans
def append_df_to_excel(df, excel_path): def append_df_to_excel(df, excel_path):
df_excel = pd.read_excel(excel_path) df_excel = pd.read_excel(excel_path)
result = pd.concat([df_excel, df], ignore_index=True) result = pd.concat([df_excel, df], ignore_index=True)
result.to_excel(excel_path, index=False) result.to_excel(excel_path, index=False)
def append_choice(ans, pre, d, df): def append_choice(ans, pre, d, df):
new_row = pre new_row = pre
new_row.append(list(d.keys())[list(d.values()).index(int(ans))]) new_row.append(list(d.keys())[list(d.values()).index(int(ans))])
@ -540,15 +562,21 @@ def append_choice(ans, pre, d, df):
n_df = pd.DataFrame(data, index=[len(df) + 1]) n_df = pd.DataFrame(data, index=[len(df) + 1])
append_df_to_excel(n_df, "restaurant.xlsx") append_df_to_excel(n_df, "restaurant.xlsx")
def get_pizza(number): def get_pizza(number):
with open("dishes.json") as f: with open("dishes.json") as f:
data = json.load(f) data = json.load(f)
for i in data: for i in data:
if i["pos_in_card"] == int(number): if i["pos_in_card"] == int(number):
food = Food(i['name'], i['pos_in_card'], i['price'], i['spiciness'], i['vege'], i['size'], i['allergens'], i['ingridients'], i['drink_in']) food = Food(i['name'], i['pos_in_card'], i['price'], i['spiciness'], i['vege'], i['size'], i['allergens'],
i['ingridients'], i['drink_in'])
return food return food
#network
def display_img(surface, image):
surface.blit(image, (120, 120))
def create_training_data(): def create_training_data():
DATADIR = "Images" DATADIR = "Images"
CATEGORIES = ["yes", "no"] CATEGORIES = ["yes", "no"]
@ -575,10 +603,11 @@ def create_training_data():
y = np.array(y) y = np.array(y)
print("Training data created!") print("Training data created!")
return X,y return X, y
def learn_neural_network(X,y):
X = X/255.0 def learn_neural_network(X, y):
X = X / 255.0
model = Sequential() model = Sequential()
@ -605,16 +634,19 @@ def learn_neural_network(X,y):
return model return model
def prepare_img(filepath): def prepare_img(filepath):
IMG_SIZE = 90 IMG_SIZE = 90
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1) / 255 return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1) / 255
def predict(model): def predict(model):
return model.predict([prepare_img('directory')]) return model.predict([prepare_img('directory')])
def reusult(prediction):
def result(prediction):
if prediction[0][0] >= 0.5: if prediction[0][0] >= 0.5:
print(math.ceil(prediction[0][0])) print(math.ceil(prediction[0][0]))
print('No pepperoni') print('No pepperoni')
@ -623,16 +655,30 @@ def reusult(prediction):
print("Pepperoni") print("Pepperoni")
#####################neural network##############################
DATADIR = "C:/Datasets/Ingridients"
CATEGORIES = ["yes", "no"]
IMG_SIZE = 90
training_data = []
create_training_data()
X = []
y = []
for features, label in training_data:
X.append(features)
y.append(label)
X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
y = np.array(y)
"""
m = learn_neural_network(X, y)
prediction = m.predict([prepare_img('p1.jpg')])
print(prediction[0][0])
result(prediction)
"""
#######################################################
map = Map() map = Map()
waiter = Waiter([32, 32]) waiter = Waiter([32, 32])
@ -656,8 +702,11 @@ for table in tables:
client = Client(generate_client()) client = Client(generate_client())
def main(): def main():
direction = [] direction = []
first_time = True
number = 0
while True: while True:
clock.tick(10) clock.tick(10)
@ -681,6 +730,7 @@ def main():
for chair_list in chairs: for chair_list in chairs:
for chair in chair_list: for chair in chair_list:
chair.render(display) chair.render(display)
key = pygame.key.get_pressed() key = pygame.key.get_pressed()
left, middle, right = pygame.mouse.get_pressed() left, middle, right = pygame.mouse.get_pressed()
if middle: if middle:
@ -689,7 +739,9 @@ def main():
while True: while True:
x = pygame.mouse.get_pos()[1] x = pygame.mouse.get_pos()[1]
y = pygame.mouse.get_pos()[0] y = pygame.mouse.get_pos()[0]
if y > 608 or y < 32 or x > 608 or x < 32 or check_collision_with_table(y,x) or check_collision_with_chair(y, x): if y > 608 or y < 32 or x > 608 or x < 32 or check_collision_with_table(y,
x) or check_collision_with_chair(
y, x):
print("I can't go there") print("I can't go there")
break break
goal = (x // 32, y // 32) goal = (x // 32, y // 32)
@ -715,16 +767,18 @@ def main():
break break
print() print()
print("Hello Sir, tell me yours preferences") print("Hello Sir, tell me yours preferences")
print("Pass: 'budget', 'spiciness', 'vege', 'level_of_hunger', 'allergy', 'favorite_ingridient', 'drink_in'\n") print(
"Pass: 'budget', 'spiciness', 'vege', 'level_of_hunger', 'allergy', 'favorite_ingridient', 'drink_in'\n")
print("Here is my list of preferences") print("Here is my list of preferences")
ingridients = tell_preferences() ingridients = tell_preferences()
print(ingridients) print(ingridients)
print() print()
pizza = get_pizza(choose_pizza(evaluate_preferences(ingridients))) pizza = get_pizza(choose_pizza(evaluate_preferences(ingridients)))
print("Our proposition:") print("Our proposition:")
print("Name = {}\nprice = {}\nspiciness = {}\nvege = {}\nsize = {}\nallergens = {}\ningridients = {}\ndrink_in = {}\n" print(
.format(pizza.name,pizza.price, pizza.spiciness,pizza.vege,pizza.size,pizza.allergens,pizza.ingridients,pizza.drink_in)) "Name = {}\nprice = {}\nspiciness = {}\nvege = {}\nsize = {}\nallergens = {}\ningridients = {}\ndrink_in = {}\n"
.format(pizza.name, pizza.price, pizza.spiciness, pizza.vege, pizza.size, pizza.allergens,
pizza.ingridients, pizza.drink_in))
if len(direction) > 0: if len(direction) > 0:
d = direction.pop(0) d = direction.pop(0)
@ -742,6 +796,16 @@ def main():
pass pass
waiter.render(display) waiter.render(display)
client.render(display) client.render(display)
if waiter.loc == [576, 32]:
if first_time:
number = np.random.randint(20)
image = pygame.image.load('test/' + str(number) + '.jpg')
first_time = False
display_img(display, image)
else:
first_time = True
pygame.display.update() pygame.display.update()