|
|
|
|
|
function startWebcam() {
|
|
|
const video = document.getElementById('video');
|
|
|
if (video) {
|
|
|
navigator.mediaDevices.getUserMedia({ video: true })
|
|
|
.then(stream => {
|
|
|
video.srcObject = stream;
|
|
|
})
|
|
|
.catch(error => {
|
|
|
console.error("Error accessing the webcam:", error);
|
|
|
});
|
|
|
} else {
|
|
|
console.error("Video element not found.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', (event) => {
|
|
|
startWebcam();
|
|
|
});
|
|
|
|
|
|
|
|
|
function startDetection() {
|
|
|
const video = document.getElementById('video');
|
|
|
const canvas = document.getElementById('overlay');
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
const realTimeOutputDiv = document.getElementById('realTimeOutput');
|
|
|
|
|
|
setInterval(() => {
|
|
|
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
|
const imageData = canvas.toDataURL('image/png');
|
|
|
|
|
|
fetch('/predict', {
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
},
|
|
|
body: JSON.stringify({ image: imageData }),
|
|
|
})
|
|
|
.then(response => response.json())
|
|
|
.then(data => {
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
if (data.prediction && data.bbox) {
|
|
|
const { x, y, w, h } = data.bbox;
|
|
|
|
|
|
|
|
|
ctx.strokeStyle = 'red';
|
|
|
ctx.lineWidth = 2;
|
|
|
ctx.strokeRect(x, y, w, h);
|
|
|
|
|
|
|
|
|
ctx.font = '16px Arial';
|
|
|
ctx.fillStyle = 'red';
|
|
|
ctx.fillText(data.prediction, x, y - 10);
|
|
|
|
|
|
realTimeOutputDiv.innerHTML = `Real-Time Prediction: ${data.prediction}`;
|
|
|
} else {
|
|
|
realTimeOutputDiv.innerHTML = 'No face detected';
|
|
|
}
|
|
|
})
|
|
|
.catch(error => {
|
|
|
console.error('Error:', error);
|
|
|
});
|
|
|
}, 1000);
|
|
|
}
|
|
|
|
|
|
|
|
|
function uploadImage() {
|
|
|
const fileInput = document.getElementById('imageUpload');
|
|
|
const uploadOutputDiv = document.getElementById('uploadOutput');
|
|
|
|
|
|
if (fileInput && fileInput.files.length > 0) {
|
|
|
const file = fileInput.files[0];
|
|
|
const formData = new FormData();
|
|
|
formData.append('image', file);
|
|
|
|
|
|
fetch('/predict', {
|
|
|
method: 'POST',
|
|
|
body: formData,
|
|
|
})
|
|
|
.then(response => response.json())
|
|
|
.then(data => {
|
|
|
if (data.prediction) {
|
|
|
uploadOutputDiv.innerHTML = `Uploaded Image Prediction: ${data.prediction}`;
|
|
|
} else {
|
|
|
uploadOutputDiv.innerHTML = 'No face detected';
|
|
|
}
|
|
|
})
|
|
|
.catch(error => {
|
|
|
console.error('Error:', error);
|
|
|
});
|
|
|
} else {
|
|
|
alert('Please select an image before uploading.');
|
|
|
}
|
|
|
}
|
|
|
|