Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- static/script.js +58 -0
- static/style.css +87 -0
static/script.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const webcam = document.getElementById("webcam");
|
| 2 |
+
const startBtn = document.getElementById("startBtn");
|
| 3 |
+
const stopBtn = document.getElementById("stopBtn");
|
| 4 |
+
const statusText = document.getElementById("status");
|
| 5 |
+
|
| 6 |
+
let stream;
|
| 7 |
+
let interval;
|
| 8 |
+
|
| 9 |
+
async function startWebcam() {
|
| 10 |
+
try {
|
| 11 |
+
stream = await navigator.mediaDevices.getUserMedia({ video: true });
|
| 12 |
+
webcam.srcObject = stream;
|
| 13 |
+
startBtn.disabled = true;
|
| 14 |
+
stopBtn.disabled = false;
|
| 15 |
+
statusText.textContent = "🎥 Webcam started";
|
| 16 |
+
|
| 17 |
+
interval = setInterval(captureAndSend, 2000); // every 2 seconds
|
| 18 |
+
} catch (err) {
|
| 19 |
+
console.error("Error accessing webcam:", err);
|
| 20 |
+
statusText.textContent = "❌ Webcam access denied";
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
function stopWebcam() {
|
| 25 |
+
if (stream) {
|
| 26 |
+
stream.getTracks().forEach(track => track.stop());
|
| 27 |
+
}
|
| 28 |
+
clearInterval(interval);
|
| 29 |
+
webcam.srcObject = null;
|
| 30 |
+
startBtn.disabled = false;
|
| 31 |
+
stopBtn.disabled = true;
|
| 32 |
+
statusText.textContent = "⏹️ Webcam stopped";
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
async function captureAndSend() {
|
| 36 |
+
const canvas = document.createElement("canvas");
|
| 37 |
+
canvas.width = 224;
|
| 38 |
+
canvas.height = 224;
|
| 39 |
+
const ctx = canvas.getContext("2d");
|
| 40 |
+
ctx.drawImage(webcam, 0, 0, 224, 224);
|
| 41 |
+
const dataUrl = canvas.toDataURL("image/jpeg");
|
| 42 |
+
|
| 43 |
+
try {
|
| 44 |
+
const response = await fetch("/predict", {
|
| 45 |
+
method: "POST",
|
| 46 |
+
headers: { "Content-Type": "application/json" },
|
| 47 |
+
body: JSON.stringify({ image: dataUrl }),
|
| 48 |
+
});
|
| 49 |
+
const result = await response.json();
|
| 50 |
+
statusText.textContent = `Prediction: ${result.label || "Error"}`;
|
| 51 |
+
} catch (err) {
|
| 52 |
+
console.error(err);
|
| 53 |
+
statusText.textContent = "⚠️ Prediction error";
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
startBtn.addEventListener("click", startWebcam);
|
| 58 |
+
stopBtn.addEventListener("click", stopWebcam);
|
static/style.css
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Reset some basics */
|
| 2 |
+
* {
|
| 3 |
+
box-sizing: border-box;
|
| 4 |
+
margin: 0;
|
| 5 |
+
padding: 0;
|
| 6 |
+
}
|
| 7 |
+
body {
|
| 8 |
+
background: #111219;
|
| 9 |
+
color: #e6e6e6;
|
| 10 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 11 |
+
display: flex;
|
| 12 |
+
justify-content: center;
|
| 13 |
+
align-items: center;
|
| 14 |
+
min-height: 100vh;
|
| 15 |
+
}
|
| 16 |
+
.container {
|
| 17 |
+
width: 100%;
|
| 18 |
+
max-width: 480px;
|
| 19 |
+
padding: 20px;
|
| 20 |
+
}
|
| 21 |
+
header {
|
| 22 |
+
text-align: center;
|
| 23 |
+
margin-bottom: 20px;
|
| 24 |
+
}
|
| 25 |
+
header h1 {
|
| 26 |
+
font-size: 2.2rem;
|
| 27 |
+
color: #00ff9d;
|
| 28 |
+
}
|
| 29 |
+
header p {
|
| 30 |
+
font-size: 1rem;
|
| 31 |
+
color: #9a9a9a;
|
| 32 |
+
}
|
| 33 |
+
.video-wrapper {
|
| 34 |
+
position: relative;
|
| 35 |
+
width: 100%;
|
| 36 |
+
border: 3px solid #00ff9d;
|
| 37 |
+
border-radius: 12px;
|
| 38 |
+
overflow: hidden;
|
| 39 |
+
}
|
| 40 |
+
video#webcam {
|
| 41 |
+
display: block;
|
| 42 |
+
width: 100%;
|
| 43 |
+
height: auto;
|
| 44 |
+
}
|
| 45 |
+
.video-wrapper .overlay {
|
| 46 |
+
position: absolute;
|
| 47 |
+
bottom: 0;
|
| 48 |
+
width: 100%;
|
| 49 |
+
background: rgba(0, 0, 0, 0.6);
|
| 50 |
+
padding: 12px;
|
| 51 |
+
text-align: center;
|
| 52 |
+
}
|
| 53 |
+
.overlay span {
|
| 54 |
+
font-size: 1.2rem;
|
| 55 |
+
color: #e6e6e6;
|
| 56 |
+
}
|
| 57 |
+
.controls {
|
| 58 |
+
display: flex;
|
| 59 |
+
justify-content: space-between;
|
| 60 |
+
|
| 61 |
+
gap: 10px;
|
| 62 |
+
margin: 15px 0;
|
| 63 |
+
}
|
| 64 |
+
.controls button {
|
| 65 |
+
flex: 1;
|
| 66 |
+
padding: 12px;
|
| 67 |
+
font-size: 1rem;
|
| 68 |
+
border: none;
|
| 69 |
+
cursor: pointer;
|
| 70 |
+
color: #111219;
|
| 71 |
+
background: #00ff9d;
|
| 72 |
+
border-radius: 8px;
|
| 73 |
+
transition: background 0.3s ease;
|
| 74 |
+
}
|
| 75 |
+
.controls button:hover {
|
| 76 |
+
background: #00e88c;
|
| 77 |
+
}
|
| 78 |
+
.controls button:disabled {
|
| 79 |
+
background: #444;
|
| 80 |
+
cursor: not-allowed;
|
| 81 |
+
}
|
| 82 |
+
footer {
|
| 83 |
+
text-align: center;
|
| 84 |
+
font-size: 0.8rem;
|
| 85 |
+
color: #555;
|
| 86 |
+
margin-top: 20px;
|
| 87 |
+
}
|