1
I’m trying to make a program that listens to what I say and responds to what I say, I have a code here more or less that I created, with the pygame.
To specify when I run the code in the pycharm it opens a window with a background image and in the terminal of the pycharm it writes I am listening only that when I speak it takes to recognize and reply back.
The code:
`import speech_recognition as sr
import pyttsx3
import pygame
pygame.init()
r = sr.Recognizer()
engine = pyttsx3.init()
x = 1280
y = 720
imagem = pygame.image.load("fundo.jpg")
BLUE = (0, 0, 255)
#janela = pygame.display.set_mode((x, y), pygame.FULLSCREEN)
janela = pygame.display.set_mode((1280, 720))
pygame.display.set_caption('I.A')
janela_aberta = True
while janela_aberta == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
janela_aberta = False
janela.blit(imagem, (0, 0))
pygame.display.update()
with sr.Microphone() as source:
print ('fale algo: ')
audio = r.listen(source)
try:
text = r.recognize_google(audio, language='pt-br')
print('você disse: {}'.format(text))
except:
print('Desculpe não escutei sua voz')
#janela.blit(imagem, (0, 0))
#pygame.draw.line(janela, BLUE, (60, 60), (120, 60), 4)
#text1 = r.recognize_google(audio, language='pt-br')
if text == 'sair':
janela_aberta = False
elif text == 'Olá':
engine.say('Ola,como você está')
engine.runAndWait()
elif text == 'bem':
engine.say('então Esta bom!')
engine.runAndWait()
#elif text == 'quit':
b = False
elif text == "como você está":
engine.say('como voçe vai?')
engine.runAndWait()
elif text == 'Bom dia':
engine.say("bom dia")
engine.runAndWait()
#pygame.draw.line(janela, BLUE, (60, 60), (120, 60), 4)
pygame.display.update()
pygame.quit()`
ola obg for helping me,where I would write 'r. adjust_for_ambient_noise' in my program,which I would have to modify,
– Ruan Ferreira
I’ll put your modified code in the answer.
– JeanExtreme002
ok very obg for your friend help
– Ruan Ferreira
could I download the API from Speech recognition or google
– Ruan Ferreira
What do you mean, download the Speech recognition API? Regarding the google API, you can install yes and follow the rules on this site https://cloud.google.com/speech-to-text/docs/reference/libraries#client-Libraries-install-python. If you don’t know, the speech_recognition library already uses the google API when you call recognize_google.
– JeanExtreme002