import io import os # Imports the Google Cloud client library from google.cloud import speech # Instantiates a client client = speech.SpeechClient() # The name of the audio file to transcribe file_name = os.path.join(os.path.dirname(__file__), '2.wav') # Loads the audio into memory with io.open(file_name, 'rb') as audio_file: content = audio_file.read() audio = speech.RecognitionAudio(content=content) # Specifies the audio encoding and language config = speech.RecognitionConfig( encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, language_code='en-US') # Detects speech in the audio file response = client.recognize(config=config, audio=audio) # Prints the transcription of the audio for result in response.results: print('Transcript: {}'.format(result.alternatives[0].transcript))