From 554d80e01d5d666fead3ae5291998d4474ed600b Mon Sep 17 00:00:00 2001 From: Shahin Date: Tue, 24 Apr 2018 15:21:59 -0700 Subject: [PATCH] Sample code for Audio Logging (Enhanced Model) (#61) * Sample code for Audio Logging (Enhanced Model) * Ran prettier --- speech/recognize.v1p1beta1.js | 72 +++++++++++++++++++ .../system-test/recognize.v1p1beta1.test.js | 5 ++ 2 files changed, 77 insertions(+) diff --git a/speech/recognize.v1p1beta1.js b/speech/recognize.v1p1beta1.js index e52a20b83f8..f9b264c1ff2 100644 --- a/speech/recognize.v1p1beta1.js +++ b/speech/recognize.v1p1beta1.js @@ -245,6 +245,65 @@ function syncRecognizeWithMetaData( // [END speech_transcribe_file_with_metadata] } +function syncRecognizeWithEnhancedModel( + filename, + encoding, + sampleRateHertz, + languageCode +) { + // [START speech_transcribe_file_with_enhanced_model] + // Imports the Google Cloud client library + const fs = require('fs'); + + // Imports the Google Cloud client library for Beta API + /** + * TODO(developer): Update client library import to use new + * version of API when desired features become available + */ + const speech = require('@google-cloud/speech').v1p1beta1; + + // Creates a client + const client = new speech.SpeechClient(); + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const filename = 'Local path to audio file, e.g. /path/to/audio.raw'; + // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; + // const sampleRateHertz = 16000; + // const languageCode = 'BCP-47 language code, e.g. en-US'; + + const config = { + encoding: encoding, + languageCode: languageCode, + useEnhanced: true, + model: 'phone_call', + }; + const audio = { + content: fs.readFileSync(filename).toString('base64'), + }; + + const request = { + config: config, + audio: audio, + }; + + // Detects speech in the audio file + client + .recognize(request) + .then(data => { + const response = data[0]; + response.results.forEach(result => { + const alternative = result.alternatives[0]; + console.log(alternative.transcript); + }); + }) + .catch(err => { + console.error('ERROR:', err); + }); + // [END speech_transcribe_file_with_enhanced_model] +} + require(`yargs`) .demand(1) .command( @@ -297,6 +356,18 @@ require(`yargs`) opts.languageCode ) ) + .command( + `sync-enhanced-model `, + `Detects speech in a local audio file using an enhanced model.`, + {}, + opts => + syncRecognizeWithEnhancedModel( + opts.filename, + opts.encoding, + opts.sampleRateHertz, + opts.languageCode + ) + ) .options({ encoding: { alias: 'e', @@ -328,6 +399,7 @@ require(`yargs`) ) .example(`node $0 sync-auto-punctuation ./resources/commercial_mono.wav`) .example(`node $0 sync-metadata ./resources/commercial_mono.wav`) + .example(`node $0 sync-enhanced-model ./resources/commercial_mono.wav`) .wrap(120) .recommendCommands() .epilogue(`For more information, see https://cloud.google.com/speech/docs`) diff --git a/speech/system-test/recognize.v1p1beta1.test.js b/speech/system-test/recognize.v1p1beta1.test.js index e51318bd674..b52cec5f9e1 100644 --- a/speech/system-test/recognize.v1p1beta1.test.js +++ b/speech/system-test/recognize.v1p1beta1.test.js @@ -74,3 +74,8 @@ test(`should run sync recognize with metadata`, async t => { const output = await runAsync(`${cmd} sync-metadata ${filepath2}`, cwd); t.true(output.includes(text3)); }); + +test(`should run sync recognize with enhanced model`, async t => { + const output = await runAsync(`${cmd} sync-enhanced-model ${filepath2}`, cwd); + t.true(output.includes(text3)); +});