Search code examples
pythongoogle-speech-api

Problems with new Google Speech API


I am trying to migrate to Google's new speech API, as the beta one is expiring. However the new one does not seem to be working. To start with, this install seems to be failing:

pip install --upgrade google-cloud-speech

It produces the following error along with a whole bunch of other errors

ImportError: cannot import name IncompleteRead

Despite that, I seem to now be able to get it working using their sample file, but my own base64 encoded data does not work....despite exactly the same data working with the beta API. Is anyone having the same problems?

The code I am using is below, which results in "No results returned from the Speech API". I cant see where the API offers any ability to parse out an error message. If I uncomment the 3 lines to use audio.raw it works fine, but that is the google supplied sample. I cant understand why my file does not work as if I use exactly the same base64 encoding approach and the same test file it works fine with the beta API.

#!/usr/bin/env python
import io
import os
# Imports the Google Cloud client library
from google.cloud import speech

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "myfile.json"

def run_quickstart(speech_content):

    # Instantiates a client
    speech_client = speech.Client()

    #file_name = os.path.join( os.path.dirname(__file__), 'audio.raw')
    #audio_file = io.open(file_name, 'rb')
    #speech_content = audio_file.read()

    sample = speech_client.sample(
        speech_content,
        source_uri=None,
        encoding='LINEAR16',
        sample_rate_hertz=16000)

    # Detects speech in the audio file
    alternatives = sample.recognize('en-US')

    for alternative in alternatives:
        print('Transcript: {}'.format(alternative.transcript))


if __name__ == '__main__':
    import base64
    file = open('test.wav', 'rb')
    data = file.read()
    speech_content = base64.b64encode(data)
    #print speech_content
    run_quickstart(speech_content)

Solution

  • In terms of the Pip install, uninstalling and reinstalling seems to have solved the problem. The version of Pip that defaults with Debian is apparently quite old and this may have been the problem. Doing an upgrade with apt-get does not seem to upgrade Pip.

    In terms of the Google API I found that the approach they use to structuring the code for Python did not work. After playing with their API tester I was able to eventually modify my original code so that it worked with the new API.