dodanie sieci neuronowej i inne zmiany

This commit is contained in:
Aliaksandr Halaunia 2023-06-01 16:09:01 +02:00
parent ca668b4b58
commit 7cb2903a1d
30 changed files with 172 additions and 15 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

8
.idea/Gra-SI_nn.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Gra-SI_nn.iml" filepath="$PROJECT_DIR$/.idea/Gra-SI_nn.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -20,13 +20,13 @@ class Agent(pygame.sprite.Sprite):
self.y_change = 0 self.y_change = 0
#self.AGENT_IMG = pygame.image.load("./zdjecia/"+self.AGENT_IMAGES[self.direction]+".png") #self.AGENT_IMG = pygame.image.load("./zdjecia/"+self.AGENT_IMAGES[self.direction]+".png")
self.AGENT_IMG_RIGHT = pygame.image.load("./zdjecia/gandalf-prawo.png") self.AGENT_IMG_RIGHT = pygame.image.load("./pozostale_zdjecia/gandalf-prawo.png")
self.AGENT_RIGHT = pygame.transform.scale(self.AGENT_IMG_RIGHT,(64,64)) self.AGENT_RIGHT = pygame.transform.scale(self.AGENT_IMG_RIGHT,(64,64))
self.AGENT_IMG_DOWN = pygame.image.load("./zdjecia/gandalf-dol.png") self.AGENT_IMG_DOWN = pygame.image.load("./pozostale_zdjecia/gandalf-dol.png")
self.AGENT_DOWN = pygame.transform.scale(self.AGENT_IMG_DOWN,(64,64)) self.AGENT_DOWN = pygame.transform.scale(self.AGENT_IMG_DOWN,(64,64))
self.AGENT_IMG_LEFT = pygame.image.load("./zdjecia/gandalf-lewo.png") self.AGENT_IMG_LEFT = pygame.image.load("./pozostale_zdjecia/gandalf-lewo.png")
self.AGENT_LEFT = pygame.transform.scale(self.AGENT_IMG_LEFT,(64,64)) self.AGENT_LEFT = pygame.transform.scale(self.AGENT_IMG_LEFT,(64,64))
self.AGENT_IMG_UP = pygame.image.load("./zdjecia/gandalf-gora.png") self.AGENT_IMG_UP = pygame.image.load("./pozostale_zdjecia/gandalf-gora.png")
self.AGENT_UP = pygame.transform.scale(self.AGENT_IMG_UP,(64,64)) self.AGENT_UP = pygame.transform.scale(self.AGENT_IMG_UP,(64,64))
self.AGENT = self.AGENT_RIGHT self.AGENT = self.AGENT_RIGHT

BIN
class_names.pkl Normal file

Binary file not shown.

View File

@ -6,6 +6,7 @@ from mobs import *
#from unknown_mob import * #unknown mob #from unknown_mob import * #unknown mob
from bfs import * from bfs import *
from heapq import * from heapq import *
from nn import *
class Game: class Game:
@ -16,10 +17,10 @@ class Game:
self.running = True self.running = True
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.BACKGROUND_IMG= pygame.image.load("./zdjecia/podloze.jpg") self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg")
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64)) self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
self.LVL_ICON_PNG = pygame.image.load("./zdjecia/lvl_icon.png") self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png")
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24)) self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
pygame.display.set_caption('Gra-SI') pygame.display.set_caption('Gra-SI')
@ -212,7 +213,6 @@ print("path_true:",path_true)
g = Game() g = Game()
g.new() g.new()
while g.running: while g.running:

View File

