参考网址 ImportError: cannot import name 'enums'
google cloud speech ImportError: cannot import name 'enums'
在我的项目中使用google-cloud-speech api时出现错误。我不在虚拟环境中使用pipenv。我安装了google-cloud-speech api
pip3 install google-cloud-speech
和
pip3 update google-cloud-speech
环境 赢10 python3.6.8
google-api-core 1.14.2
google-auth 1.6.3
google-cloud 0.34.0
google-cloud-core 1.0.3
google-cloud-speech 1.2.0
google-cloud-storage 1.18.0
google-resumable-media 0.3.3
googleapis-common-protos 1.6.0
grpc-google-cloud-speech-v1beta1 1.0.1
枚举34 1.1.6 枚举0.0.2
错误内容
from google.cloud.speech import enums
ImportError: cannot import name 'enums'
我已经尝试了以下pip命令。
pip3 install enums
pip3 install enum34
pip3 install google-cloud-speech
pip3 upgrade google-cloud-speech
这里的代码在GCP的Google Cloud Shell中执行时,以下代码运行良好,但我没有收到错误。
# !/usr/bin/env python
# coding: utf-8
import argparse
import io
import sys
import codecs
import datetime
import locale
from google.cloud import storage
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
def transcribe_gcs(gcs_uri):
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
client = speech.SpeechClient()
audio = types.RecognitionAudio(uri=gcs_uri)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC, # wavsetting
sample_rate_hertz=44100, # hertz must be fix raw files
language_code='ja-JP') #for japanese language
operation = client.long_running_recognize(config, audio)
print('Waiting for operation to complete...')
operationResult = operation.result()
d = datetime.datetime.today()
today = d.strftime("%Y%m%d-%H%M%S")
fout = codecs.open('output{}.txt'.format(today), 'a', 'utf-8')
for result in operationResult.results:
for alternative in result.alternatives:
fout.write(u'{}\n'.format(alternative.transcript))
print('alternative.transcript===',format(alternative.transcript))
fout.close()
print('alternative.transcript===',format(alternative.transcript))
print('operationResult===',operationResult)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'path', help='GCS path for audio file to be recognized')
args = parser.parse_args()
transcribe_gcs(args.path)
谢谢。
答案 0 :(得分:0)
应为:from google.cloud.speech_v1 import enums
答案 1 :(得分:0)
通过使用pip安装google.cloud.speech,您将获得最新版本,当前为V2。
在V2中,枚举和类型已被删除,不再需要。
https://github.com/googleapis/python-speech/blob/master/UPGRADING.md#enums-and-types