我的语音助手需要帮助

时间:2019-12-29 15:19:20

标签: python timer voice-recognition

我想做的是为助手设置一个计时器“命令”。 我想说“设置(任意数量)分钟的计时器” 除此之外,我希望我的声音能触发计时器x分钟。 我是python的新手,所以我对此并不了解,并且把这个助手作为一个个人项目正在使我对python有了更多的了解,并且我开始喜欢它。

def countdown(p,q,query):
    i=p
    j=q
    k=0

    while True:
        if(j==-1):
            j=59
            i -=1
        if(j > 9):  
            print("\r"+str(k)+str(i)+":"+str(j)),
        else:
            print("\r"+str(k)+str(i)+":"+str(k)+str(j)),
        time.sleep(1)
        j -= 1
        if(i==0 and j==-1):   
            break
    if(i==0 and j==-1):
        print("\rTimes up!")
        time.sleep(1)

上面是我的计时器代码。以下是我的尝试,任何人都可以帮忙吗?

import os 
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
import webbrowser
import subprocess
import datetime
import sys
from time import sleep 
import re

def myCommand():

    r = sr.Recognizer()
    with sr.Microphone(device_index=1, sample_rate=48000, chunk_size=1024) as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")    
        query = r.recognize_google(audio, language='en')
        print(f"User said: {query}\n")

    except Exception as e:
        # print(e)    
        print("Say that again please...")  
        return "None"
    return query

def countdown(p,q,query):
    i=p
    j=q
    k=0

    while True:
        if(j==-1):
            j=59
            i -=1
        if(j > 9):  
            print("\r"+str(k)+str(i)+":"+str(j)),
        else:
            print("\r"+str(k)+str(i)+":"+str(k)+str(j)),
        time.sleep(1)
        j -= 1
        if(i==0 and j==-1):   
            break
    if(i==0 and j==-1):
        print("\rGoodbye!")
        time.sleep(1)

def assistant(query, *, p):
    if "set a timer for" + str(int) + "minutes" in query:
        p=int in query
        if p is None:
            p=0
            countdown(query, p)
while True:
    assistant(myCommand())

0 个答案:

没有答案