Basic Voice Assistance with Python



我是Python的新手,我正在与我的老师在Raspberry Pi 3上开发一个基本的语音辅助。这个项目中的代码是由我收集和修复的。然而,作为一个新手,有很多问题我无法回答。你能帮我一下吗?我想开发一个助手,可以在谷歌上搜索,在YouTube上播放音乐,并显示天气预报。AI.py是我的主文件,Google_Search.py用于搜索,YouTube_Search.py用于播放音乐。当我请求搜索时,AI.py将导入Google_Search.py文件来搜索我的请求。这与YouTube文件类似。

问题是Google_Search.py和YouTube_search.py只是继续运行,不停止或允许我继续使用AI.py文件。我想运行这两个文件,而我仍然可以运行AI.py文件来停止或重新打开它们。这是密码,请帮我一下。非常感谢。

这是AI.py文件

import speech_recognition
import pyaudio
import pyttsx3
import time
import os
import webbrowser, sys, imp
import requests, json
import vlc
import pafy
from datetime import date
from datetime import datetime
from googlesearch import search
robot_ear = speech_recognition.Recognizer()
robot_mouth = pyttsx3.init()
robot_brain = ""

api_key = "82328b3addcd3f984da6c1e74cf4c7c9"
base_url = "http://api.openweathermap.org/data/2.5/weather?"
city_name = "Melbourne,au"
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
x = response.json() 
while True:
with speech_recognition.Microphone() as mic:
print("Robot: I'm Listening")
audio = robot_ear.adjust_for_ambient_noise(mic)
audio = robot_ear.listen(mic, phrase_time_limit=3)

print("Robot: ...")
try:
you = robot_ear.recognize_google(audio, language = "en")
except:
you = ""

print("You: " + you)

if you == "":
robot_brain = "I can't hear you, please try again"
elif "hello" in you:
robot_brain = "Hello Minh"

elif "play some music" in you:
import YouTube_Search
robot_brain = "Music is playing"
elif "stop music" in you:
os.system("pkill YouTube_Search")
robot_brain = "Music is stoped"
elif "search something" in you:
robot_brain = "What do you want me to search for?"
import Google_Search
imp.reload(Google_Search)
time.sleep(5)
elif "stop searching" in you:
os.system("pkill chromium")
robot_brain = "Google search is closed"

elif "weather today" in you:
if x["cod"] != "404":
y = x["main"]
current_pressure = y["pressure"]
current_humidiy = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
robot_brain = (" Temperature: " +
str(current_temperature) + " °F" + 
"n atmospheric pressure: " +
str(current_pressure) + " hPa" +
"n humidity: " +
str(current_humidiy) + " %" +
"n weather today: " +
str(weather_description))

elif "bye" in you:
robot_brain = "Bye Minh"
print("Robot: " + robot_brain)
robot_mouth.say(robot_brain)
robot_mouth.runAndWait()
break
else:
robot_brain = "I'm learning"
print("Robot: " + robot_brain)
robot_mouth.say(robot_brain)
robot_mouth.runAndWait()

这是Google_Search.py文件

import speech_recognition
import os, sys
import webbrowser
robot_brain = ""
robot_ear = speech_recognition.Recognizer()
with speech_recognition.Microphone() as mic:
#print("Robot: What do you want me to search for?")
audio = robot_ear.adjust_for_ambient_noise(mic)
audio = robot_ear.listen(mic, phrase_time_limit=2)
try:
you = robot_ear.recognize_google(audio, language = 'en-US')
except:
you = ""

search_terms = [you]

# ... construct your list of search terms ...

for term in search_terms:
url = "https://www.google.com/search?q={}".format(term)
webbrowser.open_new_tab(url)
robot_brain = ("Here is what I found for") + (" ") + str(you)

print("Robot: " + robot_brain)
os.system("pkill Google_Search")

这是YouTube_Search.py

import vlc
import pafy
import time
import urllib.request
import urllib.parse
import re
import speech_recognition

robot_ear = speech_recognition.Recognizer()
with speech_recognition.Microphone() as mic:
print("Robot: What do you want me to search for?")
audio = robot_ear.adjust_for_ambient_noise(mic)
audio = robot_ear.listen(mic, phrase_time_limit=2)
try:
you = robot_ear.recognize_google(audio, language = 'en-US')
except:
you = ""
search = you
query_string = urllib.parse.urlencode({"search_query" : search})
html_content = urllib.request.urlopen("http://www.youtube.com/results?search_query="+query_string)
search_results = re.findall(r"watch?v=(S{11})", html_content.read().decode())
#print("http://www.youtube.com/watch?v=" + search_results[0])
url = "http://www.youtube.com/watch?v=" + search_results[0]
video = pafy.new(url)
best = video.getbest()
playurl = best.url
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()

Python中的import关键字用于将其他Python源代码文件加载到当前解释器会话中。导入文件时,将首先运行该文件。如果您想调用导入文件中的程序,您应该使用函数并调用它。例如:

test.py

def myFunction():
print("Hello")

main.py

import test
test.myFunction()

最新更新