Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Speech Recognition always listen and execute commands



  • I want the program always listen till it hear the word "sofia" and again hear for command and return the text so other class processes it and again hear for the word "sofia" and another command.

    #SpeechCls
    import speech_recognition as sr 
    import time
    
    from PyQt5.QtWidgets import QMessageBox
    
    import playsound
    import voiceAssistant
    counter = 0;
    
    lang_name = "fa-IR"
    
    class Speech:
    
        def SpeechFunc(self) :
    
            sample_rate = 48000
            chunk_size = 2048
            r = sr.Recognizer() 
    
            mic_list = sr.Microphone.list_microphone_names()
                  
            global text
    
            try:
                micro_name
            except NameError:
                self.SetMicName( mic_list[0])
                
            for i, microphone_name in enumerate(mic_list): 
                if microphone_name == self.getMic():
                    device_id = i 
    
            try:
                with sr.Microphone(device_index = device_id, sample_rate = sample_rate,  
                                        chunk_size = chunk_size) as source: 
                    while 1 :
        
                        r.adjust_for_ambient_noise(source) 
                        audio = r.listen(source) 
                        try:    
                            text = r.recognize_google(audio,language=lang_name)
                        except sr.UnknownValueError: 
                        
                            text = "error : Not recognize the voice"
                        
                        except sr.RequestError as e:
                            
                                text = "error :can't connect to google" 
                                
                                
    #                             playsound.playsound('noInternet.mp3', True)
                                    
                                break
                                
                        if "sofia" in text:
                            playsound.playsound('here.mp3', True)
                            text = ""
                            print("say your command")
                            audio = r.listen(source)
                            try:
                                text = r.recognize_google(audio,language=lang_name)
                                break
                                
                              
                            except sr.UnknownValueError: 
        
                                text = "error : Not recognize the voice"
                              
                            except sr.RequestError as e:
        
                                text = "error :can't connect to google" 
            except:
                text = "error : Change the microphone"
    
        def textReturn(self):
            self.SpeechFunc()
            return text
        def mic_lists(self):
            mic_list= sr.Microphone.list_microphone_names()
            return mic_list
        def SetMicName(self,name):
            global micro_name
            micro_name = name;
        def setLang(self,lang):
            global lang_name      
            if not lang_name :
               lang_name = "fa-IR" 
            if (lang == 0):
                lang_name = "fa-IR"
            elif (lang == 1):
                lang_name  = "en-US"
            elif (lang == 2):
                lang_name  = "es"
            elif (lang == 3):
                lang_name  = "ar-IQ"
            elif (lang == 4):
                lang_name  = "tr"
        def getMic(self):
            return micro_name
    
    

    I call the above class via this command :

    self.speechText = SpeechCls.Speech().textReturn()
    

    The problem is it works on my laptop but on other computer doesn't get to command portion after saying "sofia"


  • Lifetime Qt Champion

    Hi,

    Sounds like it would rather be a question for the authors the speech_recognition/voiceAssistant packages you are using, isn't it ?



  • @SGaist So what's is your suggestion where can i ask and get answer ?


  • Banned

    Okay to help you debug this I suggest you either walk through the code using an IDE or add some print statements to figure out what it actually is and is not doing -- perhaps the interpretation is slightly different between the two machines or something else is not working as you think it ought to be working -- finding out what you are actually getting under the 2 different circumstances ought to give you a major incite to what the actual issue is. Good luck


  • Lifetime Qt Champion

    Where are these speech related classes coming from ?



  • @Denni-0 As you suggested me i end up installing python and other library and hard code for that machine. Now it works your hint about print statement and debugging was very useful. I really appreciate your help .



  • @SGaist In the main class which is the form i i call it in a different thread.


  • Banned

    I am glad it was of help -- but yes when programming never assume things are working the way you would expect them to -- always best to validate (if at all possible) exactly what is being sent and/or received and each step of the way and it greatly helps if you build a project in this manner -- in pieces testing each piece along the way as you put them into place -- this is what led to functional or unit testing of course that is only supported within environments that are geared for it otherwise you have to invent that wheel which is not always necessary -- still some kind integration test is very useful if set up properly to let you know when you make changes that you have not broken something some where else


Log in to reply