我想通过使用Google Cloud Speech API从语音输入文本输入,但由于错误发生我无法获取。我应该怎么做或者是否存在节点js中语音到文本的任何其他解决方案。 因为我想从语音中取出文本,然后将其分成几个单词,以便与我的库中存储的iMongoDBdb相匹配。
我目前的代码是:
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
// Your Google Cloud Platform project ID
const projectId = 'MY-PROJECT-ID';
// Creates a client
const client = new speech.SpeechClient({
projectId: projectId,
});
// The name of the audio file to transcribe
const fileName = 'C:/Projects/audio/public/1.mp3';
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
// 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);
});
,这个错误即将来临:
$ node speech.js
ERROR: Error: Unexpected error while acquiring application default credentials:
Could not load the default credentials. Browse to https://developers.google.com/
accounts/docs/application-default-credentials for more information.
at GoogleAuth.<anonymous> (C:\Projects\audio\node_modules\google-auth-librar
y\build\src\auth\googleauth.js:249:31)
at step (C:\Projects\audio\node_modules\google-auth-library\build\src\auth\g
oogleauth.js:47:23)
at Object.next (C:\Projects\audio\node_modules\google-auth-library\build\src
\auth\googleauth.js:28:53)
at fulfilled (C:\Projects\audio\node_modules\google-auth-library\build\src\a
uth\googleauth.js:19:58)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:3112) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejec
tion id: 3): Error: Unexpected error while acquiring application default credent
ials: Could not load the default credentials. Browse to https://developers.googl
e.com/accounts/docs/application-default-credentials for more information.
(node:3112) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
答案 0 :(得分:0)
错误是身份验证错误。
查看代码示例,我看到您正在加载来自C:/Projects/audio/public/1.mp3
的音频文件,这意味着您正在本地进行测试。
您可能没有在本地环境中设置身份验证,这就是node.js返回此错误的原因。
指南here显示了如何为本地测试设置隐式身份验证。
另一种选择是在创建客户端时提供显式的serviceaccount凭据。在这种情况下,您必须创建并下载包含serviceaccount凭据的JSON。整个过程解释为here。
其中哪一个最有效取决于您希望部署代码的位置。对于GAE&amp; GCP,第一个选项最简单,因为它们在各自的环境中包含应用程序默认凭据。对于云功能,您有使用第一个选项,因为您无法提供JSON来验证请求。