I'm trying to make a virtual assistant, right now it's suppost to just write down what I say. However when I try to test it it returns,
Traceback (most recent call last):
File "/Users/danieldossantos/Desktop/jarvis/chats/main.py", line 14, in
speech = r.recognize_google(audio, language = 'pt')
File "/Library/Python/2.7/site-packages/speech_recognition/init.py", line 858, in recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
speech_recognition.UnknownValueError
I've checked my code and I haven't found any errors, at least not that I know of,
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as s:
r.adjust_for_ambient_noise(s)
while True:
audio = r.listen(s)
speech = r.recognize_google(audio, language = 'pt')
print('VocĂȘ disse: ', speech)
yes, for me now its working. The problem was with audio ports, as most of our laptop have 2 ports :
1. audio out (Green colour)
2. microphone (pink colour)
You need to put your headphone jack in Audio out so that it can accept your speech as input.
As Your code is unable to get any input it returns error saying empty list []
get("alternative", [])) == 0.
import speech_recognition as sr
from os import walk
r = sr.Recognizer()
#optional
#r.energy_threshold = 300
def startConvertion(path = 'file.wav', lang = 'en-IN'):
with sr.AudioFile(path) as source:
#print('Fetching File')
audio_file = r.record(source)
print(r.recognize_google(audio_file, language=lang))
startConvertion()
Related
I have tried for numbers from 1 to 10 and it works well but i need it to work with all the numbers and it is not feasible to write the code for each number.
I also need it to work in sentences too which is not happening in my code.
Help me out guys, please....
This is my code....
import speech_recognition as sr
import time
t = ['one','two','three','four','five','six','seven','eight','nine','ten']
r = sr.Recognizer()
with sr.Microphone() as source:
print('Speak anything: ')
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print('You said : {} '.format(text))
time.sleep(1)
for i in range(0,10):
if (t[i] == text):
print('/n',i)
except:
print('Sorry could not recogonize your voice')
In case you don't want to take Vivek Mehta's suggestion and don't want an additional dependency, you can use a plain dictionary
import speech_recognition as sr
import time
t = {'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10}
r = sr.Recognizer()
with sr.Microphone() as source:
print('Speak anything: ')
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print('You said : {0} {1} '.format(text, t[text]))
time.sleep(1)
except:
print('Sorry could not recogonize your voice')
I have been working on an AI in PyCharm but and I have seem to have encountered an error with speech_recognition trying to call a method to try to get audio input:
/Users/waynedeng/Desktop/AI/venv/bin/python
/Users/waynedeng/Desktop/AI/dawg_2.0.py
Listening...
Traceback (most recent call last):
File "/Users/waynedeng/Desktop/AI/dawg_2.0.py", line 37, in <module>
input = read_input()
File "/Users/waynedeng/Desktop/AI/dawg_2.0.py", line 20, in read_input
audio = speech.listen(source=source, timeout=10, phrase_time_limit=5)
TypeError: listen() missing 1 required positional argument: 'self'
Process finished with exit code 1
I have tried to google my error but none of the solutions help my situation. Here is my code:
import speech_recognition as sr
import os
from playsound import playsound
import webbrowser
import random
speech = sr.Recognizer
speech.energy_threshold = 4000
greeting_dictionary = {'sup' : 'whats good','ay' : 'wassup'}
def play_sound(mp3_list):
mp3 = random.choice(mp3_list)
play_sound(mp3)
def read_input():
voice_text = ''
print('Listening...')
with sr.Microphone() as source:
audio = speech.listen(source=source, timeout=10, phrase_time_limit=5) #The error is here
try:
voice_text = speech.recognize_google(audio)
except sr.UnknownValueError:
pass
except sr.RequestError as e:
print('Network error')
except sr.WaitTimeoutError:
pass
return voice_text
if __name__ == '__main__':
playsound('mp3/dawg/greet.mp3')
while True:
input = read_input()
print('You: '.format(input))
if 'hello' in input:
continue
elif 'open' in input:
os.system('explorer ~/Desktop {}'.format(input.replace('Open ', '')))
elif 'bye' in input:
exit()
I have tried tackling the error for a week but I can't seem to fix this error
Instead of this
speech = sr.Recognizer
try this
speech = sr.Recognizer()
I am currently using google to transcribe speech to text. But it is too slow. I would like to use the google microphone search box output instead (since it is almost real time). I have been reading Selenium examples but can't find anything returning the voice search text.
My existing code:
import time, wave, pymedia.audio.sound as sound
import pyautogui
import pyaudio,os
import speech_recognition as sr
f1= wave.open( 'HelloWorld.wav', 'rb' )
sampleRate1= f1.getframerate()
channels1= f1.getnchannels()
format= sound.AFMT_S16_LE
snd1= sound.Output( sampleRate1, channels1, format )
s1= f1.readframes( 800000 )
def hello():
snd1.play( s1 )
def mainfunction(source):
audio = r.listen(source)
user = (r.recognize_google(audio, language = "en-us", show_all=False))
print(user)
if user == "hello world":
residential()
else:
pass
try:
if __name__ == "__main__":
r = sr.Recognizer()
with sr.Microphone() as source:
while 1:
r.pause_threshold = 0.5
r.energy_threshold = 150
r.adjust_for_ambient_noise(source, duration = 0.5)
mainfunction(source)
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 sleep
engine = pyttsx.init()
engine.setProperty('rate', 70)
r = sr.Recognizer()
def recognize(audio):
try:
return r.recognize(audio)
except LookupError, e:
print e
return ''
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?
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
I normally use global variables which are frowned upon but the following is correct I think? The following two def's should help...
# contains reusable print and speech
def output_modes(output):
engine = pyttsx3.init()
print(f"Output: {output}")
engine.say(output)
engine.runAndWait()
# contains reusable grabbing audio
def input_modes():
r1 = sr.Recognizer()
mic1 = sr.Microphone()
with mic1:
try:
output = r1.recognize_google(r1.listen(mic1))
output_modes()
except sr.UnknownValueError:
output = "Unknown Error M1"
output_modes()
except sr.RequestError as e:
output = "Error M2; {0}".format(e)
output_modes()
You should be able to write a While loop that can call on input_modes() to listen or output_modes to speak for example
def interact():
if input == 'Hello':
output = 'Hi there'
output_modes
import random
print((((random.randrange(1,12)//(((random.randrange(1,4)))))))+10)
print((((random.randrange(1,12)//(((random.randrange(1,4)))))))+10)
This is my code so far and it successfully generates two random numbers between the desired integers , now i need it to print in the format of,
Strength = 12
Stamina = 14
I tried like this:
import random
print ' strength = '((((random.randrange(1,12)//(((random.randrange(1,4)))))))+10)
print 'stamina ='((((random.randrange(1,12)//(((random.randrange(1,4)))))))+10)
But this returns an error for a reason unknown to me, I have been stuck on this for hours and I would really appreciate it if someone would help me with this problem, THANKS :)
print is a function in python3, use () to wrap your contents and , to separate them. And you don't need soooo many extra ()s:
In [40]: print('stamina =', random.randrange(1,12)//random.randrange(1,4)+10)
#stamina = 14
import speech_recognition as sr
import pyttsx3
listener = sr.Recognizer()
engine = pyttsx3.init()
engine.say('Iam your alexa')
engine.say('what can i do for you')
engine.runAndWait()
try:
with sr.Microphone() as source:
print('listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'alexa' in command:
print(command)
except:
pass