【问题标题】:Phython, my script can run but nothing happensPython,我的脚本可以运行但没有任何反应
【发布时间】:2021-12-20 08:26:21
【问题描述】:

所以我一直在研究我的学校项目并取得进展,在这个阶段看起来一切正常,但由于某种原因,当我运行脚本时没有任何反应。我可以尝试删除一些代码行并尝试找出问题,但我真的不知道要删除什么,因为我现在需要代码上的所有内容。如果有人可以提供帮助,我将不胜感激。

import pyttsx3
import speech_recognition as sr
import subprocess as sp
import random
import datetime
import wikipedia
import pyjokes
import sys

Audio = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

#Inicio a libraria para fazer o script falar
def talk(text):
    engine.say(text)
    engine.runAndWait()

def Commands():
    try:
        with sr.Microphone() as source:
                print("Say something: ")
                voice = Audio.listen(source)
                Order = Audio.recognize_google(voice)
                Order = Order.lower() #Passa todo o que for dito para letras minusculas
        
                if 'ray' in Order:
                    Order = Order.replace('ray', '')
                    print(Order)
                    print("You said: " + Order)
    except: 
        pass
    return Order

def ray_commands():

    order=Commands()
    print(order)
    #Caso o utilizador queira abrir algum aplicativo o programa vai reconhecer a palavra chave
    #É capaz de abrir: Calculadora, Notepad, write e paint
    if "open" in order: 
        if "calculator" in order:
            sp.Popen('C:\\Windows\\System32\\calc.exe')
        if "Notepad" in order:
            sp.Popen('C:\\Windows\\System32\\notepad.exe')
        if "wordpad" in order:
            sp.Popen('C:\\Windows\\System32\\write.exe')
        if "paint" in order:
            sp.Popen('C:\\Windows\\system32\mspaint.exe')
                    #Algumas "dad jokes" caso o utilizador esteja numa para se rir
    if "tell me a joke" in order:
        print(pyjokes.get_joke())

        if "tell me the time" in order:
            time = datetime.datetime.now().strftime('%H:%M')
                    
                    #A palavra chave para fechar o script é close
        if "close" in order:
            print("Goodbye")
            sys.exit()
    

请忽略整个代码中的 cmets,我是葡萄牙语哈哈

【问题讨论】:

  • 你不会在某个地方调用你的函数。您必须调用它们来运行它们。

标签: python speech-recognition


【解决方案1】:

您的脚本只是声明函数,而不是运行它们。

您必须实际调用每个函数才能使其运行。

将此添加到脚本底部以按顺序运行每个函数-

talk("yourtext")
ray_commands()

我没有列出Commands(),因为ray_commands() 已经调用了这个函数。

【讨论】:

  • 嗯,这非常令人尴尬,感谢您指出我花了 30 分钟的时间寻找一些解释,这很简单,哈哈,感谢您的帮助,抱歉这个愚蠢的帖子
  • 没问题。如果这解决了您的问题,请接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
相关资源
最近更新 更多