How to add timer in speech input in python? - python

I want to add the timer for the 10sec audio input
import speech_recognition as
r= Recognizer()
with Microphone() as source:
while(i==0):
print('Say Something')
audio = r.listen(source)
query = r.recognize_google(audio)
print(query)

Related

No Response from gTTS program

I am trying to write a gTTS python program that converts text to speech and accepts commands that it is told, but when I run the code, I get no response at all, no errors in the terminal and no sound is played, I am not sure what to do and I am not seeing any errors in my program. I am using Sublime Text on MacOS. Please help!!
from gtts import gTTS
import os
import time
import playsound
import speech_recognition as sr
def speak(text):
tts = gTTS(text=text, Lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
if "hello" in text:
speak("hello, how are you?")
if "What is your name" in text:
speak("My name is John")
Try using the "pttsx3" library along side this.
Here is some sample code. This should take in commands and then respond back to you.
import pyttsx3
import speech_recognition as sr
import wikipedia
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language = 'en-in')
print(f"User said: {query}\n")
except:
print("Say that again please...")
return "None"
return query
if __name__ == "__main__":
while True:
query = takeCommand().lower()
# code for executing tasks based on the query or input
if 'wikipedia' in query:
speak("Searching Wikipedia...")
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences = 3)
speak("According to Wikipedia")
print(results)
speak(results)
if 'hi' in query:
speak('hello')

Python Speech_Recognition - "This audio source is already inside a context manager"

I am trying to run the speech recognition in the background on Linux machine, but it gives me an AssertionError saying "This audio source is already inside a context manager". Everything is working fine, when using it without threading.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import speech_recognition as sr
def speech_to_text(recognizer, audio):
try:
print(recognizer.recognize_google(audio, language="de"))
except sr.UnknownValueError:
print("[!] UnknownValueError")
except sr.RequestError as e:
print("RequestError: ", e)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.adjust_for_ambient_noise(source)
stop_listening = r.listen_in_background(source, speech_to_text)
#stop_listening()
#audio = r.listen(source)
#print(r.recognize_google(audio))
if __name__ == "__main__":
get_audio()
Thanks in advance!
The issue is with your instance of microphone before your context manager 'with'.
Try this
def get_audio():
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
print("Listening...")
r.adjust_for_ambient_noise(source)
try:
stop_listening = r.listen_in_background(source, speech_to_text)
except:
print('please say that again')
return get_audio()

Python speech recognition

When i run this code and speak script return empty list:
import pyaudio
import pyttsx3
import os
import pyautogui
import speech_recognition as sr
def command():
r = sr.Recognizer()
mic = sr.Microphone()
recog = sr.Recognizer()
with mic as u_audio:
print('Speak please')
r.adjust_for_ambient_noise(u_audio)
voice = r.listen(u_audio)
try:
listening = recog.recognize_google(voice, language = 'en-EN', show_all = True)
print(listening)
except Exception as e:
print('I not understand' + str(e))
command()
command()
output:
speak please
[ ]
i do not understand, why list are empty. Maybe i should choose microphone index.
The Microphone is the Problem
The problem should be possibly be on the Microphone, because I ran the same code, and it does give the Output, it gives all possible outcomes of the speech.
Anyways, it isn't efficient. Unwanted libraries slows it down, here is the updated code:
import pyttsx3
import speech_recognition as sr
def command():
r = sr.Recognizer()
mic = sr.Microphone()
recog = sr.Recognizer()
with mic as u_audio:
r.adjust_for_ambient_noise(u_audio)
print('Speak please')
voice = r.listen(u_audio)
try:
listening = recog.recognize_google(voice, language = 'en-EN', show_all = True)
print(listening)
except Exception as e:
print('I not understand' + str(e))
command()
command()
Comment down the result!

Running a functions for a specific time

I have a function that listens to my command and converts that to text, I want to create another function that runs the first functions for a given time and if I did not speak for that time, it closes the functions and moves on
Suppose the first function is named takeCommand and the second one as ListenandWait
import speech_recognition as sr
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening...')
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Recognizing...')
query = r.recognize_google(audio, language='en-in')
print('User Said: {}\n'.format(query))
except:
print('Say that again please...')
return 'None'
return query

How do i control when to stop the audio input?

I am using the SpeechRecognition Python package to get the audio from the user.
import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
This piece of code when executed starts listening for the audio input from the user. If the user does not speak for a while it automatically stops.
I want to know how can we get to know that it has stopped listening to audio?
How can I manually disable it ? I mean if i want to listen audio for 50 seconds and then stop listening to any further audio?
as the documentation specifies, recording stops when you exit out of with. you may print something after with to know that the recording has been stopped.
here's how you can stop recording after 50 seconds.
import speech_recognition as sr
recognizer = sr.Recognizer()
mic = sr.Microphone(device_index=1)
with mic as source:
recognizer.adjust_for_ambient_noise(source)
captured_audio = recognizer.record(source=mic, duration=50)
I think you need to read the library specifications; then, you can check that using record method instead of listen method is preferable to your application.
Late and not a direct answer, but to continuously record the microphone until the letter q is pressed, you can use:
import speech_recognition as sr
from time import sleep
import keyboard # pip install keyboard
go = 1
def quit():
global go
print("q pressed, exiting...")
go = 0
keyboard.on_press_key("q", lambda _:quit()) # press q to quit
r = sr.Recognizer()
mic = sr.Microphone()
print(sr.Microphone.list_microphone_names())
mic = sr.Microphone(device_index=1)
while go:
try:
sleep(0.01)
with mic as source:
audio = r.listen(source)
print(r.recognize_google(audio))
except:
pass

Categories