Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +20 -43
static/script.js
CHANGED
|
@@ -1,50 +1,27 @@
|
|
| 1 |
-
const itsekiri_labels = [
|
| 2 |
-
'Méene', 'Méji', 'Métà', 'Mérin', 'Márùn',
|
| 3 |
-
'Méfa', 'Méje', 'Méjọ', 'Méṣan', 'Méwà'
|
| 4 |
-
];
|
| 5 |
-
|
| 6 |
const video = document.getElementById('video');
|
| 7 |
const canvas = document.getElementById('canvas');
|
| 8 |
const ctx = canvas.getContext('2d');
|
| 9 |
const predictionText = document.getElementById('prediction');
|
| 10 |
-
const fingersDisplay = document.getElementById('fingers');
|
| 11 |
-
const enableWebcamBtn = document.getElementById('enable-webcam');
|
| 12 |
-
let stream;
|
| 13 |
-
|
| 14 |
-
enableWebcamBtn.addEventListener('click', () => {
|
| 15 |
-
if (!stream) {
|
| 16 |
-
navigator.mediaDevices.getUserMedia({ video: true })
|
| 17 |
-
.then(s => {
|
| 18 |
-
stream = s;
|
| 19 |
-
video.srcObject = stream;
|
| 20 |
-
enableWebcamBtn.style.display = 'none';
|
| 21 |
-
startPrediction();
|
| 22 |
-
})
|
| 23 |
-
.catch(console.error);
|
| 24 |
-
}
|
| 25 |
-
});
|
| 26 |
-
|
| 27 |
-
function startPrediction() {
|
| 28 |
-
setInterval(() => {
|
| 29 |
-
// Draw video frame to canvas
|
| 30 |
-
ctx.drawImage(video, 0, 0, 640, 480);
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const video = document.getElementById('video');
|
| 2 |
const canvas = document.getElementById('canvas');
|
| 3 |
const ctx = canvas.getContext('2d');
|
| 4 |
const predictionText = document.getElementById('prediction');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
navigator.mediaDevices.getUserMedia({ video: true })
|
| 7 |
+
.then(stream => {
|
| 8 |
+
video.srcObject = stream;
|
| 9 |
+
})
|
| 10 |
+
.catch(console.error);
|
| 11 |
|
| 12 |
+
setInterval(() => {
|
| 13 |
+
// Crop and resize the area with digit from webcam video (adjust coords if needed)
|
| 14 |
+
ctx.drawImage(video, 100, 100, 200, 200, 0, 0, 64, 64);
|
| 15 |
+
const dataUrl = canvas.toDataURL('image/jpeg');
|
| 16 |
|
| 17 |
+
fetch('/predict', {
|
| 18 |
+
method: 'POST',
|
| 19 |
+
headers: { 'Content-Type': 'application/json' },
|
| 20 |
+
body: JSON.stringify({ image: dataUrl })
|
| 21 |
+
})
|
| 22 |
+
.then(res => res.json())
|
| 23 |
+
.then(data => {
|
| 24 |
+
predictionText.textContent = data.prediction;
|
| 25 |
+
})
|
| 26 |
+
.catch(console.error);
|
| 27 |
+
}, 500);
|
|
|