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

非常感谢你的帮助!我衷心感谢! –