0

我已经为 Windows 制作了这个 python 程序。请帮我竞选 Raspberry Pi。这是一个语音聊天机器人程序,用于识别命令并根据它回复任务。它在 windows python IDE 上运行良好,但遗憾的是在 Raspberry Pi 上没有响应。请帮我解决这个问题并告诉我如何在 Raspberry 4 中运行这个程序

此行错误:

engine = pyttsx3.init('sapi5') voices = engine.getProperty('voices') 
# print(voices[1].id) engine.setProperty('voice', voices[0].id)

程序:

import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speechRecognition
import datetime
import wikipedia #pip install wikipedia
import webbrowser
import os
import smtplib
import time
import pyfirmata
from pygame import mixer

try:

    board = pyfirmata.Arduino('COM5')
    led = board.get_pin('d:6:o')
except:
    print("Arduino is not connected")
x=0

def songPlayer(x):
    i =x

    music_dir = 'E:\\Songs'
    songs = os.listdir(music_dir)
    mixer.init()
    mixer.music.load(os.path.join(music_dir, songs[i]))
    mixer.music.play()


engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)


def speak(audio):
    engine.say(audio)
    engine.runAndWait()


def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning!")

    elif hour>=12 and hour<18:
        speak("Good Afternoon!")   

    else:
        speak("Good Evening!")  

    speak("I am cham Sir. Please tell me how may I help you")       

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")    
        query = r.recognize_google(audio, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:
        # print(e)    
        print("Say that again please...")  
        return "None"
    return query



if __name__ == "__main__":
    wishMe()
    while True:
    # if 1:
        query = takeCommand().lower()

        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            try:

                query = query.replace("wikipedia", "")
                results = wikipedia.summary(query, sentences=2)
                speak("According to Wikipedia")
                print(results)
                speak(results)

            except:
                speak("sorry I unable to find it")

        elif 'open youtube' in query:
            webbrowser.open("youtube.com")

        elif 'open google' in query:
            webbrowser.open("google.com") 


        elif 'What is your name' in query:
            speak("I am cham sir")


        elif 'who created you' in query:
            speak("mr. logesh mohan created me")

        elif 'play music' in query:
            speak("playing Music")
            songPlayer(x)


        elif 'next song' in query:
            x+=1
            songPlayer(x)


        elif 'previous song' in query:
            x-=1
            songPlayer(x)


        elif 'stop music' in query:
            mixer.music.stop()


        elif 'the time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")    
            speak(f"Sir, the time is {strTime}")

        elif 'light on' in query:
             led.write(1)

        elif 'light off' in query:
             led.write(0)

        elif 'make a call' in query:
            print(calling)





        elif 'go and sleep' in query:
            exit()

这是一个人工智能助手的程序

4

1 回答 1

0

Pyttsx3 代码初始化从 windows 更改为 Linux,因为语音 ID 会有所不同。

尝试调整此代码段,这与 Linux Ubuntu 完美配合:

import pyttsx3

engine = pyttsx3.init()

engine.setProperty('rate', 150)
engine.setProperty('volume', 0.5)
voices = engine.getProperty('voices')
for voice in voices:
    engine.setProperty('voice', voice.id)
    # engine.say('The quick brown fox jumped over the lazy dog.')
    engine.say("Hello, I am robot")
engine.runAndWait()

您可以从可用列表中选择一个语音 ID,并使用 ID 常量来初始化引擎。

由于您没有提到错误日志,因此也可能是因为您的系统上没有安装 espeak

你可以试试这个来修复 espeak 错误:

sudo apt-get update && apt-get upgrade -y && apt-get install espeak

如果问题仍然存在,请添加错误日志,以便我们进一步提供帮助。

于 2020-03-06T09:24:03.147 回答