2017-07-18 95 views
1

我试图做一个脚本语音识别。所以我有3个文件。 1文件正在听我在说什么,然后将其打印到文件中。另一个脚本正在读取文件并根据文件说的内容进行回答。 第三个脚本是刚刚开始2个其他脚本python脚本不等待输入文件

1脚本(stt.py):

import os 
import pyttsx 
import sys 
from pocketsphinx import LiveSpeech, get_model_path 
engine = pyttsx.init() 
voices = engine.getProperty('voices') 

engine.setProperty('voice', 'english+f3') 

f = open("test.out", 'w') 
sys.stdout = f 


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') 
) 

f = open("test.out", 'w') 
sys.stdout = f 
for phrase in speech: 
    print (phrase) 
    f.write ("end") 

2ST脚本(speak.py):

import pyttsx 
import sys 

engine = pyttsx.init() 
voices = engine.getProperty('voices') 

f = open("test.out", 'w') 
sys.stdout = f 

volume = engine.getProperty('volume') 
engine.setProperty('voice', 'english+f3') 
engine.setProperty('volume', volume-0.10) 
engine.say("good morning master, I'm Moas. How can I help you?") 
engine.runAndWait 



if 'start' in open('test.out').read(): 
    engine.say("Hello Admin") 
else: 
    engine.say("I did not understand") 
engine.runAndWait() 

第三脚本(启动。 PY)

execfile("speak.py") 
execfile("stt.py") 

所以当我开始 “start.py” 它打开终端,说“早安的主人,我不知道怎么帮助你,我不明白。“ 然后它只是坐着等一下。如果我只运行“speak.py”,它就会像上面那样说,然后关闭。

我想让文件做的事情是每10秒检查一次文件,看看它是否发生了变化,然后根据文件说的来回答。

任何人有任何想法?

+0

的答案[这里](https://stackoverflow.com/questions/1703640/how-to-implement-a-pythonic-equivalent -of-tail-f)可能会有所帮助。但为什么两个脚本都打开“test.out”来写,为什么stt.py打开它两次(没有介入'.close()')? –

回答

1

我相信问题是你忘了在第二个脚本括号engine.runandwait

+0

好点,但他们以后再调用'engine.runAndWait()'。 –

+0

1 upvote,你能解释为什么它有所作为?因为它看起来非常像对我的评论。 –

+0

你需要在后面有括号才能调用方法 –