Spaces:
Paused
Paused
Create script.js
Browse files- static/script.js +37 -0
static/script.js
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Access webcam
|
2 |
+
navigator.mediaDevices.getUserMedia({ video: true })
|
3 |
+
.then(stream => {
|
4 |
+
const video = document.getElementById('webcam');
|
5 |
+
video.srcObject = stream;
|
6 |
+
})
|
7 |
+
.catch(err => {
|
8 |
+
console.error("Webcam access error: ", err);
|
9 |
+
alert("Could not access webcam. Please upload an image instead.");
|
10 |
+
});
|
11 |
+
|
12 |
+
// Capture frame and send to server
|
13 |
+
function capture() {
|
14 |
+
const video = document.getElementById('webcam');
|
15 |
+
const canvas = document.getElementById('canvas');
|
16 |
+
canvas.width = video.videoWidth;
|
17 |
+
canvas.height = video.videoHeight;
|
18 |
+
canvas.getContext('2d').drawImage(video, 0, 0);
|
19 |
+
|
20 |
+
canvas.toBlob(blob => {
|
21 |
+
const formData = new FormData();
|
22 |
+
formData.append('file', blob, 'webcam.jpg');
|
23 |
+
|
24 |
+
fetch('/upload', {
|
25 |
+
method: 'POST',
|
26 |
+
body: formData
|
27 |
+
})
|
28 |
+
.then(response => response.text())
|
29 |
+
.then(html => {
|
30 |
+
document.body.innerHTML = html; // Load results page
|
31 |
+
})
|
32 |
+
.catch(err => {
|
33 |
+
console.error("Upload error: ", err);
|
34 |
+
alert("Error uploading webcam frame.");
|
35 |
+
});
|
36 |
+
}, 'image/jpeg');
|
37 |
+
}
|