首页 > 解决方案 > Google Speech to Text API 在本地提供的结果与在线演示不同

问题描述

6秒mp3音频文件(下载)首先直接在https://cloud.google.com/speech-to-text/上测试,响应符合预期。

“你好,兄弟,你好吗,我过得很好,希望妈妈过得好”

然后我创建了firebase函数(见下面的代码):

const speech = require('@google-cloud/speech').v1p1beta1
exports.speechToText = functions.https.onRequest(async (req, res) => {
  try {
    // Creates a client
    const client = new speech.SpeechClient()
    const gcsUri = `gs://xxxxx.appspot.com/speech.mp3`

    const config = {
      encoding: 'MP3',
      languageCode: 'en-US',
      enableAutomaticPunctuation: false,
      enableWordTimeOffsets: false,
    }
    const audio = {
      uri: gcsUri,
    }

    const request = {
      config: config,
      audio: audio,
    }

    // Detects speech in the audio file
    const [response] = await client.recognize(request)
    const transcription = response.results
      .map(result => result.alternatives[0].transcript)
      .join('\n')
    console.log(`Transcription: ${transcription}`)
    res.send({ response })
  } catch (error) {
    console.log('error:', error)
    res.status(400).send({
      error,
    })
  }
})

我得到以下不正确的响应:

“你好兄弟,你好吗希望一切都好”

更新: 本地运行时收到相同的错误响应。所以云功能不是问题。

更新#2:在配置中 设置model:'video'ORmodel:'phone_call'解决了这个问题。IE

    const config = {
      encoding: 'MP3',
      languageCode: 'en-US',
      enableAutomaticPunctuation: false,
      enableWordTimeOffsets: false,
      model: 'phone_call',
    }

标签: node.jsgoogle-cloud-speech

解决方案


设置model:'video'OR解决model:'phone_call'config这个问题。IE

const config = {
 encoding: 'MP3',
 languageCode: 'en-US',
 enableAutomaticPunctuation: false,
 enableWordTimeOffsets: false,
 model: 'phone_call', 
}

我想该default模型不适用于某些类型的音频。


推荐阅读