Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .cloud-repo-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"file": "recognize.js",
"docs_link": "https://cloud.google.com/speech/docs",
"usage": "node recognize.js --help"
},
{
"id": "recognize.v1p1beta1",
"name": "Speech Recognition v1p1beta1",
"file": "recognize.v1p1beta1.js",
"docs_link": "https://cloud.google.com/speech/docs",
"usage": "node recognize.v1p1beta1.js --help"
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ has instructions for running the samples.
| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Speech Recognition | [source code](https://github.com/googleapis/nodejs-speech/blob/master/samples/recognize.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/recognize.js,samples/README.md) |
| Speech Recognition v1p1beta1 | [source code](https://github.com/googleapis/nodejs-speech/blob/master/samples/recognize.v1p1beta1.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/recognize.v1p1beta1.js,samples/README.md) |

The [Speech API Node.js Client API Reference][client-docs] documentation
also contains samples.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"eslint": "^4.10.0",
"eslint-config-prettier": "^2.7.0",
"eslint-plugin-node": "^6.0.0",
"eslint-plugin-prettier": "^2.3.1",
"eslint-plugin-prettier": "^2.6.0",
"ink-docstrap": "^1.3.0",
"intelli-espower-loader": "^1.0.1",
"jsdoc": "^3.5.5",
Expand Down
34 changes: 34 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The [Cloud Speech API](https://cloud.google.com/speech/docs) enables easy integr
* [Before you begin](#before-you-begin)
* [Samples](#samples)
* [Speech Recognition](#speech-recognition)
* [Speech Recognition v1p1beta1](#speech-recognition-v1p1beta1)

## Before you begin

Expand Down Expand Up @@ -65,5 +66,38 @@ For more information, see https://cloud.google.com/speech/docs
[recognize_0_docs]: https://cloud.google.com/speech/docs
[recognize_0_code]: recognize.js

### Speech Recognition v1p1beta1

View the [source code][recognize.v1p1beta1_1_code].

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/recognize.v1p1beta1.js,samples/README.md)

__Usage:__ `node recognize.v1p1beta1.js --help`

```
recognize.v1p1beta1.js <command>

Commands:
recognize.v1p1beta1.js sync-model <filename> <model> Detects speech in a local audio file using provided model.
recognize.v1p1beta1.js sync-model-gcs <gcsUri> <model> Detects speech in an audio file located in a Google Cloud
Storage bucket using provided model.

Options:
--version Show version number [boolean]
--encoding, -e [string] [default: "LINEAR16"]
--sampleRateHertz, -r [number] [default: 16000]
--languageCode, -l [string] [default: "en-US"]
--help Show help [boolean]

Examples:
node recognize.v1p1beta1.js sync-model ./resources/Google_Gnome.wav video -e LINEAR16 -r 16000
node recognize.v1p1beta1.js sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000

For more information, see https://cloud.google.com/speech/docs
```

[recognize.v1p1beta1_1_docs]: https://cloud.google.com/speech/docs
[recognize.v1p1beta1_1_code]: recognize.v1p1beta1.js

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-speech&page=editor&open_in_editor=samples/README.md
196 changes: 196 additions & 0 deletions samples/recognize.v1p1beta1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This application demonstrates how to perform basic recognize operations with
* with the Google Cloud Speech API.
*
* For more information, see the README.md under /speech and the documentation
* at https://cloud.google.com/speech/docs.
*/

'use strict';

function syncRecognizeModelSelection(
filename,
model,
encoding,
sampleRateHertz,
languageCode
) {
// [START speech_transcribe_model_selection]
// Imports the Google Cloud client library
const fs = require('fs');
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 model = 'Model to use, e.g. phone_call, video, default';
// 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,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
model: model,
};
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];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: `, transcription);
})
.catch(err => {
console.error('ERROR:', err);
});
// [END speech_transcribe_model_selection]
}

function syncRecognizeModelSelectionGCS(
gcsUri,
model,
encoding,
sampleRateHertz,
languageCode
) {
// [START speech_transcribe_model_selection_gcs]
// Imports the Google Cloud client library
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 gcsUri = 'gs://my-bucket/audio.raw';
// const model = 'Model to use, e.g. phone_call, video, default';
// 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,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
model: model,
};
const audio = {
uri: gcsUri,
};

const request = {
config: config,
audio: audio,
};

// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: `, transcription);
})
.catch(err => {
console.error('ERROR:', err);
});
// [END speech_transcribe_model_selection_gcs]
}

require(`yargs`)
.demand(1)
.command(
`sync-model <filename> <model>`,
`Detects speech in a local audio file using provided model.`,
{},
opts =>
syncRecognizeModelSelection(
opts.filename,
opts.model,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.command(
`sync-model-gcs <gcsUri> <model>`,
`Detects speech in an audio file located in a Google Cloud Storage bucket using provided model.`,
{},
opts =>
syncRecognizeModelSelectionGCS(
opts.gcsUri,
opts.model,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.options({
encoding: {
alias: 'e',
default: 'LINEAR16',
global: true,
requiresArg: true,
type: 'string',
},
sampleRateHertz: {
alias: 'r',
default: 16000,
global: true,
requiresArg: true,
type: 'number',
},
languageCode: {
alias: 'l',
default: 'en-US',
global: true,
requiresArg: true,
type: 'string',
},
})
.example(
`node $0 sync-model ./resources/Google_Gnome.wav video -e LINEAR16 -r 16000`
)
.example(
`node $0 sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000`
)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
.help()
.strict().argv;
Binary file added samples/resources/Google_Gnome.wav
Binary file not shown.
59 changes: 59 additions & 0 deletions samples/system-test/recognize.v1p1beta1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const path = require(`path`);
const storage = require(`@google-cloud/storage`)();
const test = require(`ava`);
const uuid = require(`uuid`);

const {runAsync} = require(`@google-cloud/nodejs-repo-tools`);

const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node recognize.v1p1beta1.js`;
const cwd = path.join(__dirname, `..`);
const filename = `Google_Gnome.wav`;
const filepath = path.join(__dirname, `../resources/${filename}`);
const text = `the weather outside is sunny`;

test.before(async () => {
const [bucket] = await storage.createBucket(bucketName);
await bucket.upload(filepath);
});

test.after.always(async () => {
const bucket = storage.bucket(bucketName);
await bucket.deleteFiles({force: true});
await bucket.deleteFiles({force: true}); // Try a second time...
await bucket.delete();
});

test(`should run sync recognize with model selection`, async t => {
const model = `video`;
const output = await runAsync(`${cmd} sync-model ${filepath} ${model}`, cwd);
t.true(output.includes(`Transcription:`));
t.true(output.includes(text));
});

test(`should run sync recognize on a GCS file with model selection`, async t => {
const model = `video`;
const output = await runAsync(
`${cmd} sync-model-gcs gs://${bucketName}/${filename} ${model}`,
cwd
);
t.true(output.includes(`Transcription:`));
t.true(output.includes(text));
});