-
Notifications
You must be signed in to change notification settings - Fork 7
/
speech_to_text.py
40 lines (27 loc) · 1.02 KB
/
speech_to_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
import requests
import json
from Recorder import record_audio, read_audio
# Wit speech API endpoint
API_ENDPOINT = 'https://api.wit.ai/speech'
# Wit.ai api access token
wit_access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
def RecognizeSpeech(AUDIO_FILENAME, num_seconds = 5):
# record audio of specified length in specified audio file
record_audio(num_seconds, AUDIO_FILENAME)
# reading audio
audio = read_audio(AUDIO_FILENAME)
# defining headers for HTTP request
headers = {'authorization': 'Bearer ' + wit_access_token,
'Content-Type': 'audio/wav'}
# making an HTTP post request
resp = requests.post(API_ENDPOINT, headers = headers,
data = audio)
# converting response content to JSON format
data = json.loads(resp.content)
# get text from data
text = data['_text']
# return the text
return text
if __name__ == "__main__":
text = RecognizeSpeech('myspeech.wav', 4)
print("\nYou said: {}".format(text))