谷歌云语音ImportError:无法导入名称“枚举”

时间:2019-06-28 02:34:09

标签: python import google-speech-api pipenv google-cloud-speech

我正在为我的项目使用google-cloud-speech api。我将Pipenv用于虚拟环境,我使用

安装了google-cloud-speech api
  

pipenv安装google-cloud-speech

  

pipenv更新google-cloud-speech

我关注了这份文档https://cloud.google.com/speech-to-text/docs/reference/libraries

这是我的代码:

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)) 

当我尝试使用

运行python文件时
  

python google.py

返回错误ImportError:无法导入具有以下回溯的名称“ SpeechClient”:

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'

我做错什么了吗?当我在线搜索错误时,只有1个问题没有答案

更新: 我从

更改为
  

google.cloud导入Speech_v1作为语音

对此

  

从google.cloud导入语音

现在我又遇到了类似回溯的返回错误

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'

有人曾尝试过该库吗?因为仅遵循其文档似乎有很多错误

4 个答案:

答案 0 :(得分:1)

看到以下错误消息

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

如果执行了Google语音api的“新”安装。请参阅this page

如果更新了api,则沿着相同的行使用nanos属性将导致以下消息

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

请参阅this page。使用“微秒”代替“ nanos”。

答案 1 :(得分:0)

  • 第一个解决方案尝试检查您的python3.6 / site-packages / google / cloud,是否有speech_v1。如果没有,则需要先安装
  • 第二个解决方案尝试检查您的python3.6 / site-packages / google / cloud是否存在现有语音文件,如果存在,则导致导入的原因是阴影。因为您的别名是“语音” 希望这会有所帮助

如果您使用的是Speech_v1,请尝试以下代码行:

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

语音:

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

答案 2 :(得分:0)

如果您可以检查 this link。 Google 已将 AudioEncodings 移到 google.cloud.speech_v1.types 下,您可以通过导入类型然后运行以下代码来使用它:

from google.cloud.speech_v1 import types

types.RecognitionConfig.AudioEncoding.LINEAR16

答案 3 :(得分:0)

来自 Google Cloud 文档:

枚举和类型 警告:重大变化

The submodules enums and types have been removed.

之前:

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()