Compare commits
3 Commits
master
...
3c5b05a7bb
Author | SHA1 | Date | |
---|---|---|---|
|
3c5b05a7bb | ||
|
50292376e7 | ||
|
ffe1ff9565 |
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
__pycache__
|
121
main.py
@ -4,90 +4,10 @@ import random
|
|||||||
import land
|
import land
|
||||||
import tractor
|
import tractor
|
||||||
import blocks
|
import blocks
|
||||||
|
import nn
|
||||||
import astar_search
|
import astar_search
|
||||||
import neural_network.inference
|
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
|
import numpy as np
|
||||||
examples = [
|
|
||||||
['piasek', 'sucha', 'jalowa', 'żółty'],
|
|
||||||
['czarnoziem', 'wilgotna', 'bogata', 'brazowa'],
|
|
||||||
['rzedzina', 'wilgotna', 'bogata', 'zielona'],
|
|
||||||
['gleby murszowe', 'wilgotna', 'bogata', 'szara'],
|
|
||||||
['pustynne gleby', 'sucha', 'jalowa', 'pomarańczowa'],
|
|
||||||
['torfowiska', 'sucha', 'jalowa', 'czerwona']
|
|
||||||
]
|
|
||||||
|
|
||||||
attributes = ['typ_gleby', 'wilgotność', 'zawartość_składników', 'kolor']
|
|
||||||
|
|
||||||
# Tworzenie obiektu TreeLearn i nauka drzewa decyzyjnego
|
|
||||||
# tree_learner = TreeLearn()
|
|
||||||
# default_class = 'nieznane'
|
|
||||||
|
|
||||||
# tree_learner.train(examples, attributes, default_class)
|
|
||||||
|
|
||||||
class TreeLearn:
|
|
||||||
def __init__(self):
|
|
||||||
self.tree = None
|
|
||||||
|
|
||||||
def train(self, examples, attributes, default_class):
|
|
||||||
self.tree = self.build_tree(examples, attributes, default_class)
|
|
||||||
|
|
||||||
def build_tree(self, examples, attributes, default_class):
|
|
||||||
if not examples:
|
|
||||||
return Node(default_class)
|
|
||||||
|
|
||||||
if self.all_same_class(examples):
|
|
||||||
return Node(examples[0][-1])
|
|
||||||
|
|
||||||
if not attributes:
|
|
||||||
class_counts = self.get_class_counts(examples)
|
|
||||||
default_class = max(class_counts, key=class_counts.get)
|
|
||||||
return Node(default_class)
|
|
||||||
|
|
||||||
best_attribute = self.choose_attribute(examples, attributes)
|
|
||||||
root = Node(best_attribute)
|
|
||||||
|
|
||||||
attribute_values = self.get_attribute_values(examples, best_attribute)
|
|
||||||
|
|
||||||
for value in attribute_values:
|
|
||||||
new_examples = self.filter_examples(examples, best_attribute, value)
|
|
||||||
new_attributes = attributes[:]
|
|
||||||
new_attributes.remove(best_attribute)
|
|
||||||
new_default_class = max(self.get_class_counts(new_examples), key=lambda k: class_counts.get(k, 0))
|
|
||||||
|
|
||||||
subtree = self.build_tree(new_examples, new_attributes, new_default_class)
|
|
||||||
root.add_child(value, subtree)
|
|
||||||
|
|
||||||
return root
|
|
||||||
|
|
||||||
def all_same_class(self, examples):
|
|
||||||
return len(set([example[-1] for example in examples])) == 1
|
|
||||||
|
|
||||||
def get_class_counts(self, examples):
|
|
||||||
class_counts = {}
|
|
||||||
for example in examples:
|
|
||||||
class_label = example[-1]
|
|
||||||
class_counts[class_label] = class_counts.get(class_label, 0) + 1
|
|
||||||
return class_counts
|
|
||||||
|
|
||||||
def choose_attribute(self, examples, attributes):
|
|
||||||
# Placeholder for attribute selection logic
|
|
||||||
return attributes[0]
|
|
||||||
|
|
||||||
def get_attribute_values(self, examples, attribute):
|
|
||||||
return list(set([example[attribute] for example in examples]))
|
|
||||||
|
|
||||||
def filter_examples(self, examples, attribute, value):
|
|
||||||
return [example for example in examples if example[attribute] == value]
|
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
|
||||||
def __init__(self, label):
|
|
||||||
self.label = label
|
|
||||||
self.children = {}
|
|
||||||
|
|
||||||
def add_child(self, value, child):
|
|
||||||
self.children[value] = child
|
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
@ -105,6 +25,8 @@ class Game:
|
|||||||
self.grass_body = []
|
self.grass_body = []
|
||||||
self.red_block = [] #aim block
|
self.red_block = [] #aim block
|
||||||
|
|
||||||
|
#self.one_body = []
|
||||||
|
|
||||||
self.fawn_seed_body = []
|
self.fawn_seed_body = []
|
||||||
self.fawn_wheat_body = []
|
self.fawn_wheat_body = []
|
||||||
|
|
||||||
@ -141,6 +63,15 @@ class Game:
|
|||||||
# self.potato = blocks.Blocks(self.surface, self.cell_size)
|
# self.potato = blocks.Blocks(self.surface, self.cell_size)
|
||||||
# self.potato.locate_soil('black earth', 6, 1, [])
|
# self.potato.locate_soil('black earth', 6, 1, [])
|
||||||
|
|
||||||
|
#class_names = ['Pumpkin', 'Tomato', 'Carrot']
|
||||||
|
|
||||||
|
self.neural_network = nn.NNModel("neural_network/save/second_model.pth")
|
||||||
|
|
||||||
|
# self.pumpkin_batch = self.neural_network.input_image("resources/pampkin.png")
|
||||||
|
# self.tomato_batch = self.neural_network.input_image("resources/tomato.png")
|
||||||
|
# self.carrot_batch = self.neural_network.input_image("resources/carrot.png")
|
||||||
|
|
||||||
|
|
||||||
self.tractor = tractor.Tractor(self.surface, self.cell_size)
|
self.tractor = tractor.Tractor(self.surface, self.cell_size)
|
||||||
self.tractor.draw()
|
self.tractor.draw()
|
||||||
|
|
||||||
@ -152,13 +83,10 @@ class Game:
|
|||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
move_tractor_event = pygame.USEREVENT + 1
|
move_tractor_event = pygame.USEREVENT + 1
|
||||||
pygame.time.set_timer(move_tractor_event, 500) # tractor moves every 1000 ms
|
pygame.time.set_timer(move_tractor_event, 100) # tractor moves every 1000 ms
|
||||||
tractor_next_moves = []
|
tractor_next_moves = []
|
||||||
astar_search_object = astar_search.Search(self.cell_size, self.cell_number)
|
astar_search_object = astar_search.Search(self.cell_size, self.cell_number)
|
||||||
|
|
||||||
veggies = dict()
|
|
||||||
veggies_debug = dict()
|
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
clock.tick(60) # manual fps control not to overwork the computer
|
clock.tick(60) # manual fps control not to overwork the computer
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
@ -186,28 +114,21 @@ class Game:
|
|||||||
random_x = random.randrange(0, self.cell_number * self.cell_size, 50)
|
random_x = random.randrange(0, self.cell_number * self.cell_size, 50)
|
||||||
random_y = random.randrange(0, self.cell_number * self.cell_size, 50)
|
random_y = random.randrange(0, self.cell_number * self.cell_size, 50)
|
||||||
print("Generated target: ",random_x, random_y)
|
print("Generated target: ",random_x, random_y)
|
||||||
|
#aim-blue block
|
||||||
if self.red_block:
|
if self.red_block:
|
||||||
self.red_block.pop()
|
self.red_block.pop()
|
||||||
self.red_block.append([random_x/50, random_y/50])
|
self.red_block.append([random_x/50, random_y/50])
|
||||||
|
|
||||||
|
self.path_image = "resources/2.png"
|
||||||
|
self.aim_batch = self.neural_network.input_image(self.path_image)
|
||||||
|
self.predicate = self.neural_network.predicte(self.aim_batch)
|
||||||
|
|
||||||
|
|
||||||
# below line should be later moved into tractor.py
|
# below line should be later moved into tractor.py
|
||||||
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
|
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
|
||||||
#bandaid to know about stones
|
#bandaid to know about stones
|
||||||
tractor_next_moves = astar_search_object.astarsearch(
|
tractor_next_moves = astar_search_object.astarsearch(
|
||||||
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y], self.stone_body, self.flower_body)
|
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y], self.stone_body, self.flower_body)
|
||||||
current_veggie = next(os.walk('./neural_network/images/test'))[1][random.randint(0, len(next(os.walk('./neural_network/images/test'))[1])-1)]
|
|
||||||
if(current_veggie in veggies_debug):
|
|
||||||
veggies_debug[current_veggie]+=1
|
|
||||||
else:
|
|
||||||
veggies_debug[current_veggie] = 1
|
|
||||||
|
|
||||||
current_veggie_example = next(os.walk(f'./neural_network/images/test/{current_veggie}'))[2][random.randint(0, len(next(os.walk(f'./neural_network/images/test/{current_veggie}'))[2])-1)]
|
|
||||||
predicted_veggie = neural_network.inference.main(f"./neural_network/images/test/{current_veggie}/{current_veggie_example}")
|
|
||||||
if predicted_veggie in veggies:
|
|
||||||
veggies[predicted_veggie]+=1
|
|
||||||
else:
|
|
||||||
veggies[predicted_veggie] = 1
|
|
||||||
print("Debug veggies: ", veggies_debug, "Predicted veggies: ", veggies)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
|
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
|
||||||
elif event.type == QUIT:
|
elif event.type == QUIT:
|
||||||
|
BIN
neural_network/dataset/vegetables/test/Bean/0001.jpg
Normal file
After Width: | Height: | Size: 58 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0002.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0003.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0004.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0005.jpg
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0006.jpg
Normal file
After Width: | Height: | Size: 21 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0007.jpg
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0008.jpg
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0009.jpg
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0010.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0011.jpg
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0012.jpg
Normal file
After Width: | Height: | Size: 65 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0013.jpg
Normal file
After Width: | Height: | Size: 64 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0014.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0015.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0016.jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0017.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0018.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0019.jpg
Normal file
After Width: | Height: | Size: 69 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0020.jpg
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0021.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0022.jpg
Normal file
After Width: | Height: | Size: 21 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0100.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0101.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0102.jpg
Normal file
After Width: | Height: | Size: 23 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0103.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0104.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0105.jpg
Normal file
After Width: | Height: | Size: 62 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0106.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0107.jpg
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0108.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0109.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0110.jpg
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0111.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0112.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0113.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0114.jpg
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0115.jpg
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0116.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0117.jpg
Normal file
After Width: | Height: | Size: 53 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0118.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0119.jpg
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0120.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0121.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0122.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0123.jpg
Normal file
After Width: | Height: | Size: 23 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0124.jpg
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0125.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0126.jpg
Normal file
After Width: | Height: | Size: 64 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0127.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0128.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0129.jpg
Normal file
After Width: | Height: | Size: 49 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0130.jpg
Normal file
After Width: | Height: | Size: 8.9 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0131.jpg
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0132.jpg
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0133.jpg
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0134.jpg
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0135.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0136.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0137.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0138.jpg
Normal file
After Width: | Height: | Size: 66 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0139.jpg
Normal file
After Width: | Height: | Size: 21 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0140.jpg
Normal file
After Width: | Height: | Size: 66 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0141.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0142.jpg
Normal file
After Width: | Height: | Size: 65 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0143.jpg
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0144.jpg
Normal file
After Width: | Height: | Size: 70 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0145.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0146.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0147.jpg
Normal file
After Width: | Height: | Size: 9.5 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0148.jpg
Normal file
After Width: | Height: | Size: 64 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0149.jpg
Normal file
After Width: | Height: | Size: 36 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0150.jpg
Normal file
After Width: | Height: | Size: 8.8 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0151.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0152.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0153.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0154.jpg
Normal file
After Width: | Height: | Size: 9.8 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0199.jpg
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0200.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0201.jpg
Normal file
After Width: | Height: | Size: 62 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0202.jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0203.jpg
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0204.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0205.jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0206.jpg
Normal file
After Width: | Height: | Size: 21 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0207.jpg
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0208.jpg
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0212.jpg
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0309.jpg
Normal file
After Width: | Height: | Size: 25 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0310.jpg
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0311.jpg
Normal file
After Width: | Height: | Size: 52 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0312.jpg
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0313.jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
neural_network/dataset/vegetables/test/Bean/0314.jpg
Normal file
After Width: | Height: | Size: 20 KiB |