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!
Related
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')
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()
I have a problem that when I use SpeechRecognition library in python and when I want to put r.listen(source)in while loop my code drasticly slows down.
import speech_recognition as sr
import pyttsx3 as p
import random
from talk import *
r = sr.Recognizer()
engine = p.init()
with sr.Microphone() as source:
while 0<1:
text = r.listen(source)
try:
recognised_text = r.recognize_google(text)
print(recognised_text)
if (recognised_text =="hello" or recognised_text =="hi"):
greeting()
except sr.UnknownValueError:
print("")
except sr.RequestError as e:
print("")
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
Hi guys I'm trying to trigger the while loop in my code that starts speech recognition whenever the hotword "virgo" is said. The problem is that snowboy detects the hotword but I don't know how to execute the "while" loop once the hotword is triggered. Any help please? this may sound stupid and should be relatively easy but my brain is on fire right now. thank you!
import speech_recognition as sr
from textblob import TextBlob
import snowboydecoder
recognizer_instance = sr.Recognizer()
def detected_callback():
print ("tell me!")
detector = snowboydecoder.HotwordDetector("virgo.pmdl",sensitivity=0.5)
detector.start(detected_callback=snowboydecoder.play_audio_file,sleep_time=0.03)
detector.terminate()
while True:
with sr.Microphone() as source:
recognizer_instance.adjust_for_ambient_noise(source)
print("Listening...")
audio = recognizer_instance.listen(source)
print("copy that!")
try:
text = recognizer_instance.recognize_google(audio, language = "it-IT")
print("you said:\n", text)
except Exception as e:
break
Your while loop is always 'triggered', given the TRUE, until you break out of it. To trigger the code within the loop, do, for example:
while True:
if YOUR_TRIGGER_CONDITION:
with sr.Microphone() as source:
recognizer_instance.adjust_for_ambient_noise(source)
print("Listening...")
audio = recognizer_instance.listen(source)
print("copy that!")
try:
text = recognizer_instance.recognize_google(audio, language = "it-IT")
print("you said:\n", text)
except Exception as e:
break