【问题标题】:Runtime error when using grammars with the SpeechRecognition package将语法与 SpeechRecognition 包一起使用时出现运行时错误
【发布时间】:2018-04-09 20:07:28
【问题描述】:

所以我尝试使用 speech_recognition.recognize_sphinx() 的语法,但是,我收到以下错误:

RuntimeError: Decoder_set_fsg returned -1

这是我的代码:

Main.py

# Dependencies:
import speech_recognition as sr

# Collect audio sample
r = sr.Recognizer()
print('Please say "perquisition":')
with sr.Microphone() as source:
    audio_en = r.listen(source)
print('Processing...')

# Attempt to convert the speech to text
try:
    print(r.recognize_sphinx(audio_en, grammar='perquisition.gram'))
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))

perquisition.gram

#JSGF V1.0;
grammar perquisition;
// Grammar rule names should be [a-zA-Z0-9] only!
public <perquisition> = ( perquisition );

有什么想法吗?

【问题讨论】:

    标签: python speech-recognition grammar cmusphinx


    【解决方案1】:

    这里发生的一些事情掩盖了潜在的错误。 speech_recognition.recognize_sphinx() 只是一些 CMUsphinx 命令的包装器,可以在第 746 行找到 here。这个确切的问题有点混乱,所以我们将重点放在下面代码的 sn-p 上:

    # Dependencies
    import speech_recognition as sr
    import os
    import pocketsphinx as ps
    
    # Manually point to the grammar file
    grammar = 'search.gram'
    try:
        # Point to the model files
        language_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pocketsphinx-data", "en-US")
        acoustic_parameters_directory = os.path.join(language_directory, "acoustic-model")
        language_model_file = os.path.join(language_directory, "language-model.lm.bin")
        phoneme_dictionary_file = os.path.join(language_directory, "pronounciation-dictionary.dict")
    
        # Create a decoder object with our custom parameters
        config = ps.Decoder.default_config()
        config.set_string("-hmm",
                          acoustic_parameters_directory)  # set the path of the hidden Markov model (HMM) parameter files
        config.set_string("-lm", language_model_file)
        config.set_string("-dict", phoneme_dictionary_file)
        config.set_string("-logfn", os.devnull)  # <--- Prevents you from seeing the actual bug!!!
        decoder = ps.Decoder(config)
    
        # Convert grammar
        grammar_path = os.path.abspath(os.path.dirname(grammar))
        grammar_name = os.path.splitext(os.path.basename(grammar))[0]
        fsg_path = "{0}/{1}.fsg".format(grammar_path, grammar_name)
        if not os.path.exists(fsg_path):  # create FSG grammar if not available
            jsgf = ps.Jsgf(grammar)
            rule = jsgf.get_rule("{0}.{0}".format(grammar_name))
            fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5)
            fsg.writefile(fsg_path)
            print('Successful JSFG to FSG conversion!!!')
    
        # Pass the fsg file into the decoder
        decoder.set_fsg(grammar_name, fsg)  # <--- BUG IS HERE!!!
    
    except Exception as e:
        print('Ach no! {0}'.format(e))
    finally:
        os.remove('search.fsg')  # Remove again to help prove that the grammar to fsg conversion isn't at fault
    

    运行该代码,我们发现错误弹出的那一行,而且日志信息已被关闭!使用它,大量文本会被转储到终端中,这可能会很麻烦。在这种情况下,将它们重新打开以发现...

    ...
    ERROR: "fsg_search.c", line 141: The word 'perquisition' is missing in the dictionary
    ...
    

    现在我们正在取得进展。这给我们留下了两种选择之一。首先,我们可以扫描字典(pocketsphinx.get_model_path()+'/cmudict-en-us.dict' 或类似的)来确定一个单词是否存在。然后我们可以决定是简单地忽略这个词,还是将它添加到字典中。

    添加到字典不一定是直截了当的...根据它与字典中其他单词的相似程度,您可能能够侥幸逃脱。否则,您还必须重新训练模型。可以在here 找到有关如何执行此操作的更好的解释。享受吧。

    【讨论】:

      猜你喜欢
      • 2017-10-25
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 2023-03-22
      • 2020-02-23
      • 2021-06-03
      相关资源
      最近更新 更多