2017-11-25 183 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

它會打印什麼,但我不知道如何採取什麼說播放聲音。例如,我希望能夠說「播放按鈕」並讓它播放Pi上的Wave文件(Button.wav)。感謝您的幫助。 –

回答

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!非常感謝!!!謝謝謝謝!那麼,「if value.lower()」表示什麼?它看着python腳本的打印值嗎?非常感謝你的幫助! –

+0

value.lower()在字符串值中做小寫字母。比較'如果'播放按鈕'=='播放按鈕'是錯誤的。 'if'確實檢查字符串的值是否與'播放按鈕'相同。 @LauraJacob –

+1

非常感謝你的幫助!我衷心感謝! –