-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspeech_to_text.py
More file actions
37 lines (27 loc) · 1.07 KB
/
speech_to_text.py
File metadata and controls
37 lines (27 loc) · 1.07 KB
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
import io
import os
# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
def change_speech_to_text(speech_sample):
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
file_name = os.path.join(os.path.dirname(__file__),'resources', 'audioSad.flac')
## file_name = speech_samples
# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz=44100,
language_code='en-US')
# Detects speech in the audio file
response = client.recognize(config, audio)
text_string = ""
for result in response.results:
print('Transcript: {}'.format(result.alternatives[0].transcript))
text_string = text_string + result.alternatives[0].transcript
return text_string