【问题标题】:Send Google-Speech-Text Result to Server将 Google-Speech-Text 结果发送到服务器
【发布时间】:2020-05-04 22:17:22
【问题描述】:

我正在尝试将此示例代码生成的脚本从 google 发送到 nodejs 服务器并显示给用户。

var http = require('http');
const recorder = require('node-record-lpcm16');
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();

const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';

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 =>
    //console.log('test');
    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'
    )
    writeToServer(data.results[0].alternatives[0].transcript);
  );

function writeToServer(data){
  http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write(data);
    res.end();
  }).listen(8080);
}

// start recording and send the microphone input to the speech api.
// ensure SoX is installed, see https://www.npmjs.com/package/node-record-lpcm16#dependencies
recorder
  .record({
    sampleRateHertz: sampleRateHertz,
    threshold: 0,
    // other options, see https://www.npmjs.com/package/node-record-lpcm16#options
    verbose: false,
    recordProgram: 'rec', // try also "arecord" or "sox"
    silence: '1.0',
  })
  .stream()
  .on('error', console.error)
  .pipe(recognizeStream);

console.log('listening, press Ctrl+C to stop.');

我很难从 google 语音客户端中访问成绩单。还可以参考如何从 web 应用程序而不是本地麦克风传递麦克风输入,因为目标是通过浏览器从用户获取麦克风输入并传递给 google-speech-to-text api。

【问题讨论】:

    标签: node.js http google-cloud-platform google-speech-to-text-api


    【解决方案1】:

    我向您推荐以下工作流程:

    录制用户音频 -> 将其发送到您的网络应用程序 -> 使用 Google-Speech-To-Text 转录 -> 将响应发送给客户端

    对于您的页面渲染,我推荐了ExpressJs,它是一个最小且灵活的 Node.js Web 应用程序框架,它为 Web 和移动应用程序提供了一组强大的功能。

    设置好网络应用后,您可以尝试使用Recorder.Js 录制用户麦克风,然后将其发送到您的网络应用。

    客户:

    var filename = new Date().toISOString();
    //filename to send to server without extension 
    //upload link 
    var upload = document.createElement('a');
    upload.href = “/transcriptaudio”;
    upload.innerHTML = "Upload";
    upload.addEventListener("click", function(event) {
        var xhr = new XMLHttpRequest();
        xhr.onload = function(e) {
            if (this.readyState === 4) {
                console.log("Server returned: ", e.target.responseText);
            }
        };
        var fd = new FormData();
        fd.append("audio_data", blob, filename);
        xhr.open("POST", "upload.php", true);
        xhr.send(fd);
    })
    li.appendChild(document.createTextNode(" ")) //add a space in between 
    li.appendChild(upload) //add the upload link to li
    

    服务器:

    app.post(‘/transcriptaudio’, function (req, res) {
    
    audio = req.body.audio_data;
    
    // create a recognize stream
    const recognizeStream = client
      .streamingRecognize(audio)
      .on('error', console.error)
      .on('data', data =>
        //console.log('test');
        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'
        )
        res.send(data.results[0].alternatives[0].transcript);
      );
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      相关资源
      最近更新 更多