Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Speech Recognition always listen and execute commands
Qt 6.11 is out! See what's new in the release blog

Speech Recognition always listen and execute commands

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 4 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • behruz montazeriB Offline
    behruz montazeriB Offline
    behruz montazeri
    wrote on last edited by behruz montazeri
    #1

    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"

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      behruz montazeriB 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

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

        behruz montazeriB Offline
        behruz montazeriB Offline
        behruz montazeri
        wrote on last edited by behruz montazeri
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Where are these speech related classes coming from ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          behruz montazeriB 1 Reply Last reply
          0
          • behruz montazeriB Offline
            behruz montazeriB Offline
            behruz montazeri
            wrote on last edited by
            #5

            @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 .

            1 Reply Last reply
            0
            • SGaistS SGaist

              Where are these speech related classes coming from ?

              behruz montazeriB Offline
              behruz montazeriB Offline
              behruz montazeri
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ashley999
                Banned
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved