0

I am trying to transcribe an audio file, whose duration is less than a minute, using the Google cloud API. I am copying the code from official website https://cloud.google.com/speech-to-text/docs/sync-recognize but it gives no output. The script ends within 5 -7 seconds and no output displays on screen. I expect the transcribed audio at output.

import os
import io
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="D:\\Rehan\\naufil-0e289431cf2d.json"
def transcribe_file(speech_file):
    """Transcribe the given audio file."""
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    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-US')

    response = client.recognize(config, 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))

transcribe_file(r"D:\\Rehan\\hey.mp3")

One notable warning can be, visual code says [pylint] Module 'google.cloud.speech_v1.types' has no 'RecognitionAudio' member [no-member]. It just shows a red line under types.RecognitionAudio and types.RecognitionConfig. Although it doesnt generate any error.

Muhammad Naufil
  • 2,420
  • 2
  • 17
  • 48
  • Possible duplicate of [Google Speech Recognition API Result is Empty](https://stackoverflow.com/questions/38906527/google-speech-recognition-api-result-is-empty) – Nikolay Shmyrev Apr 13 '19 at 20:33
  • You send MP3 file, but you point that format is LINEAR, you need to convert MP3 to WAV before submitting the file – Nikolay Shmyrev Apr 13 '19 at 20:33

0 Answers0