Search code examples
pythongoogle-cloud-speech

Module 'google.cloud.speech_v1.types' has no 'RecognitionAudio' member and Module 'google.cloud.speech_v1.types' has no 'RecognitionConfig' member


I'm using Windows and Python 3.8.3, and using the Cloud Speech-to-Text API from the Google Cloud Platform, and every time I save my code it points to an error

"Module 'google.cloud.speech_v1.types' has no 'RecognitionAudio' member "

and

"Module 'google.cloud.speech_v1.types' has no 'RecognitionConfig' member "

I looked at the documentation and the only thing that talks about is versions prior to Python 2.7, which does not apply to me, does anyone know any solution to this problem?

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

Solution

  • In the documentation and Github's README, types is imported from from google.cloud.speech_v1 instead of google.cloud.speech.

    Have you already tried that?

    EDIT: After further analysis, it appears that the errors are warnings from the IDE. Google cloud SDK's import mechanism often causes the IDE to show that kind of warnings but the code still works.