adding again everything after deleting whole repository (my mistake...)
33
agent.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import pygame
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
class Agent(pygame.sprite.Sprite):
|
||||||
|
def __init__(self, width, height, pos_x, pos_y, pos):
|
||||||
|
pygame.sprite.Sprite.__init__(self)
|
||||||
|
self.view = 5 # ile pól przed sobą widzi saper
|
||||||
|
self.image = pygame.image.load(os.path.join('images', 'ratAvatar.png')).convert_alpha()
|
||||||
|
self.image_scaled = pygame.transform.scale(self.image, (width, height))
|
||||||
|
self.rect = self.image_scaled.get_rect(center = (pos_x, pos_y))
|
||||||
|
self.pos_field = pos
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def move(self, window_size, margin, distance, direction, poschanger):
|
||||||
|
|
||||||
|
if self.rect.right <= int(window_size) - margin*2 and direction == "RIGHT":
|
||||||
|
self.pos_field += poschanger
|
||||||
|
self.rect.centerx += distance
|
||||||
|
|
||||||
|
if self.rect.left >= margin*2 and direction == "LEFT":
|
||||||
|
self.pos_field += poschanger
|
||||||
|
self.rect.centerx -= distance
|
||||||
|
|
||||||
|
if self.rect.bottom <= int(window_size) - margin*2 and direction == "DOWN":
|
||||||
|
self.pos_field += poschanger
|
||||||
|
self.rect.centery += distance
|
||||||
|
|
||||||
|
if self.rect.top >= margin*2 and direction == "UP":
|
||||||
|
self.pos_field += poschanger
|
||||||
|
self.rect.centery -= distance
|
||||||
|
|
215
board.py
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
import random
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import pathfinder
|
||||||
|
import time
|
||||||
|
from decisiontree import DecisionTree
|
||||||
|
from imagerecognizer import ImageRecognizer
|
||||||
|
from objects import Grass
|
||||||
|
from objects import Mine
|
||||||
|
from objects import Rock
|
||||||
|
from objects import Puddle
|
||||||
|
|
||||||
|
|
||||||
|
MARGIN = 20 # marginesy - odległość kraty od końca okna
|
||||||
|
SQUARE_HEIGHT = 35
|
||||||
|
SQUARE_WIDTH = 35
|
||||||
|
CENTER_DIST = 38 # odległość pomiędzy środkami kwadratów
|
||||||
|
BOARDCOLS = 15 # liczba kolumn planszy
|
||||||
|
BOARDROWS = BOARDCOLS # liczba wierszy planszy
|
||||||
|
WINDOW_HEIGHT = BOARDCOLS * CENTER_DIST + (2 * MARGIN)
|
||||||
|
WINDOW_WIDTH = BOARDROWS * CENTER_DIST + (2 * MARGIN)
|
||||||
|
|
||||||
|
|
||||||
|
class Field: # obiekt clasy field jest pojedynczym polem planszy
|
||||||
|
def __init__(self, coordinates, color, fieldty):
|
||||||
|
Board.fields.append(self)
|
||||||
|
self.rect = pygame.Rect(coordinates[0], coordinates[1], SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
self.color = color
|
||||||
|
self.type = fieldty
|
||||||
|
self.object = self.assign_object()
|
||||||
|
self.deley_value = self.object.deley
|
||||||
|
self.redisp = True
|
||||||
|
|
||||||
|
def assign_object(self):
|
||||||
|
if self.type == 'T':
|
||||||
|
object = Grass(self.rect.center[0],self.rect.center[1], SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
return object
|
||||||
|
if self.type == 'P':
|
||||||
|
object = Puddle(self.rect.center[0],self.rect.center[1],SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
return object
|
||||||
|
if self.type == 'R':
|
||||||
|
object = Rock(self.rect.center[0],self.rect.center[1],SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
return object
|
||||||
|
if self.type == 'B':
|
||||||
|
object = Mine(self.rect.center[0],self.rect.center[1],SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
return object
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Board:
|
||||||
|
fields = [] # tablica obiektów klasy filed, czyli po prostu tablica pól planszy gotowych do wizualizacji
|
||||||
|
bombs = 0
|
||||||
|
def __init__(self):
|
||||||
|
self.margin = MARGIN
|
||||||
|
self.square_high = SQUARE_HEIGHT
|
||||||
|
self.square_width = SQUARE_WIDTH
|
||||||
|
self.center_dist = CENTER_DIST
|
||||||
|
self.window_height = WINDOW_HEIGHT
|
||||||
|
self.window_widhth = WINDOW_WIDTH
|
||||||
|
self.boardcols = BOARDCOLS # liczba kolumn planszy
|
||||||
|
self.boardrows = BOARDROWS # liczba wierszy planszy
|
||||||
|
self.board = [[0 for i in range(self.boardcols)] for j in range(self.boardrows)]
|
||||||
|
self.firstwinner = True
|
||||||
|
|
||||||
|
pygame.display.set_caption("Saper")
|
||||||
|
self.game_display = pygame.display.set_mode((self.window_height, self.window_widhth)) # wielkość okna adaptująca się do wielkości planszy
|
||||||
|
self.load_board()
|
||||||
|
self.first_ground = self.board[0].index('T')
|
||||||
|
self.show_board()
|
||||||
|
self.init_board()
|
||||||
|
self.bombfield = pathfinder.find_bomb(self.fields, self.first_ground)
|
||||||
|
print(self.bombfield)
|
||||||
|
self.pathf = pathfinder.Pathfind(self.fields, self.bombfield, self.first_ground)
|
||||||
|
self.ir = ImageRecognizer()
|
||||||
|
self.dt = DecisionTree()
|
||||||
|
|
||||||
|
def load_board(self): # funkcja tworząca tablicę opisującą planszę
|
||||||
|
for i in range(self.boardrows):
|
||||||
|
for j in range(self.boardcols):
|
||||||
|
chance = random.randint(1,100) # prawdopodobieństwo że dane pole będzie terenem (T), przeszkodą (O) lub bombą (B)
|
||||||
|
if chance >= 1 and chance < 80:
|
||||||
|
self.board[i][j] = 'T'
|
||||||
|
if chance >= 80 and chance < 95:
|
||||||
|
chance = random.randint(1,2)
|
||||||
|
if chance == 1:
|
||||||
|
self.board[i][j] = 'R'
|
||||||
|
if chance == 2:
|
||||||
|
self.board[i][j] = 'P'
|
||||||
|
|
||||||
|
if chance >= 95 and chance <= 100:
|
||||||
|
self.board[i][j] = 'B'
|
||||||
|
self.bombs += 1
|
||||||
|
|
||||||
|
def show_board(self): # funkcja pomocnicza wypisująca tablicę opisującą planszę
|
||||||
|
for i in range(self.boardrows):
|
||||||
|
for j in range(self.boardcols):
|
||||||
|
print(self.board[i][j], end=' ')
|
||||||
|
print("")
|
||||||
|
print("")
|
||||||
|
print("bomby:",self.bombs)
|
||||||
|
|
||||||
|
def init_board(self): # funkcja inicjująca planszę
|
||||||
|
x = self.margin
|
||||||
|
y = self.margin
|
||||||
|
for i in range(self.boardrows):
|
||||||
|
for j in range(self.boardcols):
|
||||||
|
if self.board[i][j] == 'T':
|
||||||
|
Field((x, y), (0, 128,0), 'T') # tworzony jest obiekt clasy field zawierający współżędne oraz kolor danego pola planszy
|
||||||
|
if self.board[i][j] == 'R':
|
||||||
|
Field((x, y), (255, 255, 0), 'R')
|
||||||
|
if self.board[i][j] == 'P':
|
||||||
|
Field((x, y), (255, 255, 0), 'P')
|
||||||
|
if self.board[i][j] == 'B':
|
||||||
|
Field((x, y), (165, 42, 42), 'B')
|
||||||
|
|
||||||
|
x += self.center_dist
|
||||||
|
y += self.center_dist
|
||||||
|
x = self.margin
|
||||||
|
|
||||||
|
def objects_display(self,i):
|
||||||
|
objecttype = self.fields[i].object
|
||||||
|
self.game_display.blit(objecttype.image_scaled, objecttype.rect)
|
||||||
|
|
||||||
|
def board_events(self, agent):
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
self.agent_field_update(agent)
|
||||||
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: # klawisz esc zamyka program
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
#if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and agent.pos_field+1 < len(self.fields):
|
||||||
|
# if self.fields[agent.pos_field+1].object.walkable == 0:
|
||||||
|
# self.fields[agent.pos_field].redisp = True
|
||||||
|
# agent.move(self.window_height, self.margin, self.center_dist, "RIGHT", 1)
|
||||||
|
|
||||||
|
#if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT and agent.pos_field-1 >= 0:
|
||||||
|
# if self.fields[agent.pos_field - 1].object.walkable == 0:
|
||||||
|
# self.fields[agent.pos_field].redisp = True
|
||||||
|
# agent.move(self.window_height, self.margin, self.center_dist, "LEFT", -1)
|
||||||
|
|
||||||
|
#if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN and agent.pos_field+BOARDROWS < len(self.fields):
|
||||||
|
# if self.fields[agent.pos_field + BOARDROWS].object.walkable == 0:
|
||||||
|
# self.fields[agent.pos_field].redisp = True
|
||||||
|
# agent.move(self.window_height, self.margin, self.center_dist, "DOWN", BOARDROWS)
|
||||||
|
|
||||||
|
#if event.type == pygame.KEYDOWN and event.key == pygame.K_UP and agent.pos_field-BOARDROWS >= 0:
|
||||||
|
# if self.fields[agent.pos_field - BOARDROWS].object.walkable == 0:
|
||||||
|
# self.fields[agent.pos_field].redisp = True
|
||||||
|
# agent.move(self.window_height,self.margin,self.center_dist,"UP", -BOARDROWS)
|
||||||
|
|
||||||
|
|
||||||
|
def agent_field_update(self,agent):
|
||||||
|
agent.cooldown = 100
|
||||||
|
if self.fields[agent.pos_field].type == 'B' and self.fields[agent.pos_field].object.defused == False:
|
||||||
|
if self.ir.recognize(self.fields[agent.pos_field].object.itr) == "stones":
|
||||||
|
print("to kamień!")
|
||||||
|
self.fields[agent.pos_field].object.its_rock(SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
self.fields[agent.pos_field].redisp = True
|
||||||
|
self.bombs -= 1
|
||||||
|
else:
|
||||||
|
print("to bomba!")
|
||||||
|
wtd = self.dt.make_decision(((self.bombs*100)//(BOARDCOLS**2))+1, self.fields[agent.pos_field].object.time, self.fields[agent.pos_field].object.reach)
|
||||||
|
if wtd == "Defuse":
|
||||||
|
self.fields[agent.pos_field].object.defuse(SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
self.fields[agent.pos_field].redisp = True
|
||||||
|
if wtd == "Move away":
|
||||||
|
self.fields[agent.pos_field].object.move_away(SQUARE_WIDTH, SQUARE_HEIGHT)
|
||||||
|
self.fields[agent.pos_field].redisp = True
|
||||||
|
self.bombs -= 1
|
||||||
|
if self.bombs > 0:
|
||||||
|
self.bombfield = pathfinder.find_bomb(self.fields, agent.pos_field)
|
||||||
|
#print(self.bombfield)
|
||||||
|
self.pathf = pathfinder.Pathfind(self.fields, self.bombfield, agent.pos_field)
|
||||||
|
|
||||||
|
|
||||||
|
def automove_agent(self,agent):
|
||||||
|
self.agent_field_update(agent)
|
||||||
|
if len(self.pathf.way) > 0:
|
||||||
|
wayfield = self.pathf.way[0]
|
||||||
|
self.pathf.way.remove(wayfield)
|
||||||
|
if agent.pos_field - BOARDCOLS == wayfield:
|
||||||
|
self.fields[agent.pos_field].redisp = True
|
||||||
|
agent.move(self.window_height, self.margin, self.center_dist, "UP", -BOARDCOLS)
|
||||||
|
|
||||||
|
if agent.pos_field + BOARDCOLS == wayfield:
|
||||||
|
self.fields[agent.pos_field].redisp = True
|
||||||
|
agent.move(self.window_height, self.margin, self.center_dist, "DOWN", BOARDCOLS)
|
||||||
|
|
||||||
|
if agent.pos_field - 1 == wayfield:
|
||||||
|
self.fields[agent.pos_field].redisp = True
|
||||||
|
agent.move(self.window_height, self.margin, self.center_dist, "LEFT", -1)
|
||||||
|
|
||||||
|
if agent.pos_field + 1 == wayfield:
|
||||||
|
self.fields[agent.pos_field].redisp = True
|
||||||
|
agent.move(self.window_height, self.margin, self.center_dist, "RIGHT", 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def full_board_display(self,first): # funkcja pomocnicza wywołująca program, w celu wizualizacji planszy możną ją wywołać
|
||||||
|
if self.bombs <= 0 and self.firstwinner == True:
|
||||||
|
print("wszystkie miny rozbrojone!")
|
||||||
|
self.firstwinner = False
|
||||||
|
|
||||||
|
if first == True:
|
||||||
|
self.game_display.fill((0, 0, 0))
|
||||||
|
i=0
|
||||||
|
for square in self.fields:
|
||||||
|
if square.redisp == True:
|
||||||
|
pygame.draw.rect(self.game_display, square.color, square.rect)
|
||||||
|
self.objects_display(i)
|
||||||
|
square.redisp = False
|
||||||
|
i=i+1
|
391
decisiontree.csv
Normal file
@ -0,0 +1,391 @@
|
|||||||
|
Percent of mines;Time to explosion;Range of explosion;What to do
|
||||||
|
1;1;1;Defuse
|
||||||
|
1;2;1;Defuse
|
||||||
|
1;3;1;Move away
|
||||||
|
1;4;1;Move away
|
||||||
|
1;5;1;Move away
|
||||||
|
1;6;1;Move away
|
||||||
|
1;7;1;Move away
|
||||||
|
1;8;1;Move away
|
||||||
|
1;9;1;Move away
|
||||||
|
1;10;1;Move away
|
||||||
|
1;1;2;Defuse
|
||||||
|
1;2;2;Defuse
|
||||||
|
1;3;2;Move away
|
||||||
|
1;4;2;Move away
|
||||||
|
1;5;2;Move away
|
||||||
|
1;6;2;Move away
|
||||||
|
1;7;2;Move away
|
||||||
|
1;8;2;Move away
|
||||||
|
1;9;2;Move away
|
||||||
|
1;10;2;Move away
|
||||||
|
1;1;3;Defuse
|
||||||
|
1;2;3;Defuse
|
||||||
|
1;3;3;Move away
|
||||||
|
1;4;3;Move away
|
||||||
|
1;5;3;Move away
|
||||||
|
1;6;3;Move away
|
||||||
|
1;7;3;Move away
|
||||||
|
1;8;3;Move away
|
||||||
|
1;9;3;Move away
|
||||||
|
2;10;3;Move away
|
||||||
|
2;1;1;Defuse
|
||||||
|
2;2;1;Defuse
|
||||||
|
2;3;1;Move away
|
||||||
|
2;4;1;Move away
|
||||||
|
2;5;1;Move away
|
||||||
|
2;6;1;Move away
|
||||||
|
2;7;1;Move away
|
||||||
|
2;8;1;Move away
|
||||||
|
2;9;1;Move away
|
||||||
|
2;10;1;Move away
|
||||||
|
2;1;2;Defuse
|
||||||
|
2;2;2;Defuse
|
||||||
|
2;3;2;Move away
|
||||||
|
2;4;2;Move away
|
||||||
|
2;5;2;Move away
|
||||||
|
2;6;2;Move away
|
||||||
|
2;7;2;Move away
|
||||||
|
2;8;2;Move away
|
||||||
|
2;9;2;Move away
|
||||||
|
2;10;2;Move away
|
||||||
|
2;1;3;Defuse
|
||||||
|
2;2;3;Defuse
|
||||||
|
2;3;3;Move away
|
||||||
|
2;4;3;Move away
|
||||||
|
2;5;3;Move away
|
||||||
|
2;6;3;Move away
|
||||||
|
2;7;3;Move away
|
||||||
|
2;8;3;Move away
|
||||||
|
2;9;3;Move away
|
||||||
|
2;10;3;Move away
|
||||||
|
3;1;1;Defuse
|
||||||
|
3;2;1;Defuse
|
||||||
|
3;3;1;Move away
|
||||||
|
3;4;1;Move away
|
||||||
|
3;5;1;Move away
|
||||||
|
3;6;1;Move away
|
||||||
|
3;7;1;Move away
|
||||||
|
3;8;1;Move away
|
||||||
|
3;9;1;Move away
|
||||||
|
3;10;1;Move away
|
||||||
|
3;1;2;Defuse
|
||||||
|
3;2;2;Defuse
|
||||||
|
3;3;2;Move away
|
||||||
|
3;4;2;Move away
|
||||||
|
3;5;2;Move away
|
||||||
|
3;6;2;Move away
|
||||||
|
3;7;2;Move away
|
||||||
|
3;8;2;Move away
|
||||||
|
3;9;2;Move away
|
||||||
|
3;10;2;Move away
|
||||||
|
3;1;3;Defuse
|
||||||
|
3;2;3;Defuse
|
||||||
|
3;3;3;Move away
|
||||||
|
3;4;3;Move away
|
||||||
|
3;5;3;Move away
|
||||||
|
3;6;3;Move away
|
||||||
|
3;7;3;Move away
|
||||||
|
3;8;3;Move away
|
||||||
|
3;9;3;Move away
|
||||||
|
3;10;3;Move away
|
||||||
|
4;1;1;Defuse
|
||||||
|
4;2;1;Defuse
|
||||||
|
4;3;1;Defuse
|
||||||
|
4;4;1;Defuse
|
||||||
|
4;5;1;Defuse
|
||||||
|
4;6;1;Defuse
|
||||||
|
4;7;1;Defuse
|
||||||
|
4;8;1;Defuse
|
||||||
|
4;9;1;Defuse
|
||||||
|
4;10;1;Defuse
|
||||||
|
4;1;2;Defuse
|
||||||
|
4;2;2;Defuse
|
||||||
|
4;3;2;Move away
|
||||||
|
4;4;2;Move away
|
||||||
|
4;5;2;Move away
|
||||||
|
4;6;2;Move away
|
||||||
|
4;7;2;Move away
|
||||||
|
4;8;2;Move away
|
||||||
|
4;9;2;Move away
|
||||||
|
4;10;2;Move away
|
||||||
|
4;1;3;Defuse
|
||||||
|
4;2;3;Defuse
|
||||||
|
4;3;3;Move away
|
||||||
|
4;4;3;Move away
|
||||||
|
4;5;3;Move away
|
||||||
|
4;6;3;Move away
|
||||||
|
4;7;3;Move away
|
||||||
|
4;8;3;Move away
|
||||||
|
4;9;3;Move away
|
||||||
|
4;10;3;Move away
|
||||||
|
5;1;1;Defuse
|
||||||
|
5;2;1;Defuse
|
||||||
|
5;3;1;Defuse
|
||||||
|
5;4;1;Defuse
|
||||||
|
5;5;1;Defuse
|
||||||
|
5;6;1;Defuse
|
||||||
|
5;7;1;Defuse
|
||||||
|
5;8;1;Defuse
|
||||||
|
5;9;1;Defuse
|
||||||
|
5;10;1;Defuse
|
||||||
|
5;1;2;Defuse
|
||||||
|
5;2;2;Defuse
|
||||||
|
5;3;2;Defuse
|
||||||
|
5;4;2;Defuse
|
||||||
|
5;5;2;Defuse
|
||||||
|
5;6;2;Move away
|
||||||
|
5;7;2;Move away
|
||||||
|
5;8;2;Move away
|
||||||
|
5;9;2;Move away
|
||||||
|
5;10;2;Move away
|
||||||
|
5;1;3;Defuse
|
||||||
|
5;2;3;Defuse
|
||||||
|
5;3;3;Move away
|
||||||
|
5;4;3;Move away
|
||||||
|
5;5;3;Move away
|
||||||
|
5;6;3;Move away
|
||||||
|
5;7;3;Move away
|
||||||
|
5;8;3;Move away
|
||||||
|
5;9;3;Move away
|
||||||
|
5;10;3;Move away
|
||||||
|
6;1;1;Defuse
|
||||||
|
6;2;1;Defuse
|
||||||
|
6;3;1;Defuse
|
||||||
|
6;4;1;Defuse
|
||||||
|
6;5;1;Defuse
|
||||||
|
6;6;1;Defuse
|
||||||
|
6;7;1;Defuse
|
||||||
|
6;8;1;Defuse
|
||||||
|
6;9;1;Defuse
|
||||||
|
6;10;1;Defuse
|
||||||
|
6;1;2;Defuse
|
||||||
|
6;2;2;Defuse
|
||||||
|
6;3;2;Defuse
|
||||||
|
6;4;2;Defuse
|
||||||
|
6;5;2;Defuse
|
||||||
|
6;6;2;Move away
|
||||||
|
6;7;2;Move away
|
||||||
|
6;8;2;Move away
|
||||||
|
6;9;2;Move away
|
||||||
|
6;10;2;Move away
|
||||||
|
6;1;3;Defuse
|
||||||
|
6;2;3;Defuse
|
||||||
|
6;3;3;Move away
|
||||||
|
6;4;3;Move away
|
||||||
|
6;5;3;Move away
|
||||||
|
6;6;3;Move away
|
||||||
|
6;7;3;Move away
|
||||||
|
6;8;3;Move away
|
||||||
|
6;9;3;Move away
|
||||||
|
6;10;3;Move away
|
||||||
|
7;1;1;Defuse
|
||||||
|
7;2;1;Defuse
|
||||||
|
7;3;1;Defuse
|
||||||
|
7;4;1;Defuse
|
||||||
|
7;5;1;Defuse
|
||||||
|
7;6;1;Defuse
|
||||||
|
7;7;1;Defuse
|
||||||
|
7;8;1;Defuse
|
||||||
|
7;9;1;Defuse
|
||||||
|
7;10;1;Defuse
|
||||||
|
7;1;2;Defuse
|
||||||
|
7;2;2;Defuse
|
||||||
|
7;3;2;Defuse
|
||||||
|
7;4;2;Defuse
|
||||||
|
7;5;2;Defuse
|
||||||
|
7;6;2;Defuse
|
||||||
|
7;7;2;Defuse
|
||||||
|
7;8;2;Defuse
|
||||||
|
7;9;2;Defuse
|
||||||
|
7;10;2;Defuse
|
||||||
|
7;1;3;Defuse
|
||||||
|
7;2;3;Defuse
|
||||||
|
7;3;3;Move away
|
||||||
|
7;4;3;Move away
|
||||||
|
7;5;3;Move away
|
||||||
|
7;6;3;Move away
|
||||||
|
7;7;3;Move away
|
||||||
|
7;8;3;Move away
|
||||||
|
7;9;3;Move away
|
||||||
|
7;10;3;Move away
|
||||||
|
8;1;1;Defuse
|
||||||
|
8;2;1;Defuse
|
||||||
|
8;3;1;Defuse
|
||||||
|
8;4;1;Defuse
|
||||||
|
8;5;1;Defuse
|
||||||
|
8;6;1;Defuse
|
||||||
|
8;7;1;Defuse
|
||||||
|
8;8;1;Defuse
|
||||||
|
8;9;1;Defuse
|
||||||
|
8;10;1;Defuse
|
||||||
|
8;1;2;Defuse
|
||||||
|
8;2;2;Defuse
|
||||||
|
8;3;2;Defuse
|
||||||
|
8;4;2;Defuse
|
||||||
|
8;5;2;Defuse
|
||||||
|
8;6;2;Defuse
|
||||||
|
8;7;2;Defuse
|
||||||
|
8;8;2;Defuse
|
||||||
|
8;9;2;Defuse
|
||||||
|
8;10;2;Defuse
|
||||||
|
8;1;3;Defuse
|
||||||
|
8;2;3;Defuse
|
||||||
|
8;3;3;Move away
|
||||||
|
8;4;3;Move away
|
||||||
|
8;5;3;Move away
|
||||||
|
8;6;3;Move away
|
||||||
|
8;7;3;Move away
|
||||||
|
8;8;3;Move away
|
||||||
|
8;9;3;Move away
|
||||||
|
8;10;3;Move away
|
||||||
|
9;1;1;Defuse
|
||||||
|
9;2;1;Defuse
|
||||||
|
9;3;1;Defuse
|
||||||
|
9;4;1;Defuse
|
||||||
|
9;5;1;Defuse
|
||||||
|
9;6;1;Defuse
|
||||||
|
9;7;1;Defuse
|
||||||
|
9;8;1;Defuse
|
||||||
|
9;9;1;Defuse
|
||||||
|
9;10;1;Defuse
|
||||||
|
9;1;2;Defuse
|
||||||
|
9;2;2;Defuse
|
||||||
|
9;3;2;Defuse
|
||||||
|
9;4;2;Defuse
|
||||||
|
9;5;2;Defuse
|
||||||
|
9;6;2;Defuse
|
||||||
|
9;7;2;Defuse
|
||||||
|
9;8;2;Defuse
|
||||||
|
9;9;2;Defuse
|
||||||
|
9;10;2;Defuse
|
||||||
|
9;1;3;Defuse
|
||||||
|
9;2;3;Defuse
|
||||||
|
9;3;3;Defuse
|
||||||
|
9;4;3;Defuse
|
||||||
|
9;5;3;Defuse
|
||||||
|
9;6;3;Move away
|
||||||
|
9;7;3;Move away
|
||||||
|
9;8;3;Move away
|
||||||
|
9;9;3;Move away
|
||||||
|
9;10;3;Move away
|
||||||
|
10;1;1;Defuse
|
||||||
|
10;2;1;Defuse
|
||||||
|
10;3;1;Defuse
|
||||||
|
10;4;1;Defuse
|
||||||
|
10;5;1;Defuse
|
||||||
|
10;6;1;Defuse
|
||||||
|
10;7;1;Defuse
|
||||||
|
10;8;1;Defuse
|
||||||
|
10;9;1;Defuse
|
||||||
|
10;10;1;Defuse
|
||||||
|
10;1;2;Defuse
|
||||||
|
10;2;2;Defuse
|
||||||
|
10;3;2;Defuse
|
||||||
|
10;4;2;Defuse
|
||||||
|
10;5;2;Defuse
|
||||||
|
10;6;2;Defuse
|
||||||
|
10;7;2;Defuse
|
||||||
|
10;8;2;Defuse
|
||||||
|
10;9;2;Defuse
|
||||||
|
10;10;2;Defuse
|
||||||
|
10;1;3;Defuse
|
||||||
|
10;2;3;Defuse
|
||||||
|
10;3;3;Defuse
|
||||||
|
10;4;3;Defuse
|
||||||
|
10;5;3;Defuse
|
||||||
|
10;6;3;Defuse
|
||||||
|
10;7;3;Defuse
|
||||||
|
10;8;3;Defuse
|
||||||
|
10;9;3;Defuse
|
||||||
|
10;10;3;Defuse
|
||||||
|
20;1;1;Defuse
|
||||||
|
20;2;1;Defuse
|
||||||
|
20;3;1;Defuse
|
||||||
|
20;4;1;Defuse
|
||||||
|
20;5;1;Defuse
|
||||||
|
20;6;1;Defuse
|
||||||
|
20;7;1;Defuse
|
||||||
|
20;8;1;Defuse
|
||||||
|
20;9;1;Defuse
|
||||||
|
20;10;1;Defuse
|
||||||
|
20;1;2;Defuse
|
||||||
|
20;2;2;Defuse
|
||||||
|
20;3;2;Defuse
|
||||||
|
20;4;2;Defuse
|
||||||
|
20;5;2;Defuse
|
||||||
|
20;6;2;Defuse
|
||||||
|
20;7;2;Defuse
|
||||||
|
20;8;2;Defuse
|
||||||
|
20;9;2;Defuse
|
||||||
|
20;10;2;Defuse
|
||||||
|
20;1;3;Defuse
|
||||||
|
20;2;3;Defuse
|
||||||
|
20;3;3;Defuse
|
||||||
|
20;4;3;Defuse
|
||||||
|
20;5;3;Defuse
|
||||||
|
20;6;3;Defuse
|
||||||
|
20;7;3;Defuse
|
||||||
|
20;8;3;Defuse
|
||||||
|
20;9;3;Defuse
|
||||||
|
20;10;3;Defuse
|
||||||
|
40;1;1;Defuse
|
||||||
|
40;2;1;Defuse
|
||||||
|
40;3;1;Defuse
|
||||||
|
40;4;1;Defuse
|
||||||
|
40;5;1;Defuse
|
||||||
|
40;6;1;Defuse
|
||||||
|
40;7;1;Defuse
|
||||||
|
40;8;1;Defuse
|
||||||
|
40;9;1;Defuse
|
||||||
|
40;10;1;Defuse
|
||||||
|
40;1;2;Defuse
|
||||||
|
40;2;2;Defuse
|
||||||
|
40;3;2;Defuse
|
||||||
|
40;4;2;Defuse
|
||||||
|
40;5;2;Defuse
|
||||||
|
60;6;2;Defuse
|
||||||
|
60;7;2;Defuse
|
||||||
|
60;8;2;Defuse
|
||||||
|
60;9;2;Defuse
|
||||||
|
60;10;2;Defuse
|
||||||
|
60;1;3;Defuse
|
||||||
|
60;2;3;Defuse
|
||||||
|
60;3;3;Defuse
|
||||||
|
60;4;3;Defuse
|
||||||
|
60;5;3;Defuse
|
||||||
|
60;6;3;Defuse
|
||||||
|
60;7;3;Defuse
|
||||||
|
60;8;3;Defuse
|
||||||
|
60;9;3;Defuse
|
||||||
|
60;10;3;Defuse
|
||||||
|
80;1;1;Defuse
|
||||||
|
80;2;1;Defuse
|
||||||
|
80;3;1;Defuse
|
||||||
|
80;4;1;Defuse
|
||||||
|
80;5;1;Defuse
|
||||||
|
80;6;1;Defuse
|
||||||
|
80;7;1;Defuse
|
||||||
|
80;8;1;Defuse
|
||||||
|
80;9;1;Defuse
|
||||||
|
80;10;1;Defuse
|
||||||
|
80;1;2;Defuse
|
||||||
|
80;2;2;Defuse
|
||||||
|
80;3;2;Defuse
|
||||||
|
80;4;2;Defuse
|
||||||
|
80;5;2;Defuse
|
||||||
|
100;6;2;Defuse
|
||||||
|
100;7;2;Defuse
|
||||||
|
100;8;2;Defuse
|
||||||
|
100;9;2;Defuse
|
||||||
|
100;10;2;Defuse
|
||||||
|
100;1;3;Defuse
|
||||||
|
100;2;3;Defuse
|
||||||
|
100;3;3;Defuse
|
||||||
|
100;4;3;Defuse
|
||||||
|
100;5;3;Defuse
|
||||||
|
100;6;3;Defuse
|
||||||
|
100;7;3;Defuse
|
||||||
|
100;8;3;Defuse
|
||||||
|
100;9;3;Defuse
|
||||||
|
100;10;3;Defuse
|
|
29
decisiontree.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
import pandas
|
||||||
|
from sklearn import tree
|
||||||
|
from sklearn.tree import DecisionTreeClassifier
|
||||||
|
|
||||||
|
decision = {'Defuse': 0, 'Move away': 1}
|
||||||
|
variables = {'Percent of mines', 'Time to explosion', 'Range of explosion'}
|
||||||
|
|
||||||
|
class DecisionTree:
|
||||||
|
def __init__(self):
|
||||||
|
self.data = pandas.read_csv("decisiontree.csv", sep=';')
|
||||||
|
#print(self.data)
|
||||||
|
self.data['What to do'] = self.data['What to do'].map(decision)
|
||||||
|
self.treevariables = self.data[variables]
|
||||||
|
self.treeresault = self.data['What to do']
|
||||||
|
self.decisiontree = DecisionTreeClassifier(random_state=0)
|
||||||
|
self.decisiontree = self.decisiontree.fit(self.treevariables.values, self.treeresault.values)
|
||||||
|
|
||||||
|
def make_decision(self, minespercent, exptime, exprange):
|
||||||
|
wtd = self.decisiontree.predict([[minespercent, exptime, exprange]])
|
||||||
|
#print("zmienne:", minespercent, exptime, exprange)
|
||||||
|
#print("wdt:", wtd)
|
||||||
|
if wtd == [0]:
|
||||||
|
return "Defuse"
|
||||||
|
if wtd == [1]:
|
||||||
|
return "Move away"
|
||||||
|
|
||||||
|
|
54
imagerecognizer.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import tensorflow
|
||||||
|
from tensorflow import keras
|
||||||
|
from keras import layers as ls
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
image_height = 64
|
||||||
|
image_width = 64
|
||||||
|
batch_size = 32
|
||||||
|
sets_path = "./learning_sets"
|
||||||
|
model_path="./IR.model"
|
||||||
|
train_new_model=True
|
||||||
|
|
||||||
|
class ImageRecognizer():
|
||||||
|
def __init__(self):
|
||||||
|
if train_new_model == True:
|
||||||
|
# deklarowanie setów treningowych i testowych
|
||||||
|
self.trainset = keras.utils.image_dataset_from_directory(sets_path, validation_split=0.2, subset="training",seed=0, image_size=(image_height, image_width),batch_size=batch_size)
|
||||||
|
self.testset = keras.utils.image_dataset_from_directory(sets_path, validation_split=0.2,subset="validation", seed=0,image_size=(image_height, image_width),atch_size=batch_size)
|
||||||
|
self.class_names = self.trainset.class_names
|
||||||
|
self.trainset = self.trainset.cache().shuffle(1000).prefetch(buffer_size=tensorflow.data.AUTOTUNE)
|
||||||
|
self.testset = self.testset.cache().prefetch(buffer_size=tensorflow.data.AUTOTUNE)
|
||||||
|
#tworzenie modelu
|
||||||
|
self.model = keras.models.Sequential()
|
||||||
|
self.model.add(ls.Rescaling(1. / 255, input_shape=(image_height, image_width, 3)))
|
||||||
|
self.model.add(ls.Conv2D(16, (3,3), activation='relu'))
|
||||||
|
self.model.add(ls.MaxPooling2D((2,2)))
|
||||||
|
self.model.add(ls.Conv2D(32, (3,3), activation='relu'))
|
||||||
|
self.model.add(ls.MaxPooling2D((2,2)))
|
||||||
|
self.model.add(ls.Conv2D(64, (3,3), activation='relu'))
|
||||||
|
self.model.add(ls.MaxPooling2D(2,2))
|
||||||
|
self.model.add(ls.Flatten())
|
||||||
|
self.model.add(ls.Dense(128, activation='relu'))
|
||||||
|
self.model.add(ls.Dense(2))
|
||||||
|
#kompilacja modelu
|
||||||
|
self.model.compile(optimizer='adam',loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
|
||||||
|
#epochowanie
|
||||||
|
self.model.fit(self.trainset,validation_data=self.testset,epochs=10)
|
||||||
|
self.model.save(model_path)
|
||||||
|
else:
|
||||||
|
self.class_names = ("bombs","stones")
|
||||||
|
self.model = tensorflow.keras.models.load_model(model_path)
|
||||||
|
|
||||||
|
def recognize(self,image_path):
|
||||||
|
#wybor obrazu do rekognizacji
|
||||||
|
image = keras.utils.load_img(image_path, target_size=(image_height, image_width))
|
||||||
|
image_array = keras.utils.img_to_array(image)
|
||||||
|
image_array = tensorflow.expand_dims(image_array, 0)
|
||||||
|
#predykcja
|
||||||
|
prediction = self.model.predict(image_array)
|
||||||
|
classification = tensorflow.nn.softmax(prediction[0])
|
||||||
|
#wynik predykcji
|
||||||
|
print("Image: ",image_path," is classified as: ",format(self.class_names[np.argmax(classification)])," with: ", 100 * np.max(classification), " accuracy")
|
||||||
|
return format(self.class_names[np.argmax(classification)])
|
BIN
images/mine.png
Normal file
After Width: | Height: | Size: 77 KiB |
BIN
images/mine_defused.png
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
images/mine_moved.png
Normal file
After Width: | Height: | Size: 70 KiB |
BIN
images/puddle.png
Normal file
After Width: | Height: | Size: 65 KiB |
BIN
images/ratAvatar.png
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
images/rock.png
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
images/testAvatar.png
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
images/tile.png
Normal file
After Width: | Height: | Size: 27 KiB |
BIN
learning_sets/bombs/001cf011d0.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
learning_sets/bombs/002b3fc5b7.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
learning_sets/bombs/003a5def71.jpg
Normal file
After Width: | Height: | Size: 40 KiB |
BIN
learning_sets/bombs/003b0d7815.jpg
Normal file
After Width: | Height: | Size: 32 KiB |
BIN
learning_sets/bombs/0045b1933b.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
learning_sets/bombs/007fd28988.jpg
Normal file
After Width: | Height: | Size: 51 KiB |
BIN
learning_sets/bombs/00a3d12e74.jpg
Normal file
After Width: | Height: | Size: 9.3 KiB |
BIN
learning_sets/bombs/00e31ac3a5.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
learning_sets/bombs/010794bc3a.jpg
Normal file
After Width: | Height: | Size: 128 KiB |
BIN
learning_sets/bombs/013d0a4931.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
learning_sets/bombs/015d65f975.jpg
Normal file
After Width: | Height: | Size: 205 KiB |
BIN
learning_sets/bombs/01a595731d.jpg
Normal file
After Width: | Height: | Size: 7.8 KiB |
BIN
learning_sets/bombs/02347b321c.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
learning_sets/bombs/0238f3db21.jpg
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
learning_sets/bombs/029ce26d41.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
learning_sets/bombs/02e7fc534a.jpg
Normal file
After Width: | Height: | Size: 147 KiB |
BIN
learning_sets/bombs/02f27f32f3.jpg
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
learning_sets/bombs/03525ad961.jpg
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
learning_sets/bombs/0352e0f838.jpg
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
learning_sets/bombs/036d02b927.jpg
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
learning_sets/bombs/0390434f0d.jpg
Normal file
After Width: | Height: | Size: 8.3 KiB |
BIN
learning_sets/bombs/03b41d3cf2.jpg
Normal file
After Width: | Height: | Size: 9.5 KiB |
BIN
learning_sets/bombs/041bd9a3b2.jpg
Normal file
After Width: | Height: | Size: 54 KiB |
BIN
learning_sets/bombs/044c77c6fc.jpg
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
learning_sets/bombs/04a908e956.jpg
Normal file
After Width: | Height: | Size: 120 KiB |
BIN
learning_sets/bombs/04d3470413.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
learning_sets/bombs/04fc6531af.jpg
Normal file
After Width: | Height: | Size: 40 KiB |
BIN
learning_sets/bombs/055704303c.jpg
Normal file
After Width: | Height: | Size: 211 KiB |
BIN
learning_sets/bombs/061c0a80fc.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
learning_sets/bombs/063dc12af0.jpg
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
learning_sets/bombs/0668b05c0f.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
learning_sets/bombs/0689d369cc.jpg
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
learning_sets/bombs/068d0918e2.jpg
Normal file
After Width: | Height: | Size: 54 KiB |
BIN
learning_sets/bombs/06a6d4dc31.jpg
Normal file
After Width: | Height: | Size: 5.4 KiB |
BIN
learning_sets/bombs/06aa532a38.jpg
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
learning_sets/bombs/06ad2bdaea.jpg
Normal file
After Width: | Height: | Size: 7.8 KiB |
BIN
learning_sets/bombs/06c95b038b.jpg
Normal file
After Width: | Height: | Size: 75 KiB |
BIN
learning_sets/bombs/06f2d848b5.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
learning_sets/bombs/08061954b9.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
learning_sets/bombs/08114bd86c.jpg
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
learning_sets/bombs/08411cfed5.jpg
Normal file
After Width: | Height: | Size: 62 KiB |
BIN
learning_sets/bombs/086dab9038.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
learning_sets/bombs/08f84432bd.jpg
Normal file
After Width: | Height: | Size: 9.9 KiB |
BIN
learning_sets/bombs/0902da089d.jpg
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
learning_sets/bombs/09974bb4bf.jpg
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
learning_sets/bombs/0a1a85c4d3.jpg
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
learning_sets/bombs/0a431849ea.jpg
Normal file
After Width: | Height: | Size: 235 KiB |
BIN
learning_sets/bombs/0aa23ee9a4.jpg
Normal file
After Width: | Height: | Size: 5.0 KiB |
BIN
learning_sets/bombs/0b13dc4324.jpg
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
learning_sets/bombs/0b2ef491ec.jpg
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
learning_sets/bombs/0b33caa155.jpg
Normal file
After Width: | Height: | Size: 7.8 KiB |
BIN
learning_sets/bombs/0b38f678d7.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
learning_sets/bombs/0b668a6887.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
learning_sets/bombs/0b709a1dea.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
learning_sets/bombs/0c6ec0155c.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
learning_sets/bombs/0c89e21587.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
learning_sets/bombs/0cbe1919fc.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
learning_sets/bombs/0cc17dcc05.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
learning_sets/bombs/0d0ba239ad.jpg
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
learning_sets/bombs/0d2cf20982.jpg
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
learning_sets/bombs/0d467b09be.jpg
Normal file
After Width: | Height: | Size: 520 KiB |
BIN
learning_sets/bombs/0d83dafcb8.jpg
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
learning_sets/bombs/0dc8768b7a.jpg
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
learning_sets/bombs/0dcd97e0fe.jpg
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
learning_sets/bombs/0dd7fa4f19.jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
learning_sets/bombs/0df3f43cea.jpg
Normal file
After Width: | Height: | Size: 121 KiB |
BIN
learning_sets/bombs/0e5a39772f.jpg
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
learning_sets/bombs/0e9164bc78.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
learning_sets/bombs/0e940f8df6.jpg
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
learning_sets/bombs/0eaceabee9.jpg
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
learning_sets/bombs/0eb4b992e2.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
learning_sets/bombs/0ef87f1dfb.jpg
Normal file
After Width: | Height: | Size: 114 KiB |
BIN
learning_sets/bombs/0f0a3b8f40.jpg
Normal file
After Width: | Height: | Size: 65 KiB |
BIN
learning_sets/bombs/0f5d614f90.jpg
Normal file
After Width: | Height: | Size: 6.9 KiB |
BIN
learning_sets/bombs/0f6c0485a6.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
learning_sets/bombs/0f79df18da.jpg
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
learning_sets/bombs/0f967344a6.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
learning_sets/bombs/0fcc984cec.jpg
Normal file
After Width: | Height: | Size: 9.3 KiB |
BIN
learning_sets/bombs/0fdc677ad7.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
learning_sets/bombs/0fff134694.jpg
Normal file
After Width: | Height: | Size: 95 KiB |
BIN
learning_sets/bombs/0fff3f27ec.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
learning_sets/bombs/1016d532aa.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
learning_sets/bombs/10c063224e.jpg
Normal file
After Width: | Height: | Size: 87 KiB |
BIN
learning_sets/bombs/110580e678.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
learning_sets/bombs/11062129c2.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
learning_sets/bombs/1112c82b26.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
learning_sets/bombs/116ffadd10.jpg
Normal file
After Width: | Height: | Size: 30 KiB |