模块“ google.cloud.speech_v1.types”没有“ RecognitionAudio”成员,模块“ google.cloud.speech_v1.types”没有“ RecognitionConfig”成员

时间:2020-06-02 02:46:28

标签: python google-cloud-speech

我正在使用Windows和Python 3.8.3,并使用了Google Cloud Platform的Cloud Speech-to-Text API,每次保存代码时都指向错误

“模块'google.cloud.speech_v1.types'没有'RecognitionAudio'成员“

“模块'google.cloud.speech_v1.types'没有'RecognitionConfig'成员“

我查看了文档,唯一谈论的是Python 2.7之前的版本,该版本不适用于我,有人知道该问题的任何解决方案吗?

import telebot
import requests
from pydub import AudioSegment

import os
import io

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

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./chatbotia-279018-759b32f57985.json"

token = "1233361335:AAEO4qSTP6EraU6DvzU"
bot = telebot.TeleBot(token)
downloadAudio = "https://api.telegram.org/file/bot{token}/".format(token = token)

@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.reply_to(message, "Bem vindo ao bot de escrita, por favor mande um áudio ? que lhe será retornado da mesma forma, mas em uma mensagem escrita?")
    bot.reply_to(message, "qualquer duvida mande /help.")

@bot.message_handler(commands=['help'])
def send_help(message):
    bot.reply_to(message, " disque 190")

@bot.message_handler(content_types=['voice'])
def handlerAudio(message):

    #get audio from telegram 
    messageVoice = message.voice

    #get download link 
    audioPath = bot.get_file(messageVoice.file_id).file_path
    audioLink = downloadAudio+audioPath

    #download file
    audioFile = requests.get(audioLink)
    audioName = "audio.ogg"

    #save locally
    open(audioName, 'wb').write(audioFile.content)

    #convert format to .WAV
    AudioSegment.from_file(audioName).export("audio.wav", format="wav")
    sound = AudioSegment.from_wav("audio.wav")
    sound = sound.set_channels(1) #convert mono
    sound.export("audio.wav", format="wav")

client = speech.SpeechClient()

with io.open("audio.wav", 'rb') as audio_file:
        content = audio_file.read()

audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=16000,
    language_code='en-US')

response = client.recognize(config, audio)
for result in response.results:
  print(u'Transcript: {}'.format(result.alternatives[0].transcript))

bot.polling()

1 个答案:

答案 0 :(得分:0)

documentationGithub's README中,types是从google.cloud.speech_v1而不是google.cloud.speech导入的。

您已经尝试过了吗?

编辑:经过进一步分析,看来这些错误是来自IDE的警告。 Google Cloud SDK的导入机制通常会导致IDE显示这种警告,但代码仍然有效。