首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python的基本语音辅助

Python的基本语音辅助
EN

Stack Overflow用户
提问于 2021-01-26 01:33:13
回答 1查看 70关注 0票数 0

我是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文件

代码语言:javascript
复制
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文件

代码语言:javascript
复制
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

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2021-01-31 04:35:39

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

test.py

代码语言:javascript
复制
def myFunction():
  print("Hello")

main.py

代码语言:javascript
复制
import test
test.myFunction()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65894755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档