diff --git a/a_star.py b/a_star.py index c24582c..2444bf2 100644 --- a/a_star.py +++ b/a_star.py @@ -6,7 +6,7 @@ MAX_ROWS = 15 MAX_COLS = 25 mapping = { - 'd': 10, + 'd': 2, 's': 20, 'r': 999 } diff --git a/decision_tree.py b/decision_tree.py index a451691..867f2d5 100644 --- a/decision_tree.py +++ b/decision_tree.py @@ -27,7 +27,7 @@ def treelearn(): # visualize and display decision tree data = tree.export_graphviz(decision_tree, out_file=None, feature_names=attributes) graph = pydotplus.graph_from_dot_data(data) - graph.write_png(os.path.join('resources', 'decision_tree.png')) + # graph.write_png(os.path.join('resources', 'decision_tree.png')) img = pltimg.imread(os.path.join('resources', 'decision_tree.png')) imgplot = plt.imshow(img) diff --git a/neural.py b/neural.py new file mode 100644 index 0000000..e2d457b --- /dev/null +++ b/neural.py @@ -0,0 +1,58 @@ +import tensorflow as tf +import glob +import numpy as np +import os + +from PIL import Image +from sklearn.model_selection import train_test_split +from tensorflow import keras + +images = [] +labels = [] + +class_names = ['fragile', 'priority', 'skull'] + +image_dir = 'C:\\Users\\mateu\\Desktop\\SI projekt\\resources' +image_size = (32, 32) + +for class_index, class_name in enumerate(class_names): + class_path = os.path.join(image_dir, class_name) + file_names = os.listdir(class_path) + print(class_path) + for file_name in file_names: + image_path = os.path.join(class_path, file_name) + image = Image.open(image_path).convert('L') + image = image.resize(image_size) + image_array = np.array(image) / 255.0 + images.append(image_array) + labels.append(class_index) + +images = np.array(images) +labels = np.array(labels) + +X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42) + +num_classes = 3 +y_train_encoded = keras.utils.to_categorical(y_train, num_classes) +y_test_encoded = keras.utils.to_categorical(y_test, num_classes) + +model = keras.Sequential([ + keras.layers.Flatten(input_shape=image_size), + keras.layers.Dense(64, activation='relu'), + keras.layers.Dense(num_classes, activation='softmax') +]) + +model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) + +batch_size = 32 +epochs = 10 + +model.fit(X_train, y_train_encoded, batch_size=batch_size, epochs=epochs, + validation_data=(X_test, y_test_encoded)) + +test_loss, test_acc = model.evaluate(X_test, y_test_encoded) +print('Test accuracy:', test_acc) + +predictions = model.predict(X_test) + +model.save('resources/model.h5') diff --git a/requirements.txt b/requirements.txt index dfce884..c3f72ae 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/resources/model.h5 b/resources/model.h5 new file mode 100644 index 0000000..0e692e1 Binary files /dev/null and b/resources/model.h5 differ diff --git a/resources/package/8.jpg b/resources/package/8.jpg new file mode 100644 index 0000000..b03ade3 Binary files /dev/null and b/resources/package/8.jpg differ diff --git a/resources/package/9.jpg b/resources/package/9.jpg new file mode 100644 index 0000000..bd01700 Binary files /dev/null and b/resources/package/9.jpg differ diff --git a/test.py b/test.py new file mode 100644 index 0000000..d8a852a --- /dev/null +++ b/test.py @@ -0,0 +1,25 @@ +import numpy as np +from PIL import Image +from tensorflow import keras + +saved_model_path = 'resources/model.h5' +model = keras.models.load_model(saved_model_path) + + +image_path = 'resources/indeks-fragile.png' +image_size = (32, 32) + +image = Image.open(image_path).convert('L') +image = image.resize(image_size) +image_array = np.array(image) / 255.0 +input_data = np.expand_dims(image_array, axis=0) + + +predictions = model.predict(input_data) +predicted_class = np.argmax(predictions, axis=1) + + +class_names = ['fragile', 'priority', 'skull'] +predicted_label = class_names[predicted_class[0]] + +print('Predicted class:', predicted_label)