简体   繁体   English

从 google.cloud.speech_v1 导入枚举时遇到问题

[英]trouble importing enums from google.cloud.speech_v1

I have this code:我有这段代码:

from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
import os
import importlib

# Import the enums module from the google.cloud.speech_v1 package
enums = importlib.import_module("google.cloud.speech_v1.enums")

# Set your Google Cloud project and service account credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "creds.json"


# Create a client for the Google Cloud Speech-to-Text API
stt_client = speech_v1.SpeechClient()

# Transcribe the audio data
response = stt_client.recognize(
  audio=speech_v1.types.RecognitionAudio(uri="gs://focus-0/speech-to-text-sample.wav"),
  config=speech_v1.types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
    sample_rate_hertz=48000,
    language_code="en-US"
  )
)

# Print the transcribed text
for result in response.results:
  print("Transcription: {}".format(result.alternatives[0].transcript))

When I run it, I get this:当我运行它时,我得到这个:

Traceback (most recent call last):
  File "/Users/dir/git/fp-scrapers/speech/1-STT.py", line 5, in <module>
    from google.cloud.speech_v1 import enums
ImportError: cannot import name 'enums' from 'google.cloud.speech_v1' (/opt/homebrew/lib/python3.9/site-packages/google/cloud/speech_v1/__init__.py)

I have tried several ways to import enums, but none of them have worked.我尝试了几种导入枚举的方法,但都没有奏效。

Does anyone see what I'm doing wrong?有没有人看到我做错了什么?

enums and types have been removed in the 2.x versions of the library枚举和类型已在库的 2.x 版本中删除

Mentioned in this github .Refer to this migration guide .在这个github中提到。参考这个迁移指南 You can refer to this quick start for setup instructions and an updated client library您可以参考此快速入门以获取设置说明和更新的客户端库

Before:前:

from google.cloud import speech

encoding = speech.enums.RecognitionConfig.AudioEncoding.LINEAR16
audio = speech.types.RecognitionAudio(content=content)

After:后:

from google.cloud import speech

encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16
audio = speech.RecognitionAudio(content=content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM