如何修复语音识别器python 3中的“权限错误:[Errno 13]”

时间:2019-09-07 05:57:59

标签: python speech-recognition

我的问题实际上很奇怪。我的程序有时运行良好,但有时却表明我没有要求。例如:我说“你好”是指我完全不询问时间的时间。 同样,当它无法识别来自用户的任何语音时,也会说出时间。 当我问什么日期时,它说的是日期,但随后显示错误消息

我试图切换if语句,但我对语音识别了解不多,所以没有尝试很多改动

import os
import time
from datetime import date
import playsound
import speech_recognition as sr 
from gtts import gTTS

def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))
    return said

text = get_audio()

today = date.today()
t = time.localtime()
current_time = time.strftime("%H:%M", t)
d2 = today.strftime("%B %d, %Y")

if "hello" in text:
    speak("hello sir")

if "date" in text:
    speak("the date is: " + d2)

if "what's the time" or "what is the time" in text:
    speak("The time is: " + current_time)

当我询问日期时显示的错误消息:

Traceback (most recent call last):
File "d:/python programs/hello.py", line 42, in <module>
speak("The time is: " + current_time)
File "d:/python programs/hello.py", line 11, in speak
tts.save(filename)
File "E:\python\lib\site-packages\gtts\tts.py", line 248, in save
with open(str(savefile), 'wb') as f:PermissionError: [Errno 13] Permission denied: 'voice.mp3'

当它无法识别用户的声音并返回异常时: 它说出了时间

这些都是程序的所有问题。

1 个答案:

答案 0 :(得分:0)

每次生成unique文件名而不是“ voice.mp3”:

import uuid
filename = str(uuid.uuid4()) + ".mp3"