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

View File

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

View File

@ -1,5 +1,6 @@
from flask import Flask, redirect, jsonify, request from flask import Flask, redirect, jsonify, request
import decision_tree import decision_tree
from neural_network import *
app = Flask( app = Flask(
__name__, __name__,
@ -31,6 +32,28 @@ def api_predict_shelf():
return jsonify([ n2t['polka'][clf.predict([X]).tolist()[0]] ]) 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('/') @app.route('/')
def index(): def index():
return redirect('/index.html') return redirect('/index.html')

View File

@ -30,8 +30,8 @@ class NeuralNetwork:
self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5) self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5)
self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5) self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5)
""" używane przy pobieraniu danych o nauczonej sieci, z pliku """ """ używane przy pobieraniu danych o nauczonej sieci, z pliku """
# self.weights = np.load(fileWeight) self.weights = np.load(fileWeight)
# self.hidden = np.load(fileHidden) self.hidden = np.load(fileHidden)
self.lr = learningGrade self.lr = learningGrade
@ -136,23 +136,26 @@ li = []
ourOwnDataset = [] ourOwnDataset = []
record_cache = None record_cache = None
def testCase(inputWord): def testCase(inputWord):
len = len(inputWord) length = len(inputWord)
word = "" word = ""
for i in range(0,len-2): for i in range(0, length):
imgArray = imageio.imread(imageFileName, as_gray=True) imgArray = imageio.imread(inputWord[i], as_gray=True)
imgData = 255 - imgArray.reshape(784) imgData = 255 - imgArray.reshape(784)
imgData = (imgData/255 * 0.99) + 0.01 imgData = (imgData/255 * 0.99) + 0.01
#inputWord[i] #inputWord[i]
word = word + recognizeLet(letterNetwork ,imgData)
word = word + recognizeNum(digitNetwork, inputWord[-2]) if i < length - 2:
word = word + recognizeNum(digitNetwork ,inputWord[-1]) word = word + recognizeLet(letterNetwork, imgData)
else:
word = word + recognizeNum(digitNetwork, imgData)
#assert record_cache.shape == ourOwnDataset[0].shape #assert record_cache.shape == ourOwnDataset[0].shape
#labelInput = np.asfarray(li) #labelInput = np.asfarray(li)
#print(labelInput) #print(labelInput)
print('slowo: ', word) print('slowo: ', word)
return word
pass pass
@ -234,5 +237,5 @@ print('slowo: ', word)
##################################### URUCHOMIENIE TRENINGU ##################################### 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) # 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() { constructor() {
OrdersView.instance = this; OrdersView.instance = this;
this.orders = [];
this.lastRecognizedText = '';
this.addMultipleCanvases(10); this.addMultipleCanvases(10);
} }
@ -23,6 +26,8 @@ class OrdersView {
canvas.height = 28; canvas.height = 28;
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 28, 28);
ctx.drawImage(img, 0, 0, 28, 28); ctx.drawImage(img, 0, 0, 28, 28);
canvas.toBlob((blob) => { 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', { const response = await fetch('/api/neural', {
method: 'POST', method: 'POST',
body: formData body: formData
}); });
//const json = await response.json(); //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(); 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();
}
} }