-2

I am trying to make a voice activated virtual assistant of sorts using python, but I am not sure how to detect and distinguish between the different voice commands. Currently it just repeats back to you, "You Said [whatever i said]" but i want it to respond differently to different things that I say. I am quite new to python and don't know what I should do. Does anyone know how I could do this?

TheRobynn
  • 1
  • 2

1 Answers1

0

You have to define what you want it to do. The last two lines of this tell the program to do something if the input is hello. So when you run it, you say "hello" and it will have a different response. If it does not detect that you said "hello" then it will not do anything. I might recommend finding a project on github where they have already done an assistant like this and start to try to understand what they did and edit to the specifications you want.

import speech_recognition as sr

sample_rate = 48000
chunk_size = 2048
r = sr.Recognizer()
device_id = 1

with sr.Microphone(device_index=device_id, sample_rate=sample_rate, chunk_size=chunk_size) as source:
    print("Say something...")
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)
    text = r.recognize_google(audio)
    if text.lower() == "hello":
        print("Hi, how are you?")
patmcb
  • 443
  • 2
  • 9