【问题标题】:Python SpeechRecognition vs. Google Cloud Speech APIPython SpeechRecognition 与 Google Cloud Speech API
【发布时间】:2025-01-12 15:35:02
【问题描述】:

我正在使用 Google Cloud Speech API。我想知道我是否使用 python 语音识别库并调用谷歌云语音 API,这仍然是使用 API 的有效方式吗?我只是想转录文本。

我对它们之间的区别感到困惑,如果我只想转录音频,是否有任何建议的方法。

使用 Python 语音识别:

import speech_recognition as sr
r = sr.Recognizer()
r.recognize_google_cloud()
harvard = sr.AudioFile('harvard.wav')
with harvard as source:
   audio = r.record(source)
r.recognize_google(audio)

不使用 Python SpeechRecognition:

from google.cloud import speech_v1 as speech


def speech_to_text(config, audio):
    client = speech.SpeechClient()
    response = client.recognize(config, audio)
    print_sentences(response)


def print_sentences(response):
    for result in response.results:
        best_alternative = result.alternatives[0]
        transcript = best_alternative.transcript
        confidence = best_alternative.confidence
        print('-' * 80)
        print(f'Transcript: {transcript}')
        print(f'Confidence: {confidence:.0%}')


config = {'language_code': 'en-US'}
audio = {'uri': 'gs://cloud-samples-data/speech/*lyn_bridge.flac'}

【问题讨论】:

    标签: python google-cloud-platform speech-recognition google-cloud-speech


    【解决方案1】:

    如果您只打算使用 Google Cloud Platform 进行语音识别,那么 SpeechClient 会更好,因为它是由 Google 维护的。

    如果您想尝试不同的语音识别服务,speech_recognition 会有所帮助,因为它更通用。

    任何调用 api 的方式都可以。这些库只是为了让您更轻松。

    【讨论】:

      【解决方案2】:

      Google Cloud Client Libraries 是以编程方式访问云 API 的推荐选项:

      • 以每种语言提供惯用的、生成的或手写的代码,使 Cloud API 使用起来简单直观。
      • 处理与服务器通信的所有底层细节,包括通过 Google 进行身份验证。
      • 可以使用熟悉的包管理工具(例如 npm 和 pip)进行安装。
      • 在某些情况下,使用 gRPC 可为您带来性能优势。您可以在下面的 gRPC API 部分了解更多信息。

      另外,请注意best practices,以便从 API 中获得更好的结果。

      【讨论】: