机器学习训练手写数字识别(三)
接上帖:
接下来是网页前端的结果展示代码,用的 flask 框架,下面贴出来一部分代码,其余的分享到下载链接中供大家下载测试用。
以下为部分 js 代码
/* global $ */
class Main {
constructor() {
this.canvas = document.getElementById('main');
this.input = document.getElementById('input');
this.canvas.width = 449; // 16 * 28 + 1
this.canvas.height = 449; // 16 * 28 + 1
this.ctx = this.canvas.getContext('2d');
this.canvas.addEventListener('mousedown', this.onMouseDown.bind(this));
this.canvas.addEventListener('mouseup', this.onMouseUp.bind(this));
this.canvas.addEventListener('mousemove', this.onMouseMove.bind(this));
this.initialize();
}
initialize() {
this.ctx.fillStyle = '#FFFFFF';
this.ctx.fillRect(0, 0, 449, 449);
this.ctx.lineWidth = 1;
this.ctx.strokeRect(0, 0, 449, 449);
this.ctx.lineWidth = 0.05;
for (var i = 0; i < 27; i++) {
this.ctx.beginPath();
this.ctx.moveTo((i + 1) * 16, 0);
this.ctx.lineTo((i + 1) * 16, 449);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo( 0, (i + 1) * 16);
this.ctx.lineTo(449, (i + 1) * 16);
this.ctx.closePath();
this.ctx.stroke();
}
this.drawInput();
$('#output td').text('').removeClass('success');
}
onMouseDown(e) {
this.canvas.style.cursor = 'default';
this.drawing = true;
this.prev = this.getPosition(e.clientX, e.clientY);
}
onMouseUp() {
this.drawing = false;
this.drawInput();
}
onMouseMove(e) {
if (this.drawing) {
var curr = this.getPosition(e.clientX, e.clientY);
this.ctx.lineWidth = 16;
this.ctx.lineCap = 'round';
this.ctx.beginPath();
this.ctx.moveTo(this.prev.x, this.prev.y);
this.ctx.lineTo(curr.x, curr.y);
this.ctx.stroke();
this.ctx.closePath();
this.prev = curr;
}
}
getPosition(clientX, clientY) {
var rect = this.canvas.getBoundingClientRect();
return {
x: clientX - rect.left,
y: clientY - rect.top
};
}
drawInput() {
var ctx = this.input.getContext('2d');
var img = new Image();
img.onload = () => {
var inputs = [];
var small = document.createElement('canvas').getContext('2d');
small.drawImage(img, 0, 0, img.width, img.height, 0, 0, 28, 28);
var data = small.getImageData(0, 0, 28, 28).data;
for (var i = 0; i < 28; i++) {
for (var j = 0; j < 28; j++) {
var n = 4 * (i * 28 + j);
inputs[i * 28 + j] = (data[n + 0] + data[n + 1] + data[n + 2]) / 3;
ctx.fillStyle = 'rgb(' + [data[n + 0], data[n + 1], data[n + 2]].join(',') + ')';
ctx.fillRect(j * 5, i * 5, 5, 5);
}
}
if (Math.min(...inputs) === 255) {
return;
}
$.ajax({
url: '/api/mnist',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(inputs),
success: (data) => {
for (let i = 0; i < 2; i++) {
var max = 0;
var max_index = 0;
for (let j = 0; j < 10; j++) {
var value = Math.round(data.results[i][j] * 1000);
if (value > max) {
max = value;
max_index = j;
}
var digits = String(value).length;
for (var k = 0; k < 3 - digits; k++) {
value = '0' + value;
}
var text = '0.' + value;
if (value > 999) {
text = '1.000';
}
$('#output tr').eq(j + 1).find('td').eq(i).text(text);
}
for (let j = 0; j < 10; j++) {
if (j === max_index) {
$('#output tr').eq(j + 1).find('td').eq(i).addClass('success');
} else {
$('#output tr').eq(j + 1).find('td').eq(i).removeClass('success');
}
}
}
}
});
};
img.src = this.canvas.toDataURL();
}
}
$(() => {
var main = new Main();
$('#clear').click(() => {
main.initialize();
});
});
以下为前端展示网页 html
<!DOCTYPE html>
<html>
<head>
<title>MNIST</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script type="text/javascript" src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/main.js') }}"></script>
</head>
<body>
<!--<a href="https://github.com/sugyan/tensorflow-mnist"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="机器学习训练手写数字识别(三)" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a>-->
<div class="container">
<h1>MNIST</h1>
<div class="row">
<div class="col-md-6">
<p>draw a digit here!</p>
<canvas id="main"></canvas>
<p>
<button id="clear" class="btn btn-default">clear</button>
</p>
</div>
<div class="col-md-6">
<p>input:</p>
<canvas id="input" style="border:1px solid" width="140" height="140"></canvas>
<hr>
<p>output:</p>
<!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
<div id="regression" style="width: 600px;height:300px;">
<table id="result" class="table table-condensed table-hover text-center" border="1">
<!-- On rows -->
<tr>
<td></td>
<td>regression</td>
<td>convolutional</td>
</tr>
<tr>
<td>0</td>
<td></td>
<td></td>
</tr>
<tr>
<td>1</td>
<td></td>
<td></td>
</tr>
<tr>
<td>2</td>
<td></td>
<td></td>
</tr>
<tr>
<td>3</td>
<td></td>
<td></td>
</tr>
<tr>
<td>4</td>
<td></td>
<td></td>
</tr>
<tr>
<td>5</td>
<td></td>
<td></td>
</tr>
<tr>
<td>6</td>
<td></td>
<td></td>
</tr>
<tr>
<td>7</td>
<td></td>
<td></td>
</tr>
<tr>
<td>8</td>
<td></td>
<td></td>
</tr>
<tr>
<td>9</td>
<td></td>
<td></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
这个必须点赞👍