带有React的Google语音转文本

时间:2020-01-30 00:25:49

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

我正在针对文本Web应用程序进行简单的演讲,我已经拥有有效的服务器端nodejs代码和简单的react页面,但是我不知道如何将它们粘合在一起,我试图实现各种不同的怪异事物,或者react应用程序未获取任何数据作为回报,或者错误500。

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

编辑:我已经解决了获得命令后停止记录器的问题,并且效果很好,但是,欢迎进行任何改进。解决方案非常简单,我刚刚将阈值从null修改为thresholdEnd: 0.5。仍未解决此Express应用程序的前端。

编辑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;

1 个答案:

答案 0 :(得分: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。但是上面的代码将使数据产生反应