File size: 9,730 Bytes
26c453d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//meta(not so imp) functions by vkc!

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"
});


// back btn
backBtn.addEventListener("click", function() {
    window.location = '/'
})


let webcamON = false

//open github repo
document.querySelector('#webcam-banner').addEventListener('click', function() {
    window.open('https://github.com/vivekkushalch/Indian-Sign-Language-Recognition-System/', '_blank');
})



// test function
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"
        });
    }
}
// addTestLines(10);





// adds new line in transcript box
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"
    })
}


// start sign prediction
// cameraBtn.addEventListener("click", function() {
//     if (cameraBtn.style.background === '#62E6BF') {
//         cameraBtn.style.background = "#EAF1C5"; //deactivate btn
//         alert('stopping... Press OK')
//         location.reload()

//     } else {
//         cameraBtn.style.background = "#62E6BF";
//         document.getElementById("webcam-banner").style.display = "none"; // remove banner
//         document.getElementById("canvas").style.display = "block"; // display web cam 
//         alert('starting.... Press OK')
//         init();
//     }
// });

// start sign prediction
cameraBtn.addEventListener("click", function() {
    if (webcamON) {
        location.reload()
    } else {
        webcamON = true
        cameraBtn.style.background = "#62E6BF";
        document.getElementById("webcam-banner").style.display = "none"; // remove banner
        document.getElementById("canvas").style.display = "block"; // display web cam 
        alert('starting.... Press OK')
        init();
    }
})

// text to speach btn colour change
gttsBtn.addEventListener("click", function() {
    if (gttsBtn.style.backgroundColor === 'rgb(212, 236, 126)') {
        gttsBtn.style.background = "#EAF1C5"; //dactivate btn
        // alert('stopping... Press OK')
        // location.reload()

    } else {
        gttsBtn.style.background = "#62E6BF";
        // alert('starting.... Press OK')
        // init();
    }
});



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);
    };
};


//upload img
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)
        // let fileInput = document.querySelector('#file-input')
        // fileInput.click();
        // fileInput.addEventListener('change', async(event) => {
        //     const file = event.target.files[0];
        //     if (!file) {
        //         console.error('No file selected.');
        //         return;
        //     }

    //     const reader = new FileReader();
    //     reader.readAsDataURL(file);
    //     reader.onload = async() => {
    //         let img = document.createElement('img')
    //         img.className = 'webcam-view'
    //         document.querySelector(".webcam-view").appendChild(img).src = reader.result;

    //         //predict image

    //         model = window.tmImage.load(modelURL, metadataURL);
    //         let flip = true;
    //         // maxPredictions = model.getTotalClasses();
    //         const prediction = await model.predict(img);
    //         console.log(prediction)
    //         addNewTranslateLine(prediction)

    //     };
    //     reader.onerror = (error) => {
    //         console.error(error);
    //     };
    // });



})





//text to speech
async function tts(text) {
    if ('speechSynthesis' in window) {
        // Speech Synthesis is supported 🎉
        console.log('');
    } else {
        alert('Text to speech not available 😞');
        location.reload();
    }

    let msg = new SpeechSynthesisUtterance(text);
    window.speechSynthesis.speak(msg);

}

// delay function
const delay = ms => new Promise(res => setTimeout(res, ms));

/////////////////////////////////////
let index = 0
const spamFilter = ['I', `❤️`, 'I', 'N', 'D', 'I', 'A']
let beforeTextDone = 0;

/////////////////////////////////////
// tensorflow.js magic /////////////
////////////////////////////////////
let model, webcam, ctox, labelContainer, maxPredictions;
async function init() {
    //hello, welcome, thankyou, iloveu {>= 0.95}
    // const modelURL = "https://storage.googleapis.com/tm-model/C2gYk6JPd/model.json";
    // const metadataURL = "https://storage.googleapis.com/tm-model/C2gYk6JPd/metadata.json";

    //1,2,3,4,5{==1.0 meh}
    // const modelURL = "https://storage.googleapis.com/tm-model/qWNVsgTyJ/model.json";
    // const metadataURL = "https://storage.googleapis.com/tm-model/qWNVsgTyJ/metadata.json";



    // load the model and metadata
    // Refer to tmImage.loadFromFiles() in the API to support files from a file picker
    // or files from your local hard drive
    // Note: the pose library adds "tmImage" object to your window (window.tmImage)
    model = await tmImage.load(modelURL, metadataURL);
    maxPredictions = model.getTotalClasses();

    // Convenience function to setup a webcam
    const flip = true; // whether to flip the webcam
    webcam = new tmImage.Webcam(200, 200, flip); // width, height, flip 180
    await webcam.setup(); // request access to the webcam
    await webcam.play();
    window.requestAnimationFrame(loop);

    // append elements to the DOM
    document.querySelector(".webcam-view").appendChild(webcam.canvas).className = 'canvas';
    labelContainer = document.getElementById("last-line");
    for (let i = 0; i < maxPredictions; i++) { // and class labels
        labelContainer.appendChild(document.createElement("div"));
    }
}

async function loop() {
    webcam.update(); // update the webcam frame
    await predict();
    window.requestAnimationFrame(loop);
}

// run the webcam image through the image model
async function predict() {
    // predict can take in an image, video or canvas html element
    const prediction = await model.predict(webcam.canvas);
    for (let i = 0; i < maxPredictions; i++) {
        // if (prediction[i].probability.toFixed(2) == 1.00) {
        //     console.log(prediction[i].probability.toFixed(2), prediction[i].className)
        // }
        // const classPrediction =  prediction[i].className + ": " + prediction[i].probability.toFixed(2);
        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)') { //btn active
                        await tts(prediction[i].className)
                            // delay(0)
                    } else {
                        console.log('')
                    }
                }
            }

            // labelContainer.childNodes[i].innerHTML = prediction[i].className;


        }

    }

    // for (let i = 0; i < maxPredictions; i++) {
    //     const classPrediction =
    //         prediction[i].className + ": " + prediction[i].probability.toFixed(2);
    //     labelContainer.childNodes[i].innerHTML = classPrediction;
    // }

}