pythonspeech-recognitiongoogle-speech-api

speech_recognition does not work with .pyw files


I'm creating a voice assistant with a graphical interface, I use speech_recognition to capture audio and recognize it into text, but if I use my script with the .py extension it works, instead if I use .pyw it doesn't work

I have searched a lot but can't find an answer to this problem

I don't get any errors if I use .py, so I don't understand why it doesn't work

import speech_recognition as sr
from speech_recognition import Microphone


device_index = 1
sample_rate = 48000
chunk_size = 1024
r = sr.Recognizer()




while True:
      with Microphone(device_index=device_index, sample_rate=sample_rate, chunk_size=chunk_size) as source:
           r.adjust_for_ambient_noise(source, duration=0.7)
           r.pause_threshold = 400
        
           try:
               audio = r.listen(source,timeout=None,phrase_time_limit=5)


               Input = str(r.recognize_google(audio,language="it-IT",pfilter=0,show_all=False,with_confidence=False)).lower()

               with open("Inputs.txt", 'a') as fp:
                    fp.write(Input)

           except Exception as e:
               r = sr.Recognizer()

Solution

  • audio = r.listen(source,timeout=None,phrase_time_limit=5)
    data = r.recognize_google(audio,language="it-IT",pfilter=0,show_all=True,with_confidence=True)
    

    I found a solution to the problem, i dont know why but if show_all = False, it works only with .py, instead if show_all = True it also works with .pyw

    Input = (data['alternative'][0]['transcript']).lower()
    confidence = (data['alternative'][0]['confidence'])