product assigning.py to shelf.py; returning coordinates to shelf.py

This commit is contained in:
shaaqu 2020-06-09 17:17:08 +02:00
parent a08199b267
commit f4e67bce3b
46 changed files with 26348 additions and 67 deletions

View File

@ -3,5 +3,5 @@
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (AL-2020)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (AL-2020)" project-jdk-type="Python SDK" />
</project>

View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.7 (AL-2020)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.8 (AL-2020)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

26
assigning.py Normal file
View File

@ -0,0 +1,26 @@
import os
from codes_recognizer.rocognizer import recognizer
from board import get_shelfs
def assigning(product_code_path):
code = recognizer(product_code_path)
shelfs = get_shelfs()
for shelf in shelfs:
if shelf.is_empty():
shelf.add_product(code)
c = shelf.get_coordinates()
break
else:
if shelf.get_product() == code:
shelf.add_product(code)
c = shelf.get_coordinates()
break
print("Product", product_code_path, "is on", c)
return c

View File

@ -1,9 +1,10 @@
import pygame
from settings import Settings
from field import Field
from shelf import Shelf
settings = Settings()
shelfs_o = []
def create_board(screen):
board = []
@ -19,6 +20,7 @@ def create_board(screen):
if field.center_x == shelf[0] and field.center_y == shelf[1]:
field.is_shelf = True
field.image = pygame.image.load('img/shelf.png')
shelfs_o.append(Shelf(x, y))
row.append(field)
board.append(row)
@ -46,3 +48,6 @@ def draw_board(board):
field.blitme()
def get_shelfs():
return shelfs_o

View File

@ -68,8 +68,8 @@ def create_image(product):
print(string)
img = Image.new('RGB', (560, 112), color='white')
d = ImageDraw.Draw(img)
fnt = ImageFont.truetype('D:\Studia\Projects\AL-2020\img\codes\\arial_bold.ttf', 75)
d.text((30, 30), string, font=fnt, fill=(0, 0, 0))
fnt = ImageFont.truetype('img\codes\\arial_bold.ttf', 75)
d.text((28, 28), string, font=fnt, fill=(0, 0, 0))
path = 'img/codes/' + product.name + '.png'
img.save(path)
return path

View File

@ -1,56 +0,0 @@
import cv2
import torch
from nn_model import Net
from torchvision.transforms import transforms
def recognizer(paths):
codes = []
code = []
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
# load nn model
model = Net()
model.load_state_dict(torch.load('model.pt'))
model.eval()
for path in paths:
img = cv2.imread(path)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.GaussianBlur(img_gray, (5, 5), 0)
ret, im_th = cv2.threshold(img_gray, 90, 255, cv2.THRESH_BINARY_INV)
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
for rect in rects:
# Crop image
crop_img = img[rect[1]:rect[1] + rect[3] + 10, rect[0]:rect[0] + rect[2] + 10, 0]
# Resize the image
roi = cv2.resize(crop_img, (28, 28), interpolation=cv2.INTER_CUBIC)
# roi = cv2.dilate(roi, (3, 3))
# plt.imshow(roi)
# plt.show()
im = transform(roi)
im = im.view(1, 1, 28, 28)
with torch.no_grad():
logps = model(im)
ps = torch.exp(logps)
probab = list(ps.numpy()[0])
code.append(probab.index(max(probab)))
codes.append(code)
# cv2.imshow("Code", img)
# cv2.waitKey()
return codes

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

File diff suppressed because it is too large Load Diff

View File

@ -18,4 +18,4 @@ class Net(nn.Module):
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x)
return F.log_softmax(x, 1)

View File

@ -0,0 +1,52 @@
import cv2
import torch
from nn_model import Net
from torchvision.transforms import transforms
def recognizer(path):
codes = []
code = []
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
# load nn model
model = Net()
model.load_state_dict(torch.load('model.pt'))
model.eval()
img = cv2.imread(path)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.GaussianBlur(img_gray, (5, 5), 0)
ret, im_th = cv2.threshold(img_gray, 90, 255, cv2.THRESH_BINARY_INV)
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
for rect in rects:
# Crop image
crop_img = img[rect[1]:rect[1] + rect[3] + 10, rect[0]:rect[0] + rect[2] + 10, 0]
# Resize the image
roi = cv2.resize(crop_img, (28, 28), interpolation=cv2.INTER_CUBIC)
# roi = cv2.dilate(roi, (3, 3))
# plt.imshow(roi)
# plt.show()
im = transform(roi)
im = im.view(1, 1, 28, 28)
with torch.no_grad():
logps = model(im)
ps = torch.exp(logps)
probab = list(ps.numpy()[0])
code.append(probab.index(max(probab)))
codes.append(code)
# cv2.imshow("Code", img)
# cv2.waitKey()
return codes

View File

@ -7,7 +7,7 @@ import data
from agent import Agent
from settings import Settings
from board import create_board, draw_board
from board import create_board, draw_board, get_shelfs
from random import randint, choice
from mcda import selectedSupply
from product import FinalProduct
@ -95,4 +95,5 @@ def run():
pygame.display.flip()
run()

View File

@ -3,9 +3,24 @@ import pygame
class Shelf:
def __init__(self, id):
self.id = id
def __init__(self, x, y):
self.x = x
self.y = y
self.products = []
def addProduct(self, sweet):
self.products.append(sweet)
def add_product(self, product):
self.products.append(product)
def get_product(self):
return self.products[0]
def get_coordinates(self):
c = [self.x, self.y]
return c
def is_empty(self):
if len(self.products) == 0:
return True
else:
return False