Spaces:
Running
Running
Download app.js from JaveriaZia/Prototype: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/spaces/JaveriaZia/Prototype/resolve/main/app.js
- Command line
-
hf download hf://spaces/JaveriaZia/Prototype/app.js
-
curl -L -o app.js https://huggingface.co/spaces/JaveriaZia/Prototype/resolve/main/app.js
5.11 kB
| const camera = document.getElementById("camera"); | |
| const cameraMessage = document.getElementById("cameraMessage"); | |
| const startCameraBtn = document.getElementById("startCamera"); | |
| const stopCameraBtn = document.getElementById("stopCamera"); | |
| const signClass = document.getElementById("signClass"); | |
| const recognizeBtn = document.getElementById("recognizeBtn"); | |
| const prediction = document.getElementById("prediction"); | |
| const message = document.getElementById("message"); | |
| const addBtn = document.getElementById("addBtn"); | |
| const removeBtn = document.getElementById("removeBtn"); | |
| const clearBtn = document.getElementById("clearBtn"); | |
| const speakBtn = document.getElementById("speakBtn"); | |
| const stopSpeakBtn = document.getElementById("stopSpeakBtn"); | |
| const speechStatus = document.getElementById("speechStatus"); | |
| let cameraStream = null; | |
| let selectedPrediction = ""; | |
| let speechEngine = window.speechSynthesis; | |
| // Placeholder labels: replace these with your actual dataset labels. | |
| const signClasses = [ | |
| "Class 1", "Class 2", "Class 3", "Class 4", | |
| "Class 5", "Class 6", "Class 7", "Class 8", | |
| "Class 9", "Class 10", "Class 11" | |
| ]; | |
| // Display the 11 supported demo classes. | |
| const classList = document.getElementById("classList"); | |
| signClasses.forEach(function (className) { | |
| const item = document.createElement("div"); | |
| item.className = "class-item"; | |
| item.textContent = className; | |
| classList.appendChild(item); | |
| }); | |
| // Start the real webcam preview. | |
| startCameraBtn.addEventListener("click", async function () { | |
| if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { | |
| cameraMessage.textContent = | |
| "Camera access is not supported by this browser."; | |
| return; | |
| } | |
| try { | |
| cameraStream = await navigator.mediaDevices.getUserMedia({ | |
| video: true, | |
| audio: false | |
| }); | |
| camera.srcObject = cameraStream; | |
| camera.style.display = "block"; | |
| cameraMessage.style.display = "none"; | |
| } catch (error) { | |
| cameraMessage.style.display = "block"; | |
| cameraMessage.textContent = | |
| "Camera permission was denied or the camera is unavailable."; | |
| } | |
| }); | |
| // Stop the webcam and release the camera. | |
| stopCameraBtn.addEventListener("click", function () { | |
| if (cameraStream) { | |
| cameraStream.getTracks().forEach(function (track) { | |
| track.stop(); | |
| }); | |
| cameraStream = null; | |
| camera.srcObject = null; | |
| } | |
| camera.style.display = "none"; | |
| cameraMessage.style.display = "block"; | |
| cameraMessage.textContent = "Camera stopped."; | |
| }); | |
| // Simulate a prediction. This is not AI recognition. | |
| recognizeBtn.addEventListener("click", function () { | |
| const chosenClass = signClass.value; | |
| if (!chosenClass) { | |
| prediction.textContent = "Please select a demo class first."; | |
| selectedPrediction = ""; | |
| return; | |
| } | |
| selectedPrediction = chosenClass; | |
| // Example confidence value for demonstration only. | |
| const demoConfidence = (90 + Math.random() * 8).toFixed(1); | |
| prediction.innerHTML = ` | |
| <strong>Demo prediction:</strong> ${chosenClass}<br> | |
| <strong>Example confidence:</strong> ${demoConfidence}%<br> | |
| <span class="text-warning"> | |
| Simulated result — no AI model is connected. | |
| </span> | |
| `; | |
| }); | |
| // Add the selected demo class to the message. | |
| addBtn.addEventListener("click", function () { | |
| if (!selectedPrediction) { | |
| alert("Please simulate a demo recognition first."); | |
| return; | |
| } | |
| if (message.value.trim() !== "") { | |
| message.value += " "; | |
| } | |
| message.value += selectedPrediction; | |
| }); | |
| // Remove the last added word/class. | |
| removeBtn.addEventListener("click", function () { | |
| const words = message.value.trim().split(/\s+/); | |
| if (message.value.trim() === "") { | |
| return; | |
| } | |
| words.pop(); | |
| message.value = words.join(" "); | |
| }); | |
| // Clear the complete message. | |
| clearBtn.addEventListener("click", function () { | |
| message.value = ""; | |
| }); | |
| // Speak the composed message using browser speech synthesis. | |
| speakBtn.addEventListener("click", function () { | |
| const textToSpeak = message.value.trim(); | |
| if (!textToSpeak) { | |
| speechStatus.textContent = "Please enter a message first."; | |
| return; | |
| } | |
| if (!("speechSynthesis" in window)) { | |
| speechStatus.textContent = | |
| "Text-to-speech is not supported by this browser."; | |
| return; | |
| } | |
| speechEngine.cancel(); | |
| const speech = new SpeechSynthesisUtterance(textToSpeak); | |
| speech.lang = "en-US"; | |
| speech.rate = 0.9; | |
| speech.onstart = function () { | |
| speechStatus.textContent = "Speaking..."; | |
| }; | |
| speech.onend = function () { | |
| speechStatus.textContent = "Speech finished."; | |
| }; | |
| speech.onerror = function () { | |
| speechStatus.textContent = "Speech could not be played."; | |
| }; | |
| speechEngine.speak(speech); | |
| }); | |
| // Stop speech playback. | |
| stopSpeakBtn.addEventListener("click", function () { | |
| if ("speechSynthesis" in window) { | |
| speechEngine.cancel(); | |
| speechStatus.textContent = "Speech stopped."; | |
| } | |
| }); | |
| // Stop camera when the page is closed or refreshed. | |
| window.addEventListener("beforeunload", function () { | |
| if (cameraStream) { | |
| cameraStream.getTracks().forEach(function (track) { | |
| track.stop(); | |
| }); | |
| } | |
| }); | |