简体   繁体   中英

Recording audio from microphone in Python

I'm trying to create a virtual assistant in python, which works using speech recognition. While running the program though, it isn't taking any voice input, though there are no errors. My microphone is properly connected and I'm pretty sure there is something wrong with the code. Please help me with this.

# 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 responds back using audio and text from response
        assistantResponse(response)

I think the problem is that the wake word should be lowered (Python is case-sensitive)

BTW, check out my Virtual Assistant, Ida

# 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.lower() in text: #convert the phrase to lower case words as well
            return True
        else: return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM