【问题标题】:Python 3 and Pocket SphinxPython 3 和袖珍狮身人面像
【发布时间】:2017-11-25 20:30:35
【问题描述】:

我正在尝试通过对音频控制的音乐播放器进行编程来为我的祖父创建一个简单的音乐播放器,他的按钮有问题。我正在使用带有 Python 3 和 Pocket Sphinx 的 Raspberry Pi 3。因为 Pocket Sphinx 不需要 Internet,所以我会使用它,因为我的祖父无法访问 Internet。

我的问题是如何获取“说”的值,例如:“播放按钮”并让它播放波形文件“按钮”?

这是我必须构建的基本程序:

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

非常感谢您提供的任何帮助。请善待,因为我是初学者。

【问题讨论】:

  • 你得到什么错误?
  • 它会打印所说的内容,但我不知道如何将所说的内容播放声音。例如,我希望能够说“播放按钮”并让它播放 Pi 上的 Wave 文件 (Button.wav)。感谢您的帮助。

标签: python audio raspberry-pi pocketsphinx


【解决方案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()

【讨论】:

  • 你做到了 Elis Byberi!非常感谢!!!谢谢谢谢!那么,“if value.lower()”是什么意思呢?它是在查看 python 脚本上的打印值吗?非常感谢您的帮助!
  • value.lower() 在值中使用小写的字符串。比较 if 'Play Button' == 'play button' 是错误的。 if 确实检查值中的字符串是否与“播放按钮”相同。 @劳拉雅各布
  • 非常感谢您的帮助!我真诚地感谢它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多