📜  Python – 使用语音识别获取今天的当前日期

📅  最后修改于: 2022-05-13 01:54:23.728000             🧑  作者: Mango

Python – 使用语音识别获取今天的当前日期

由于我们正在做的工作量很大,我们经常不记得日期的哪一天。因此,这是一个Python程序,借助它,我们只需与笔记本电脑或手机进行简单的聊天即可找到日期。

需要的模块

  • DateTime:这是Python中的一个库,借助它我们可以操作日期和时间。它预装了Python ,所以我们不必安装。
  • pyttsx3:这是文本到语音的转换库。它有助于与用户交流。这个模块没有内置在Python中。要安装它,请在终端中键入以下命令。
pip install pyttsx3
  • SpeechRecognition:它有助于语音识别。语音识别是将音频转换为文本的过程。这个模块没有内置在Python中。要安装它,请在终端中键入以下命令。
pip install SpeechRecognition

现在让我们编写借助语音识别来告诉日期的程序。

步骤1:制定接受命令的方法。这样我们的程序就可以接受我们的命令。

Python3
import datetime
import pyttsx3
import speech_recognition as sr
 
 
def take_commands():
     
    # Making the use of Recognizer and Microphone
    # Method from Speech Recognition for taking
    # commands
    r = sr.Recognizer()
     
    with sr.Microphone() as source:
        print('Listening')
         
        # seconds of non-speaking audio before
        # a phrase is considered complete
        r.pause_threshold = 0.7
        audio = r.listen(source)
        try:
            print("Recognizing")
             
            # for listening the command in indian english
            Query = r.recognize_google(audio, language='en-in')
             
            # for printing the query or the command that we give
            print("the query is printed='", Query, "'")
        except Exception as e:
             
            # this method is for handling the exception
            # and so that assistant can ask for telling
            # again the command
            print(e) 
            print("Say that again sir")
            return "None"
         
    return Query


Python3
def Speak(audio):
     
    # initial constructor of pyttsx3
    engine = pyttsx3.init()
     
    # getter and setter method
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.say(audio)
    engine.runAndWait()


Python3
def tellDay():
     
    # the weekday method is a method from datetime
    # library which helps us to an integer
    # corresponding to the day of the week
    # this dictionary will help us to map the
    # integer with the day and we will check for
    # the condition and if the condition is true
    # it will return the day
    day = datetime.datetime.today().weekday() + 1
     
    Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday',
                4: 'Thursday', 5: 'Friday', 6: 'Saturday',
                7: 'Sunday'}
     
    if day in Day_dict.keys():
        day_of_the_week = Day_dict[day]
        print(day_of_the_week)
        Speak("The day is " + day_of_the_week)


Python3
if __name__ == '__main__':
    command=take_commands()
     
    if "day" in command:
        tellDay()


Python3
import datetime
import pyttsx3
import speech_recognition as sr
 
 
def take_commands():
     
    # Making the use of Recognizer and Microphone
    # Method from Speech Recognition for taking
    # commands
    r = sr.Recognizer()
     
    with sr.Microphone() as source:
        print('Listening')
         
        # seconds of non-speaking audio before
        # a phrase is considered complete
        r.pause_threshold = 0.7
        audio = r.listen(source)
        try:
            print("Recognizing")
             
            # for listening the command in indian english
            Query = r.recognize_google(audio, language='en-in')
             
            # for printing the query or the command that we give
            print("the query is printed='", Query, "'")
        except Exception as e:
             
            # this method is for handling the exception
            # and so that assistant can ask for telling
            # again the command
            print(e) 
            print("Say that again sir")
            return "None"
         
    return Query
 
def Speak(audio):
     
    # initial constructor of pyttsx3
    engine = pyttsx3.init()
     
    # getter and setter method
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.say(audio)
    engine.runAndWait()
 
def tellDay():
     
    # the weekday method is a method from datetime
    # library which helps us to an integer
    # corresponding to the day of the week
    # this dictionary will help us to map the
    # integer with the day and we will check for
    # the condition and if the condition is true
    # it will return the day
    day = datetime.datetime.today().weekday() + 1
     
    Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday',
                4: 'Thursday', 5: 'Friday', 6: 'Saturday',
                7: 'Sunday'}
     
    if day in Day_dict.keys():
        day_of_the_week = Day_dict[day]
        print(day_of_the_week)
        Speak("The day is " + day_of_the_week)
 
# Driver Code
if __name__ == '__main__':
    command=take_commands()
     
    if "day" in command:
        tellDay()



第 2 步:创建 Speak 方法,以便我们的程序可以与我们交谈。

Python3

def Speak(audio):
     
    # initial constructor of pyttsx3
    engine = pyttsx3.init()
     
    # getter and setter method
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.say(audio)
    engine.runAndWait()

第 3 步:告诉日期方法

Python3

def tellDay():
     
    # the weekday method is a method from datetime
    # library which helps us to an integer
    # corresponding to the day of the week
    # this dictionary will help us to map the
    # integer with the day and we will check for
    # the condition and if the condition is true
    # it will return the day
    day = datetime.datetime.today().weekday() + 1
     
    Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday',
                4: 'Thursday', 5: 'Friday', 6: 'Saturday',
                7: 'Sunday'}
     
    if day in Day_dict.keys():
        day_of_the_week = Day_dict[day]
        print(day_of_the_week)
        Speak("The day is " + day_of_the_week)


第 4 步:帮助我们执行程序的 main 方法

Python3

if __name__ == '__main__':
    command=take_commands()
     
    if "day" in command:
        tellDay()


整个程序:

Python3

import datetime
import pyttsx3
import speech_recognition as sr
 
 
def take_commands():
     
    # Making the use of Recognizer and Microphone
    # Method from Speech Recognition for taking
    # commands
    r = sr.Recognizer()
     
    with sr.Microphone() as source:
        print('Listening')
         
        # seconds of non-speaking audio before
        # a phrase is considered complete
        r.pause_threshold = 0.7
        audio = r.listen(source)
        try:
            print("Recognizing")
             
            # for listening the command in indian english
            Query = r.recognize_google(audio, language='en-in')
             
            # for printing the query or the command that we give
            print("the query is printed='", Query, "'")
        except Exception as e:
             
            # this method is for handling the exception
            # and so that assistant can ask for telling
            # again the command
            print(e) 
            print("Say that again sir")
            return "None"
         
    return Query
 
def Speak(audio):
     
    # initial constructor of pyttsx3
    engine = pyttsx3.init()
     
    # getter and setter method
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.say(audio)
    engine.runAndWait()
 
def tellDay():
     
    # the weekday method is a method from datetime
    # library which helps us to an integer
    # corresponding to the day of the week
    # this dictionary will help us to map the
    # integer with the day and we will check for
    # the condition and if the condition is true
    # it will return the day
    day = datetime.datetime.today().weekday() + 1
     
    Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday',
                4: 'Thursday', 5: 'Friday', 6: 'Saturday',
                7: 'Sunday'}
     
    if day in Day_dict.keys():
        day_of_the_week = Day_dict[day]
        print(day_of_the_week)
        Speak("The day is " + day_of_the_week)
 
# Driver Code
if __name__ == '__main__':
    command=take_commands()
     
    if "day" in command:
        tellDay()

输出:

Listening
Recognizing
the query is printed=' today's day '
Wednesday

注意:还会创建语音生成的输出。