Azure语音转文本西班牙语识别异常:设置es-ES仍识别为英文求助
问题:Azure语音识别设置'es-ES'仍识别为英文
我用Python编写了如下Azure Function,已将语音识别语言设置为es-ES,但测试时系统仍将西班牙语语音识别为英文——比如我说“Soy Anna”,识别结果为“Sorry, Anna”。不确定是设置有误还是按需付费(Pay-As-You-Go)层不支持西班牙语,多次调整设置未解决,希望实现西班牙语语音的正确识别。
代码如下:
import logging import azure.functions as func import azure.cognitiveservices.speech as speechsdk import azure_config def main(req: func.HttpRequest) -> func.HttpResponse: language = 'es-ES' speech_config = speechsdk.SpeechConfig(subscription=azure_config.speech_subscription_key, region=azure_config.speech_region, speech_recognition_language=language) audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True) speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config) logging.info("Speak into your microphone.") speech_recognition_result = speech_recognizer.recognize_once_async().get() if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech: logging.info("Recognized: {}".format(speech_recognition_result.text)) elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch: logging.info("No speech could be recognized: {}".format(speech_recognition_result.no_match_details)) elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled: cancellation_details = speech_recognition_result.cancellation_details logging.info("Speech Recognition canceled: {}".format(cancellation_details.reason)) if cancellation_details.reason == speechsdk.CancellationReason.Error: logging.info("Error details: {}".format(cancellation_details.error_details)) logging.info("Did you set the speech resource key and region values?") return func.HttpResponse("Speech recognition completed.", status_code=200)
排查与解决方案
1. 核心问题:Azure Function无法访问本地麦克风
你的代码中使用了use_default_microphone=True,这在本地调试时可以调用你电脑的麦克风,但部署到Azure云端后,Function运行在服务器环境中,没有可用的麦克风硬件,实际接收的是服务器环境的噪音,因此被误识别为英文内容。
修正方案:修改Function接收客户端上传的音频数据(比如音频文件或流),再进行语音识别,示例代码如下:
import logging import azure.functions as func import azure.cognitiveservices.speech as speechsdk import azure_config def main(req: func.HttpRequest) -> func.HttpResponse: language = 'es-ES' speech_config = speechsdk.SpeechConfig(subscription=azure_config.speech_subscription_key, region=azure_config.speech_region, speech_recognition_language=language) # 从HTTP请求中获取音频数据 audio_data = req.get_body() if not audio_data: return func.HttpResponse("未提供音频数据", status_code=400) # 创建推送音频流并写入数据 push_stream = speechsdk.audio.PushAudioInputStream() push_stream.write(audio_data) audio_config = speechsdk.audio.AudioConfig(stream=push_stream) speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config) logging.info("开始处理音频识别") speech_recognition_result = speech_recognizer.recognize_once_async().get() # 结果处理逻辑保持不变 if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech: recognized_text = speech_recognition_result.text logging.info(f"识别结果: {recognized_text}") return func.HttpResponse(f"识别结果:{recognized_text}", status_code=200) elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch: msg = f"无法识别语音: {speech_recognition_result.no_match_details}" logging.info(msg) return func.HttpResponse(msg, status_code=400) elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled: cancellation_details = speech_recognition_result.cancellation_details msg = f"语音识别被取消: {cancellation_details.reason}" logging.info(msg) if cancellation_details.reason == speechsdk.CancellationReason.Error: msg += f"\n错误详情: {cancellation_details.error_details}" logging.info(msg) return func.HttpResponse(msg, status_code=500)
2. 验证语言设置是否生效
可以在创建SpeechConfig后添加日志,确认语言参数是否正确加载:
logging.info(f"当前识别语言: {speech_config.speech_recognition_language}")
如果输出不是es-ES,检查azure_config模块是否有覆盖设置的逻辑。
3. 确认区域与语言兼容性
Pay-As-You-Go层完全支持西班牙语语音识别,只需确认你的语音资源所在区域(speech_region)支持西班牙语。可以在Azure门户的语音资源页面查看该区域支持的语言列表,确保es-ES在列。
4. 确保音频质量
客户端上传的音频需要满足:
- 采样率16kHz、16位单声道PCM格式(推荐)
- 无明显杂音,发音清晰标准
内容的提问来源于stack exchange,提问作者greyhound131
相关产品推荐
相关产品推荐

