random recognition added

This commit is contained in:
Роман Юдешко 2023-06-01 15:31:41 +02:00
parent 0396350f3a
commit 9f70657d0b
5 changed files with 75 additions and 43 deletions

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

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

114
field.py
View File

@ -1,15 +1,14 @@
import asyncio
import os
import random
import time
from heapq import *
from enum import Enum, IntEnum
from queue import PriorityQueue
from collections import deque
from threading import Thread
from IC3 import tree
import pygame
#from recognition_v1.proverka import *
from IC3 import tree
pygame.init()
BLACK = (0, 0, 0)
@ -17,15 +16,15 @@ WHITE = (200, 200, 200)
BLUE = (46, 34, 240)
WINDOW_DIMENSIONS = 900
BLOCK_SIZE = 60
ROCKS_NUMBER = 30
ROCKS_NUMBER = 20
VEGETABLES_NUMBER = 20
VEGETABLES = ('Potato', 'Broccoli', 'Carrot', 'Onion')
VEGETABLES = ('Potato', 'Broccoli', 'Carrot', 'Capsicum')
BOARD_SIZE = int(WINDOW_DIMENSIONS / BLOCK_SIZE)
WATER_TANK_CAPACITY = 10
GAS_TANK_CAPACITY = 250
SPAWN_POINT = (0, 0)
SKLEP_POINT = (14, 14)
TIMEOUT = 30
TIMEOUT = 1
tractor_image = pygame.transform.scale(pygame.image.load("images/tractor_image.png"), (BLOCK_SIZE, BLOCK_SIZE))
rock_image = pygame.transform.scale(pygame.image.load("images/rock_image.png"), (BLOCK_SIZE, BLOCK_SIZE))
@ -33,8 +32,11 @@ potato_image = pygame.transform.scale(pygame.image.load("images/potato.png"), (B
carrot_image = pygame.transform.scale(pygame.image.load("images/carrot.png"), (BLOCK_SIZE, BLOCK_SIZE))
broccoli_image = pygame.transform.scale(pygame.image.load("images/broccoli.png"), (BLOCK_SIZE, BLOCK_SIZE))
onion_image = pygame.transform.scale(pygame.image.load("images/onion.png"), (BLOCK_SIZE, BLOCK_SIZE))
capsicum_image = pygame.transform.scale(pygame.image.load("images/capsicum.png"), (BLOCK_SIZE, BLOCK_SIZE))
unknown_image = pygame.transform.scale(pygame.image.load("images/unknown.png"), (BLOCK_SIZE, BLOCK_SIZE))
gas_station_image = pygame.transform.scale(pygame.image.load("images/gas_station.png"), (BLOCK_SIZE, BLOCK_SIZE))
gas_station_closed_image = pygame.transform.scale(pygame.image.load("images/gas_station_closed.png"), (BLOCK_SIZE, BLOCK_SIZE))
gas_station_closed_image = pygame.transform.scale(pygame.image.load("images/gas_station_closed.png"),
(BLOCK_SIZE, BLOCK_SIZE))
sklep_station_image = pygame.transform.scale(pygame.image.load("images/storage_open.png"), (BLOCK_SIZE, BLOCK_SIZE))
sklep_closed_station_image = pygame.transform.scale(pygame.image.load("images/storage_closed.png"),
(BLOCK_SIZE, BLOCK_SIZE))
@ -76,7 +78,7 @@ def draw_interface():
tractor.gas = GAS_TANK_CAPACITY
if (tractor.x, tractor.y) == SKLEP_POINT:
tractor.collected_vegetables = {vegetables.POTATO: 0, vegetables.BROCCOLI: 0, vegetables.CARROT: 0,
vegetables.ONION: 0}
vegetables.CAPSICUM: 0}
global sc
sc = pygame.display.set_mode((WINDOW_DIMENSIONS, WINDOW_DIMENSIONS))
@ -90,7 +92,13 @@ def draw_interface():
grid = Grid(BOARD_SIZE, BOARD_SIZE, BLOCK_SIZE)
graph1 = Graph(grid)
t2 = Thread(target=close_open, args=(grid,))
t2.setDaemon(True)
t2.start()
fl_running = True
determine_thread = Thread(target=grid.determine)
determine_thread.setDaemon(True)
determine_thread.start()
while fl_running:
draw_grid()
# region events
@ -110,9 +118,6 @@ def draw_interface():
elif event.type == pygame.MOUSEBUTTONDOWN:
startpoint = (tractor.x, tractor.y, tractor.direction)
endpoint = get_click_mouse_pos()
a, c = graph1.a_star(startpoint, endpoint, grid)
b = getRoad(startpoint, c, a)
#movement(tractor, grid, b)
decisionTree(startpoint, endpoint, tractor, grid, graph1)
# a, c = graph1.a_star(startpoint, endpoint)
# b = getRoad(startpoint, c, a)
@ -122,9 +127,6 @@ def draw_interface():
# graph1.initialize_graph(grid)
class Direction(IntEnum):
UP = 0
RIGHT = 1
@ -136,7 +138,8 @@ class vegetables(Enum):
POTATO = 3
BROCCOLI = 4
CARROT = 5
ONION = 6
CAPSICUM = 6
UNKNOWN = 7
class types(Enum):
@ -145,7 +148,8 @@ class types(Enum):
POTATO = 3
BROCCOLI = 4
CARROT = 5
ONION = 6
CAPSICUM = 6
UNKNOWN = 7
class Grid:
@ -154,20 +158,13 @@ class Grid:
self.height = height
self.block_size = block_size
self.grid = [[types.EMPTY for col in range(BOARD_SIZE)] for row in range(BOARD_SIZE)]
self.photo_paths = [['' for col in range(BOARD_SIZE)] for row in range(BOARD_SIZE)]
self.vegetables_locations = []
self.initialize_grid()
self.is_gas_station_closed = True
self.is_storage_closed = True
t2 = Thread(target=self.close_open, args=(self,))
t2.setDaemon(True)
t2.start()
self.is_gas_station_closed = False
self.is_storage_closed = False
def close_open(self):
while True:
time.sleep(TIMEOUT)
self.is_gas_station_closed = bool(random.getrandbits(1))
self.is_storage_closed = bool(random.getrandbits(1))
def add_object(self, x, y, type_of_object: types):
def add_object(self, x, y, type_of_object):
if self.grid[x][y] == types.EMPTY:
self.grid[x][y] = type_of_object
return True
@ -185,7 +182,9 @@ class Grid:
for i in range(VEGETABLES_NUMBER):
x, y = random.randrange(0, BOARD_SIZE), random.randrange(0, BOARD_SIZE)
if self.grid[x][y] == types.EMPTY and (x, y) != (0, 0):
self.add_object(x, y, random.choice(list(vegetables)))
if self.add_object(x, y, vegetables.UNKNOWN): # random.choice(list(vegetables)))
self.vegetables_locations.append((x, y))
self.photo_paths[x][y] = get_random_photo_path()
else:
i -= 1
for i in range(ROCKS_NUMBER):
@ -195,13 +194,27 @@ class Grid:
else:
i -= 1
def determine(self):
for x, y in self.vegetables_locations:
if self.grid[x][y] == vegetables.UNKNOWN:
timeout = time.time() + 0.5
while True:
sc.blit(pygame.transform.scale(pygame.image.load(self.photo_paths[x][y]), (BLOCK_SIZE*2, BLOCK_SIZE*2)),
((x * BLOCK_SIZE) - BLOCK_SIZE/2, (y * BLOCK_SIZE) - BLOCK_SIZE/2))
if time.time() > timeout:
break
# time.sleep(1)
random_veg = random.choice(list(vegetables))
while random_veg == vegetables.UNKNOWN:
random_veg = random.choice(list(vegetables))
self.grid[x][y] = random_veg
# self.grid[x][y] = VegebatlesRecognizer.recognize(self.photo_paths[x][y])
class Graph:
def __init__(self, grid: Grid):
self.graph = {}
#<<<<<<< HEAD
#=======
# self.initialize_graph(grid)
# def initialize_graph(self, grid: Grid):
@ -210,7 +223,6 @@ class Graph:
# for direction in Direction:
# self.graph[(x, y, direction)] = get_next_nodes(x, y, direction, grid)
#>>>>>>> 2287b2b09f973efdd2dec60834b09109e0b221b9
def a_star(self, start, goal, grid: Grid):
# not finished yet https://www.youtube.com/watch?v=abHftC1GU6w
queue = PriorityQueue()
@ -250,7 +262,7 @@ class Tractor:
self.gas = GAS_TANK_CAPACITY
self.water = WATER_TANK_CAPACITY
self.collected_vegetables = {vegetables.POTATO: 0, vegetables.BROCCOLI: 0, vegetables.CARROT: 0,
vegetables.ONION: 0}
vegetables.CAPSICUM: 0}
self.image = pygame.transform.scale(pygame.image.load("images/tractor_image.png"), (BLOCK_SIZE, BLOCK_SIZE))
def rot_center(self, direc: Direction):
@ -304,7 +316,6 @@ def get_next_nodes(x, y, direction: Direction, grid: Grid):
def movement(tractor: Tractor, grid: Grid, road):
print(road)
n = len(road)
for i in range(n - 1):
aA = road[i]
@ -366,8 +377,10 @@ def updateDisplay(tractor: Tractor, grid: Grid):
sc.blit(carrot_image, (x * BLOCK_SIZE + 5, y * BLOCK_SIZE + 5))
elif grid.grid[x][y] == vegetables.BROCCOLI:
sc.blit(broccoli_image, (x * BLOCK_SIZE + 5, y * BLOCK_SIZE + 5))
elif grid.grid[x][y] == vegetables.ONION:
sc.blit(onion_image, (x * BLOCK_SIZE + 5, y * BLOCK_SIZE + 5))
elif grid.grid[x][y] == vegetables.CAPSICUM:
sc.blit(capsicum_image, (x * BLOCK_SIZE + 5, y * BLOCK_SIZE + 5))
elif grid.grid[x][y] == vegetables.UNKNOWN:
sc.blit(unknown_image, (x * BLOCK_SIZE + 5, y * BLOCK_SIZE + 5))
elif grid.grid[x][y] == types.ROCK:
sc.blit(rock_image, (x * BLOCK_SIZE, y * BLOCK_SIZE))
sc.blit(gas_station_image, (SPAWN_POINT[0] * BLOCK_SIZE, SPAWN_POINT[1] * BLOCK_SIZE))
@ -384,8 +397,8 @@ def updateDisplay(tractor: Tractor, grid: Grid):
vegetables_text = font.render(
'Potato: ' + str(tractor.collected_vegetables[vegetables.POTATO]) + ' Broccoli: ' + str(
tractor.collected_vegetables[vegetables.BROCCOLI]) + ' Carrot: ' + str(
tractor.collected_vegetables[vegetables.CARROT]) + ' Onion: ' + str(
tractor.collected_vegetables[vegetables.ONION]), True, WHITE, BLACK)
tractor.collected_vegetables[vegetables.CARROT]) + ' Capsicum: ' + str(
tractor.collected_vegetables[vegetables.CAPSICUM]), True, WHITE, BLACK)
vegetables_textrect = vegetables_text.get_rect()
vegetables_textrect.center = (WINDOW_DIMENSIONS // 2, WINDOW_DIMENSIONS - 30)
sc.blit(vegetables_text, vegetables_textrect)
@ -512,4 +525,23 @@ def decisionTree(startpoint, endpoint, tractor, grid, graph1):
print("GAME OVER")
def close_open(grid: Grid):
while True:
time.sleep(TIMEOUT)
grid.is_gas_station_closed = bool(random.getrandbits(1))
grid.is_storage_closed = bool(random.getrandbits(1))
def get_random_photo_path():
dir_num = random.randint(1, 4)
image_dir = "test_image_dataset"
if dir_num == 1:
image_dir = "test_image_dataset/Broccoli"
if dir_num == 2:
image_dir = "test_image_dataset/Capsicum"
if dir_num == 3:
image_dir = "test_image_dataset/Carrot"
if dir_num == 4:
image_dir = "test_image_dataset/Potato"
return image_dir + '\\' + random.choice(os.listdir(image_dir))

BIN
images/capsicum.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

BIN
images/unknown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB