实时语音识别

14

我有一个使用speech_recognition软件包的Python脚本,用于识别语音并返回所说内容的文本。不过转录会有几秒钟的延迟。是否有另一种编写此脚本的方法可以在每个单词被说出时即刻返回?我有另一个使用pysphinx软件包来实现此功能的脚本,但结果不准确。

安装依赖项:

pip install SpeechRecognition
pip install pocketsphinx

脚本1 - 延迟的语音转文字:

import speech_recognition as sr  

# obtain audio from the microphone  
r = sr.Recognizer()  
with sr.Microphone() as source:  
    print("Please wait. Calibrating microphone...")  
    # listen for 5 seconds and create the ambient noise energy level  
    r.adjust_for_ambient_noise(source, duration=5)  
    print("Say something!")  
    audio = r.listen(source)  

    # recognize speech using Sphinx  
    try:  
        print("Sphinx thinks you said '" + r.recognize_sphinx(audio) + "'")  
    except sr.UnknownValueError:  
        print("Sphinx could not understand audio")  
    except sr.RequestError as e:  
        print("Sphinx error; {0}".format(e))

脚本2-即时但不太准确的语音转文本:

import os
from pocketsphinx import LiveSpeech, get_model_path

model_path = get_model_path()
speech = LiveSpeech(
    verbose=False,
    sampling_rate=16000,
    buffer_size=2048,
    no_search=False,
    full_utt=False,
    hmm=os.path.join(model_path, 'en-us'),
    lm=os.path.join(model_path, 'en-us.lm.bin'),
    dic=os.path.join(model_path, 'cmudict-en-us.dict')
)
for phrase in speech:
    print(phrase)

你很可能是在像树莓派这样的设备上运行此程序,而这种设备并不足够强大,无法运行具有大词汇量连续语音识别和大型词典的应用。 - Nikolay Shmyrev
如果你监听1秒钟然后打印单词,可能会有一些丢失,但它将按单词返回,这样可以吗? - Aqua 4
1
你确定两个系统都使用相同的语言模型吗? - SuperKogito
1个回答

2

如果你恰好拥有CUDA-enabled GPU,那么你可以尝试使用Mozilla的DeepSpeech GPU库。如果你没有CUDA-enabled GPU,他们也有一个CPU版本。 在CPU上,使用DeepSpeech转录音频文件的时间为1.3倍,而在GPU上,速度为0.3倍,即它可以在0.33秒内转录1秒的音频文件。 快速入门:

# Create and activate a virtualenv
virtualenv -p python3 $HOME/tmp/deepspeech-gpu-venv/
source $HOME/tmp/deepspeech-gpu-venv/bin/activate

# Install DeepSpeech CUDA enabled package
pip3 install deepspeech-gpu

# Transcribe an audio file.
deepspeech --model deepspeech-0.6.1-models/output_graph.pbmm --lm deepspeech- 
0.6.1-models/lm.binary --trie deepspeech-0.6.1-models/trie --audio audio/2830- 
3980-0043.wav

一些重要的注意事项- Deepspeech-gpu 有一些依赖项,如tensorflow、CUDA、cuDNN等。因此,请查看它们的github repo以获取更多详细信息- https://github.com/mozilla/DeepSpeech


那么与硬件无关的东西呢? - Damian-Teodor Beleș
@Damian-TeodorBeleș,您能否详细说明一下?我不确定您在问什么。 - Glitch101
1
如果这个条件不成立怎么办: “如果你恰好拥有一个CUDA启用的GPU,那么你可以尝试使用Mozilla的DeepSpeech。”? - Damian-Teodor Beleș
DeepSpeech也可以在CPU上运行。只是在GPU上进行推理比在CPU上更快。除此之外,都是一样的。 - Glitch101
1
好的,我明白了,谢谢。但是在“音频文件”中,“live”部分是什么? - Damian-Teodor Beleș

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接