gpt4 book ai didi

Python SpeechRecognition 与 Google Cloud Speech API

转载 作者:行者123 更新时间:2023-12-04 13:35:16 25 4
gpt4 key购买 nike

我正在使用 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/brooklyn_bridge.flac'}

最佳答案

如果您只打算使用 Google Cloud Platform 进行语音识别,那么 SpeechClient 会更好,因为它是由 Google 维护的。
如果您想尝试不同的语音识别服务,speech_recognition 会有所帮助,因为它更通用。
任何调用 api 的方式都可以。这些库只是为了让您更轻松。

关于Python SpeechRecognition 与 Google Cloud Speech API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62526560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com