给我一个手写数字识别的前端代码
时间: 2024-05-04 07:22:22 浏览: 78
手写数字识别代码
4星 · 用户满意度95%
这里是一个基于JavaScript和HTML的手写数字识别前端代码示例:
HTML代码
```html
<!DOCTYPE html>
<html>
<head>
<title>Handwritten Digit Recognition</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<canvas id="canvas" width="280" height="280"></canvas>
<div class="buttons">
<button id="clear-button">Clear</button>
<button id="predict-button">Predict</button>
</div>
<div class="result">
<p>Predicted Digit: <span id="prediction"></span></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码
```css
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: #F5F5F5;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
}
canvas {
border: 1px solid #CCC;
background-color: #FFF;
margin-bottom: 20px;
}
.buttons {
display: flex;
flex-direction: row;
margin-bottom: 20px;
}
button {
border: none;
background-color: #4CAF50;
color: #FFF;
font-size: 16px;
padding: 10px;
margin-right: 10px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #3E8E41;
}
.result {
display: none;
}
.result p {
font-size: 24px;
font-weight: bold;
}
.result span {
color: #4CAF50;
font-size: 36px;
}
```
JavaScript代码
```javascript
const canvas = document.getElementById('canvas')
const context = canvas.getContext('2d')
const clearButton = document.getElementById('clear-button')
const predictButton = document.getElementById('predict-button')
const predictionText = document.getElementById('prediction')
let isDrawing = false
let lastX = 0
let lastY = 0
// Set up canvas
context.lineWidth = 20
context.lineJoin = 'round'
context.lineCap = 'round'
context.strokeStyle = '#000'
// Clear canvas
clearButton.addEventListener('click', () => {
context.clearRect(0, 0, canvas.width, canvas.height)
predictionText.innerText = ''
predictionText.parentNode.parentNode.style.display = 'none'
})
// Get mouse position on canvas
function getMousePos(canvas, event) {
const rect = canvas.getBoundingClientRect()
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
}
}
// Draw on canvas
canvas.addEventListener('mousedown', (event) => {
isDrawing = true
const mousePos = getMousePos(canvas, event)
lastX = mousePos.x
lastY = mousePos.y
})
canvas.addEventListener('mousemove', (event) => {
if (isDrawing) {
const mousePos = getMousePos(canvas, event)
context.beginPath()
context.moveTo(lastX, lastY)
context.lineTo(mousePos.x, mousePos.y)
context.stroke()
lastX = mousePos.x
lastY = mousePos.y
}
})
canvas.addEventListener('mouseup', () => {
isDrawing = false
})
canvas.addEventListener('mouseleave', () => {
isDrawing = false
})
// Convert canvas to image data
function getImageData() {
const imageData = context.getImageData(0, 0, canvas.width, canvas.height)
const data = imageData.data
for (let i = 0; i < data.length; i += 4) {
const brightness = (data[i] + data[i + 1] + data[i + 2]) / 3
data[i] = brightness
data[i + 1] = brightness
data[i + 2] = brightness
}
return imageData
}
// Preprocess image data
function preprocess(imageData) {
const tensor = tf.browser.fromPixels(imageData, 1)
const resized = tf.image.resizeBilinear(tensor, [28, 28])
const casted = resized.cast('float32')
const scaled = casted.div(255.0)
const expanded = scaled.expandDims()
return expanded
}
// Load model and make prediction
predictButton.addEventListener('click', async () => {
const imageData = getImageData()
const preprocessed = preprocess(imageData)
const model = await tf.loadLayersModel('model/model.json')
const prediction = model.predict(preprocessed)
const result = prediction.dataSync()
const maxIndex = result.indexOf(Math.max(...result))
predictionText.innerText = maxIndex.toString()
predictionText.parentNode.parentNode.style.display = 'block'
})
```
这个示例代码使用了TensorFlow.js来加载和使用一个预训练的手写数字识别模型。当用户在画布上手写一个数字后,点击"Predict"按钮就会调用预训练模型进行预测,并把结果显示在页面上。
阅读全文