繁体   English   中英

从 Python 中的麦克风录制音频

[英]Recording audio from microphone in Python

我正在尝试在 python 中创建一个虚拟助手,它使用语音识别。 虽然在运行程序时,它没有接受任何语音输入,尽管没有错误。 我的麦克风连接正确,我很确定代码有问题。 请帮我解决一下这个。

# 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)

我认为问题在于唤醒词应该降低(Python区分大小写)

顺便说一句,看看我的虚拟助手 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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