Python Forum
The script won't take voice input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The script won't take voice input
#1
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
warnings.filterwarnings('ignore')

# 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 = ''
    try:
        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

recordAudio()

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

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

    #save the audio to a file
    myobj.save('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  = datetime.datetime.now()
    my_date = datetime.datetime.today()
    weekday = calendar.day_name[my_date.weekday()] #sunday
    monthNum = now.month
    dayNum = now.day

    #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
        assistantResponse(response)
Reply
#2
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?
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,235 May-18-2021, 06:31 AM
Last Post: Skaperen
  Voice packs for Linux espeak gr3yali3n 0 1,616 Dec-24-2020, 07:42 PM
Last Post: gr3yali3n
  Simulation of Text to speech (without using mic) to Voice recognition suported hardwa Helloworld20 2 2,192 Apr-08-2020, 02:13 AM
Last Post: Helloworld20
  How to write a script to execute a program need passing additional input? larkypython 2 2,471 Nov-23-2019, 04:38 AM
Last Post: larkypython
  Any Google Voice users looking for a coding project? pydiot 3 3,829 May-31-2018, 10:11 PM
Last Post: pydiot
  voice assistant python based pyaudio error hardik 6 5,024 Oct-06-2017, 07:29 PM
Last Post: hardik
  Some help on user input in script Kaos_Method 0 2,775 Jul-12-2017, 04:49 AM
Last Post: Kaos_Method

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020