Python语音识别与Google云语音API

2024-05-01 21:17:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在玩谷歌云语音API。我想知道如果我使用python语音识别库并调用google cloud语音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语音识别:

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/brooklyn_bridge.flac'}

Tags: apiconfigcloudresponseasgoogle语音audio
2条回答

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

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

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

若你们只打算使用谷歌云平台进行语音识别,那个么SpeechClient会更好,因为它是由谷歌维护的

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

任何调用api的方法都可以。图书馆只是为了让你更容易

相关问题 更多 >