2017-11-25 5 views
3

私はオーディオ制御の音楽プレーヤーをプログラミングすることでボタンに問題がある祖父のためのシンプルな音楽プレーヤーを作成しようとしています。私はPython 3とPocket SphinxでRaspberry Pi 3を使用しています。 Pocket Sphinxはインターネットを必要としないので、私の祖父はインターネットにアクセスできないので、私はそれを使用します。Python 3とPocket Sphinx

私の質問は、「再生ボタン」などの「言われた」値をとって、どのようにしてウェーブファイル「ボタン」を再生するのですか?ここで

は、私は基本的なプログラムを構築するために持っているものです。

import speech_recognition as sr 
import pygame 
from pygame import mixer 
mixer.init() 

r = sr.Recognizer() 
m = sr.Microphone() 

Button = pygame.mixer.Sound('/home/pi/Downloads/button8.wav') 

try: 
    print("A moment of silence, please...") 
    with m as source: r.adjust_for_ambient_noise(source) 
    print("Set minimum energy threshold to {}".format(r.energy_threshold)) 
    while True: 
     print("Say something!") 
     with m as source: audio = r.listen(source) 
     print("Got it! Now to recognize it...") 
     try: 
      # recognize speech using Sphinx 
      value = r.recognize_sphinx(audio) 
      print("You said {}".format(value)) #uses unicode for strings and this is where I am stuck 
      pygame.mixer.Sound.play(Button) 
      pygame.mixer.music.stop() 
     except sr.UnknownValueError: 
      print("Oops! Didn't catch that") 
     except sr.RequestError as e: 
      print("Uh oh! Couldn't request results; {0}".format(e)) 
except KeyboardInterrupt: 
    pass 

あなたが提供することができます任意の助けをありがとうございました。私は初心者なので、親切にしてください。

+0

あなたはどのようなエラーが出るのですか? –

+0

それは何が言われているかを印刷しますが、私はどのように音を再生すると言われているかわからない。たとえば、 "Play Button"と言うと、Pi(Button.wav)でWaveファイルを再生できるようにしたいと考えています。ご協力ありがとうございました。 –

答えて

1

「再生ボタン」にそれを比較してみてください。

# recognize speech using Sphinx 
value = r.recognize_sphinx(audio) 
print("You said {}".format(value)) 

if value.lower() == 'play button': 
    pygame.mixer.Sound.play(Button) 
    pygame.mixer.music.stop() 
+1

あなたはそれをElis Byberiさんにしました!どうもありがとうございます!!!ありがとうありがとう!では、 "value.lower()"が示す場合はどうなりますか?それはpythonスクリプトの印刷値を見ていますか?手伝ってくれてどうもありがとう! –

+0

value.lower()は値の文字列の小文字を処理しません。 'if 'Play Button' == 'play button''の比較はfalseです。 'if'は値の文字列が '再生ボタン'と同じかどうかをチェックします。 @LauraJacob –

+1

ありがとうございました!私は心から感謝しています! –

関連する問題