Reputation: 186
I'm trying to implement Google's Speech to Text transcription in my web app and am having a lot of trouble.
I decided to start on the ground floor and see if I could at least implement their quickstart examples for node.js. In both of the examples below (one aiming to transcribe a local audio file, the other aiming to transcribe audio at a uri), I end up with the exact same error in the console:
(node:4836) UnhandledPromiseRejectionWarning: Error: The file at { does not exist, or it is not a file. ENOENT: no such file or directory, lstat '[Path to Project Folder]/{'
URI Example (based on this)
//Use dotenv to Pull in my Credentials
require('dotenv').config();
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
// Creates a client
const client = new speech.SpeechClient();
async function quickstart() {
// The path to the remote LINEAR16 file
const gcsUri = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
uri: gcsUri,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
}
quickstart();
Local Audio File Example (Based on this)
require('dotenv').config();
const speech = require('@google-cloud/speech');
const fs = require('fs');
async function main() {
const client = new speech.SpeechClient();
const filename = './resources/Sample.mp3';
const file = fs.readFileSync(filename);
const audioBytes = file.toString('base64');
const audio = {
content: audioBytes
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US'
};
const request = {
audio: audio,
config: config
};
const [response] = await client.recognize(request);
const transcription = response.results.map(result =>
result.alternatives[0].transcript).join('\n'));
console.log(`Transcription: ${transcription}`);
}
main().catch(console.error);
What am I missing here? Thanks in advance!
Upvotes: 0
Views: 1249
Reputation: 186
Ok! I figured it out...
Turns out that in my .env, I had my GOOGLE_APPLICATION_CREDENTIALS environment variable set to the copied and pasted contents of the json service account key instead of setting it to the path to the json file.
Setting it to the path of the key fixed my issue
Upvotes: 1