Speech Recognition not recognizing what I say - python

My problem is really simple but has been bugging me since quite a while now. I just want my program to print what I said but all happens is that it shows Listening... and when I speak something, It shows Recognizing... and ends.
Here's my code:
import speech_recognition as sr
def listen():
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'You said: {query}')
except:
return ""
query = str(query)
return query.lower()
listen()
Please tell me where I am wrong or how I can fix this.

Related

I want to capturing only icmp packet but getting error only [duplicate]

Every time I run the code my code aimed at building a weak Ai platform I receive a AttributeError: 'NoneType' object has no attribute 'lower', and I have totally no clue why as it works fine in a tutorial i am following.can someone please walk me through fixing this as I am fairly new to python. Thanks
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pythoncom
print("Initializing Bot")
MASTER = "Bob"
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(text):
engine.say(text)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour <12:
speak("Good Morning" + MASTER)
elif hour>=12 and hour<18:
speak("Good Afternoon" + MASTER)
else:
speak("Good Evening" + MASTER)
speak("How may I assist you?")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try :
print("Recognizing...")
query = r.recognize_google(audio, language ='en-in')
print(f"user said: {query}\n")
except Exception as e:
print("Sorry i didn't catch that...")
speak("Initializing bot...")
wishMe()
query = takeCommand()
#Logic
if 'wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences =2)
print(results)
speak(results)
if 'open youtube' in query.lower():
webbrowser.open("youtube.com")
Alternatively the microphone also does not pick up an input, any ideas on why this is also the case?
The error is because the variable query is sometimes None. And you are applying .lower() function on it which only works on str type objects.
You can control this by putting your code inside a if loop which runs only when there's a string in query variable. Like this maybe:
wishMe()
query = takeCommand()
#Logic
if query:
if 'wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences =2)
print(results)
speak(results)
if 'open youtube' in query.lower():
webbrowser.open("youtube.com")
You functions are not returning anything. So for example:
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try :
print("Recognizing...")
query = r.recognize_google(audio, language ='en-in')
print(f"user said: {query}\n")
except Exception as e:
print("Sorry i didn't catch that...")
return query
Don't forget to mark my answer accepted if it helped you
I'm trying to help you, but I do not have the context from what are you trying to do but I will give you an example:
wishMe()
query = takeCommand()
#Logic
if query:
# an then you can check your condition
query.lower()
You need to add return type of takeCommand function... then
query=takeCommand
function can work otherwise it can back you a error just nonetype...
so, add return query in takeCommand function after try and except
I hope it will help you
Thanks

Faster speech recognition using python

I have been making a speech recognition and here is my code-
`import speech_recognition as sr
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 0.9
audio = r.listen(source,0,5)
try:
print("Recognizing...")
query = r.recognize_google(audio,language="en-in")
print(f"You Said : {query}")
except:
return ""
query = str(query)
return query.lower()`
This code works very accurately...but i want to make it faster. More like the speech recognition of google that shows the words as you speak. How do I do it ?
I tried the Watson speech to text of IBM but since its not free i cant apply it for a long term.

Keyboard key hold to run and release to pause the speech recognizer in python

I want hold key (eg. KeyA) to start and release key to pause
So basically when I hold KeyA I should be able to say "hello" and the speech_recognizer will run and as I release the KeyA it should pause the speech_recognizer but still able to speak the answer. speak() is text to speech
same like alexa on firestick tv remote we have to hold alexa button then speak and then release the button
HERE IS MY CODE
def takeCommand():
# speech recognizer
r = sr.Recognizer()
with sr.Microphone() as source:
print("Hearing...")
# r.pause_threshold = 1
r.chunk_size = 2048
r.adjust_for_ambient_noise(source, duration=0.5)
r.energy_threshold = 300
audio = r.listen(source)
try:
print("Recognizing....")
query = r.recognize_google(audio, language='en-in')
print(f"You said: {query}\n")
except Exception as e:
print(e) # to remove error from console remove this
print("Say that again please...")
return "None"
return query
pass
while True:
query = takeCommand().lower()
if 'hello' in query:
speak("Hello, how are you?")

Truncated speech-to-text output from wav file with SpeechRecognition recognize_google()

I have speech audio files in wav format that are 60 seconds each. However, the output gets truncated and only captures about 15% of the length. I have tried this both in my local Jupyter Notebook but also through Google Colab. According to the documentation, this request is below the threshold of the API. What am I doing wrong or how can I get around this limitation?
# select a recognizer session
# recognize_google() : Google Web Speech API
r = sr.Recognizer()
interview = sr.AudioFile('sample.wav')
with interview as source:
print('Ready...')
r.pause_threshold = 2
audio = r.record(source, duration=60)
type(audio)
transcription = r.recognize_google(audio, language='en_CA')
print(transcription)
Try to use this code and if output still same as old you can ident try and except block or change pause_threshold value
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile("sample.wav") as source:
print("Ready")
r.pause_threshold = 0.6
audio = r.record(source)
try:
s = r.recognize_google(audio)
print("Text: "+s)
except sr.UnknownValueError:
print("Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Error {0}".format(e))

How can I get the microphone input in python?

I want to make a Python program where microphone audio input is received.
I already tried pyaudio but I can't understand how it works.
There is this module called gTTS that you can use instead.
The get_audio function will be able to detect a users voice, translate the audio to text and return it to us. It will even wait until the user is speaking to start translating/recording the audio
Here's a complete example on Getting user input using the get_audio function.
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

Categories