How can i make the python to wait till i complete speaking?

2024/11/18 20:17:35

I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.

import speech_recognition as sr
import webbrowser
import pyttsx
from time import sleepengine = pyttsx.init()
engine.setProperty('rate', 70)
r = sr.Recognizer()def recognize(audio):try:return r.recognize(audio)except LookupError, e:print ereturn ''
with sr.Microphone() as source:while True:engine.say("Hi How can i help you ?")sleep(0.15)print "Start Speaking"audio = r.listen(source)words = recognize(audio)print("You said " + words)if words == "Facebook":engine.say("Shall i open the Facebook page for you ?")engine.runAndWait()audio = r.listen(source)words = recognize(audio)if words == "Yes":webbrowser.open('https://www.facebook.com')elif words == "stop":break

Here I tried sleep also but before the engine speaks I can see the text Start Speaking getting printed. Instead of Sleep, is there any nice way to capture the speech in microphone and wait till say something or for a long silence?

Answer

This method:

        engine.runAndWait()

waits for speech to complete. You need to use it not just after engine.say("Shall i open the Facebook page for you ?"), but also after engine.say("Hi How can i help you ?")instead of sleep

https://en.xdnf.cn/q/120035.html

Related Q&A

Facing AttributeError: list object has no attribute lower

I have posted my sample train data as well as test data along with my code. Im trying to use Naive Bayes algorithm to train the model.But, in the reviews Im getting list of list. So, I think my code is…

why I cannot use max() function in this case? [duplicate]

This question already has answers here:Why do I get "TypeError: int object is not iterable" when trying to sum digits of a number? [duplicate](4 answers)Closed 1 year ago.n,m,k=map(int, inpu…

SQLALchemy and Python - Getting the SQL result

I am using cloudkitty which is rating module in OpenStacks.But here question is regarding the SQLAlchemy and Python.I am new to SQLAlchemy.I need to fetch some details from a table using a API call.So …

ValueError: invalid literal for int() with base 10: python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Closed 10 years ago.Questions asking for code must demonstrate a minimal understanding of the proble…

python code to connect to sftp server

I found this code to connect to remote sftp server with the help of username ,password and host but i also need to include the port number, can any one let em know how to include the port number in thi…

Python: get the max value with the location above and below than the max

If I have a dataframe like this, index User Value location1 1 1.0 4.5 2 1 1.5 5.23 1 3.0 7.04 1 2.5 7.55 2 1.0 11.56 2 1.…

Retrieve smart cards PAN with Python and pyscard

Im trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I can…

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

trying to write a Python function: def compare_lengths(x, y, z) which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length. For example, if the funct…

How to split a string in Python by 2 or 3, etc [duplicate]

This question already has answers here:Split string every nth character(21 answers)How to iterate over a list in chunks(40 answers)Closed 10 years ago.Does anyone know if its possible in python to spli…