mitsface / index.html
Habeeb13's picture
Upload 5 files
f98d0eb verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Facial Expression Recognition</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
margin: 0;
padding: 0;
}
.container {
width: 70%;
margin: auto;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: 50px;
}
button, input[type="file"] {
margin: 10px;
padding: 10px 15px;
border: none;
border-radius: 5px;
background: #007BFF;
color: white;
cursor: pointer;
}
button:hover {
background: #0056b3;
}
input[type="file"] {
cursor: pointer;
}
.output {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
<h1>Facial Expression Recognition</h1>
<div class="container">
<!-- Real-Time Detection Section -->
<h2>Real-Time Detection</h2>
<video id="video" width="480" height="360" autoplay></video>
<button onclick="startRealTimePrediction()">Start Real-Time Detection</button>
<div id="realTimeOutput" class="output"></div>
<!-- Image Upload Section -->
<h2>Image Upload</h2>
<input type="file" id="imageInput" accept="image/*">
<button onclick="uploadImage()">Upload and Predict</button>
<div id="uploadOutput" class="output"></div>
</div>
<script>
// Real-Time Prediction (using Base64)
function startRealTimePrediction() {
const video = document.getElementById('video');
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
video.srcObject = stream;
})
.catch(err => {
console.error("Error accessing camera: ", err);
});
setInterval(() => {
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
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 => {
document.getElementById('realTimeOutput').textContent = `Prediction: ${data.prediction}`;
})
.catch(error => console.error('Error:', error));
}, 1000); // Perform predictions every second
}
// Upload Image Prediction
function uploadImage() {
const input = document.getElementById('imageInput');
const file = input.files[0];
if (!file) {
alert('Please upload an image.');
return;
}
const formData = new FormData();
formData.append('image', file);
fetch('/predict', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
document.getElementById('uploadOutput').textContent = `Prediction: ${data.prediction}`;
})
.catch(error => console.error('Error:', error));
}
</script>
</body>
</html>