-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeech.py
340 lines (254 loc) · 10.9 KB
/
speech.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf8 -*-
# coding : utf8
# Tested with python2.7 on raspberry PI 3 the 21/05/2018
# Tested with Python2.7 on Ubuntu the 19 mai 2018
# When the button is pressed it takes a picture and then speak itusing google speech engine
#
# $sudo pip install pyowm
# $sudo pip2 install pytesseract
# $sudo apt-get install tesseract-ocr
# $pip instal SpeechRecognition
# $sudo pip install gTTS
# install pa_stable_v190600_20161030.tgz : ./configure && sudo make install
# install PyAudio-0.2.11.tar.gz : sudo python setup.py install
# $sudo apt install libasound-dev portaudio19-dev libportaudio2 libportaudiocpp0 ffmpeg libav-tools ???
#
# https://openclassrooms.com/forum/sujet/installation-pyaudio?page=1
# With pa_stable_v190600_20161030.tgz
#
# React to : 'hello' 'date' 'time' 'weather' 'email' 'events
#
# $sudo apt-get install flac
#
# $jackd -r -d alsa 44100
from __future__ import print_function
from googleapiclient import discovery
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from gtts import gTTS
import time
import datetime
import pyowm
import os
import sys
import speech_recognition as sr
import pyowm
import RPi.GPIO as GPIO
###############################################################################
## React to vocal commands : 'date', "météo", "médicaments", "mail", "événements"
###############################################################################
def main_loop():
global r
global source
r = sr.Recognizer()
with sr.Microphone(device_index=1) as source:
r.adjust_for_ambient_noise(source, duration = 1)
r.energy_threshold =1000
print("Say something !")
audio = r.listen(source,None,2)
print("Trying to recognize audio")
try:
t=r.recognize_google(audio, language='fr_FR')
print ("You just said : " +t)
if(t.find("date")!=-1):
print("=>date")
speak_date()
elif(t.encode("utf-8").find("météo")!=-1):
print(u"=>météo")
speak_weather()
elif(t.encode("utf-8").find("médicament")!=-1):
print(u"=>médicaments")
say_something(u"N oubliez pas de prendre votre doliprane à 18 heure")
elif(t.find("mail")!=-1):
print(u"=>mail")
speak_email()
elif(t.encode("utf-8").find("événement")!=-1): # évênement !!! Souci !
print(u"=>évènements")
speak_calendar()
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
say_something(u"Merci de répété votre demande")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
###############################################################################
###############################################################################
def get_vocal_command():
global r
global source
print("Say something !")
audio = r.listen(source,None,2)
try:
t=r.recognize_google(audio, language='fr_FR')
print ("You just said : " +t)
return t
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
say_something(u"Merci de répété votre demande")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
###############################################################################
###############################################################################
def say_something(msg):
print("=>say_something() : " + msg)
tts = gTTS(text=msg , lang='fr')
tts.save("result.mp3")
os.system("mpg123 result.mp3")
###############################################################################
###############################################################################
def speak_date():
# Get local date and time
date = datetime.datetime.now()
# Speak the date and time
msg_to_say = "Nous sommes le " + str(date.day) + " " + str(date.month) + " " + " deux mille dix huit " #str(date.year)
msg_to_say += " Il est " + str(date.hour) + " heure et " + str(date.minute) + " minutes"
say_something(msg_to_say)
###############################################################################
# It is possible to give more informations from retrieved object.
# Cf. https://github.com/csparpa/pyowm/blob/master/pyowm/docs/usage-examples.md
###############################################################################
def speak_weather():
# Initialize API access
owm = pyowm.OWM('38e32c9eea23385bc6fa2e452f821eb5')
# Get current weather in Paris
observation = owm.weather_at_place('Paris,FR')
w = observation.get_weather()
# Weather details
wi=w.get_wind()
h=w.get_humidity()
t=w.get_temperature('celsius')
# Speak the weather report
msg_to_say = u"Il fait " + str(t['temp']) + u" degrés et le niveau d'humidité est " + str(h)
msg_to_say += u" pour cent. La vitesse du vent est " + str(wi['speed']) + u" kilomètre heure "
say_something(msg_to_say)
###############################################################################
def speak_calendar():
# Setup the Calendar API
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
store = file.Storage('credentials_cal.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('calendar', 'v3', http=creds.authorize(Http()))
# Call the Calendar API to get 5 events
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
events_result = service.events().list(calendarId='primary', timeMin=now, maxResults=5, singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
# Reading the events
for event in events:
start_date = event['start'].get('dateTime', event['start'].get('date'))
print(start_date, event['summary'])
try :
start_date_troncated = start_date[0:-6]
eventdate = time.strptime(start_date_troncated, '%Y-%m-%dT%H:%M:%S')
except Exception as e:
#print("No hour")
eventdate = time.strptime(start_date, '%Y-%m-%d')
msg_to_say = u" " + str(event['summary']) + " le " + str(eventdate.tm_mday) + " " + str(eventdate.tm_mon) + " deux mille dix huit " # + str(eventdate.tm_year)
say_something(msg_to_say)
# Adding the taxi feature
msg_to_say = u"Voulez vous commander un taxi ?"
say_something(msg_to_say)
vocal_command = get_vocal_command()
if(vocal_command == None):
return
if(vocal_command.find("oui")!=-1):
print(u"=>Commander un taxi")
msg_to_say = u"Très bien. Une demande de taxi a été faite."
say_something(msg_to_say)
elif(vocal_command.find("non")!=-1):
print(u"=>Ne pas commander de taxi")
msg_to_say = u"D'accord."
say_something(msg_to_say)
###############################################################################
def GetMessage(service, user_id, msg_id):
"""Get a Message with given ID.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
msg_id: The ID of the Message required.
Returns:
A Message.
"""
try:
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
return message
except Exception as e:
print('Exception' + str(e))
###############################################################################
def ListMessagesWithLabels(service, user_id, label_ids=[]):
"""List all Messages of the user's mailbox with label_ids applied.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
label_ids: Only return Messages with these labelIds applied.
Returns:
List of Messages that have all required Labels applied. Note that the
returned list contains Message IDs, you must use get with the
appropriate id to get the details of a Message.
"""
try:
response = service.users().messages().list(userId=user_id, labelIds=label_ids).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
msg_cmpt = 0
for msg_id in response['messages']:
msg_cmpt += 1
current_msg = GetMessage(service, user_id, msg_id['id'])
print ('Message %d : %s\n' % (msg_cmpt, current_msg['snippet']))
# Read the message
msg_to_say = u" " + current_msg['snippet']
say_something(msg_to_say)
msg_to_say = u"Voulez vous lire le message suivant ?"
say_something(msg_to_say)
vocal_command = get_vocal_command()
if(vocal_command == None):
return
if(vocal_command.find("non")!=-1):
msg_to_say = u"D'accord."
say_something(msg_to_say)
return
except Exception as e:
print('Exception' + str(e))
###############################################################################
def ListMessagesLabels(service, user_id, label_ids=[]):
labels = results.get('labels', [])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name'])
###############################################################################
def speak_email():
# Setup the Gmail API
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
store = file.Storage('credentials_email.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
creds = tools.run_flow(flow, store)
# Call the Gmail API
service = build('gmail', 'v1', http=creds.authorize(Http()))
ListMessagesWithLabels(service, 'me', 'UNREAD')
#results = service.users().labels().list(userId='me').execute()
############################################################################
def list_microphones():
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for 'Microphone(device_index={0})'".format(index, name))
print("\n\n")
############################################################################
if __name__ == "__main__":
say_something("ceci est un test")
# initialize Button
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# main loop
while 1:
bouton = GPIO.input(3)
if bouton==0 :
main_loop()