每当我尝试在语音识别程序中访问文件时,我都会遇到许可拒绝错误

发布于 2025-01-28 00:52:46 字数 2155 浏览 6 评论 0原文

我正在尝试使用语音识别,并将其用作某些语句的输入,同时使用弹奏和GTTS模块对我进行“说话”。但是我遇到了一个找不到解决方案的问题,我尝试了最常见的解决方案,但没有运气。

该程序使用弹奏speech_recognitiongtts模块和两个功能; speak()让程序可以使用Google的文本进行声音翻译与用户交谈,get_audio()使用secement_recognition代码>的识别器和麦克风类。

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

run = True

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

while run == True:

    text = get_audio()

    if "hello" in text:
        speak("Hello, how are you?")

    if "what are you" in text:
        print("")
        speak("I am a speech recognition program")
    
    if "goodbye" in text:
        speak("Talk to you later" or "Bye" or "Cya")
        run = False

我已经设置了一个段循环设置的程序,以便可以进行对话,并且一旦用户说“再见”,它才会断开。问题似乎是.mp3文件(voice.mp3,这是speak()函数用来存储为程序播放的程序来存储音频的内容)创建后可以访问。 Python文件和MP3文件都存储在同一文件夹中。

这是全部错误消息:

who are you
hello
Traceback (most recent call last):
  File "c:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition\main_va.py", line 34, in <module>
    speak("Hello, how are you?")
  File "c:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition\main_va.py", line 12, in speak   
    tts.save(filename)
  File "C:\Python\Python310\lib\site-packages\gtts\tts.py", line 328, in save
    with open(str(savefile), "wb") as f:
PermissionError: [Errno 13] Permission denied: 'voice.mp3'
PS C:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition> 

我收到了第一个呼叫(“您是谁”)的回复,但是第二个呼叫后弹出了错误消息(“ Hello”)。

规格:Python 3.10.4

  • 突击1.2.2
  • 休息是最新的

I'm trying out speech recognition and using it as input for some statements while having the program "speak" back to me using the playsound and gTTS modules. But I have ran into an issue that I can't find the solution for, I tried the most common solutions but with no luck.

The program uses the playsound, speech_recognition, and gTTS modules and two functions; speak() lets the program speak back to the user using google's text to sound translation, and get_audio() that receives input from the user's microphone using speech_recognition's recognizer and microphone classes.

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

run = True

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

while run == True:

    text = get_audio()

    if "hello" in text:
        speak("Hello, how are you?")

    if "what are you" in text:
        print("")
        speak("I am a speech recognition program")
    
    if "goodbye" in text:
        speak("Talk to you later" or "Bye" or "Cya")
        run = False

I have the program set up with a while loop so a conversation can play out, and it only breaks once the user says "Goodbye". The problem seems to be that the .mp3 file (voice.mp3 which is what the speak() function uses to store audio for the program to play back) can't be accessed after its creation. Both the python file and mp3 file are stored within the same folder.

Here is the error message in full:

who are you
hello
Traceback (most recent call last):
  File "c:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition\main_va.py", line 34, in <module>
    speak("Hello, how are you?")
  File "c:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition\main_va.py", line 12, in speak   
    tts.save(filename)
  File "C:\Python\Python310\lib\site-packages\gtts\tts.py", line 328, in save
    with open(str(savefile), "wb") as f:
PermissionError: [Errno 13] Permission denied: 'voice.mp3'
PS C:\Users\User\OneDrive\Documents\VS Code Projects\Speech Recognition> 

I received a response on the first call ("who are you"), but then the error message popped up after the second call ("hello").

Specs: python 3.10.4

  • playsound 1.2.2
  • Rest is up to date

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2025-02-04 00:52:46

您的解决方案正常。

我只是会对其进行调整以将文件留在后面(以防您要为了测试目的而收听),然后在开始时在开始时将其删除。

还将文件名传递给您的功能可确保没有任何硬编码

def speak(text, filename):
    if os.path.exists(filename):
        os.remove(filename) 
    tts = gTTS(text=text, lang="en")
    tts.save(filename)
    playsound.playsound(filename)

Your solution works fine.

I just would tweak it to leave the file behind (in case you want to listen to it for testing purposes) and instead, remove it at the beginning if it exists.

Also passing filename to your function ensures nothing is hard coded

def speak(text, filename):
    if os.path.exists(filename):
        os.remove(filename) 
    tts = gTTS(text=text, lang="en")
    tts.save(filename)
    playsound.playsound(filename)
无妨# 2025-02-04 00:52:46

我找到了一个解决方案,似乎可以正常工作。我每次使用它后每次删除.mp3文件,因此在speak()函数的末尾,我只使用os.remove(filename),然后是下一个时间想说一些新文件。

我发现了其他一些解决方案,说您每次制作时都应该重命名该文件名,但这对我来说会变得太多。

这是我对代码所做的更改,它只是speak()函数中的一条线:

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

这对我来说非常适合我,它可以根据需要接受尽可能多的输入,因为每次使用speak()函数时,都会删除和重新创建文件。

同样,如果建议或找到更好,更高效的解决方案,我将很乐意接受。

I found a solution that seems to work just fine; I delete the .mp3 file each time after I use it, so at the end of the speak() function I just use os.remove(filename) and then the next time it wants to say something a new file is created.

I found some other solutions saying that you should rename the filename every time you make one, but that would make too much clutter for me.

Here is the change that I made to my code, it was just a single line within the speak() function:

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

This works perfectly for me so far, it can take in as many inputs as needed since the file is deleted and recreated every time the speak() function is used.

Again if a better and more efficient solution is suggested or found, I'll gladly take it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文