-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_text.py
56 lines (45 loc) · 1.52 KB
/
speech_text.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
from google.cloud import speech
from recording import *
#establishes client connection
client = speech.SpeechClient.from_service_account_file('key.json')
def d1(input):
trig = "give me directions to "
loc = input.lower().index(trig)
input = input[loc + len(trig) : len(input)]
return input
def d2(input):
trig = "give me directions from "
loc = input.lower().index(trig)
input = input[loc + len(trig) : len(input)]
input = input.split(" to ")
return input
def stt():
#sets file name
#if user say directions in this format "Give me directions to _____" or "Give me directions from _____ to _______"
file_name = audio()
#reads in the mp3 data
with open(file_name, 'rb') as i:
mp3_data = i.read()
#sets audio file and instantiates the recongition process
audio_file = speech.RecognitionAudio(content=mp3_data)
#sets speed and language for the voice
config = speech.RecognitionConfig(
sample_rate_hertz=44100,
enable_automatic_punctuation = True,
language_code = "en-US"
)
#finishes client session
response = client.recognize(
config = config,
audio = audio_file
)
origin = "" #raspberry pi location
destination = ""
if("give me directions to" in response.lower()):
destination = d1(response)
else:
o_words = d2(response)
origin = o_words[0]
destination = o_words[1]
out = [origin, destination]
return out