首页 > 解决方案 > 我写了一个虚拟助手程序,但程序没有检测到我的声音,也没有回复。请帮帮我

问题描述

import pyaudio 
import speech_recognition as sr
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import wikipedia

#To ignore any warning mesages
warnings.filterwarnings('ignore')

#record audio and return it as string
def recordAudio():

    #Record the audio
    r = sr.Recognizer() #creating a recognizer object

    #open the microphone and start recording
    with sr.Microphone() as source:
        print('say something!')
        audio = r.listen(source)

    #Use googles speech recognition
    data = ''
    try:
        data = r.recognize_google(audio)
        print('You said:  '+data)
    except sr.UnknownValueError: #check for unknown errors 
        print('Sorry I didnt get you')
    except sr.RequestError as e:
        print('Request results from google speech recognition service error'+e)

    return data

# A function to get the virtual assistant's response

def assistantResponse(text):

    print(text)

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

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

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

# a function for wake words for phrase
def wakeWord(text):
    WAKE_WORDS = ['butler']

    text = text.lower() #converting all text to lower case

    #Check to look for wake word
    for phrase in WAKE_WORDS:
        if phrase in text:
            return True

    #executed if wake word isn't found in the text from the loop
    return 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()] #eg Friday
    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', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st']

    return 'Today is ' +weekday +' ' +month_names[monthNum -1] + ' the ' +ordinalNumbers[dayNum -1] + '.' #my code gives the wrong date this is the code foe tha day and date

# A function to return a random greeting response
def greeting(text):

    #greeting inputs
    GREETING_INPUTS = ['hi', 'hey', 'hello there', 'wassup', 'greeting', 'namaste']

    #greeting responses
    GREETING_RESPONSES = ['hi sir', 'hello there sir', 'howdy', 'to infinity and beyond']

    # if users input is a greeting, then give a response
    for word in text.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES) +'.'

    #if no greeting was detected then return an empty string
    return ''

# A function to get a person's first and last name from the text
def getPerson(text):

    wordList = text.split() #Splitting the text into 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
    if(wakeWord(text) == True):

        #check for greetings by the user
        response = response + greeting(text)

        #check to see if the user said anything to have to do with the date
        if('date' in text):
            get_date=getDate()
            response = response + ' ' +get_date

        #check if user says who is
        if('who is' in text):
            person = getPerson(text)
            wiki = wikipedia.summary(person, sentences=2)
            response = response +' '+ wiki

        #ceck for time
        if('time' in text):
            now=datetime.datetime.now()
            meridiem=''
            if now.hour >=12:
                meridiem = 'p.m'
                hour = hour - 12
            else:
                meridiem = 'a.m'
                hour=now.hour
            
        #Check minute into proper string
        if now.minute < 10:
            minute = '0'+str(now.minute)
        else:
            minute=str(now.minute)

        response = response + ' '+ 'It is' +str(hour)+ ':'+minute+ ' '+meridiem+' .'

        #Have the assistant respond back using audio
        assistantResponse(response)
  1. 我用过谷歌语音识别
  2. 我的代码出于某种原因给出了错误的日期
  3. 程序根本不接我的声音,只在房间非常安静的时候才接我的声音
  4. 该程序也没有回复我
  5. 有什么办法可以把声音改成男声吗?

我对 python 比较陌生并且想创建一些东西,所以我从教程中获得了帮助,现在我不知道如何修复它。帮助将不胜感激。

谢谢你

标签: python

解决方案


推荐阅读