visualising sectors & neural network
@ -45,7 +45,7 @@ class Environment:
|
||||
def update_all_elements(self):
|
||||
self.use_astar() # wywyoływanie za każdym razem astar jest bardzo zasobożerne. Lepiej raz na przejście
|
||||
self.update_truck()
|
||||
# time.sleep(0.5)
|
||||
time.sleep(0.2)
|
||||
|
||||
def use_astar(self):
|
||||
start_state = State(1,self.truck.x,self.truck.y) # sprawić aby paczka i shelf były wyszukiwane raz
|
||||
|
@ -34,7 +34,6 @@ class Moving_truck:
|
||||
self.astar.reset_grid()
|
||||
self.move_without_swapping(truck_x, truck_y, package_x, package_y)
|
||||
|
||||
|
||||
def move_package_to_shelf(self, x, y):
|
||||
truck_x = self.truck.x
|
||||
truck_y = self.truck.y
|
||||
|
22
Package.py
@ -8,12 +8,13 @@ from Package_types import Package_types
|
||||
import math
|
||||
from CompanyFactory import CompanyFactory
|
||||
from decision_tree.Decision_tree import DecisionTree
|
||||
from neural_network.NeuralNetwork import NeuralNetwork
|
||||
|
||||
|
||||
class Package(Field):
|
||||
def __init__(self, window, x, y, type):
|
||||
Field.__init__(self, window, x, y)
|
||||
self.mark_image = self.get_marking_photo()
|
||||
# self.mark_image = self.get_marking_photo()
|
||||
self.type = type
|
||||
self.company = CompanyFactory()
|
||||
self.payed_upfront = random.randint(0,1)
|
||||
@ -31,27 +32,12 @@ class Package(Field):
|
||||
self.company.payment_delay, self.payed_upfront,
|
||||
self.company.shipping_type)
|
||||
decision = tree.decision
|
||||
# print("Decision tree: ", decision)
|
||||
return decision
|
||||
|
||||
def get_marking_photo(self):
|
||||
file_path_type = ["resources/package_markings/*.jpg"]
|
||||
images = glob2.glob(random.choice(file_path_type))
|
||||
random_image = random.choice(images)
|
||||
return random_image
|
||||
|
||||
def draw(self):
|
||||
self.color = Types_colors.get_package_color(self.type)
|
||||
# self.color = (110,64,48)
|
||||
block = pygame.Rect(self.x * G_var().RECT_SIZE, self.y *
|
||||
G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE)
|
||||
pygame.draw.rect(self.window, self.color, block)
|
||||
|
||||
def choose_the_closest_shelf(self, shelves):
|
||||
array = []
|
||||
for shelf in shelves:
|
||||
if shelf.type == self.type:
|
||||
segment = math.sqrt((shelf.x - self.x) **
|
||||
2 + (shelf.y - self.y)**2)
|
||||
array.append((segment, shelf))
|
||||
array.sort(key=lambda y: y[0])
|
||||
print(array)
|
||||
return array[0]
|
||||
|
@ -3,19 +3,40 @@ from Global_variables import Global_variables as G_var
|
||||
from Package import Package
|
||||
from Package_types import Package_types
|
||||
from decision_tree.Decision_tree import DecisionTree
|
||||
import glob2
|
||||
from neural_network.NeuralNetwork import NeuralNetwork
|
||||
|
||||
|
||||
class Packages_spawner:
|
||||
def __init__(self, window,enviroment_2d):
|
||||
self.window = window
|
||||
self.enviroment_2d = enviroment_2d
|
||||
|
||||
|
||||
|
||||
def read_photo_and_determine_marking(self): # USING NEURAL NETWORK
|
||||
image = self.get_marking_photo()
|
||||
net = NeuralNetwork()
|
||||
marking = net.use_neural_net(image)
|
||||
if marking == "fragile":
|
||||
type = Package_types.fragile
|
||||
elif marking == "priority":
|
||||
type = Package_types.priority
|
||||
return type
|
||||
|
||||
def spawn_package(self):
|
||||
package_x = random.randrange(22, 26)
|
||||
package_y = random.randrange(1, 13)
|
||||
package_type = random.choice(list(Package_types))
|
||||
# package_type = random.choice(list(Package_types))
|
||||
package_type = self.read_photo_and_determine_marking()
|
||||
new_package = Package(self.window, package_x, package_y, package_type)
|
||||
# sector_type = self.use_decision_tree(new_package, weekend)
|
||||
self.enviroment_2d[package_x][package_y] = new_package
|
||||
# return sector_type
|
||||
|
||||
def get_marking_photo(self):
|
||||
file_path_type = ["resources/markings/*.png"]
|
||||
images = glob2.glob(random.choice(file_path_type))
|
||||
random_image = random.choice(images)
|
||||
return random_image
|
||||
|
||||
|
||||
|
||||
|
||||
|
22
Shelf.py
@ -1,7 +1,7 @@
|
||||
import pygame
|
||||
from Field import Field
|
||||
from Global_variables import Global_variables as G_var
|
||||
from Package_types import Package_types
|
||||
from Sectors_types import Sectors_types
|
||||
from Types_colors import Types_colors
|
||||
|
||||
|
||||
@ -14,7 +14,25 @@ class Shelf(Field):
|
||||
self.rect = pygame.Rect(self.x * G_var().RECT_SIZE, self.y *
|
||||
G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE)
|
||||
|
||||
|
||||
|
||||
def draw(self):
|
||||
my_font = pygame.font.SysFont('Arial', 30)
|
||||
self.rect = pygame.Rect(self.x * G_var().RECT_SIZE, self.y *
|
||||
G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE)
|
||||
pygame.draw.rect(self.window, self.color, self.rect)
|
||||
pygame.draw.rect(self.window, self.color, self.rect)
|
||||
text = self.choose_letter_acc_to_sector()
|
||||
text_surface = my_font.render(text, False, (255, 255, 255))
|
||||
self.window.blit(text_surface, (self.x * G_var().RECT_SIZE, self.y *
|
||||
G_var().RECT_SIZE))
|
||||
|
||||
def choose_letter_acc_to_sector(self):
|
||||
if self.sector == Sectors_types.normal:
|
||||
letter = "N"
|
||||
elif self.sector == Sectors_types.fragile:
|
||||
letter = "F"
|
||||
elif self.sector == Sectors_types.shipping_today:
|
||||
letter = "T!"
|
||||
else:
|
||||
letter = "T"
|
||||
return letter
|
||||
|
@ -36,7 +36,6 @@ class DecisionTree:
|
||||
decision = Sectors_types.shipping_tomorrow
|
||||
elif decision == 4:
|
||||
decision = Sectors_types.shipping_today
|
||||
# print(decision)
|
||||
return decision
|
||||
|
||||
def tree_as_txt(self, decision_tree):
|
||||
@ -62,4 +61,4 @@ class DecisionTree:
|
||||
# # tree_as_txt(drzewo)
|
||||
# tree_to_png(drzewo)
|
||||
# # tree_to_structure(drzewo)
|
||||
#
|
||||
|
||||
|
71
neural_network/NeuralNetwork.py
Normal file
@ -0,0 +1,71 @@
|
||||
import cv2
|
||||
import matplotlib.pyplot as plt
|
||||
import os
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
||||
import tensorflow as tf
|
||||
from tensorflow.keras import models, layers
|
||||
import numpy as np
|
||||
|
||||
BATCH_SIZE = 1 # 32
|
||||
IMG_SIZE = (32, 32)
|
||||
# MODEL_PATH = "C:/Users/annas/Desktop/projekty_studia/sztuczna/AIprojekt-wozek/neural_network/image_classifier.model"
|
||||
MODEL_PATH = "neural_network/image_classifier.model"
|
||||
|
||||
|
||||
class NeuralNetwork:
|
||||
# def __init__(s):
|
||||
# self.photo = photo
|
||||
|
||||
def load_data(self):
|
||||
path_markings = "resources/package_markings_data/"
|
||||
path_val_markings = "resources/package_markings_val/"
|
||||
|
||||
training_images = tf.keras.utils.image_dataset_from_directory(path_markings, shuffle=True,
|
||||
batch_size=BATCH_SIZE,
|
||||
image_size=IMG_SIZE)
|
||||
|
||||
validation_data = tf.keras.utils.image_dataset_from_directory(path_val_markings, shuffle=True,
|
||||
batch_size=BATCH_SIZE,
|
||||
image_size=IMG_SIZE)
|
||||
AUTOTUNE = tf.data.AUTOTUNE
|
||||
train_dataset = training_images.prefetch(buffer_size=AUTOTUNE)
|
||||
validation_dataset = validation_data.prefetch(buffer_size=AUTOTUNE)
|
||||
return train_dataset, validation_dataset
|
||||
|
||||
def train(self):
|
||||
model = models.Sequential()
|
||||
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
|
||||
model.add(layers.MaxPooling2D((2, 2)))
|
||||
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(layers.MaxPooling2D((2, 2)))
|
||||
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(layers.Flatten())
|
||||
model.add(layers.Dense(64, activation='relu'))
|
||||
model.add(layers.Dense(2, activation='softmax'))
|
||||
|
||||
model.compile(optimizer='adam', loss="sparse_categorical_crossentropy", metrics=['accuracy'])
|
||||
train_dataset, validation_dataset = self.load_data()
|
||||
model.fit(train_dataset, epochs=10, validation_data=(validation_dataset))
|
||||
|
||||
loss, accuracy = model.evaluate(validation_dataset)
|
||||
print(f"Loss = {loss}", f"Accuracy = {accuracy}")
|
||||
model.save(MODEL_PATH)
|
||||
|
||||
def use_neural_net(self, image):
|
||||
model = models.load_model(MODEL_PATH)
|
||||
|
||||
img = cv2.imread(image)
|
||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
||||
plt.imshow(img, cmap=plt.cm.binary)
|
||||
plt.show()
|
||||
class_names = ["fragile", "priority"]
|
||||
prediction = model.predict(np.array([img]))
|
||||
index = np.argmax(prediction)
|
||||
prediction = class_names[index]
|
||||
print(prediction)
|
||||
return prediction
|
||||
|
||||
|
||||
# net = NeuralNetwork("C:/Users/annas/Desktop/projekty_studia/sztuczna/AIprojekt-wozek/priority (2).png")
|
||||
# net.train()
|
||||
# print(net.use_neural_net())
|
0
neural_network/__init__.py
Normal file
14
neural_network/image_classifier.model/keras_metadata.pb
Normal file
BIN
neural_network/image_classifier.model/saved_model.pb
Normal file
BIN
neural_network/image_classifier.model/variables/variables.index
Normal file
BIN
resources/markings/0_5t76yr.png
Normal file
After Width: | Height: | Size: 2.1 KiB |
BIN
resources/markings/135resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/136aa.png
Normal file
After Width: | Height: | Size: 277 B |
BIN
resources/markings/137aa.png
Normal file
After Width: | Height: | Size: 391 B |
BIN
resources/markings/138resized.png
Normal file
After Width: | Height: | Size: 715 B |
BIN
resources/markings/142aa.png
Normal file
After Width: | Height: | Size: 409 B |
BIN
resources/markings/144resized.png
Normal file
After Width: | Height: | Size: 470 B |
BIN
resources/markings/152resized.png
Normal file
After Width: | Height: | Size: 453 B |
BIN
resources/markings/157resized.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
resources/markings/160resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/163resized.png
Normal file
After Width: | Height: | Size: 2.3 KiB |
BIN
resources/markings/168resized.png
Normal file
After Width: | Height: | Size: 1006 B |
BIN
resources/markings/169resized.png
Normal file
After Width: | Height: | Size: 1003 B |
BIN
resources/markings/171resized.png
Normal file
After Width: | Height: | Size: 2.6 KiB |
BIN
resources/markings/172resized.png
Normal file
After Width: | Height: | Size: 418 B |
BIN
resources/markings/176resized.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
resources/markings/177resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/178resized.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
resources/markings/179resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/184resized.png
Normal file
After Width: | Height: | Size: 1008 B |
BIN
resources/markings/186resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/190resized.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
resources/markings/191resized.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
resources/markings/194resized.png
Normal file
After Width: | Height: | Size: 1010 B |
BIN
resources/markings/198resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/199resized.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
resources/markings/201resized.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
resources/markings/203resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/204resized.png
Normal file
After Width: | Height: | Size: 667 B |
BIN
resources/markings/209resized.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
resources/markings/210resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/211resized.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
resources/markings/212resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/215resized.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
resources/markings/217resized.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
resources/markings/218resized.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
resources/markings/221resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/229resized.png
Normal file
After Width: | Height: | Size: 710 B |
BIN
resources/markings/231resized.png
Normal file
After Width: | Height: | Size: 618 B |
BIN
resources/markings/232resized.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
resources/markings/235resized.png
Normal file
After Width: | Height: | Size: 533 B |
BIN
resources/markings/236resized.png
Normal file
After Width: | Height: | Size: 1.7 KiB |
BIN
resources/markings/237resized.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
resources/markings/244resized.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
resources/markings/245resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/249resized.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
resources/markings/253resized.png
Normal file
After Width: | Height: | Size: 473 B |
BIN
resources/markings/254resized.png
Normal file
After Width: | Height: | Size: 713 B |
BIN
resources/markings/256resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/257resized.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
resources/markings/258resized.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
resources/markings/259resized.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
resources/markings/261resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/263resized.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
resources/markings/264resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/266resized.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
resources/markings/268resized.png
Normal file
After Width: | Height: | Size: 2.5 KiB |
BIN
resources/markings/287resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/290resized.png
Normal file
After Width: | Height: | Size: 570 B |
BIN
resources/markings/294resized.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
resources/markings/312resized.png
Normal file
After Width: | Height: | Size: 691 B |
BIN
resources/markings/313resized.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
resources/markings/314resized.png
Normal file
After Width: | Height: | Size: 748 B |
BIN
resources/markings/330resized.png
Normal file
After Width: | Height: | Size: 933 B |
BIN
resources/markings/334resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/335resized.png
Normal file
After Width: | Height: | Size: 454 B |
BIN
resources/markings/356resized.png
Normal file
After Width: | Height: | Size: 754 B |
BIN
resources/markings/357resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/markings/364resized.png
Normal file
After Width: | Height: | Size: 1.0 KiB |
BIN
resources/markings/367resized.png
Normal file
After Width: | Height: | Size: 955 B |
BIN
resources/markings/400resized.png
Normal file
After Width: | Height: | Size: 477 B |
BIN
resources/markings/409resized.png
Normal file
After Width: | Height: | Size: 992 B |
BIN
resources/markings/511resized.png
Normal file
After Width: | Height: | Size: 503 B |
BIN
resources/markings/5_7yr.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
resources/markings/707resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 7.9 KiB |
Before Width: | Height: | Size: 138 KiB |
Before Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 7.7 KiB |
Before Width: | Height: | Size: 7.3 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 19 KiB |
BIN
resources/package_markings_data/fragile/1000resized.png
Normal file
After Width: | Height: | Size: 1.7 KiB |
BIN
resources/package_markings_data/fragile/1001resized.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/package_markings_data/fragile/1002resized.png
Normal file
After Width: | Height: | Size: 975 B |
BIN
resources/package_markings_data/fragile/1003resized.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
resources/package_markings_data/fragile/1004resized.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
resources/package_markings_data/fragile/1005resized.png
Normal file
After Width: | Height: | Size: 2.3 KiB |