visualising sectors & neural network

This commit is contained in:
Anna Śmigiel 2022-05-26 22:56:40 +02:00
parent 3b04067915
commit ca6d893a2b
2457 changed files with 136 additions and 28 deletions

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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)
#

View 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())

View File

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 277 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 715 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1006 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1003 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1008 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1010 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 667 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 710 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 533 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 473 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 713 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 570 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 748 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 933 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 754 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 955 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 477 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 992 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 503 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 138 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 975 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Some files were not shown because too many files have changed in this diff Show More