2021-08-28

Function for user to select between input modes in Python

I am trying to make a assistant sort of thing in python, so basically what I want is that it asks the user to select between two INPUT modes i.e. text or Speech Input.

but the problem is that I am not able to implement it into the main code. Below is the sample code I tried making :

import keyboard
import speech_recognition as sr



def takecommandti():
    print(" ")
    rt=input(">>")
    return rt.lower()

def takeCommandsi():     
        print("Listening....")
        r = sr.Recognizer()
        r.dynamic_energy_threshold=False
        r.energy_threshold=4000
        r.pause_threshold = 1
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
            said=""
        try:
            print("Recognizing....")
            said = r.recognize_google(audio,language='en-in')
            print(f"You Said : {said}\n")
        except sr.UnknownValueError :
            print("could not understand audio \n ~Trying Again~")
            return takeCommandsi()
        except sr.RequestError as e:
            print("Could not request results, check your internet connection; {0}".format(e))
            return "None"
        return said.lower()


input_mode_selection_variable = 0
print("Please Select in which input Mode you want to Use :) ")
print("1.Press 't' for Text Input Mode \n2.Press 's' for Speech Input Mode ")

while True :
    if keyboard.is_pressed('t'):
        print('You have successfully Selected : Text Input Mode')
        input_mode_selection_variable=False
        break
    elif keyboard.is_pressed('s'):
        print('You have successfully Selected : Speech Input Mode')
        input_mode_selection_variable=True
        break
    
def takecommand():
    if input_mode_selection_variable == False:
        takecommandti()
    elif input_mode_selection_variable ==True:
        takeCommandsi()

any suggestions or better code is Welcome :)



from Recent Questions - Stack Overflow https://ift.tt/2WvvftA
https://ift.tt/eA8V8J

No comments:

Post a Comment