Spaces:
Sleeping
Sleeping
Create static/js/script.js
Browse files- static/js/script.js +73 -0
static/js/script.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
$(document).ready(function() {
|
| 2 |
+
const video = document.getElementById('videoElement');
|
| 3 |
+
const canvas = document.getElementById('canvas');
|
| 4 |
+
const capturedImage = document.getElementById('capturedImage');
|
| 5 |
+
const hfResult = document.getElementById('hfResult');
|
| 6 |
+
const cameraSelect = document.getElementById('cameraSelect');
|
| 7 |
+
let stream = null;
|
| 8 |
+
|
| 9 |
+
// Open camera
|
| 10 |
+
$('#openCamera').click(function() {
|
| 11 |
+
$('#cameraContainer').show();
|
| 12 |
+
startCamera(cameraSelect.value);
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
// Switch camera
|
| 16 |
+
cameraSelect.addEventListener('change', function() {
|
| 17 |
+
if (stream) {
|
| 18 |
+
stopCamera();
|
| 19 |
+
startCamera(this.value);
|
| 20 |
+
}
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
// Capture image
|
| 24 |
+
$('#captureButton').click(function() {
|
| 25 |
+
canvas.width = video.videoWidth;
|
| 26 |
+
canvas.height = video.videoHeight;
|
| 27 |
+
canvas.getContext('2d').drawImage(video, 0, 0);
|
| 28 |
+
const dataUrl = canvas.toDataURL('image/jpeg');
|
| 29 |
+
|
| 30 |
+
// Send to server
|
| 31 |
+
$.ajax({
|
| 32 |
+
type: 'POST',
|
| 33 |
+
url: '/capture',
|
| 34 |
+
data: { image: dataUrl },
|
| 35 |
+
success: function(response) {
|
| 36 |
+
if (response.status === 'success') {
|
| 37 |
+
capturedImage.src = response.image_url;
|
| 38 |
+
capturedImage.style.display = 'block';
|
| 39 |
+
hfResult.textContent = JSON.stringify(response.hf_result, null, 2);
|
| 40 |
+
stopCamera();
|
| 41 |
+
$('#cameraContainer').hide();
|
| 42 |
+
} else {
|
| 43 |
+
alert('Error: ' + response.message);
|
| 44 |
+
}
|
| 45 |
+
},
|
| 46 |
+
error: function() {
|
| 47 |
+
alert('Failed to capture image.');
|
| 48 |
+
}
|
| 49 |
+
});
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
function startCamera(facingMode) {
|
| 53 |
+
const constraints = {
|
| 54 |
+
video: { facingMode: facingMode }
|
| 55 |
+
};
|
| 56 |
+
navigator.mediaDevices.getUserMedia(constraints)
|
| 57 |
+
.then(function(mediaStream) {
|
| 58 |
+
stream = mediaStream;
|
| 59 |
+
video.srcObject = stream;
|
| 60 |
+
})
|
| 61 |
+
.catch(function(err) {
|
| 62 |
+
alert('Error accessing camera: ' + err.message);
|
| 63 |
+
});
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
function stopCamera() {
|
| 67 |
+
if (stream) {
|
| 68 |
+
stream.getTracks().forEach(track => track.stop());
|
| 69 |
+
stream = null;
|
| 70 |
+
video.srcObject = null;
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
});
|