how to improve the time in Python 3 voice recognition?

Asked

Viewed 784 times

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

1 answer

0

As far as I know, the only way to decrease recognition time is to have great audio quality. You can improve audio quality using the method r.adjust_for_ambient_noise who receives a source(in your case the microphone) and the duration.

What this method does is to adjust the Recognizer to the sound of the environment in which you are. You can set the method to adjust for 1 second and put it before making the audio capture to the recognizer always adapt to the environment, improving audio and recognition.

Your code would look like this:

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: ')

        # Ajusta o recognizer antes de capturar seu microfone.
        r.adjust_for_ambient_noise(source,duration=1)

        # Captura o som do microfone.
        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,

  • I’ll put your modified code in the answer.

  • ok very obg for your friend help

  • could I download the API from Speech recognition or google

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.