Ric*_*rth 5 google-api node.js vue.js electron
由于这是我在这里的第一个问题,我很乐意得到有关如何更好地提出问题的提示。
我目前正在 Electron-Vue 中开发一个应用程序,我们想添加 Google Speech-To-Text 流。我可以使它适用于音频文件,但不适用于来自麦克风的流。这些是重现我的问题的步骤。
npm install -g vue-cli
vue init simulatedgreg/electron-vue my-project
Run Code Online (Sandbox Code Playgroud)
设置 GCP 控制台项目并按照此处所述获取凭据:https : //cloud.google.com/speech-to-text/docs/quickstart-client-libraries
按照此处所述安装和初始化 Cloud SDK:https : //cloud.google.com/sdk/docs/
安装 SoX 并将其添加到 PATH 变量 https://sourceforge.net/projects/sox/files/latest/download
专门为目标电子版本安装 GRPC。Simulatedgreg 使用 2.0.14:
npm install --runtime=electron --target=2.0.14 --disturl=https://atom.io/download/electron
Run Code Online (Sandbox Code Playgroud)
安装谷歌云语音和 node-record-lpcm-16
npm install @google-cloud/speech
npm install node-record-lpcm16
Run Code Online (Sandbox Code Playgroud)
现在,对于任何无法在 NodeJS 上使用 Google Speech to Text api 的人来说,这一步很重要。转到 node_modules 并查找 node-record-lpcm16 并打开 index.js。将 cmdArgs 更改为:
var cmd = 'sox';
var cmdArgs = [
'-q', // show no progress
'-t', 'waveaudio', // input-type
'-d', // use default recording device
'-r', options.sampleRate.toString(), // sample rate
'-c', '1', // channels
'-e', 'signed-integer', // sample encoding
'-b', '16', // precision (bits)
'-t', 'raw', // output-type
'-' // pipe
];
Run Code Online (Sandbox Code Playgroud)
将您从 API 生成的 credentials.json 文件放在项目文件夹中。
最后是 Vue 本身的代码:
<template>
<div id="wrapper">
<button @click="record">Test</button>
</div>
</template>
<script>
export default {
name: 'landing-page',
methods: {
record(){
this.version = process.versions.electron;
const record = require('node-record-lpcm16');
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
// Creates a client
const client = new speech.SpeechClient({
keyFilename: './credentials.json'
});
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'nl-NL';
const request = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
},
interimResults: false, // If you want interim results, set this to true
};
// Create a recognize stream
const recognizeStream = client
.streamingRecognize(request)
.on('error', console.error)
.on('data', data =>
process.stdout.write(
data.results[0] && data.results[0].alternatives[0]
? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
: `\n\nReached transcription time limit, press Ctrl+C\n`
)
);
// Start recording and send the microphone input to the Speech API
record
.start({
sampleRateHertz: sampleRateHertz,
threshold: 0,
// Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
verbose: false,
recordProgram: 'sox', // Try also "arecord" or "sox"
silence: '10.0',
})
.on('error', console.error)
.pipe(recognizeStream);
console.log('Listening, press Ctrl+C to stop.');
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我确实意识到,因为我让它在 NodeJS 本身中工作,我可以使用与电子应用程序捆绑在一起的单独 NodeJS 服务器并通过 websocket 进行通信,但我希望它对最终用户尽可能简单。提前致谢。
| 归档时间: |
|
| 查看次数: |
453 次 |
| 最近记录: |