Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google cloud speech ImportError: cannot import name 'enums'

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

like image 637
Linh Nguyen Avatar asked Oct 15 '22 13:10

Linh Nguyen


1 Answers

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'.

like image 71
ksridhar Avatar answered Oct 31 '22 12:10

ksridhar