-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
121 lines (102 loc) · 3.69 KB
/
script.js
File metadata and controls
121 lines (102 loc) · 3.69 KB
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
const video = document.getElementById('webcam');
const liveView = document.getElementById('liveView');
const demosSection = document.getElementById('demos');
const enableWebcamButton = document.getElementById('webcamButton');
document.addEventListener('keydown', function(event) {
if(event.keyCode == 37) {
window.location.href="index.html";
}
else if(event.keyCode == 39) {
window.location.href="VoiceToText.html";
}
});
function getUserMediaSupported() {
return !!(navigator.mediaDevices &&
navigator.mediaDevices.getUserMedia);
}
if (getUserMediaSupported()) {
enableWebcamButton.addEventListener('click', enableCam);
} else {
console.warn('getUserMedia() is not supported by your browser');
}
function enableCam(event) {
if (!model) {
return;
}
event.target.classList.add('removed');
const constraints = {
video: true
};
navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
video.srcObject = stream;
video.addEventListener('loadeddata', predictWebcam);
});
}
var children = [];
function predictWebcam() {
model.detect(video).then(function (predictions) {
for (let i = 0; i < children.length; i++) {
liveView.removeChild(children[i]);
}
children.splice(0);
for (let n = 0; n < predictions.length; n++) {
if (predictions[n].score > 0.66) {
const p = document.createElement('p');
p.innerText = predictions[n].class + ' - with '
+ Math.round(parseFloat(predictions[n].score) * 100)
+ '% confidence.';
p.style = 'margin-left: ' + predictions[n].bbox[0] + 'px; margin-top: '
+ (predictions[n].bbox[1] - 10) + 'px; width: '
+ (predictions[n].bbox[2] - 10) + 'px; top: 0; left: 0;';
const highlighter = document.createElement('div');
highlighter.style.position = "absolute";
highlighter.setAttribute('class', 'highlighter');
highlighter.style = 'left: ' + predictions[n].bbox[0] + 'px; top: '
+ predictions[n].bbox[1] + 'px; width:'
+ predictions[n].bbox[2] + 'px; height: '
+ predictions[n].bbox[3] + 'px;';
liveView.appendChild(highlighter);
liveView.appendChild(p);
children.push(highlighter);
children.push(p);
}
}
window.requestAnimationFrame(predictWebcam);
});
}
speechSynthesis.cancel();
var msg = new SpeechSynthesisUtterance("Please wait for the model to finish loading");
window.speechSynthesis.speak(msg);
var model = undefined;
cocoSsd.load().then(function (loadedModel) {
speechSynthesis.cancel();
var msg = new SpeechSynthesisUtterance("The model has finished loading. Press space once to enable the camera. Put an item in front of the camera to detect it. Press space to tell you the items displayed.");
window.speechSynthesis.speak(msg);
model = loadedModel;
demosSection.classList.remove('invisible');
});
var clicked = false;
document.onkeypress = function (space) {
space = space || window.event;
var button = document.getElementById("webcamButton");
if(!clicked){
button.click();
clicked = true;
}
speechSynthesis.cancel();
ttsObjects();
};
function ttsObjects(){
model.detect(video).then(function (predictions) {
for (let i = 0; i < children.length; i++) {
liveView.removeChild(children[i]);
}
children.splice(0);
for (let n = 0; n < predictions.length; n++) {
if (predictions[n].score > 0.66) {
var msg = new SpeechSynthesisUtterance(predictions[n].class);
window.speechSynthesis.speak(msg);
}
}
});
}