如何通过Azure SpeechService Python SDK获取音频转录的置信度
Azure语音Python SDK连续识别获取单词级置信度方案
问题根因
你遇到的NBest信息“被屏蔽”属于误解:连续识别模式本身不会隐藏详细输出,你直接打印事件对象只会输出简略文本,完整的详细结果存放在结果对象的json字段中。
修正步骤
- 首先将OutputFormat替换为明确的枚举值,避免硬编码数值出错
- 在
recognized事件回调中解析evt.result.json,即可获取包含NBest列表、整句置信度、单词级置信度的完整数据 - 替换硬编码的
time.sleep逻辑,用循环等待识别完成的标志,避免长音频被截断
修正后完整代码
import azure.cognitiveservices.speech as speechsdk import json import time SUBSCRIPTION_KEY = "你的服务密钥" REGION = "你的服务区域" def speech_recognize_continuous_from_file(): """基于音频文件的连续语音识别,获取单词级置信度""" speech_config = speechsdk.SpeechConfig(subscription=SUBSCRIPTION_KEY, region=REGION) speech_config.speech_recognition_language="pt-BR" audio_config = speechsdk.audio.AudioConfig(filename="file.wav") speech_config.enable_dictation() # 用明确的枚举值设置详细输出,替代硬编码的1 speech_config.output_format = speechsdk.OutputFormat.Detailed speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config) done = False def stop_cb(evt): print('CLOSING on {}'.format(evt)) nonlocal done done = True # 处理最终识别结果,提取置信度 def handle_recognized(evt): if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech: # 解析完整的详细结果json detailed_result = json.loads(evt.result.json) # 取NBest列表,默认第一条是置信度最高的结果 best_result = detailed_result['NBest'][0] # 整句置信度 sentence_confidence = best_result['Confidence'] print(f"整句文本:{best_result['Display']},整句置信度:{sentence_confidence}") # 遍历获取每个单词的置信度 print("单词级置信度:") for word in best_result['Words']: print(f"单词:{word['Word']},置信度:{word['Confidence']},开始偏移:{word['Offset']},时长:{word['Duration']}") elif evt.result.reason == speechsdk.ResultReason.NoMatch: print(f"未识别到语音:{evt.result.no_match_details}") # 绑定事件回调 speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt.result.text))) speech_recognizer.recognized.connect(handle_recognized) speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt))) speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED {}'.format(evt))) speech_recognizer.canceled.connect(lambda evt: print(f'CANCELED 原因:{evt.reason},错误详情:{evt.error_details}')) speech_recognizer.session_stopped.connect(stop_cb) speech_recognizer.canceled.connect(stop_cb) # 启动连续识别 speech_recognizer.start_continuous_recognition() # 替换硬编码sleep,循环等待识别完成 while not done: time.sleep(0.5) speech_recognizer.stop_continuous_recognition() speech_recognize_continuous_from_file()
关键说明
- 只有
recognized事件返回的是最终确认结果,才有准确的置信度数据,recognizing返回的中间临时结果不会包含完整的NBest和单词级置信度 - NBest列表默认返回最多5个候选识别结果,可根据需求遍历所有候选做进一步校验
- 单词级结果中同时返回了每个单词的时间偏移和时长,可用于对齐原始音频做质量校验
内容的提问来源于stack exchange,提问作者vribeir0
相关产品推荐
相关产品推荐

