-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstt.py
57 lines (51 loc) · 1.8 KB
/
stt.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
import json
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
STT_NAME = "GOOGLE"
def stt_init():
"""
Initialize choosen STT.
Supports IBM Watson and Google Cloud.
Returns: stt (SpeechToText)
"""
if (STT_NAME == "IBM"):
f = open(join(dirname(__file__), "data/", "credentials/", "IBM_STT_key"), "r")
IMB_KEY = f.readline()
f.close()
f = open(join(dirname(__file__), "data/", "credentials/", "IBM_STT_url"), "r")
IMB_URL = f.readline()
f.close()
authenticator = IAMAuthenticator(IMB_KEY)
speech_to_text = SpeechToTextV1(authenticator=authenticator)
speech_to_text.set_service_url(IMB_URL)
return speech_to_text
elif (STT_NAME == "GOOGLE_CLOUD"):
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=join(dirname(__file__), "data/", "credentials/", "GOOGLE_STT_key.JSON")
elif (STT_NAME == "GOOGLE"):
pass
else:
print("ERROR - WRONG STT NAME")
def stt_transcript(stt, audioSource):
"""
Recognizes the voice to return a text.
Parameters: audioSource(Audio)
Returns: text (string)
"""
if (STT_NAME == "IBM"):
results = stt.recognize(audio=audioSource.get_wav_data(), content_type='audio/wav').get_result()
print(results)
r = ""
try:
r = results.get('results').pop().get('alternatives').pop().get('transcript')
pass
except:
print("ERROR")
pass
print(r)
return (r)
elif (STT_NAME == "GOOGLE_CLOUD"):
return stt.recognize_google_cloud(audioSource, language = 'en-US')
else:
print("ERROR - WRONG STT NAME")