1

problem screenshot

How to trigger commands after speech to text conversion? Now i have text but unable to compare it using if condition in python so I can perform my required task.I have used pocketsphinx for speech recognition in raspberry pi.

   import os
   from pocketsphinx import LiveSpeech, get_model_path

   model_path = get_model_path()

   speech = LiveSpeech(
        verbose=False,
        sampling_rate=16000,
        buffer_size=2048,
        no_search=False,
        full_utt=False,
        hmm=os.path.join(model_path, 'en-us'),
        lm=os.path.join(model_path, 'en-us.lm.bin'),
        dic=os.path.join(model_path, 'cmudict-en-us.dict')
    )

    for phrase in speech:
       print(phrase)
       if phrase == "HOME"
           print (OK)

Code doesn't give any error and work fine; what i say it prints on the screen i.e. Code works till last 3rd line [print (phrase)] and give expected results but last 2 line doesn't perform required task but give no error

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • What is it prints exactly? Code looks correct except of value of OK (is it variable or you mean 'OK'?), maybe you must take account of case (```phrase.lower() == 'home'```) or check not for exact equality (```'home' in phrase.lower()```, ```phrase.lower().startswith('home')```) – Sav Jun 28 '19 at 06:17
  • Thanks for your consideration; I have attached the result, and i have consider the CASES of word "HOME", whereas by OK i meant a word "OK" not a variable – Hassaan Khan Jun 28 '19 at 12:39
  • looking into documentation I've seen what phrase isn't a string. change ```print(phrase)``` to ```print(phrase, type(phrase), dir(phrase))``` and show the result, it may hint how to compare it or get a string from it – Sav Jun 28 '19 at 13:24
  • Really appreciate your answer; u r right that it's not a string ;when i print (type(phrase)) it shows[] Could u guide me how to compare class and string ; Since I am a NOOB in programming , when i type [dir(phrase)] it shows number of complex directories. – Hassaan Khan Jun 28 '19 at 16:17

1 Answers1

0

You can use

if str(phrase) == "HOME":

or

if phrase.hypothesis() == "HOME":
Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • Thanks for your help; Could you spare some time and help me with my problem which i faced after that; Please review it https://stackoverflow.com/q/56820304/11711773,, your assistance will be highly appreciated – Hassaan Khan Jun 29 '19 at 19:31