首页 文章

React-Native语音到文本

提问于
浏览
2

我正在尝试使用Watson Speech to Text API在react-native应用程序中录制音频,然后将音频转换为文本 .

我无法解决这个问题,任何帮助都会受到赞赏 .

我可以录制音频,但我无法弄清楚如何将文件发送到后端或只是直接发送到前端的Watson API .

用于节点的Watson API Cloud库具有以下内容:

var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
      var fs = require('fs');

      var speech_to_text = new SpeechToTextV1({
      username: '<username>',
      password: '<password>'
      });

        var params = {
        // From file
        audio: fs.createReadStream('./resources/speech.wav'),
        content_type: 'audio/l16; rate=44100'
         };

       speech_to_text.recognize(params, function(err, res) {
       if (err)
       console.log(err);
       else
      console.log(JSON.stringify(res, null, 2));
       });

不幸的是,我无法访问前端的'fs'来创建Streams . 该文件保存在客户端前端的隐藏文件夹中(我也有路径)

最终我想以某种方式创建一个流,以便我可以发送音频,以便自动转换为文本并降低速度 .

像这样:

fs.createReadStream('./resources/speech.wav')
   .pipe(speech_to_text.createRecognizeStream({ content_type:       'audio/l16; rate=44100' }))
  .pipe(fs.createWriteStream('./transcription.txt'));

任何想法如何在前端使用录制的音频路径完成所有这些 . 有什么工作吗?谢谢!

2 回答

  • 0

    React Native支持开箱即用的websockets:https://facebook.github.io/react-native/docs/network.html

    Watson API支持websockets作为Speech to Text API的一部分:https://www.ibm.com/watson/developercloud/doc/speech-to-text/websockets.shtml(参见"Sending audio and receiving recognition results" websocket.send(blob)

    这似乎是一个合理的解决方案 .

  • 2

    我已经整理了一个使用watson-developer-cloud / swift-sdk的本机模块,并实现了语音到文本 .

    https://github.com/pwcremin/react-native-watson

    您可以参考我的代码以获取如何实现它的示例,或者只使用该模块 .

    react-native-watson模块使用麦克风为您处理流媒体:

    import {SpeechToText} from 'react-native-watson';
    
    SpeechToText.initialize("username", "password")
    
    // will transcribe microphone audio
    SpeechToText.startStreaming((error, text) =>
            {
                console.log(text)
            })
    
    SpeechToText.stopStreaming()
    

相关问题