参考网址 导入错误:无法导入名称"枚举">
谷歌云语音导入错误:无法导入名称"枚举">
我在为我的项目使用 google-cloud-speech api 时遇到错误。我没有将pipenv用于虚拟环境。我安装了谷歌云语音 API
pip3 install google-cloud-speech
和
pip3 update google-cloud-speech
环境 赢10 蟒蛇3.6.8
谷歌 API 核心 1.14.2
谷歌身份验证 1.6.3
谷歌云 0.34.0
谷歌云核心 1.0.3
谷歌云语音 1.2.0
谷歌云存储 1.18.0
谷歌-可恢复媒体 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的谷歌云外壳中执行的代码,以下代码运行良好,我没有收到错误。
# !/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)
提前谢谢。
通过使用pip安装google.cloud.speech,您将获得最新版本,当前为V2。
在 V2 中,枚举和类型已被删除,不再需要。
https://github.com/googleapis/python-speech/blob/master/UPGRADING.md#enums-and-types
应该是:from google.cloud.speech_v1 import enums
尝试使用,
from google.cloud.language_v1 import *
然后,您可以直接访问以下所有内容
LanguageServiceAsyncClient
AnalyzeEntitiesRequest
AnalyzeEntitiesResponse
AnalyzeEntitySentimentRequest
AnalyzeEntitySentimentResponse
AnalyzeSentimentRequest
AnalyzeSentimentResponse
AnalyzeSyntaxRequest
AnalyzeSyntaxResponse
AnnotateTextRequest
AnnotateTextResponse
ClassificationCategory
ClassifyTextRequest
ClassifyTextResponse
DependencyEdge
Document
EncodingType
Entity
EntityMention
LanguageServiceClient
PartOfSpeech
Sentence
Sentiment
TextSpan
Token