0

我正在尝试在我的网络应用程序中实现 Google 的 Speech to Text 转录,但遇到了很多麻烦。

我决定从底层开始,看看我是否至少可以实现他们的 node.js 快速入门示例。在下面的两个示例中(一个旨在转录本地音频文件,另一个旨在转录 uri 中的音频),我最终在控制台中遇到了完全相同的错误:

(节点:4836)UnhandledPromiseRejectionWarning:错误:{ 处的文件不存在,或者它不是文件。ENOENT: 没有这样的文件或目录,lstat '[Path to Project Folder]/{'

URI 示例(基于

//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();

本地音频文件示例(基于

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);

我在这里想念什么?提前致谢!

4

1 回答 1

1

好的!我想到了...

事实证明,在我的 .env 中,我将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为 json 服务帐户密钥的复制和粘贴内容,而不是将其设置为 json 文件的路径。

将其设置为密钥的路径解决了我的问题

于 2021-03-25T13:44:10.030 回答