【问题标题】:Google-Cloud-Speech speach-to-text error #4058Google-Cloud-Speech 语音转文本错误 #4058
【发布时间】:2020-01-27 20:27:40
【问题描述】:

我想识别 NodeJS 服务器中的音频文件。

我在Windows 10上使用命令行Node的版本是10.6.0,我已经安装了@google-cloud/speech 经常使用 npm。更重要的是,我已经声明了凭据的环境变量(在此解释 https://cloud.google.com/docs/authentication/getting-started?hl=en),并且我已将 json 文件复制到“凭据”文件夹中:

设置 GOOGLE_APPLICATION_CREDENTIALS="C:\Users\Me\Documents\NodeJs\Project1\credentials\RDCommandeVocale-b521de3b57d9.json"

文件是通过 ffmpeg 使用以下命令编码的:

ffmpeg -i newRecording.aac -vol 512 -c flac -ar 16000 newRecording.flac

我的源代码是:

const folderName = "uploaded";
const fileName = "newRecording";
const client = new speech.SpeechClient();

const config = {
  encoding:"FLAC",
  sampleRateHertz: 16000,
  languageCode: "fr-FR"
};
const audioBytes = fs.readFileSync(`${__dirname}\\` + folderName + "\\" + fileName + ".flac").toString('base64');
//This doesn't work else with this : 
//const audioBytes = fs.readFileSync(".\\uploaded" + fileName + ".flac").toString('base64');
// ... nor this one
//const audioBytes = fs.readFileSync("./uploaded" + fileName + ".flac").toString('base64');

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

client.recognize(request).then( response=>{
  const transcription = response.results 
  .map(result => result.alternatives[0].transcript)// récupérer uniquement la première alternative
  .join('\n');
  console.log("Textual transcription: ", transcription );
  res.status(200)
  .json({ status: "success", message: transcription });
},
(err)=>{
  console.log("Transcription ERROR : ", JSON.stringify(err));
  res.status(500)
  .json({ status: "error", message: JSON.stringify(err) });
});

我收到此错误:

文本转录:{"errno":-4058,"syscall":"lstat","code":"ENOENT","path":"c:\Users\Me\Documents\Me\NodeJs\Project1\ \"C:"}

Google Cloud API 文档中的任何地方都引用了这种错误类型吗?

【问题讨论】:

  • 我认为这可能是目录错误(此处采用的想法:[stackoverflow.com/questions/48370690/…),但您能告诉我这是否可能是身份验证问题吗?向 Google Cloud Speech API 发送请求是否需要 Google Cloud 的身份验证?

标签: node.js windows google-cloud-speech


【解决方案1】:

以下代码 sn-p 功能齐全,将在控制台上显示response 对象。您错过了 request 对象的 audio 属性需要 { contents: audioBytes } 对象这一事实。

对于本次测试,我还删除了您添加的所有服务器端代码。

由于您似乎更喜欢Promise 方法(而不是await/async),所以我保留了它,但使用.catch 方法而不是捕获任何错误,在.then 内的第二个函数中作为您做了。

const speech = require('@google-cloud/speech');
const fs = require('fs')
const client = new speech.SpeechClient();

const config = {
    encoding: 'FLAC',
    sampleRateHertz: 16000,
    languageCode: 'en'
};

const audioBytes = fs.readFileSync('test.flac').toString('base64');

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

client
    .recognize(request)
    .then( response => {
        const transcription = response.results 
        console.log("Textual transcription: ", JSON.stringify(response, {}, 2));
    })
    .catch((err) => {
        console.log("Transcription ERROR : ", err);
    });

通过我的音频文件,我得到了以下回复:

$ node transcribe-test.js 
Textual transcription:  [
  {
    "results": [
      {
        "alternatives": [
          {
            "words": [],
            "transcript": "the following is an audio abstract for 2019",
            "confidence": 0.8660579323768616
          }
        ],
        "channelTag": 0
      }
    ]
  },
  null,
  null
]

祝你好运!

【讨论】:

    【解决方案2】:

    这似乎不是 Google 的文字转语音服务的错误,而是找不到目录的错误。

    探索 Node 的内置 path module,尤其是 join() method

    你可能想重写这一行:

    const audioBytes = fs.readFileSync(`${__dirname}\\` + folderName + "\\" + fileName + ".flac").toString('base64');
    

    像这样:

    const path = require('path')
    ...
    const file = path.join(__dirname, folderName, fileName, '.flac')
    const audioBytes = fs.readFileSync(file).toString('base64')
    

    虽然这对您来说可能并不重要,但这也适用于 Windows 以外的系统,因为 Node 会自动执行正确的操作。

    【讨论】:

    • 可以使用'path'模块,我看到文件存在这个:console.log("audioBytes ="+JSON.stringify(audioBytes) );。但是现在它在调试控制台中发现了一个错误:error = {} 根据本文档:cloud.google.com/speech-to-text/docs/troubleshooting,似乎“空响应”可能是由错误的文件格式引起的。我会检查好的文件格式。谢谢!
    • @FrançoisGardien 我建议删除日志语句中的 JSON.stringify(err) 并保留 err。至少在我的测试中,这会产生更多信息性错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多