feat: neural network interface

This commit is contained in:
Marcin Czerniak 2021-06-21 12:03:27 +02:00
parent 0290293ca0
commit e14915f1df
15 changed files with 76 additions and 15 deletions

View File

@ -14,6 +14,8 @@
<!-- Styles -->
<link rel="stylesheet" href="styles.css">
<script src="view/ordersView.js"></script>
<!-- logic -->
<script src="configuration.js"></script>
<script src="utilities.js"></script>
@ -27,7 +29,6 @@
<script src="view/floor.js"></script>
<script src="view/grid.js"></script>
<script src="view/ordersView.js"></script>
<script src="logic/pathfinding.js"></script>
<script src="view/timeController.js"></script>
<script src="view/agentController.js"></script>
@ -316,11 +317,10 @@
</div>
</div>
<div class="line gaps">
<div class="response">
<!-- PYTHON RESPONSE HERE -->
</div>
<div class="response"></div>
<button class="clear" onClick="OrdersView.instance.clear()">CLEAR</button>
<button class="order" onClick="OrdersView.instance.order()">ORDER</button>
<button class="order" onClick="OrdersView.instance.addOrderByLastRecognizedString()">OK</button>
</div>
</div>
</div>

View File

@ -10,6 +10,7 @@ class Agent extends AgentController {
const cycle = async () => {
try {
const jobRequest = Products.instance.getJobRequest();
console.log(jobRequest);
if (jobRequest) {
await this.takeFromStore(jobRequest.product, jobRequest.amount);
await this.deliver(jobRequest.x, jobRequest.y);

View File

@ -1,5 +1,6 @@
from flask import Flask, redirect, jsonify, request
import decision_tree
from neural_network import *
app = Flask(
__name__,
@ -31,6 +32,28 @@ def api_predict_shelf():
return jsonify([ n2t['polka'][clf.predict([X]).tolist()[0]] ])
@app.route('/api/neural', methods=['POST'])
def naural():
length = int(request.form['length'])
print(length)
data = list()
for fileIndex in range(length):
file = request.files[f'file-{fileIndex}']
file.save(f'user_input/file-{fileIndex}.png')
data.append(f'user_input/file-{fileIndex}.png')
return jsonify(testCase(data))
# try:
# return("Image uploaded")
# print("Image uploaded")
# except Exception as err:
# print("Error occurred")
# print(err)
# return("Error, image not received.")
@app.route('/')
def index():
return redirect('/index.html')

View File

@ -30,8 +30,8 @@ class NeuralNetwork:
self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5)
self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5)
""" używane przy pobieraniu danych o nauczonej sieci, z pliku """
# self.weights = np.load(fileWeight)
# self.hidden = np.load(fileHidden)
self.weights = np.load(fileWeight)
self.hidden = np.load(fileHidden)
self.lr = learningGrade
@ -136,23 +136,26 @@ li = []
ourOwnDataset = []
record_cache = None
def testCase(inputWord):
len = len(inputWord)
length = len(inputWord)
word = ""
for i in range(0,len-2):
imgArray = imageio.imread(imageFileName, as_gray=True)
for i in range(0, length):
imgArray = imageio.imread(inputWord[i], as_gray=True)
imgData = 255 - imgArray.reshape(784)
imgData = (imgData/255 * 0.99) + 0.01
#inputWord[i]
if i < length - 2:
word = word + recognizeLet(letterNetwork, imgData)
word = word + recognizeNum(digitNetwork, inputWord[-2])
word = word + recognizeNum(digitNetwork ,inputWord[-1])
else:
word = word + recognizeNum(digitNetwork, imgData)
#assert record_cache.shape == ourOwnDataset[0].shape
#labelInput = np.asfarray(li)
#print(labelInput)
print('slowo: ', word)
return word
pass
@ -234,5 +237,5 @@ print('slowo: ', word)
##################################### URUCHOMIENIE TRENINGU
trainNetwork(letterNetwork, "Lweights_test.npy", "Lhidden_test.npy", let_train_images, let_train_labels)
# trainNetwork(letterNetwork, "Lweights_test.npy", "Lhidden_test.npy", let_train_images, let_train_labels)
# trainNetwork(digitNetwork, "Dweights_test.npy", "Dhidden_test.npy", let_train_images, let_train_labels)

BIN
src/user_input/file-0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 399 B

BIN
src/user_input/file-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 612 B

BIN
src/user_input/file-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 574 B

BIN
src/user_input/file-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 684 B

BIN
src/user_input/file-4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 542 B

BIN
src/user_input/file-5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 B

BIN
src/user_input/file-6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 686 B

BIN
src/user_input/file-7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 477 B

View File

@ -3,6 +3,9 @@ class OrdersView {
constructor() {
OrdersView.instance = this;
this.orders = [];
this.lastRecognizedText = '';
this.addMultipleCanvases(10);
}
@ -23,6 +26,8 @@ class OrdersView {
canvas.height = 28;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 28, 28);
ctx.drawImage(img, 0, 0, 28, 28);
canvas.toBlob((blob) => {
@ -34,13 +39,17 @@ class OrdersView {
});
});
})
).then(async () => {
).then(async (res) => {
formData.append('length', res.length);
const response = await fetch('/api/neural', {
method: 'POST',
body: formData
});
//const json = await response.json();
console.log(await response.text());
const res_ = (await response.text()).replace(/[\"]{1,}/g, '');
document.querySelector('.response').innerHTML = res_;
this.lastRecognizedText = res_;
});
}
@ -115,4 +124,29 @@ class OrdersView {
ctx.stroke();
}
}
addOrderByLastRecognizedString() {
this.addOrderByString(this.lastRecognizedText);
}
addOrderByString(str) {
const numberStr = str.substring(str.length - 2, str.length);
const text = str.substring(0, str.length - 2);
const number = parseInt(numberStr);
this.orders.push({
product: Product.REGISTRY[text],
amount: number,
x: 11,
y: 7,
});
this.closeWindow();
}
getOrder() {
if (!this.orders.length) return null;
return this.orders.shift();
}
}