我的语法有约束。我刚刚试过python
,我是从AI开始的。我制作像贾维斯这样的机器人来帮我打开谷歌或youtube。从Pythonspot.com中提供的教程中可以看到 Ubuntu 教程,但我使用 Windows 。并且有些工具或插件在Windows中不起作用mpg321
。我找到了mixer.music
的替代品来播放AI的声音。这有效,但我对第二个声音有限制,即我使用audio.mp3
发出第一个声音然后当我的第二个声音使用相同的文件名即audio.mp3
并且我有这样的约束
追踪(最近的呼叫最后):
文件" D:\ @ AI Projects \ Jarvis \ Jarvis.py",第71行,in 贾维斯(数据)
文件" D:\ @ AI Projects \ Jarvis \ Jarvis.py",第53行,在贾维斯说话(ctime())
文件" D:\ @ AI Projects \ Jarvis \ Jarvis.py",第17行,发言tts.save(" audio.mp3")
文件" C:\ Users \ inialdan \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ gtts \ tts.py", 第110行,保存 打开(savefile,' wb')为f:PermissionError:[Errno 13]权限被拒绝:' audio.mp3'
这是我的代码
#!/usr/bin/env python3
# Requires PyAudio and PySpeech.
import speech_recognition as sr
from time import ctime
import time
import os
import subprocess
from gtts import gTTS
from pygame import mixer
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
mixer.init()
mixer.music.load('D:/@AI Projects/Jarvis/audio.mp3')
mixer.music.play()
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Try to say something!")
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said : " + data)
except sr.UnknownValueError:
print("I'm Sorry, i couldn't understand what you mean ...")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return data
def jarvis(data):
CHROME = os.path.join('C:\\', 'Program Files (x86)', 'Google', 'Chrome', 'Application', 'chrome.exe')
if "jarvis" in data:
speak("Yes, sir ?")
if "what is your name" in data:
speak("You can call me, Jarvis")
if "where do you leave" in data:
speak("In your heart.")
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if "where is" in data:
data = data.split(" ")
location = data[2]
speak("Hold on Aldan, I will show you where " + location + " is.")
os.system('taskkill /im chrome.exe')
subprocess.call([CHROME, "https://www.google.nl/maps/place/" + location + "/&"])
if "open" in data:
data = data.split(" ")
application = data[1]
speak("Hold on Aldan, I will show you " + application)
os.system('taskkill /im chrome.exe')
subprocess.call([CHROME, "https://www." + application + ".com"])
time.sleep(2)
speak("Hi Aldan, How may I assist you?")
while 1:
data = recordAudio()
jarvis(data)
我已经尝试过os.remove();删除audio.mp3并重写它。但仍然失败
答案 0 :(得分:3)
我在TemporaryFile的帮助下修改了Speak方法。
Plunker Demo
from googletrans import Translator
import pygame, time
import tempfile
from gtts import gTTS
from pygame import mixer
from tempfile import TemporaryFile
def speak(text, lang='en'):
"""Text to speech. For funp."""
try:
translator = Translator()
tts = gTTS(text=translator.translate(text, dest=lang).text, lang=lang)
mixer.init()
sf = TemporaryFile()
tts.write_to_fp(sf)
sf.seek(0)
mixer.music.load(sf)
mixer.music.play()
except Exception:
raise

答案 1 :(得分:0)
只需创建第二个def即可删除文件名,并在您讲话()之后运行该命令
def complete():
os.remove('audio.mp3')
speak("blah blah")
complete()