-1

该程序几天前运行良好,今天刚刚停止。没有一个字母被改变。我的故障排除步骤之一是删除文件'output1.mp3'并检查它是否会以这种方式工作,但它没有。另一件事是,当它没有打印出错误时,它会继续播放这个声音文件,无论它是否说正确。这是我得到的最新错误:

Traceback (most recent call last):
  File "main3.py", line 123, in <module>
    start()
  File "main3.py", line 117, in start
    tts(say)
  File "main3.py", line 24, in tts
    play('output1.mp3')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\playsound.py", line 35, in _playsoundWin
    winCommand('open "' + sound + '" alias', alias)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\playsound.py", line 31, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
    Error 275 for command:
        open "output1.mp3" alias playsound_0.8842337577803419
    Cannot find the specified file.  Make sure the path and filename are correct.

这是我使用的代码:

import boto3                               # used to 'pythonize' Amazon Polly TTS
import speech                              # speech recognition
from playsound import playsound as play    # for playing sound files
import sys                                 # basically only for exiting
#  import locator                          # for determining the location of the user based on IP address
import locator2                            # for confirming auto-detected location
#  import locator3                         # for definitely confirming auto-detection location
import question                            # module for answering any question

from datetime import datetime              # for displaying the date and time
#  from time import sleep                  # sleep (wai()t for) function
from os import popen as read               # for reading command outputs "read('COMMAND').read()"

def tts(text):
    polly = boto3.client("polly")
    spoken_text = polly.synthesize_speech(Text=str(text),
                                          OutputFormat='mp3',
                                          VoiceId='Brian')
    with open('output11.mp3', 'wb') as f:
        f.write(spoken_text['AudioStream'].read())
        f.close()
    play('output11.mp3')

def ask(query):
    question.question(query)
    response = question.answer
    print(response)
    tts(response)
    ai()

def time():
    now = datetime.now()
    print("Current date and time: ")
    print(now.strftime("%H:%M") + "\n")
    tts("It is " + now.strftime("%H:%M"))
    ai()

def weather():
    response = "Based on your IP address, I've detected that you're located in %s. Is that correct?" % locator2.city
    print(response)
    tts(response)
    speech.speech()
    if 'yes' in speech.x:
        z = read('weather "%s"' % locator2.city).read()
        print(z)
        tts(z)
        ai()
    else:
        response = 'Please say the name of the city you would like the weather information for. \n'
        print(response)
        tts(response)
        speech.speech()
        city = speech.x
        wdr = read('weather "%s"' % city).read()
        print(wdr)
        tts(wdr)
        ai()

def thank():
    response = "You're very welcome! \n"
    print(response)
    tts(response)
    ai()

def ext():
    response = "Goodbye!"
    print(response)
    tts(response)
    sys.exit()

def error():
    response = "Invalid request detected, please try again...\n"
    print(response)
    tts(response)
    ai()

def ai():
    print('Started listening - Speak!')
    speech.speech()
    spoken = speech.x

    # TODO new commands should be written above this, and their trigger words below :)
    question_words = ['?', 'what', 'where', 'when', 'who', 'how', 'why']

    if 'time' in spoken:
        time()

    elif 'weather' in spoken:
        weather()

    elif any(word in spoken for word in question_words):
        ask(spoken)

    elif 'thank' in spoken:
        thank()

    elif 'exit' or 'quit' or 'deactivate' in spoken:
        ext()

    else:
        error()

def start():
    say = "Hello! My name is Dave, and I'm your personal assistant. How may I help you today? \n"
    print(say)
    tts(say)
    ai()

if __name__ == '__main__':
    try:
        start()
    except KeyboardInterrupt:
        ext()

语音合成器是Amazon Polly。顺便说一句,我使用 PyCharm 作为 IDE 并在 Windows 10 上工作。当我切换到我的 Linux 机器时,语音识别部分会中断。

更新:我稍微调整了代码并设法修复了 pyaudio 错误,但我在此过程中遇到了另一个错误,这次是关于权限。这是错误日志:

Traceback (most recent call last):
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 123, in <module>
    start()
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 118, in start
    ai()
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 96, in ai
    time()
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 39, in time
    tts("It is " + now.strftime("%H:%M"))
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 21, in tts
    with open('output11.mp3', 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'output11.mp3'

更新 2:我一直在琢磨,我发现这个问题只存在于我的 Windows 10 机器上,该程序在 Linux 上运行良好。

4

5 回答 5

1

尝试使用音频文件的绝对路径(完整路径)而不是相对路径。

例如:“C:/Users/Adam/Desktop/dolphin.wav” 而不仅仅是“dolphin.wav”

这对我有用。

于 2020-03-01T07:02:21.887 回答
0

我只是将音频文件从sample.wavto重命名sample ,然后将其运行为playsound('sample.wav')

幸运的是它跑了。然后我才知道以前它被存储为sample.wav.wav.

因此,让我们保留您的音频文件名output.wav并尝试将您的音频文件运行为:

playsound('output.wav.wav') # or else rename it to output and run as 
playsound('output.wav')     # likewise for other audio formats too
于 2019-10-31T09:38:00.783 回答
0

我通过将 .py 和 .wav 文件移动到文件系统内部较浅的文件夹解决了这个问题。

于 2020-09-25T16:22:30.600 回答
0

UPDATE2 的答案:将 output11.mp3 复制到另一个位置并将路径更改为新位置:

with open('CHANGE-THIS-PATH', 'wb') as f:

还要确保 python 以管理员身份运行。

于 2019-02-05T14:17:38.367 回答
0

playsound 库中有一个 windows 目录。如果这仅在 Linux 上失败,您应该在 linux 机器上安装 playsound lib,然后仅将 main3.py 复制到它。

于 2019-02-03T11:09:51.780 回答