gpt4 book ai didi

python - 编辑 Azure Python 代码以清理语音转文本输出

转载 作者:行者123 更新时间:2023-12-01 07:34:01 25 4
gpt4 key购买 nike

我正在使用 Microsoft Azure 的语音转文本 API,它运行良好,但输出很麻烦,我想清理它,以便只显示识别的语音。

this is what the output looks like

azure提供的python代码片段是:

try:
import azure.cognitiveservices.speech as speechsdk
import sys
sys.exit(1)

speech_key, service_region = "***", "***"

weatherfilename = os.path.join(
os.path.dirname(__file__),
'orf_audio_2',
'716_anton.wav')
# def speech_recognize_once_from_file():
"""performs one-shot speech recognition with input from an audio file"""
# <SpeechRecognitionWithFile>
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
audio_config = speechsdk.audio.AudioConfig(filename=weatherfilename)
# Creates a speech recognizer using a file as audio input.
# The default language is "en-us".
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

start_continuous_recognition() instead.
result = speech_recognizer.recognize_once()

# Check the result
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(result.text))
elif result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(result.no_match_details))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech Recognition canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
# </SpeechRecognitionWithFile>

最佳答案

示例代码中的

result.text是识别语音的最简单输出。

我使用默认麦克风的测试:

enter image description here

<小时/>

请参阅下面对我有用的代码片段。

import azure.cognitiveservices.speech as speechsdk
import time
# Creates an instance of a speech config with specified subscription key and service region.
# Replace with your own subscription key and service region (e.g., "westus").
speech_key, service_region = "***", "***"

weatherfilename = "D:\\whatstheweatherlike.wav"

speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
audio_config = speechsdk.audio.AudioConfig(filename=weatherfilename)

# Creates a recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
speech_recognizer.session_stopped.connect(lambda evt: print('\nSESSION STOPPED {}'.format(evt)))
speech_recognizer.recognized.connect(lambda evt: print('\n{}'.format(evt.result.text)))

# print('Say a few words\n\n')
speech_recognizer.start_continuous_recognition()
time.sleep(10)
speech_recognizer.stop_continuous_recognition()

speech_recognizer.session_started.disconnect_all()
speech_recognizer.recognized.disconnect_all()
speech_recognizer.session_stopped.disconnect_all()

输出如下:

enter image description here

关于python - 编辑 Azure Python 代码以清理语音转文本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57084223/

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