import sys
sys.path.insert(0, '/home/ons21553/wspace/interview-transcripts/src')
from common import *
%load_ext autoreload
%autoreload 2
The autoreload extension is already loaded. To reload it, use: %reload_ext autoreload
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
from common import *
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = from_root('dcm-iw-transcripts-db3f1fa338cc.json')
# Instantiates a client
client = speech.SpeechClient()
import recordings.harvard as hv
r = hv.load_all()[0]
with io.open(r.audio_fpath, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
language_code='en-US')
# Detects speech in the audio file
response = client.recognize(config, audio)
r.structured_transcript
text | speaker | from | end | |
---|---|---|---|---|
0 | The small pup gnawed a hole in the sock. | NaN | NaN | NaN |
1 | The fish twisted and turned on the bent hook. | NaN | NaN | NaN |
2 | Press the pants and sew a button on the vest. | NaN | NaN | NaN |
3 | The swan dive was far short of perfect. | NaN | NaN | NaN |
4 | The beauty of the view stunned the young boy. | NaN | NaN | NaN |
5 | Two blue fish swam in the tank. | NaN | NaN | NaN |
6 | Her purse was full of useless trash. | NaN | NaN | NaN |
7 | The colt reared and threw the tall rider. | NaN | NaN | NaN |
8 | It snowed, rained, and hailed the same morning. | NaN | NaN | NaN |
9 | Read verse out loud for pleasure. | NaN | NaN | NaN |
response
results { alternatives { transcript: "The small pop not a hole in the sock the fish twisted and turned on the bent hook." confidence: 0.7337599396705627 } } results { alternatives { transcript: " Pasta passes so it button on the vest." confidence: 0.6798253059387207 } } results { alternatives { transcript: " The Swan Dive was Far short of perfect." confidence: 0.738798975944519 } } results { alternatives { transcript: " the beauty of the views done the young boy" confidence: 0.742624819278717 } } results { alternatives { transcript: " to blue fish swim in the tank" confidence: 0.7812932133674622 } } results { alternatives { transcript: " her purse was full of useless trash." confidence: 0.7841848731040955 } } results { alternatives { transcript: " The Colt reader does through the tall rider." confidence: 0.6258752942085266 } } results { alternatives { transcript: " Is snow rain and hail the same morning?" confidence: 0.6540632247924805 } } results { alternatives { transcript: " Read burst out loud for pleasure." confidence: 0.6588537693023682 } }
response.results[4]
alternatives { transcript: " to blue fish swim in the tank" confidence: 0.7812932133674622 }
for result in response.results:
print('Transcript: {}'.format(result.alternatives[0].transcript))
from google.cloud import speech
client = speech.SpeechClient()
with open(speech_file, 'rb') as audio_file:
content = audio_file.read()
audio = speech.types.RecognitionAudio(content=content)
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=44100,
language_code='en-US',
audio_channel_count=2,
enable_separate_recognition_per_channel=True)
response = client.recognize(config, audio)
for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print('-' * 20)
print('First alternative of result {}'.format(i))
print(u'Transcript: {}'.format(alternative.transcript))
print(u'Channel Tag: {}'.format(result.channel_tag))