【问题标题】:Google Speech-To-Text with React带有 React 的 Google Speech-To-Text
【发布时间】:2020-01-30 00:25:49
【问题描述】:

我正在开发一个简单的语音到文本网络应用程序,我已经有工作的服务器端 nodejs 代码和简单的反应页面,但我不知道如何将它们粘合在一起,我正在尝试实现各种不同的奇怪的东西,要么反应应用程序没有得到任何数据作为回报,要么有错误 500。

我想在从 Google 获取转录后为记录器实现 stop() 函数,因为它被设置为只侦听短命令,但我可以实现的唯一解决方案是 setTimeout 函数,它不完全是我的想要。

编辑:我已经解决了在获得命令后停止记录器的问题,它工作得很好,但是,欢迎任何改进。解决方案很简单,我只是将阈值从 null 修改为 0.5,thresholdEnd: 0.5。仍然没有解决这个快递应用的前端。

编辑 2:有趣的是,我无意中发现了 this stuff,这正是我想要的……为了找到这个惊人且超级简单的解决方案付出了巨大的努力,尤其是如果您关注此 medium article

有人可以帮帮我吗?

服务器端代码:

'use strict';

const express = require('express');
const app = express();
const port = 3002;

const cors = require('cors')

// Node-Record-lpcm16
const recorder = require('node-record-lpcm16');

// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

function speechFunction() {
    const encoding = 'LINEAR16';
    const sampleRateHertz = 16000;
    const languageCode = 'en-US';
    const command_and_search = 'command_and_search';
    const keywords = ['turn on', 'turn off', 'turn it on', 'turn it off'];

    const request = {
        config: {
            encoding: encoding,
            sampleRateHertz: sampleRateHertz,
            languageCode: languageCode,
            model: command_and_search,
            speech_contexts: keywords
        },
        singleUtterance: true,
        interimResults: false // If you want interim results, set this to true
    };


    // Creates a client
    const client = new speech.SpeechClient();

    // Create a recognize stream
    const recognizeStream = client
    .streamingRecognize(request)
    .on('error', console.error)
    .on('data', data =>
        // process.stdout.write(
        console.log(    
        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
    recorder
    .record({
        sampleRateHertz: sampleRateHertz,
        threshold: 0, //silence threshold
        recordProgram: 'rec', // Try also "arecord" or "sox"
        silence: '5.0', //seconds of silence before ending
        endOnSilence: true,
        thresholdEnd: 0.5
    })
    .stream()
    .on('error', console.error)
    .pipe(recognizeStream);

    console.log('Listening, press Ctrl+C to stop.');
    // [END micStreamRecognize]
}

app.use(cors());

app.use('/api/speech-to-text/',function(req, res){
    speechFunction(function(err, result){
        if (err) {
            console.log('Error retrieving transcription: ', err);
            res.status(500).send('Error 500');
            return;
        }
        res.send(result);
    })
});

// app.use('/api/speech-to-text/', function(req, res) {
//     res.speechFunction();
// });

// app.get('/speech', (req, res) => res.speechFunction);

app.listen(port, () => {
    console.log(`Listening on port: ${port}, at http://localhost:${port}/`);
});

反应

import React, { Component } from 'react';
import './App.css';

class App extends Component {

  constructor() {
    super()
    this.state = {
    }
  }

  onListenClick() {
    fetch('http://localhost:3002/api/speech-to-text/')
      .then(function(response) {
        console.log(response);
        this.setState({text: response});
      })
      .catch(function(error) {
        console.log(error);
      });
  }

  render() {
    return (
      <div className="App">
        <button onClick={this.onListenClick.bind(this)}>Start</button>
        <div style={{fontSize: '40px'}}>{this.state.text}</div>
      </div>
    );
  }
}

export default App;

【问题讨论】:

  • 我有一个错误events.js:177 throw er; // Unhandled 'error' event ^ Error: spawn sox ENOENT请帮助我。
  • 你能写更多关于你的问题吗?你想达到什么目标?
  • 我已经安装了sox 14.4.1.exe。 recoder.record({ sampleRate: 16000, channels: 1, compress: false, threshold: 0.5, thresholdStart: null, thresholdEnd: 0.5, silence: '1.0', recorder: 'rec', endOnSilence: false, audioType: 'wav' })我有一个错误Error: spawn rec ENOENT

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


【解决方案1】:

将 onListenClick() 代码替换为以下代码

async onListenClick(){
const response= await axios.get('http://localhost:3002/api/speech-to-text/')
console.log(response)
this.setState(text:response.data)
}

我试图弄清楚如何通过单击按钮来停止 google api。但是上面的代码会带上react的数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-28
    • 2019-04-16
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多