google cloud speech ImportError: cannot import name 'enums'

房东的猫 提交于 2020-06-01 06:57:08

问题


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


回答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


来源:https://stackoverflow.com/questions/56800281/google-cloud-speech-importerror-cannot-import-name-enums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!