-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.py
39 lines (27 loc) · 1.16 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
import six
import os
import io
from google.cloud import speech
credential_path = "creds.json"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credential_path
# for result in response.results:
# print("Transcript: {}".format(result.alternatives[0].transcript))
async def transcribe_streaming(ctx,stream_file):
"""Transcribe the given audio file."""
from google.cloud import speech
import io
client = speech.SpeechClient()
with io.open(stream_file, "rb") as audio_file:
content = audio_file.read()
audio = speech.RecognitionAudio(content=content)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code="en-US",
)
response = client.recognize(config=config, audio=audio)
# Each result is for a consecutive portion of the audio. Iterate through
# them to get the transcripts for the entire audio file.
for result in response.results:
# The first alternative is the most likely one for this portion.
print(u"Transcript: {}".format(result.alternatives[0].transcript))