-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathESP32_SpeechRecognition.html
277 lines (260 loc) · 10.8 KB
/
ESP32_SpeechRecognition.html
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
<!--
Author : ChungYi Fu (Kaohsiung, Taiwan) 2019-08-06 23:00
https://www.facebook.com/francefu
Page
https://fustyles.github.io/webduino/ESP32_SpeechRecognition.html?STAIP&en-US
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<body onload="if (language!='') document.getElementById('lang').value=language;">
Language
<select id="lang" name="lang" onchange="recognition.lang=this.value;try{startButton(event);}catch(e){}">
<option value="en-US">UnitedStates</option>
<option value="cmn-Hant-TW">中文(台灣)</option>
<option value="af-ZA">Afrikaans</option>
<option value="am-ET">አማርኛ</option>
<option value="az-AZ">Azərbaycanca</option>
<option value="bn-BD">বাংলাদেশ</option>
<option value="bn-IN">ভারত</option>
<option value="id-ID">BahasaIndonesia</option>
<option value="ms-MY">BahasaMelayu</option>
<option value="ca-ES">Català</option>
<option value="cs-CZ">Čeština</option>
<option value="da-DK">Dansk</option>
<option value="de-DE">Deutsch</option>
<option value="en-AU">Australia</option>
<option value="en-CA">Canada</option>
<option value="en-IN">India</option>
<option value="en-KE">Kenya</option>
<option value="en-TZ">Tanzania</option>
<option value="en-GH">Ghana</option>
<option value="en-NZ">NewZealand</option>
<option value="en-NG">Nigeria</option>
<option value="en-ZA">SouthAfrica</option>
<option value="en-PH">Philippines</option>
<option value="en-GB">UnitedKingdom</option>
<option value="en-US">UnitedStates</option>
<option value="es-AR">Argentina</option>
<option value="es-BO">Bolivia</option>
<option value="es-CL">Chile</option>
<option value="es-CO">Colombia</option>
<option value="es-CR">CostaRica</option>
<option value="es-EC">Ecuador</option>
<option value="es-SV">ElSalvador</option>
<option value="es-ES">España</option>
<option value="es-US">EstadosUnidos</option>
<option value="es-GT">Guatemala</option>
<option value="es-HN">Honduras</option>
<option value="es-MX">México</option>
<option value="es-NI">Nicaragua</option>
<option value="es-PA">Panamá</option>
<option value="es-PY">Paraguay</option>
<option value="es-PE">Perú</option>
<option value="es-PR">PuertoRico</option>
<option value="es-DO">RepúblicaDominicana</option>
<option value="es-UY">Uruguay</option>
<option value="es-VE">Venezuela</option>
<option value="eu-ES">Euskara</option>
<option value="fil-PH">Filipino</option>
<option value="fr-FR">Français</option>
<option value="jv-ID">BasaJawa</option>
<option value="gl-ES">Galego</option>
<option value="gu-IN">ગુજરાતી</option>
<option value="hr-HR">Hrvatski</option>
<option value="zu-ZA">IsiZulu</option>
<option value="is-IS">Íslenska</option>
<option value="it-IT">Italia</option>
<option value="it-CH">Svizzera</option>
<option value="kn-IN">ಕನ್ನಡ</option>
<option value="km-KH">ភាសាខ្មែរ</option>
<option value="lv-LV">Latviešu</option>
<option value="lt-LT">Lietuvių</option>
<option value="ml-IN">മലയാളം</option>
<option value="mr-IN">मराठी</option>
<option value="hu-HU">Magyar</option>
<option value="lo-LA">ລາວ</option>
<option value="nl-NL">Nederlands</option>
<option value="ne-NP">नेपालीभाषा</option>
<option value="nb-NO">Norskbokmål</option>
<option value="pl-PL">Polski</option>
<option value="pt-BR">Brasil</option>
<option value="pt-PT">Portugal</option>
<option value="ro-RO">Română</option>
<option value="si-LK">සිංහල</option>
<option value="sl-SI">Slovenščina</option>
<option value="su-ID">BasaSunda</option>
<option value="sk-SK">Slovenčina</option>
<option value="fi-FI">Suomi</option>
<option value="sv-SE">Svenska</option>
<option value="sw-TZ">Tanzania</option>
<option value="sw-KE">Kenya</option>
<option value="ka-GE">ქართული</option>
<option value="hy-AM">Հայերեն</option>
<option value="ta-IN">இந்தியா</option>
<option value="ta-SG">சிங்கப்பூர்</option>
<option value="ta-LK">இலங்கை</option>
<option value="ta-MY">மலேசியா</option>
<option value="te-IN">తెలుగు</option>
<option value="vi-VN">TiếngViệt</option>
<option value="tr-TR">Türkçe</option>
<option value="ur-PK">پاکستان</option>
<option value="ur-IN">بھارت</option>
<option value="el-GR">Ελληνικά</option>
<option value="bg-BG">български</option>
<option value="ru-RU">Pусский</option>
<option value="sr-RS">Српски</option>
<option value="uk-UA">Українська</option>
<option value="ko-KR">한국어</option>
<option value="cmn-Hans-CN">普通话(中国大陆)</option>
<option value="cmn-Hans-HK">普通话(香港)</option>
<option value="yue-Hant-HK">粵語(香港)</option>
<option value="cmn-Hant-TW">中文(台灣)</option>
<option value="ja-JP">日本語</option>
<option value="hi-IN">हिन्दी</option>
<option value="th-TH">ภาษาไทย</option>
</select><br>
<input type="checkbox" id="chk" name="chk" checked>Say "ok" or "okay" to execute command.<br>
<div id="showText"></div>
<div id="result" style="color:red;font-weight:bold;"></div><br>
<div id="command" style="color:blue;font-weight:bold;"></div>
<script>
var recognizing = false;
var ignore_onend;
var two_line = /\n\n/g;
var one_line = /\n/g;
var first_char = /\S/;
var errTemp = "";
var errTimer;
var chkTimer;
if ('webkitSpeechRecognition' in window) {
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
var strUrl=location.search;
if (strUrl.indexOf("?")!=-1) {
var STAIP = strUrl.split("?")[1].split("&")[0];
var language = strUrl.split("?")[1].split("&")[1];
var checkbox = strUrl.split("?")[1].split("&")[2];
}
if (language=="")
recognition.lang="en-US";
else
recognition.lang=language;
if (checkbox=="false")
document.getElementById("chk").checked = false;
if (!recognizing) startButton(event);
recognition.onstart = function() {
recognizing = true;
};
recognition.onerror = function(event) {
if (event.error == 'no-speech') ignore_onend = true;
if (event.error == 'audio-capture') ignore_onend = true;
if (event.error == 'not-allowed') ignore_onend = true;
console.log(event.error);
};
recognition.onend = function() {
recognizing = false;
if (ignore_onend) return;
recognition.start();
};
recognition.onresult = function(event) {
var interim_transcript = '';
var final_transcript = '';
var Recognition_interim = '';
var Recognition_final = '';
if (typeof(event.results) == 'undefined') {
recognition.onend = null;
recognition.stop();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript = event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
final_transcript = capitalize(final_transcript);
Recognition_interim = linebreak(interim_transcript);
if (Recognition_interim=='') {
Recognition_final = linebreak(final_transcript);
console.log("result= " + Recognition_final);
if (Recognition_final.toLowerCase().trim()=="ok"||Recognition_final.toLowerCase().trim()=="okay") {
document.getElementById("showText").innerHTML = "";
document.getElementById("result").innerHTML = "";
location.href = document.getElementById("command").innerHTML;
return;
}
else {
document.getElementById("showText").innerHTML = "";
document.getElementById("result").innerHTML = Recognition_final;
document.getElementById("command").innerHTML = "http://" +STAIP + "?speech=" + Recognition_final.toLowerCase().trim() + ";" + document.getElementById("lang").value + ";" + document.getElementById("chk").checked;
if (document.getElementById("chk").checked == false) {
document.getElementById("showText").innerHTML = "";
document.getElementById("result").innerHTML = "";
location.href = document.getElementById("command").innerHTML;
return;
}
}
}
else {
Recognition_final = "";
document.getElementById("showText").innerHTML = Recognition_interim;
console.log(Recognition_interim);
clearTimeout(errTimer);
errTemp = Recognition_interim;
errTimer = setTimeout(function() {
if (errTemp==document.getElementById("showText").innerHTML&&errTemp!="") {
if (errTemp.toLowerCase().trim()=="ok"||errTemp.toLowerCase().trim()=="okay") {
document.getElementById("showText").innerHTML = "";
document.getElementById("result").innerHTML = "";
location.href = document.getElementById("command").innerHTML;
return;
}
else {
document.getElementById("showText").innerHTML = "";
document.getElementById("result").innerHTML = errTemp;
document.getElementById("command").innerHTML = "http://" +STAIP + "?speech=" + errTemp.toLowerCase().trim() + ";" + document.getElementById("lang").value + ";" + document.getElementById("chk").checked;
if (document.getElementById("chk").checked == false) {
document.getElementById("showText").innerHTML = "";
document.getElementById("result").innerHTML = "";
location.href = document.getElementById("command").innerHTML;
return;
}
}
}
}, 3000);
}
if (document.getElementById("showText").innerHTML!="") {
document.getElementById("result").innerHTML = "";
}
};
}
function linebreak(s) {
return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
function capitalize(s) {
return s.replace(first_char, function(m) { return m.toUpperCase(); });
}
function startButton(event) {
if (recognizing) {
recognition.stop();
}
recognition.start();
ignore_onend = false;
}
setInterval(async function(){
if (!recognizing) {
try {
startButton(event);
}
catch (e) {
}
}
},100);
</script>
</body>
</html>