我正在尝试使用 Azure 语音识别器服务进行语音转文本,但是当我运行此代码时,出现错误
代码是;
import azure.cognitiveservices.speech as speechsdk
subscription_key = "key is here"
service_region = "region is here too"
audio_file = "data file path"
speech_config = speechsdk.SpeechConfig(subscription=subscription_key, region=service_region)
audio_input = speechsdk.AudioConfig(filename=audio_file)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input,language="tr")
result = speech_recognizer.recognize_once()
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Metin: {}".format(result.text))
elif result.reason == speechsdk.ResultReason.NoMatch:
print("Eşleşme bulunamadı: {}".format(result.no_match_details.reason))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Tanıma iptal edildi: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Hata ayrıntıları: {}".format(cancellation_details.reason_details))
错误是;
---> 16 speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input,language="tr")
RuntimeError: Exception with error code:
[CALL STACK BEGIN]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1aa875) [0x7fd4c79aa875]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1aaf64) [0x7fd4c79aaf64]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1ac537) [0x7fd4c79ac537]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1acbee) [0x7fd4c79acbee]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1919bc) [0x7fd4c79919bc]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x194b3e) [0x7fd4c7994b3e]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1346c5) [0x7fd4c79346c5]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x19b253) [0x7fd4c799b253]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x19b6c2) [0x7fd4c799b6c2]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x13e447) [0x7fd4c793e447]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1e65f6) [0x7fd4c79e65f6]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x139b9b) [0x7fd4c7939b9b]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x20dfe2) [0x7fd4c7a0dfe2]
/usr/local/lib/python3.10/dist-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(recognizer_create_speech_recognizer_from_source_lang_config+0x116) [0x7fd4c78bf641]
/lib/x86_64-linux-gnu/libffi.so.8(+0x7e2e) [0x7fd509008e2e]
/lib/x86_64-linux-gnu/libffi.so.8(+0x4493) [0x7fd509005493]
/usr/lib/python3.10/lib-dynload/_ctypes.cpython-310-x86_64-linux-gnu.so(+0xa3e9) [0x7fd50902e3e9]
[CALL STACK END]
Exception with an error code: 0xa (SPXERR_INVALID_HEADER)
我有 Azure 门户帐户,并且我从 Azure 语音服务获取相关信息
根据提供的信息,错误消息表明
SpeechRecognizer
对象的配置可能存在问题。
一个可能的问题可能是您在代码中使用的语言代码。 要解决此问题,您可以使用 tr-TR 代替 tr 作为您的语言代码。
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input,language="tr-TR")
通过上述更改,代码成功执行。
请参阅此文档以获取有关其他支持的语言和代码的详细信息。