/
SpeechRecognitionLoop.py
50 lines (42 loc) · 1.56 KB
/
SpeechRecognitionLoop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import speech_recognition as sr
import webbrowser
import subprocess
import string
listen = True
while listen == True:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Di algo!")
audio = r.listen(source)
try:
print("He entendido " + r.recognize_google(audio))
except sr.UnknownValueError:
print("No te he entendido bien")
except sr.RequestError as e:
print("No hemos podido conectar con el servicio de Google; {0}".format(e))
# Wheater
if "time" in r.recognize_google(audio):
r1 = sr.Recognizer()
url1 = 'https://www.eltiempo.es/'
with sr.Microphone() as source:
print('¿De que ciudad? ')
audio1 =r1.listen(source)
try:
print("Google Speech recognition thinks you said " + r1.recognize_google(audio) + webbrowser.open_new(url1+r1.recognize_google(audio1)))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio.")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition.")
# Youtube
if "video" in r.recognize_google(audio):
r2 = sr.Recognizer()
url2 = 'https://youtube.com/results?search_query='
with sr.Microphone() as source:
print ("Dime que quieres ver ")
audio2 =r2.listen(source)
try:
print("Google Speech recognition thinks you said " + r2.recognize_google(audio2) + webbrowser.open_new(url2+r2.recognize_google(audio2)))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio.")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition.")