编辑Azure Python代码以清除语音到文本输出

编辑Azure Python代码以清除语音到文本输出,python,azure,speech-to-text,Python,Azure,Speech To Text,我正在使用Microsoft Azure的语音到文本API,它工作得很好,但输出很麻烦,我想清理它,以便只显示识别的语音 azure提供的python代码段是: try: import azure.cognitiveservices.speech as speechsdk import sys sys.exit(1) speech_key, service_region = "***", "***" weatherfilename = os.path.join( os.pa

我正在使用Microsoft Azure的语音到文本API,它工作得很好,但输出很麻烦,我想清理它,以便只显示识别的语音

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>
试试看:
将azure.cognitiveservices.speech作为speechsdk导入
导入系统
系统出口(1)
语音键,服务区域=“***”,“***”
weatherfilename=os.path.join(
os.path.dirname(_文件__),
“orf_音频_2”,
“716_anton.wav”)
#def speech_recognize_once__from_file():
“”“使用音频文件的输入执行一次性语音识别”“”
# 
speechsdk.SpeechConfig(订阅=语音密钥,区域=服务区域)
audio\u config=speechsdk.audio.AudioConfig(文件名=weatherfilename)
#使用文件作为音频输入创建语音识别器。
#默认语言为“en-us”。
语音识别器=speechsdk.SpeechRecognizer(语音配置=语音配置,音频配置=音频配置)
改为启动连续识别()。
结果=语音识别器。识别一次()
#检查结果
如果result.reason==speechsdk.ResultReason.RecognizedSpeech:
打印(“已识别:{}”。格式(result.text))
elif result.reason==speechsdk.ResultReason.NoMatch:
打印(“无法识别语音:{}”。格式(结果。无匹配详细信息))
elif result.reason==speechsdk.ResultReason.cancelled:
取消详情=结果。取消详情
打印(“语音识别已取消:{}”。格式(取消详细信息。原因))
如果取消\u details.reason==speechsdk.CancellationReason.Error:
打印(“错误详细信息:{}”。格式(取消详细信息。错误详细信息))
# 

结果。示例代码中的文本是识别语音的最简单输出

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


请参考下面的代码片段,这对我来说是有效的

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()
输出如下所示:


不知道你的意思是什么。输出仅显示
result.text
,没有您所说的其他繁琐内容。你想得到什么样的输出?我已经编辑了这个问题,包括输出的图像和azure提供的更多代码。我已经尝试在文件中的几个不同位置抛出“result.text”,但它似乎总是提供错误。请立即隐藏您的隐私密钥!这对你来说是个风险,谢谢!这非常有效。同时也感谢你为我隐藏我的隐私密钥!噢。@liam不客气。顺便说一句,你可以标记我的答案供其他人参考,谢谢!