[英]Getting started with speech recognition and python
我想知道從哪里可以開始使用語音識別。 不是圖書館或任何相當“黑盒”的東西,而是我想知道我可以在哪里實際制作一個簡單的語音識別腳本。 我做了一些搜索,發現並不多,但我看到的是有“聲音”或音節的字典可以拼湊成文本。 所以基本上我的問題是我可以從哪里開始?
此外,由於這有點樂觀,我也可以在我的程序中使用一個庫(目前)。 我看到一些文本庫和 API 的語音只吐出一個結果。 這沒關系,但這將是不現實的。 我當前的程序已經檢查了輸入的任何文本的語法和所有內容,所以如果我要說,前十名來自語音到文本軟件的結果,而不是它可以檢查每一個並排除任何沒有意義的內容.
如果您真的想從頭開始理解語音識別,請為 python 尋找一個好的信號處理包,然后獨立於軟件閱讀語音識別。
但是語音識別是一個極其復雜的問題(基本上是因為我們說話時聲音會以各種方式相互作用)。 即使您從可以使用的最佳語音識別庫開始,您也絕不會發現自己無事可做。
因為谷歌關閉了它的平台
——
你可以使用https://pypi.python.org/pypi/pygsr
$> pip install pygsr
用法示例:
from pygsr import Pygsr
speech = Pygsr()
# duration in seconds
speech.record(3)
# select the language
phrase, complete_response = speech.speech_to_text('en_US')
print phrase
Pocketsphinx 也是一個不錯的選擇。 SWIG 提供了 Python 綁定,可以輕松集成到腳本中。
例如:
from os import environ, path
from itertools import izip
from pocketsphinx import *
from sphinxbase import *
MODELDIR = "../../../model"
DATADIR = "../../../test/data"
# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'hmm/en_US/hub4wsj_sc_8k'))
config.set_string('-lm', path.join(MODELDIR, 'lm/en_US/hub4.5000.DMP'))
config.set_string('-dict', path.join(MODELDIR, 'lm/en_US/hub4.5000.dic'))
decoder = Decoder(config)
# Decode static file.
decoder.decode_raw(open(path.join(DATADIR, 'goforward.raw'), 'rb'))
# Retrieve hypothesis.
hypothesis = decoder.hyp()
print 'Best hypothesis: ', hypothesis.best_score, hypothesis.hypstr
print 'Best hypothesis segments: ', [seg.word for seg in decoder.seg()]
# Access N best decodings.
print 'Best 10 hypothesis: '
for best, i in izip(decoder.nbest(), range(10)):
print best.hyp().best_score, best.hyp().hypstr
# Decode streaming data.
decoder = Decoder(config)
decoder.start_utt('goforward')
stream = open(path.join(DATADIR, 'goforward.raw'), 'rb')
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf, False, False)
else:
break
decoder.end_utt()
print 'Stream decoding result:', decoder.hyp().hypstr
我知道這個問題很舊,但只適用於未來的人:
我使用speech_recognition
-Module 並且我喜歡它。 唯一的問題是,它需要互聯網,因為它使用谷歌來識別語音。 但這在大多數情況下應該不是問題。 識別工作幾乎完美。
編輯:
speech_recognition
包不僅可以使用 google 進行翻譯,還可以使用 CMUsphinx(允許離線識別)等。 唯一的區別是識別命令的細微變化:
https://pypi.python.org/pypi/SpeechRecognition/
這是一個小代碼示例:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source: # use the default microphone as the audio source
audio = r.listen(source) # listen for the first phrase and extract it into audio data
try:
print("You said " + r.recognize_google(audio)) # recognize speech using Google Speech Recognition - ONLINE
print("You said " + r.recognize_sphinx(audio)) # recognize speech using CMUsphinx Speech Recognition - OFFLINE
except LookupError: # speech is unintelligible
print("Could not understand audio")
只有一件事對我來說效果不佳:無限循環聆聽。 幾分鍾后它掛斷了。 (它沒有崩潰,只是沒有響應。)
編輯:如果您想使用沒有無限循環的麥克風,您應該指定錄音長度。 示例代碼:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak:")
audio = r.listen(source, None, "time_to_record") # recording
對於那些想深入了解 Python 語音識別主題的人,這里有一些鏈接:
這可能是要學習的最重要的東西:信號處理的基本概念,尤其是數字信號處理 (DSP)。 對抽象概念的一點理解將使您為 scipy.signal 中令人眼花繚亂的工具做好准備。
首先是模數轉換(ADC)。 這確實屬於音頻工程領域,現在是錄音過程的一部分,即使您所做的只是將麥克風連接到計算機上。
如果您從模擬錄音開始,這可能是將舊磁帶或黑膠長播放記錄轉換為數字形式或從舊錄像帶中提取音頻的問題。 最簡單的方法是將源播放到計算機的音頻輸入插孔中,並使用內置硬件和軟件將原始線性脈沖編碼調制 (LPCM) 數字信號捕獲到文件中。 你提到的 Audacity 是一個很好的工具,還有更多。
傅立葉變換是你的朋友。 在數據科學術語中,它非常適合特征提取和特征空間降維,特別是如果您正在尋找跨越整個樣本過程中聲音變化的特征。 這里沒有空間解釋,但對於機器學習算法來說,時域中的原始數據比頻域中的原始數據更難處理。
特別是您將使用快速傅立葉變換 (FFT),這是離散傅立葉變換 (DFT) 的一種非常有效的形式。 現在 FFT 通常是在 DSP 硬件中完成的。
import speech_recognition as SRG
import time
store = SRG.Recognizer()
with SRG.Microphone() as s:
print("Speak...")
audio_input = store.record(s, duration=7)
print("Recording time:",time.strftime("%I:%M:%S"))
try:
text_output = store.recognize_google(audio_input)
print("Text converted from audio:\n")
print(text_output)
print("Finished!!")
print("Execution time:",time.strftime("%I:%M:%S"))
except:
print("Couldn't process the audio input.")
這應該有效。 您會將來自默認麥克風的音頻輸入保存為 text_output 變量中的文本形式。 您可以查看此鏈接以獲取更多信息: https : //www.journaldev.com/37873/python-speech-to-text-speechrecognition
我們基本上做的是首先從麥克風錄制音頻,然后我們使用該音頻作為語音識別器的輸入。 唯一的問題是您需要有效的互聯網連接和這兩個必需的 Python 庫speech_recognition
和pyaudio
。
這是使用離線庫在 python 中開始語音識別的簡單方法:-
SpeechRecognition
谷歌 APi:-
recognize_google()
要求:-
1-Python 3.9
2-Anaconda (Launch Jupyter)
在代碼中
步驟1:-
pip install pyaudio
pip install --upgrade pyaudio
pip install speechrecognition
pip install wheel
pip install google-api-python-client
pip install monotonic
第2步:-
import speech_recognition as sr
r = sr.Recognizer()
第 3 步:-使用默認麥克風作為音頻源
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration = 5)
audio = r.listen(source)
第4步:-
print("Speak Anything :")
try:
text = r.recognize_google(audio)
print("You said : {}".format(text))
except:
print("Sorry could not recognize what you said")
第 5 步:-
當上面的代碼運行完美時:但給出緩慢或有時空白的響應然后添加幾行: -
r.pause_threshold = 1
完整代碼
pip install pyaudio
pip install --upgrade pyaudio
pip install speechrecognition
pip install wheel
pip install google-api-python-client
pip install monotonic
*Here is the code*
import speech_recognition as sr
def recog():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say Something")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
print("Recognizing..")
text = r.recognize_google(audio, language='en-in') # Specify Language Code
print("You said {}".format(text))
except Exception as e:
print(e)
print("Sorry")
recog()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.