{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n" ] } ], "source": [ "import sys\n", "sys.path.insert(0, '/home/ons21553/wspace/interview-transcripts/src')\n", "from common import *\n", "\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import io\n", "import os\n", "\n", "# Imports the Google Cloud client library\n", "from google.cloud import speech\n", "from google.cloud.speech import enums\n", "from google.cloud.speech import types" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from common import *" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = from_root('dcm-iw-transcripts-db3f1fa338cc.json')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "# Instantiates a client\n", "client = speech.SpeechClient()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "import recordings.harvard as hv\n", "r = hv.load_all()[0]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "with io.open(r.audio_fpath, 'rb') as audio_file:\n", " content = audio_file.read()\n", " audio = types.RecognitionAudio(content=content)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "config = types.RecognitionConfig(\n", " encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,\n", " language_code='en-US')\n", "\n", "# Detects speech in the audio file\n", "response = client.recognize(config, audio)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
textspeakerfromend
0The small pup gnawed a hole in the sock.NaNNaNNaN
1The fish twisted and turned on the bent hook.NaNNaNNaN
2Press the pants and sew a button on the vest.NaNNaNNaN
3The swan dive was far short of perfect.NaNNaNNaN
4The beauty of the view stunned the young boy.NaNNaNNaN
5Two blue fish swam in the tank.NaNNaNNaN
6Her purse was full of useless trash.NaNNaNNaN
7The colt reared and threw the tall rider.NaNNaNNaN
8It snowed, rained, and hailed the same morning.NaNNaNNaN
9Read verse out loud for pleasure.NaNNaNNaN
\n", "
" ], "text/plain": [ " text speaker from end\n", "0 The small pup gnawed a hole in the sock. NaN NaN NaN\n", "1 The fish twisted and turned on the bent hook. NaN NaN NaN\n", "2 Press the pants and sew a button on the vest. NaN NaN NaN\n", "3 The swan dive was far short of perfect. NaN NaN NaN\n", "4 The beauty of the view stunned the young boy. NaN NaN NaN\n", "5 Two blue fish swam in the tank. NaN NaN NaN\n", "6 Her purse was full of useless trash. NaN NaN NaN\n", "7 The colt reared and threw the tall rider. NaN NaN NaN\n", "8 It snowed, rained, and hailed the same morning. NaN NaN NaN\n", "9 Read verse out loud for pleasure. NaN NaN NaN" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.structured_transcript" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "results {\n", " alternatives {\n", " transcript: \"The small pop not a hole in the sock the fish twisted and turned on the bent hook.\"\n", " confidence: 0.7337599396705627\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" Pasta passes so it button on the vest.\"\n", " confidence: 0.6798253059387207\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" The Swan Dive was Far short of perfect.\"\n", " confidence: 0.738798975944519\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" the beauty of the views done the young boy\"\n", " confidence: 0.742624819278717\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" to blue fish swim in the tank\"\n", " confidence: 0.7812932133674622\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" her purse was full of useless trash.\"\n", " confidence: 0.7841848731040955\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" The Colt reader does through the tall rider.\"\n", " confidence: 0.6258752942085266\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" Is snow rain and hail the same morning?\"\n", " confidence: 0.6540632247924805\n", " }\n", "}\n", "results {\n", " alternatives {\n", " transcript: \" Read burst out loud for pleasure.\"\n", " confidence: 0.6588537693023682\n", " }\n", "}" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "alternatives {\n", " transcript: \" to blue fish swim in the tank\"\n", " confidence: 0.7812932133674622\n", "}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response.results[4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "for result in response.results:\n", " print('Transcript: {}'.format(result.alternatives[0].transcript))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from google.cloud import speech\n", "client = speech.SpeechClient()\n", "\n", "with open(speech_file, 'rb') as audio_file:\n", " content = audio_file.read()\n", "\n", "audio = speech.types.RecognitionAudio(content=content)\n", "\n", "config = speech.types.RecognitionConfig(\n", " encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,\n", " sample_rate_hertz=44100,\n", " language_code='en-US',\n", " audio_channel_count=2,\n", " enable_separate_recognition_per_channel=True)\n", "\n", "response = client.recognize(config, audio)\n", "\n", "for i, result in enumerate(response.results):\n", " alternative = result.alternatives[0]\n", " print('-' * 20)\n", " print('First alternative of result {}'.format(i))\n", " print(u'Transcript: {}'.format(alternative.transcript))\n", " print(u'Channel Tag: {}'.format(result.channel_tag))\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 4 }