1

I'm trying to make a script with speech recognition. So I have 3 files. 1 file is listening to what I'm saying and then printing it to a file. The other script is reading the file and answer depending on what the file is saying. The third script is just starting the 2 other scripts

1st script(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 script (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()

3rd script (start.py)

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

So when I start "start.py" It opens terminal, says " goodmorning master, im moas how can i help you, i did not understand". Then it just sits and wait for nothing. If I only run "speak.py" it says the same thing like above and then shuts down.

What I want the file to do is to check the file every 10th second to see if it has changed and then answer depending on what the file is saying.

Anyone got any ideas?

  • The answers [here](https://stackoverflow.com/questions/1703640/how-to-implement-a-pythonic-equivalent-of-tail-f) may be helpful. But why do both scripts open "test.out" for writing, and why does stt.py open it twice (without an intervening `.close()`)? – PM 2Ring Jul 18 '17 at 18:09

1 Answers1

1

I believe the problem is you forgot parentheses in the second script for engine.runandwait

Cary Shindell
  • 1,336
  • 8
  • 25
  • Good point, but they later call `engine.runAndWait()` anyway. – PM 2Ring Jul 18 '17 at 18:11
  • for 1 upvote, could you explain why it makes a difference? because it looks very much like a comment to me. – Jean-François Fabre Jul 18 '17 at 18:11
  • you need to have the parentheses after in order to call a method – Cary Shindell Jul 18 '17 at 18:12
  • Upvote nullified. Please refrain from posting one liners as answers. This isn't the first time you've done this. You should probably look at [answer]. – cs95 Jul 18 '17 at 18:14
  • Now I feel stupid.. Now she starts on the next line, but she still waits, But I find another problem. the text aren't written in the file until I close the script.. I tried added "f.close" in the "stt.py" but still doesn't work – ragefocker Jul 18 '17 at 18:16
  • @cᴏʟᴅsᴘᴇᴇᴅ why should i make the answer longer than necessary if the solution is simple? – Cary Shindell Jul 18 '17 at 18:17
  • @CaryShindell Because your solution didn't solve OP's problem. – cs95 Jul 18 '17 at 18:17
  • Pretty sure it did and they subsequently ran into another problem – Cary Shindell Jul 18 '17 at 18:18
  • nope, she just said "I" from the next line and then "speak.py" shuts down. – ragefocker Jul 18 '17 at 18:21
  • I could argue but it's clear you don't listen. Why don't you invest any time or effort into writing meaningful answers that actually solves OP's issue? (Which, you didn't by the way. His code is still hanging, because although you exposed one bug, you left out the other dozen). Cheers, I believe you'll do the right thing. Please look at where exactly OP is going wrong and help them out until their code works. That's the hallmark of a good answer (not that I'd know). – cs95 Jul 18 '17 at 18:24
  • I am currently researching to try to figure out the other bugs. I prefer to solve one bug at a time, and thought that I'd point that one at so they could see if fixing it would solve the problem. If not, I can continue to help them in the comments and/or edit answer – Cary Shindell Jul 18 '17 at 18:28
  • @ragefocker you say you want your program to check the file every 10 seconds. I don't see any such loop in the speak.py file though – Cary Shindell Jul 18 '17 at 18:29
  • sorry, I thought it fixed the problem but it didn't. I accidently wrote "python start.py" instead of just testing the speak script, which is why I thought it worked. My bad – ragefocker Jul 18 '17 at 18:30
  • So what is the problem now? Still the same? – Cary Shindell Jul 18 '17 at 18:32
  • What I need help with is to make speak.py check the output file like every 10th second or something, without shutting down + making stt.py print to the file without that I need to shut down the script. Currently, stt.py only prints to the output AFTER it shuts down – ragefocker Jul 18 '17 at 18:33
  • I think part of the problem is you don't close and reopen the files after writing to them. If you want to check every 10 seconds, you could do something like time.sleep(10) in a while loop – Cary Shindell Jul 18 '17 at 18:35