@ -14,7 +14,7 @@ class Rocks(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.ROCKS_PNG = pygame.image.load("./zdjecia/rock.png") self.ROCKS_PNG = pygame.image.load("./pozostale_zdjecia/rock.png")
self.ROCKS = pygame.transform.scale(self.ROCKS_PNG,(64,64)) self.ROCKS = pygame.transform.scale(self.ROCKS_PNG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -39,7 +39,7 @@ class Grass(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.GRASS_PNG = pygame.image.load("./zdjecia/grass.png") self.GRASS_PNG = pygame.image.load("./pozostale_zdjecia/grass.png")
self.GRASS = pygame.transform.scale(self.GRASS_PNG,(64,64)) self.GRASS = pygame.transform.scale(self.GRASS_PNG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -67,7 +67,7 @@ class Health_flower(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.FLOWER_IMG = pygame.image.load("./zdjecia/flower.png") self.FLOWER_IMG = pygame.image.load("./pozostale_zdjecia/flower.png")
self.FLOWER = pygame.transform.scale(self.FLOWER_IMG,(64,64)) self.FLOWER = pygame.transform.scale(self.FLOWER_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -92,7 +92,7 @@ class Little_Rocks(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.LITTLE_ROCKS_PNG = pygame.image.load("./zdjecia/little_rocks.png") self.LITTLE_ROCKS_PNG = pygame.image.load("./pozostale_zdjecia/little_rocks.png")
self.LITTLE_ROCKS = pygame.transform.scale(self.LITTLE_ROCKS_PNG,(64,64)) self.LITTLE_ROCKS = pygame.transform.scale(self.LITTLE_ROCKS_PNG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])

View File

@ -14,7 +14,7 @@ class Archer_ork(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.ARCHER_ORK_IMG = pygame.image.load("./zdjecia/ork_lucznik.png") self.ARCHER_ORK_IMG = pygame.image.load("./pozostale_zdjecia/ork_lucznik.png")
self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64)) self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -41,7 +41,7 @@ class Infantry_ork(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.INFANTRY_ORK_IMG = pygame.image.load("./zdjecia/ork-piechota.png") self.INFANTRY_ORK_IMG = pygame.image.load("./pozostale_zdjecia/ork-piechota.png")
self.INFANTRY_ORK = pygame.transform.scale(self.INFANTRY_ORK_IMG,(64,64)) self.INFANTRY_ORK = pygame.transform.scale(self.INFANTRY_ORK_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -98,7 +98,7 @@ class Sauron(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.SAURON_IMG = pygame.image.load("./zdjecia/sauron.png") self.SAURON_IMG = pygame.image.load("./pozostale_zdjecia/sauron.png")
self.SAURON = pygame.transform.scale(self.SAURON_IMG,(64,64)) self.SAURON = pygame.transform.scale(self.SAURON_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])

122
nn.py Normal file
View File

@ -0,0 +1,122 @@
import os
import glob
import PIL
from PIL import Image
import tensorflow as tf
import pickle
from tensorflow import keras
from keras import layers
from keras.models import Sequential
import pathlib
class NeuralN:
# @staticmethod
# def predict(image):
data_dir = pathlib.Path('zdjecia')
saved_model_path = pathlib.Path('trained_model.h5')
class_names_path = pathlib.Path("class_names.pkl")
image_count = sum(len(files) for _, _, files in os.walk(data_dir))
print(image_count)
# ORK_ARCHER = list(glob.glob('C:\\mobs_photos\\ORK_ARCHER'))
# im = PIL.Image.open(ORK_ARCHER[0])
# im.show()
if os.path.exists(saved_model_path):
model = tf.keras.models.load_model(saved_model_path)
print("Saved model loaded")
with open(class_names_path, 'rb') as f:
class_names = pickle.load(f)
print("Class names loaded.")
else:
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(180, 180),
batch_size=32)
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(180, 180),
batch_size=32)
# test_ds = tf.keras.utils.image_dataset_from_directory(
# data_dir,
# seed=123,
# image_size=(180, 180),
# batch_size=32)
class_names = train_ds.class_names
print(class_names)
num_classes = len(class_names)
model = Sequential([
layers.Rescaling(1. / 255, input_shape=(180, 180, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True),
metrics=['accuracy'])
model.summary()
epochs = 1
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
model.save("trained_model.h5")
print("Model trained and saved.")
with open(class_names_path, 'wb') as f:
pickle.dump(train_ds.class_names, f)
print("Class names saved.")
# loaded_model = tf.keras.models.load_model("trained_model.h5")
probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
#image_path = image
image_path = pathlib.Path('path_to_image')
image = Image.open(image_path)
# Preprocess the image
image = image.resize((180, 180)) # Resize to match the input size of the model
image_array = tf.keras.preprocessing.image.img_to_array(image)
image_array = image_array / 255.0 # Normalize pixel values
# Add an extra dimension to the image array
image_array = tf.expand_dims(image_array, 0)
# Make the prediction
predictions = probability_model.predict(image_array)
# Convert the predictions to class labels
predicted_label = class_names[predictions[0].argmax()]
#actions = {
# 'ORK_MELEE': 'fight',
# 'ORK_ARCHER': 'change_dir',
# 'SAURON': 'change_dir'
#}
# Get the action for the predicted character
#action = actions.get(predicted_label, 'unknown')
# Print the predicted label
print(predicted_label)
# return predicted_label, action

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

Before

Width:  |  Height:  |  Size: 170 KiB

After

Width:  |  Height:  |  Size: 170 KiB

View File

Before

Width:  |  Height:  |  Size: 170 KiB

After

Width:  |  Height:  |  Size: 170 KiB

View File

Before

Width:  |  Height:  |  Size: 165 KiB

After

Width:  |  Height:  |  Size: 165 KiB

View File

Before

Width:  |  Height:  |  Size: 164 KiB

After

Width:  |  Height:  |  Size: 164 KiB

View File

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View File

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 92 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 212 KiB

After

Width:  |  Height:  |  Size: 212 KiB

View File

Before

Width:  |  Height:  |  Size: 216 KiB

After

Width:  |  Height:  |  Size: 216 KiB

View File

Before

Width:  |  Height:  |  Size: 227 KiB

After

Width:  |  Height:  |  Size: 227 KiB

View File

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 85 KiB

View File

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 49 KiB

View File

Before

Width:  |  Height:  |  Size: 69 KiB

After

Width:  |  Height:  |  Size: 69 KiB

View File

Before

Width:  |  Height:  |  Size: 135 KiB

After

Width:  |  Height:  |  Size: 135 KiB

BIN
trained_model.h5 Normal file

Binary file not shown.

View File

@ -14,7 +14,7 @@ class Unknown_mob(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.UNKNOWN_MOB_IMG = pygame.image.load("./zdjecia/dragon.jpg") self.UNKNOWN_MOB_IMG = pygame.image.load("./pozostale_zdjecia/dragon.jpg")
self.UKNOWN_MOB = pygame.transform.scale(self.UNKNOWN_MOB_IMG,(64,64)) self.UKNOWN_MOB = pygame.transform.scale(self.UNKNOWN_MOB_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])