Skip to content

Commit 0247593

Browse files
authored
Merge pull request #9 from hkomine/master
New example of speech stream recognition with chosen model.
2 parents 9b16099 + 1241b0c commit 0247593

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

examples/static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h2>Speech to Text</h2>
1414
<li><a href="microphone-word-confidence.html">Transcribe from Microphone, with Word Confidence</a></li>
1515
<li><a href="microphone-streaming-text-to-console.html">Transcribe from Microphone, send text to console</a></li>
1616
<li><a href="microphone-streaming-object-to-console.html">Transcribe from Microphone, send JSON to console (includes text and metadata)</a></li>
17-
<li><a href="microphone-streaming.html">Transcribe from Microphone, Streaming</a></li>
17+
<li><a href="microphone-streaming-model.html">Transcribe from Microphone, Streaming with chosen model</a></li>
1818
<li><a href="file-streaming.html">Transcribe from file, Streaming</a></li>
1919
<li><a href="file-realtime-vs-no-realtime.html">Transcribe from file, Comparing <code>{realtime: true}</code> to <code>{realtime: false}</code></a></li>
2020
<li><a href="file-promise.html">Transcribe from file, Promise</a></li>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Watson Speech to Text client example - custom model</title>
6+
</head>
7+
<body>
8+
9+
<section>
10+
<h2>Transcribe from Microphone</h2>
11+
<p>Model: <select id="model"></select> <small><i>Choose the model to be used for the speech recognition.</i></small></p>
12+
<button id="button">Start Microphone Transcription</button>
13+
<button id="stop">Stop</button>
14+
15+
<h2>Output:</h2>
16+
<div id="output">--</div>
17+
</section>
18+
19+
<script src="watson-speech.js"></script>
20+
<!-- window.fetch pollyfill for IE/Edge & Older Chrome/FireFox -->
21+
<script src="bower_components/fetch/fetch.js"></script>
22+
23+
<h2>Code for this demo:</h2>
24+
25+
<pre><code><script style="display: block;">
26+
function getTtsToken() {
27+
return fetch('/api/speech-to-text/token')
28+
.then(function(response) {
29+
return response.text();
30+
});
31+
}
32+
33+
// fetch the models and populate the dropdown
34+
getTtsToken()
35+
.then(function (token) {
36+
return WatsonSpeech.SpeechToText.getModels({token: token});
37+
}).then(function (models) {
38+
39+
var dropdown = document.querySelector('#model');
40+
models.forEach(function (m) {
41+
var o = document.createElement('option');
42+
o.value = m.name;
43+
o.textContent = m.name;
44+
if (m.name == 'en-US_BroadbandModel') {
45+
o.selected = true;
46+
}
47+
dropdown.appendChild(o);
48+
});
49+
50+
}).catch(console.error.bind(console));
51+
52+
// recognize the text using the chosen model
53+
document.querySelector('#button').onclick = function () {
54+
getTtsToken().then(function (token) {
55+
56+
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
57+
token: token,
58+
model: document.querySelector('#model').value,
59+
outputElement: '#output' // CSS selector or DOM Element
60+
});
61+
62+
stream.on('error', function(err) {
63+
console.log(err);
64+
});
65+
66+
document.querySelector('#stop').onclick = stream.stop.bind(stream);
67+
68+
}).catch(function(error) {
69+
console.log(error);
70+
});
71+
};
72+
73+
</script></code></pre>
74+
75+
</body>
76+
</html>

0 commit comments

Comments
 (0)