Skip to content

Instantly share code, notes, and snippets.

@ecovictoriano
Created December 28, 2017 00:46
Show Gist options
  • Save ecovictoriano/47a5820748187281ddc566ba6c76a1c7 to your computer and use it in GitHub Desktop.
Save ecovictoriano/47a5820748187281ddc566ba6c76a1c7 to your computer and use it in GitHub Desktop.
Transcribe audio file to text (speech-to-text) using Google Cloud Platform's Speech API

Transcribe audio file to text (speech-to-text) using Google Cloud Platform's Speech API

Convert audio to text using GCP Speech API

Requirements

pip install --upgrade google-cloud-speech

Export the GCP credential env and execute the request

#!/bin/bash

export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/credentials-file.json

python gcp.py
# gcp.py
import io, os, subprocess, json
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

def transcribe_file(speech_file):
  client = speech.SpeechClient()

  with io.open(speech_file, 'rb') as audio_file:
    content = audio_file.read()

  audio = types.RecognitionAudio(content=content)
  config = types.RecognitionConfig(
    encoding          = enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz = 16000,
    language_code     = 'en-GB'
    # language_code     = 'en-US'
  )

  response = client.recognize(config, audio)
  return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment