Real time SpeechRecognition

Alright, so I have been testing out options for using SpeechRecognition to work in real time, and the best way so far came from ChatGPT (funny enough). 

Through the prompt "could you generate python code that uses speech recognition to detect speech from a mic and in real time generates and updates a variable or file with the speech recognized" and after a couple iterations I got the following code. 

import speech_recognition as sr
# create a recognizer object
r = sr.Recognizer()
# use the default microphone as the audio source
with sr.Microphone() as source:
    # adjust for ambient noise
    r.adjust_for_ambient_noise(source)
    print("Listening...")
    
    # continuously listen for speech and update the variable with recognized speech
    while True:
        # listen for speech from the microphone
        audio = r.listen(source)
        
        try:
            # recognize speech using Google Speech Recognition
            text = r.recognize_google(audio)
            
            # update variable with recognized speech
            recognized_speech = text
            print(f"Recognized speech: {recognized_speech}")
            
            # alternatively, write the recognized speech to a file
            with open("recognized_speech.txt", "a") as f:
                f.write(recognized_speech + "\n")
        
        # handle exceptions for speech recognition
        except sr.UnknownValueError:
            print("Speech recognition could not understand audio")
        except sr.RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e))

And so far it seems to be working alright, the variable being generated and print the variable,

In short the system is recognizing the speech being inputted into the mic and generating a variable that is being updated every break between the speech as an str variable named "recognized_speech" as well as a .txt file that appends whatever the last line recognized is. Which is very nice, I'm unsure as to if it will work in changing in real time the MIDI file, but its a start. :3



I LEFT THIS AS A DRAFT AAAAAAAAAAAAAAAAH

Comments

Popular Posts