document.addEventListener("DOMContentLoaded", () => {
document
.getElementById("detectBtn")
.addEventListener("click", onDetectEmotionClick);
document.getElementById("settingsBtn").addEventListener("click", onDetectEmotionClick);
initWebcam();
const technicalSection = document.getElementById("technical-section");
technicalSection.style.display = "none";
});
// Send the image to the Flask backend
async function sendImageToBackend(imageData) {
try {
// Get base64 string and remove the prefix
const base64String = imageData.src.split(',')[1];
const response = await fetch("/upload", { // Changed to relative path
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
image: base64String
})
});
if (!response.ok) {
throw new Error(`Server responded with status: ${response.status}`);
}
const result = await response.json();
console.log("Emotion detection result:", result);
return result;
} catch (error) {
console.error("Error during API call:", error);
throw error;
}
}
// Handle the Detect Emotion button click
async function onDetectEmotionClick() {
const initialContent = document.getElementById("initial-content");
const calculatingContent = document.getElementById("calculating-content");
const detectBtn = document.getElementById("detectBtn");
const technicalSection = document.getElementById("technical-section");
const loadingSpinner = document.querySelector(".loading-spinner");
try {
// Hide initial content and show loading
initialContent.style.display = "none";
calculatingContent.style.display = "flex";
detectBtn.style.opacity = "0";
loadingSpinner.style.display = "block";
// Hide technical section while processing
technicalSection.style.display = "none";
// Capture and process image
const capturedImage = captureImage();
const result = await sendImageToBackend(capturedImage);
if (result.error) {
throw new Error(result.error);
}
// Hide loading spinner after getting results
loadingSpinner.style.display = "none";
// Show and update technical section
technicalSection.style.display = "block";
void technicalSection.offsetWidth;
technicalSection.classList.add('visible');
// Update technical section with processing steps
updateTechnicalSection(result);
// Update emotion display with personalized message
document.querySelector(".result-emoji").textContent = result.emoji;
document.querySelector(".result-comment").textContent = getEmotionMessage(result.emotion);
// Update button
detectBtn.innerHTML = 'Show Details';
detectBtn.style.opacity = "1";
// Remove old click handler and add new one
detectBtn.removeEventListener('click', onDetectEmotionClick);
detectBtn.addEventListener('click', handleShowDetails);
} catch (error) {
console.error("Error:", error);
loadingSpinner.style.display = "none";
document.querySelector(".result-emoji").textContent = "❌";
document.querySelector(".result-comment").textContent = "Failed to detect emotion. Please try again.";
detectBtn.style.opacity = "1";
technicalSection.style.display = "none";
}
}
// Initialize when document is loaded
document.addEventListener("DOMContentLoaded", () => {
const detectBtn = document.getElementById("detectBtn");
const settingsBtn = document.getElementById("settingsBtn");
const technicalSection = document.getElementById("technical-section");
// Hide technical section initially
technicalSection.style.display = "none";
// Add click handlers
detectBtn.addEventListener("click", onDetectEmotionClick);
settingsBtn.addEventListener("click", onDetectEmotionClick);
initWebcam();
});
function updateTechnicalSection(result) {
console.log("Full result:", result);
console.log("Processing steps:", result.processing_steps);
console.log("Detailed steps:", result.processing_steps.detailed_steps);
// Update preprocessed image with processing info
document.getElementById("preprocessed-image").innerHTML = `
Image Size: ${result.processing_steps.original_size[0]}x${result.processing_steps.original_size[1]}
Color Mode: ${result.processing_steps.color_mode}