1

I constructed a simple app to recognize the speech and speak the results. But when i made a APK out of a python file through Buildozer, The tts worked out but the stt i.e the speech recognition didn't worked. The speech-to-text and the TTS library i used here is the Plyer

here is the code:

import kivy

from kivy.app import App

from kivy.uix.switch import Switch

from kivy.uix.gridlayout import GridLayout

from kivy.uix.label import Label

from kivy.uix.image import Image


class SwitchContainer(GridLayout):

     def __init__(self, **kwargs):

          super(SwitchContainer, self).__init__(**kwargs)

          self.cols = 1


          self.add_widget(Label(text="speech recognizer", pos_hint= {'x': 5, 'y': 
5,"right":5,"top":50},font_size= '90sp'))


          self.settings_sample = Switch(active=False)

          self.add_widget(self.settings_sample)

          self.settings_sample.bind(active=switch_callback)       




def switch_callback(switchObject, switchValue):

     print('Value of sample settings is:', switchValue)
     if(switchValue):
          from plyer import tts
          tts.speak('hello, will you please tell me the port')
          try:
              from plyer import stt
              stt.start()
              sst.stop()
              from plyer import tts
              tts.speak(stt.results)
          except Exception as e:

              from plyer import tts
              tts.speak(' sir,its not working')


     else:
          print("off")

class SwitchExample(App):

     def build(self):

          return SwitchContainer()



# Run the kivy app

if __name__ == '__main__':

     SwitchExample().run()

The GUI of this app consist only a button when switched on the speech is recognized and the results are spoken out through TTS or if any error is found it tell that "sir it's not working. What's wrong am i doing over here.

Amit Nikhade
  • 39
  • 1
  • 7
  • In what way is it not working? – AMC Jun 06 '20 at 19:02
  • when i run the APK it does not works, means in the code the( try and expect ) block, the exception is put in TTS as "sir it's not working. So what happens here is it doesn't recognizes voice and speaks "sir it's not working. – Amit Nikhade Jun 06 '20 at 19:08
  • Why are you seemingly importing the same thing (`from plyer import stt`) over and over? By the way, using `except Exception` like that is bad practice. See, for example: https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. – AMC Jun 06 '20 at 19:10
  • you're absolutely right but still the speech recognition is not working friend. what else should i do according to you. I think stt.start() this part is not working. – Amit Nikhade Jun 06 '20 at 19:13
  • What exception does it throw? – AMC Jun 06 '20 at 19:16
  • I have put this "except: from plyer import tts tts.speak(' sir,its not working') so it's speaking up "sir it's not working". So i think problem must be in stt.start() – Amit Nikhade Jun 06 '20 at 19:18
  • Pardon? My comment was about the specific exception which occurs when the TTS fails. – AMC Jun 06 '20 at 19:22
  • Actually i don't know whether it fail or something else happens. But it doesn't work. And I am in a big trouble. And the code works fine when i take out the speech recognition block. – Amit Nikhade Jun 06 '20 at 19:25
  • _Actually i don’t know whether it fail or something else happens._ Alright, that’s quite important information. Why catch an exception there, then? – AMC Jun 06 '20 at 19:25
  • ya sure i'll but can you please tell me, what should i include in the code. I am new to python. sorry for that. – Amit Nikhade Jun 06 '20 at 19:27
  • I’m not sure I understand what you mean, sorry. – AMC Jun 06 '20 at 19:28
  • I mean to say that what changes should i do in the code. can you please tell me. – Amit Nikhade Jun 06 '20 at 19:30
  • I really don’t know. – AMC Jun 06 '20 at 19:32
  • it's ok friend. And thank's – Amit Nikhade Jun 06 '20 at 19:34
  • Good luck, I hope you find something. – AMC Jun 06 '20 at 19:34

1 Answers1

0

I had the same problem but it's solved Just follow these steps

1> Install the app on Android 2> After Installation locate the app and long press so you will get two options a>App info b> Uninstall and click on App info 3> In App info go to the permissions to turn on microphone 4> Restart the application and check

It worked for me!!

Or you can give runtime permission to the app in python code also

from android.permissions import request_permissions, Permission
class SwitchExample(App):

     def build(self):

          return SwitchContainer()
      
     # in SwitchExampleApp class
     def on_start(self):
          request_permissions([Permission.RECORD_AUDIO]) 
    # In buildozer.spec file you have to give android.permissions = RECORD_AUDIO for speech to text

Try the above code in your app. You may get error on windows(as this works only on android) but after converting to android you will receive permission for microphone when you open it for first time.

you can give multiple permissions in the above code check this python-for-android and this various permissions for android apk.

And one thing>> In buildozer.spec you have to give requirements=python3,kivy,plyer # if you are using plyer