gpt4 book ai didi

python - 实时语音识别

转载 作者:行者123 更新时间:2023-12-03 14:29:29 27 4
gpt4 key购买 nike

我有一个 Python 脚本,使用 Speech_recognition 包来识别语音并返回所说内容的文本。然而,转录有几秒钟的延迟。是否有另一种方法可以编写此脚本以在说出每个单词时返回它?我有另一个脚本来执行此操作,使用 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)

最佳答案

如果您碰巧有一个支持 CUDA 的 GPU,那么您可以尝试 Mozilla 的 DeepSpeech GPU 库。如果您没有启用 CUDA 的 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

关于python - 实时语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47004955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com