Python语音助手访问被拒绝错误

时间:2021-05-28 06:12:24

标签: python voice-recognition

我在用python做语音助手,但是报错in this picture,我随便放了文件名,又没变,怎么解决,只能交流一次

1 个答案:

答案 0 :(得分:0)

代码:

from playsound import playsound
from gtts import gTTS
import speech_recognition as sr
import os as pc
import time as saat 
from datetime import date, datetime
import random
from random import choice

r = sr.Recognizer()

def record(ask = False):
    with sr.Microphone() as source:
        if ask:
            print(ask)
        audio = r.listen(source)
        voice = ""
        try:
            voice = r.recognize_google(audio, language="tr-TR")
        except sr.UnknownValueError:
            print("Asistan: Anlayamadım")
            speak("anlayamadım")
        except sr.RequestError:
            print("Asistan: Sistem çalışmıyor")
            speak("Sistem çalışmıyor")
        return voice

def response(voice):
    if "merhaba" in voice:
        speak("sanada merhaba")
    if "selam" in voice:
        speak("selam")
    if "teşekkür ederim" in voice or "teşekkürler" in voice:
        speak("rica ederim")
    if "görüşürüz" in voice or "bay bay" in voice:
        speak("görüşürüz")
        exit()
    if "hangi gündeyiz" in voice or "hangi gün" in voice or "bugün hangi gün":
        today = saat.strftime("%A")
        today.capitalize()
        if today == "Monday":
            today = "Pazartesi"

        elif today == "Tuesday":
            today = "Salı"

        elif today == "Wednesday":
            today = "Çarşamba"

        elif today == "Thursday":
            today = "Perşembe"

        elif today == "Friday":
            today = "Cuma"

        elif today == "Saturday":
            today = "Cumartesi"

        elif today == "Sunday":
            today = "Pazar"

        speak(today)

    if "saat kaç" in voice or "şuan saat kaç" in voice:
        selection = ["Saat şu an: ", "Hemen bakıyorum: "]
        clock = datetime.now().strftime("%H:%M")
        selection = random.choice(selection)
        speak(selection + clock)

ad = random.randint(0,9)
adil = str(ad)
def speak(string):
    tts = gTTS(text=string, lang="tr", slow=False)
    file = adil + '.mp3'
    tts.save(file)
    playsound(file)
    pc.remove(file)

playsound('DING.mp3')
while True:
    voice = record()
    if voice != '':
        voice = voice.lower()
        print(voice.capitalize())
        response(voice)