2016-01-04 14 views
6

私はPythonで音声を検出する方法を知っていますが、この質問はより具体的です: Pythonが単語を1つだけ聞いて、 。Pythonで音声認識で1単語を検出するには

私が知っている、私はPythonはすべての時間を聞いてみましょうし、その 擬似コードのようなものを作ることができます:

while True: 
    if stt.listen() == "keyword": 
     return True 

私はすでにそれをした、プログラムが(いつも聞いていくつかの分後にハングアップされます最後に見てください)。だから私は特定の単語を聞くしか方法が必要です。

"ハングアップ"とはどういう意味ですか?プログラムはクラッシュしていませんが、応答しません。もう私の声を聞いていないし、STRG + Cを押すと何もしない。

私はこのような何かを探しています:

while True: 
    if stt.waitFor("keyword"): 
     return True 

は、あなたが理解を願って、詳細については敬具

+0

speech_recognitionパッケージを参照してください。私はいくつかの技術的な細部を忘れてしまいました。 https://pypi.python.org/pypi/SpeechRecognition/ –

+0

リアルタイムまたはWAVファイルからですか? – erip

+0

それはマイクロホンを使用したリアルタイム処理です –

答えて

4
import sys, os 
from pocketsphinx.pocketsphinx import * 
from sphinxbase.sphinxbase import * 
import pyaudio 

modeldir = "../../../model" 
datadir = "../../../test/data" 

# Create a decoder with certain model 
config = Decoder.default_config() 
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us')) 
config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict')) 
config.set_string('-keyphrase', 'forward') 
config.set_float('-kws_threshold', 1e+20) 


p = pyaudio.PyAudio() 
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024) 
stream.start_stream() 

# Process audio chunk by chunk. On keyword detected perform action and restart search 
decoder = Decoder(config) 
decoder.start_utt() 
while True: 
    buf = stream.read(1024) 
    if buf: 
     decoder.process_raw(buf, False, False) 
    else: 
     break 
    if decoder.hyp() != None: 
     print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()]) 
     print ("Detected keyword, restarting search") 
     decoder.end_utt() 
     decoder.start_utt() 

は、Python 2.7でhttp://cmusphinx.sourceforge.net

+0

うわー、ありがとう、できるだけ早くそれを試してみましょう。 –

+0

申し訳ありませんが、Pocketsphinx、Sphinxbase、および依存関係はインストールできません。私たちを手伝ってくれますか? –

+0

確かに、何を試して、何がうまくいかなかったかを教えてください。 'pip install pocketsphinx'が動作するはずです。 –