首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gTTS程序无响应

gTTS程序无响应
EN

Stack Overflow用户
提问于 2020-12-31 17:03:37
回答 1查看 49关注 0票数 1

我正在尝试编写一个gTTS python程序,将文本转换为语音,并接受命令,但当我运行代码时,我根本没有得到任何响应,终端中没有错误,也没有声音播放,我不确定要做什么,我的程序中也没有看到任何错误。我在MacOS上使用Sublime Text。请帮帮我!!

代码语言:javascript
复制
from gtts import gTTS
import os 
import time
import playsound
import speech_recognition as sr


def speak(text):
    tts = gTTS(text=text, Lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

        return said

text = get_audio()

if "hello" in text:
    speak("hello, how are you?")

if "What is your name" in text:
    speak("My name is John")
EN

回答 1

Stack Overflow用户

发布于 2020-12-31 17:15:03

尝试将"pttsx3“库与此一起使用。下面是一些示例代码。这应该接受命令,然后返回给您。

代码语言:javascript
复制
import pyttsx3
import speech_recognition as sr
import wikipedia

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



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

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:
        print("Say that again please...")
        return "None"
    return query

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

        # code for executing tasks based on the query or input
        if 'wikipedia' in query:
            speak("Searching Wikipedia...")
            query = query.replace("wikipedia", "")
            results = wikipedia.summary(query, sentences = 3)
            speak("According to Wikipedia")
            print(results)
            speak(results)
        if 'hi' in query:
            speak('hello')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65518756

复制
相关文章

相似问题

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