9

I'm using google-cloud-speech api for my project . I'm using pipenv for virtual environment i installed google-cloud-speech api with

pipenv install google-cloud-speech

and

pipenv update google-cloud-speech

i followed this docs https://cloud.google.com/speech-to-text/docs/reference/libraries

This is my code:

google.py:

# !/usr/bin/env python
# coding: utf-8
import argparse
import io
import sys
import codecs
import datetime
import locale
import os

from google.cloud import speech_v1 as speech
from google.cloud.speech import enums
from google.cloud.speech import types

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join("alt_speech_dev_01-fa5fec6806d9.json")
def get_model_by_language_id(language_id):
    model = ''
    if language_id == 1:
        model = 'ja-JP'
    elif language_id == 2:
        model = 'en-US'
    elif language_id == 3:
        model = "zh-CN"
    else:
        raise ('Not Match Lang')
    return model

def transcribe_gcs_without_speech_contexts(audio_file_path, model):
    client = speech.SpeechClient()
    with io.open(audio_file_path, 'rb') as audio_file:
        content = audio_file.read()
        audio = types.RecognitionAudio(content=content)

    config = {
        "encoding": enums.RecognitionConfig.AudioEncoding.FLAC,
        "sample_rate_hertz": 16000,
        "languageCode": model
        }



    operation = client.long_running_recognize(config, audio)
    print('Waiting for operation to complete...')
    operationResult = operation.result()

    ret=''
    for result in operationResult.results:
      for alternative in result.alternatives:
          ret = alternative.transcript

    return ret

def transcribe_gcs(audio_file_path, model, keywords=None):
    client = speech.SpeechClient()
    with io.open(audio_file_path, 'rb') as audio_file:
        content = audio_file.read()
        audio = types.RecognitionAudio(content=content)

    config = {
        "encoding": enums.RecognitionConfig.AudioEncoding.FLAC,
        "sample_rate_hertz": 16000,
        "languageCode": model,
        "speech_contexts":[{"phrases":keywords}]
        }



    operation = client.long_running_recognize(config, audio)
    print('Waiting for operation to complete...')
    operationResult = operation.result()

    ret=''
    for result in operationResult.results:
      for alternative in result.alternatives:
          ret = alternative.transcript

    return ret

transcribe_gcs_without_speech_contexts('alt_en.wav', get_model_by_language_id(2)) 

When i try to run the python file with

python google.py

it return error ImportError: cannot import name 'SpeechClient' with the following traceback:

Traceback (most recent call last):
  File "google.py", line 11, in <module>
    from google.cloud import speech_v1 as speech
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/__init__.py", line 17, in <module>
    from google.cloud.speech_v1.gapic import speech_client
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/gapic/speech_client.py", line 18, in <module>
    import pkg_resources
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3241, in <module>
    @_call_aside
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3225, in _call_aside
    f(*args, **kwargs)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in _initialize_master_working_set
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in <genexpr>
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2776, in activate
    declare_namespace(pkg)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2275, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2208, in _handle_ns
    loader.load_module(packageName)
  File "/home/hoanglinh/Documents/practice_speech/google.py", line 12, in <module>
    from google.cloud.speech import enums
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech.py", line 19, in <module>
    from google.cloud.speech_v1 import SpeechClient
ImportError: cannot import name 'SpeechClient'

Am i doing something wrong ? when i search the error online there only 1 question with no answer to it

UPDATE: i changed from

google.cloud import speech_v1 as speech

to this

from google.cloud import speech

now i got another return error with traceback like so

Traceback (most recent call last):
  File "google.py", line 11, in <module>
    from google.cloud import speech
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech.py", line 19, in <module>
    from google.cloud.speech_v1 import SpeechClient
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/__init__.py", line 17, in <module>
    from google.cloud.speech_v1.gapic import speech_client
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/google/cloud/speech_v1/gapic/speech_client.py", line 18, in <module>
    import pkg_resources
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3241, in <module>
    @_call_aside
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3225, in _call_aside
    f(*args, **kwargs)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in _initialize_master_working_set
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3269, in <genexpr>
    for dist in working_set
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2776, in activate
    declare_namespace(pkg)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2275, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/home/hoanglinh/Documents/practice_speech/.venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2208, in _handle_ns
    loader.load_module(packageName)
  File "/home/hoanglinh/Documents/practice_speech/google.py", line 12, in <module>
    from google.cloud.speech import enums
ImportError: cannot import name 'enums'

Have anyone tried this library before ? because it seem there so much errors just with following the docs of its

Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67
  • how did you solve? I'm getting similar error: _ImportError: cannot import name 'enums' from 'google.cloud.speech_v1'_ when I deleted v1 then getting: _ImportError: cannot import name 'enums' from 'google.cloud.speech'_ Is there any idea to solve? – hasany Oct 13 '20 at 07:06

4 Answers4

7

The following error message is seen

    from google.cloud.speech import enums
ImportError: cannot import name 'enums' 

if an 'new' installation of the google speech api was performed. Please see this page.

Along the same lines usage of nanos attributes would result in the following message if you have update the api

AttributeError: 'datetime.timedelta' object has no attribute 'nanos'

Please see this page. Use 'microseconds' instead of 'nanos'.

ksridhar
  • 189
  • 2
  • 9
1
  • First solution try to check your python3.6/site-packages/google/cloud if there is speech_v1. if there is none, you need to install it first
  • Second solution try to check your python3.6/site-packages/google/cloud if there is an existing speech file, if it exists then the cause of the import is shadowing. since your alias is 'speech' Hope this helps

try this line of codes if your using speech_v1:

from google.cloud import speech_v1 as speech
from google.cloud.speech_v1 import enums
from google.cloud.speech_v1 import types

speech:

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
1

If you can check this link. Google has moved the AudioEncodings under google.cloud.speech_v1.types you can use it by importing types and then running the code below:

from google.cloud.speech_v1 import types

types.RecognitionConfig.AudioEncoding.LINEAR16
0

From Google Cloud documentation :

Enums and Types WARNING: Breaking change

The submodules enums and types have been removed.

Before:

from google.cloud import videointelligence

features = [videointelligence.enums.Feature.SPEECH_TRANSCRIPTION]
video_context = videointelligence.types.VideoContext()
After:

from google.cloud import videointelligence

features = [videointelligence.Feature.SPEECH_TRANSCRIPTION]
video_context = videointelligence.VideoContext()
london_utku
  • 1,070
  • 2
  • 16
  • 36