PyDub: [WinError 6] The handle is invalid. While recognize_google transcribe text. FFMPEG

49 Views Asked by At

I am trying to transcribe the audio to text using the pydub and speech recognition library. My code is successfully converting the '.mp3' file to '.wav' but while conversion/transcribing using recognize_google library I am getting this error

"ERROR: [WinError 6] The handle is invalid. "

The twist is when i am running this script directly its running fine but when my API created in Flask calls this script its failing.

My Flask application is HOSTED on IIS Server.

with recaptcha_audio as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.record(source)
sleep(2)
try:            
    transcribed = recognizer.recognize_google(audio, language="en-US")
except Exception as e:
    log.error(f"-----------------------ERROR: {str(e)}")
    return f"-----------------------ERROR: {str(e)}"

I tried changing the audio files and also manipulating the AudioSegment.py subprocess implementation with "stdin=subprocess.DEVNULL".

No success yet. Please suggest a way out for my Flask Application with IIS, to implement this script.

1

There are 1 best solutions below

0
Zanyar Jalal On

After spending several days investigating, I've identified a solution for recognizer.recognize_google or Speech-to-Text (STT) issues when working with virtual environments and IIS deployment.

The key to resolving the issue is ensuring that you create and use the virtual environment instead use Python from c:/python.

Here's a concise guide:

  1. Create Virtual Environment:

    Use the following command to create a virtual environment named 'env':

    python -m venv env

  2. Activate Virtual Environment:

    .\env\Scripts\activate

  3. Dependency Installation

    While the virtual environment is active, install the required pip libraries for your project