|
|
|
|
|
const modelURL = "https://teachablemachine.withgoogle.com/models/riOJKjJZ1/model.json"; |
|
const metadataURL = "https://teachablemachine.withgoogle.com/models/riOJKjJZ1/metadata.json"; |
|
|
|
|
|
|
|
let gttsBtn = document.getElementById('gtts-btn'); |
|
let cameraBtn = document.getElementById('camera-btn'); |
|
let uploadBtn = document.querySelector('#upload-btn'); |
|
let textArea = document.querySelector('#textarea'); |
|
let backBtn = document.querySelector("#back-btn") |
|
let line = document.createElement('p'); |
|
|
|
|
|
line.className = "line" |
|
line.id = "last-line" |
|
line.innerHTML = `Ready to roll !` |
|
textArea.appendChild(line) |
|
line.scrollIntoView({ |
|
behavior: "smooth", |
|
block: "end", |
|
inline: "nearest" |
|
}); |
|
|
|
|
|
|
|
backBtn.addEventListener("click", function() { |
|
window.location = '/' |
|
}) |
|
|
|
|
|
let webcamON = false |
|
|
|
|
|
document.querySelector('#webcam-banner').addEventListener('click', function() { |
|
window.open('https://github.com/vivekkushalch/Indian-Sign-Language-Recognition-System/', '_blank'); |
|
}) |
|
|
|
|
|
|
|
|
|
async function addTestLines(totalLines) { |
|
for (let i = 1; i < totalLines + 1; i++) { |
|
let line = document.createElement('p'); |
|
line.className = "line" |
|
line.innerHTML = `Hello` |
|
textArea.appendChild(line) |
|
line.scrollIntoView({ |
|
behavior: "smooth", |
|
block: "end", |
|
inline: "nearest" |
|
}); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function addNewTranslateLine(text) { |
|
let lastLine = textArea.lastElementChild; |
|
try { |
|
document.querySelector('#last-line').id = ''; |
|
} catch (err) { |
|
console.log(err) |
|
} |
|
|
|
let newLine = document.createElement('p'); |
|
newLine.innerHTML = text; |
|
newLine.className = "line"; |
|
newLine.id = 'last-line'; |
|
textArea.appendChild(newLine); |
|
newLine.scrollIntoView({ |
|
behavior: "smooth", |
|
block: "end", |
|
inline: "nearest" |
|
}) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cameraBtn.addEventListener("click", function() { |
|
if (webcamON) { |
|
location.reload() |
|
} else { |
|
webcamON = true |
|
cameraBtn.style.background = "#62E6BF"; |
|
document.getElementById("webcam-banner").style.display = "none"; |
|
document.getElementById("canvas").style.display = "block"; |
|
alert('starting.... Press OK') |
|
init(); |
|
} |
|
}) |
|
|
|
|
|
gttsBtn.addEventListener("click", function() { |
|
if (gttsBtn.style.backgroundColor === 'rgb(212, 236, 126)') { |
|
gttsBtn.style.background = "#EAF1C5"; |
|
|
|
|
|
|
|
} else { |
|
gttsBtn.style.background = "#62E6BF"; |
|
|
|
|
|
} |
|
}); |
|
|
|
|
|
|
|
let file; |
|
|
|
async function predictImage(file) { |
|
const modelURL = 'https://teachablemachine.withgoogle.com/models/riOJKjJZ1/'; |
|
const model = await tmImage.load(modelURL + 'model.json', modelURL + 'metadata.json'); |
|
|
|
|
|
const imagePreview = document.getElementsByClassName('webcam-view'); |
|
|
|
|
|
|
|
const reader = new FileReader(); |
|
reader.readAsDataURL(file); |
|
reader.onload = async() => { |
|
imagePreview.src = reader.result; |
|
const imageElement = document.createElement('img'); |
|
imageElement.src = reader.result; |
|
const prediction = await model.predict(imageElement); |
|
addNewTranslateLine(predictions) |
|
console.log(prediction); |
|
}; |
|
}; |
|
|
|
|
|
|
|
uploadBtn.addEventListener('click', function() { |
|
uploadBtn.style.background = "#62E6BF"; |
|
uploadBtn.style.background = "" |
|
const fileInput = document.querySelector('#file-input'); |
|
fileInput.click() |
|
fileInput.addEventListener('change', async(event) => { |
|
file = event.target.files[0]; |
|
if (!file) { |
|
console.error('No file selected.'); |
|
return; |
|
} |
|
}) |
|
predictImage(file) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
async function tts(text) { |
|
if ('speechSynthesis' in window) { |
|
|
|
console.log(''); |
|
} else { |
|
alert('Text to speech not available 😞'); |
|
location.reload(); |
|
} |
|
|
|
let msg = new SpeechSynthesisUtterance(text); |
|
window.speechSynthesis.speak(msg); |
|
|
|
} |
|
|
|
|
|
const delay = ms => new Promise(res => setTimeout(res, ms)); |
|
|
|
|
|
let index = 0 |
|
const spamFilter = ['I', `❤️`, 'I', 'N', 'D', 'I', 'A'] |
|
let beforeTextDone = 0; |
|
|
|
|
|
|
|
|
|
let model, webcam, ctox, labelContainer, maxPredictions; |
|
async function init() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model = await tmImage.load(modelURL, metadataURL); |
|
maxPredictions = model.getTotalClasses(); |
|
|
|
|
|
const flip = true; |
|
webcam = new tmImage.Webcam(200, 200, flip); |
|
await webcam.setup(); |
|
await webcam.play(); |
|
window.requestAnimationFrame(loop); |
|
|
|
|
|
document.querySelector(".webcam-view").appendChild(webcam.canvas).className = 'canvas'; |
|
labelContainer = document.getElementById("last-line"); |
|
for (let i = 0; i < maxPredictions; i++) { |
|
labelContainer.appendChild(document.createElement("div")); |
|
} |
|
} |
|
|
|
async function loop() { |
|
webcam.update(); |
|
await predict(); |
|
window.requestAnimationFrame(loop); |
|
} |
|
|
|
|
|
async function predict() { |
|
|
|
const prediction = await model.predict(webcam.canvas); |
|
for (let i = 0; i < maxPredictions; i++) { |
|
|
|
|
|
|
|
|
|
console.log(prediction[i].probability.toFixed(2), prediction[i].className) |
|
if (prediction[i].probability.toFixed(2) == 1.0) { |
|
if (prediction[i].className == spamFilter[index]) { |
|
index += 1; |
|
if (index == spamFilter.length) { |
|
index = 0; |
|
} |
|
if (document.querySelector('#last-line').innerHTML != prediction[i].className) { |
|
await addNewTranslateLine(prediction[i].className); |
|
if (gttsBtn.style.backgroundColor === 'rgb(212, 236, 126)') { |
|
await tts(prediction[i].className) |
|
|
|
} else { |
|
console.log('') |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|