Skip to content

Commit

Permalink
Added metadata sample code (#58)
Browse files Browse the repository at this point in the history
* Added metadata sample code

* Fixed function name.

* Ran prettier.
  • Loading branch information
happyhuman authored and Ace Nassri committed Nov 17, 2022
1 parent e49eac0 commit 74cc771
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
76 changes: 75 additions & 1 deletion speech/recognize.v1p1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function syncRecognizeWithAutoPunctuation(
// [START speech_transcribe_file_with_auto_punctuation]
// Imports the Google Cloud client library
const fs = require('fs');
const speech = require('@google-cloud/speech');
const speech = require('@google-cloud/speech').v1p1beta1;

// Creates a client
const client = new speech.SpeechClient();
Expand Down Expand Up @@ -184,6 +184,67 @@ function syncRecognizeWithAutoPunctuation(
// [END speech_transcribe_file_with_auto_punctuation]
}

function syncRecognizeWithMetaData(
filename,
encoding,
sampleRateHertz,
languageCode
) {
// [START speech_transcribe_file_with_metadata]
// 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 encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const recognitionMetadata = {
interactionType: 'DISCUSSION',
microphoneDistance: 'NEARFIELD',
recordingDeviceType: 'SMARTPHONE',
recordingDeviceName: 'Pixel 2 XL',
industryNaicsCodeOfAudio: 519190,
};

const config = {
encoding: encoding,
languageCode: languageCode,
metadata: recognitionMetadata,
};

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_metadata]
}

require(`yargs`)
.demand(1)
.command(
Expand Down Expand Up @@ -224,6 +285,18 @@ require(`yargs`)
opts.languageCode
)
)
.command(
`sync-metadata <filename>`,
`Detects speech in a local audio file with metadata.`,
{},
opts =>
syncRecognizeWithMetaData(
opts.filename,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.options({
encoding: {
alias: 'e',
Expand Down Expand Up @@ -254,6 +327,7 @@ require(`yargs`)
`node $0 sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000`
)
.example(`node $0 sync-auto-punctuation ./resources/commercial_mono.wav`)
.example(`node $0 sync-metadata ./resources/commercial_mono.wav`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
Expand Down
6 changes: 6 additions & 0 deletions speech/system-test/recognize.v1p1beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const filepath1 = path.join(__dirname, `../resources/${filename1}`);
const filepath2 = path.join(__dirname, `../resources/${filename2}`);
const text1 = `the weather outside is sunny`;
const text2 = `Terrific. It's on the way.`;
const text3 = `Chrome`;

test.before(async () => {
const [bucket] = await storage.createBucket(bucketName);
Expand Down Expand Up @@ -68,3 +69,8 @@ test(`should run sync recognize with auto punctuation`, async t => {
);
t.true(output.includes(text2));
});

test(`should run sync recognize with metadata`, async t => {
const output = await runAsync(`${cmd} sync-metadata ${filepath2}`, cwd);
t.true(output.includes(text3));
});

0 comments on commit 74cc771

Please sign in to comment.