Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 The script won't take voice input
Hello! I've written a script for a virtual assistant. But when I try running it, the script won't take any voice input I give. I have attached the code with this thread. Please review it and let me know if there is any problem with it. Thanks in advance for your help!

# Author: Shishir Modi
# Created on: 7th July 2020
# This is a script for a virtual assistant.

# Import packages
import speech_recognition as sr
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import wikipedia

# Ignore any warning messages

# Record audio and return as string 
def recordAudio():

    # record the audio
    r = sr.Recognizer() #creating a speech recognizer

    # open the microphone and start recording
    with sr.Microphone() as source:
        print('Hello! How may I help you?')
        audio = r.listen(source)
    # Use google speech recognition
    data = ''
        data = r.recognize_google(audio)
        print('You said : '+data)
    except sr.UnknownValueError : 
        print("Sorry, I didn't quite understand that")
    except sr.RequestError :
        print('Please check your network connection.')
    return data


# A function to get the virtual assistant response
def assistantResponse(text):

    # convert the text to speech
    myobj = gTTS(text= text, lang='en', slow=False)

    #save the audio to a file'assistant_response.mp3')

    #SPlay the converted file
    os.system('start assistant_response.mp3')

# A function for wake word
def wakeWord(text):
    WAKE_WORDS = {'Hey Jarvis', 'Jarvis', 'Hello Jarvis', 'I need help'} #list of wake words

    text = text.lower() #convert the text to lower case words

    #Check to see if user input is a wake word
    for phrase in WAKE_WORDS:
        if phrase in text:
            return True
        else: return False

    #If The wake word is not found in text from the loop , so it returns False

#A function to get the current date
def getDate():

    now  =
    my_date =
    weekday = calendar.day_name[my_date.weekday()] #sunday
    monthNum = now.month
    dayNum =

    #A list of months
    month_names = ['January', 'February', 'March', ' April', 'May', 'June', 'July','August', 'September', ' October', 'November', 'December']

    #A list of ordinal Numbers
    ordinalNumbers = [ '1st', '2nd', '3rd', ' 4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th', 
    '17th', '18th', '19th', '20th', '21st', '22nd', '23rd','24rd', '25th', '26th', '27th', '28th', '29th', '30th', '31st']

    return 'Today is '+weekday+' '+month_names[monthNum - 1]+' the '+ ordinalNumbers[dayNum -1]+' .'

#Function to return greeting
def greeting(text):

    #Greeting inputs
    GREETING_INPUTS = ['Hi!', 'Hey!', 'Hola!', 'Wassup?', 'Hello!']

    #Greeting response
    GREETING_RESPONSES = ['Howdy', 'All that good', 'Hello master', 'Hey there!']

    #If users input is a greeting, then return a randomly chosen greetng response
    for word in text.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)
    #If no greeting was detected
    return ''

#A functions to get persons name from text
def getPerson(text):

    wordList = text.split() #splits the text to words

    for i in range(0, len(wordList)):
        if i+3 <= len(wordList)-1 and wordList[i].lower() == 'who' and wordList[i+1].lower() == 'is':
            return wordList[i+2] + ' '+ wordList[i+3]

while True:

    #record the audio
    text = recordAudio()
    response = '' 

    #check for the wake word / phrase
    if (wakeWord(text) == True):
        #check for greetings by the user
        response = response + greeting(text)

        #check to see if the user has said anything about data
        if('date' in text):
            get_date = getDate()
            response = response + ' '+get_date

        #check to see if the user said 'who is' 
        if('who is' in text):
            person = getPerson(text) 
            wiki = wikipedia.summary(person, sentences=4)
            response = response +' '+ wiki

        #assistant respond back using audio and text from response
Try running (from the package documentation)
import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
what happens?
(Jul-20-2020, 04:35 PM)Larz60+ Wrote: Try running (from the package documentation)
import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
what happens?

Thanks! I'm able to use my script now.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Simulation of Text to speech (without using mic) to Voice recognition suported hardwa Helloworld20 2 364 Apr-08-2020, 02:13 AM
Last Post: Helloworld20
  How to write a script to execute a program need passing additional input? larkypython 2 541 Nov-23-2019, 04:38 AM
Last Post: larkypython
  Any Google Voice users looking for a coding project? pydiot 3 1,521 May-31-2018, 10:11 PM
Last Post: pydiot
  voice assistant python based pyaudio error hardik 6 2,410 Oct-06-2017, 07:29 PM
Last Post: hardik
  Some help on user input in script Kaos_Method 0 1,238 Jul-12-2017, 04:49 AM
Last Post: Kaos_Method

Forum Jump:

Users browsing this thread: 1 Guest(s)