I have this code to call the Speech recognition and it always fails with opening streams error. I tried both ways - audio stream and wav file. neither is working.
def recognize_speech_from_base64(base64_audio: str,languagecode):
"""
Decodes the base64-encoded audio and performs speech recognition using Azure.
"""
try:
# Decode base64 to raw bytes
audio_bytes = decode_base64_audio(base64_audio)
if audio_bytes is None:
return {"status": "error", "message": "Invalid Base64 input"}
# Save audio as WAV file
wav_filename = "/Users/raj/Downloads/Sports_fixed.wav" #convert_webm_to_wav(audio_bytes)
if wav_filename is None:
return {"status": "error", "message": "Failed to save audio file"}
# Initialize Azure Speech SDK
speech_config = speechsdk.SpeechConfig(subscription=AZURE_SPEECH_KEY, region=AZURE_REGION)
speech_config.speech_recognition_language = languagecode
# Use file-based AudioConfig
audio_config = speechsdk.audio.AudioConfig(filename=f"{wav_filename}")
# Create a speech recognizer
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
print(f"Processing file: {wav_filename}")
print("Recognizing speech...")
result = speech_recognizer.recognize_once() # Perform recognition
# Clean up the temporary file
# os.remove(wav_filename)
# Check recognition result
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized Text:", result.text)
return {"status": "success", "transcription": result.text}
elif result.reason == speechsdk.ResultReason.NoMatch:
print("No speech recognized.")
return {"status": "error", "message": "No speech recognized."}
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print(f"Speech recognition canceled: {cancellation_details.reason}")
return {"status": "error", "message": f"Canceled: {cancellation_details.reason}"}
except Exception as e:
print(f"Error in speech recognition: {e}")
return {"status": "error", "message": str(e)}
Error:
Recognizing speech...
Info: Error opening streams - read error=1
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/src/uws_client.c Func:on_underlying_io_open_complete Line:751 underlying on_io_open_complete was called again after upgrade request was sent.
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_destroy Line:196 tlsio_appleios_destroy called while not in TLSIO_STATE_CLOSED.
Info: Error opening streams - read error=1
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/src/uws_client.c Func:on_underlying_io_open_complete Line:751 underlying on_io_open_complete was called again after upgrade request was sent.
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_destroy Line:196 tlsio_appleios_destroy called while not in TLSIO_STATE_CLOSED.
Info: Error opening streams - read error=1
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/src/uws_client.c Func:on_underlying_io_open_complete Line:751 underlying on_io_open_complete was called again after upgrade request was sent.
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_destroy Line:196 tlsio_appleios_destroy called while not in TLSIO_STATE_CLOSED.
Info: Error opening streams - read error=1
Error: Time:Fri Feb 28 15:10:20 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:21 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/src/uws_client.c Func:on_underlying_io_open_complete Line:751 underlying on_io_open_complete was called again after upgrade request was sent.
Error: Time:Fri Feb 28 15:10:21 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:21 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_destroy Line:196 tlsio_appleios_destroy called while not in TLSIO_STATE_CLOSED.
Info: Error opening streams - read error=1
Error: Time:Fri Feb 28 15:10:21 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Error: Time:Fri Feb 28 15:10:21 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/src/uws_client.c Func:on_underlying_io_open_complete Line:751 underlying on_io_open_complete was called again after upgrade request was sent.
Error: Time:Fri Feb 28 15:10:21 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_close_async Line:383 NULL on_io_close_complete
Speech recognition canceled: CancellationReason.Error
Error: Time:Fri Feb 28 15:10:22 2025 File:/Users/runner/work/1/s/external/azure-c-shared-utility/pal/ios-osx/tlsio_appleios.c Func:tlsio_appleios_destroy Line:196 tlsio_appleios_destroy called while not in TLSIO_STATE_CLOSED.
Response : {'status': 'error', 'message': 'Canceled: CancellationReason.Error'}