首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gTTS与python不工作

gTTS与python不工作
EN

Stack Overflow用户
提问于 2017-07-27 09:54:41
回答 2查看 6.9K关注 0票数 0

我正在使用gTTS做一个语音识别项目。问题是,当我运行代码时,系统不会响应它。(它不会回答我的问题)据我所知,我试过了,但没能解决。如果有人能帮我解决这个问题,我会非常感激的。提前谢谢。这是我的代码:

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


def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.wav")
    os.system("audio.wav")


def recordaudio():
    # Record Audio
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        print("Say something!")
    audio = r.listen(source)
    time.sleep(2)
    # Speech recognition using Google Speech Recognition
    data = ""
    try:
        data = r.recognize_google(audio)
        print("You said: " + data)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return data


def ADA(data):
    if "how are you" in data:
        speak("I am fine")

    if "what time is it" in data:
        speak(ctime())

    if "What is your name" in data:
        speak("Call me ADA.")

    if "where is" in data:
        data = data.split(" ")
        location = data[2]
        speak("Hold on Sir, I will show you where " + location + " is.")
        os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")


# initialization
time.sleep(2)
speak("Hi Touseef, what can I do for you?")
while 1:
   data = recordaudio()
   ADA(data) 

我分别测试了语音识别和gtts库,以检查它们是否正常工作。他们俩都没什么问题。但是当我试图在我的实际代码中使用它们时,有些地方出了问题,我无法弄清楚。

下面是库的代码片段。

gTTS

代码语言:javascript
复制
from gtts import gTTS
import os
tts = gTTS(text='Helllo, Good morning my name is ADA. How can I help you?', lang='en')
tts.save("good.mp3")
os.system("good.mp3")

SpeechRecognition

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

r = sr.Recognizer()
with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source) 
    print("Say something!")
    audio = r.listen(source)


try:
   print(r.recognize_google(audio))
except sr.UnknownValueError:
   print("Could not understand audio")
except sr.RequestError as e:
   print("Could not request results from Google Speech Recognition service; {0}".format(e))

我是个学生,这是我的学术项目。谁来帮我弄清楚。

EN

回答 2

Stack Overflow用户

发布于 2017-07-27 10:22:36

试试这个;

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


def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio1.wav")
    os.system("audio1.wav")


def recordaudio():
    # obtain audio from the microphone
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print ('Attention! Say something')
        audio = r.listen(source)

    # recognize speech using Google Speech Recognition
    try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
        data='something'
        data = r.recognize_google(audio,language='en-US')

    except sr.UnknownValueError:
        print ('Attention ! Google could not understand audio')
        data='Could not understand anything'
    except sr.RequestError as e:

       print ('Attention ! Could not request results from Google service.')

    return data


def ADA(data):
    if "how are you" in data:
        speak("I am fine")

    if "what time is it" in data:
        speak(ctime())

    if "What is your name" in data:
        speak("Call me ADA.")

    if "where is" in data:
        data = data.split(" ")
        location = data[2:]
        speak("Hold on Sir, I will show you where " + location + " is.")
        os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")


# initialization
time.sleep(2)
#speak("Hi Touseef, what can I do for you?")
while 1:
   data = recordaudio()
   ADA(data) 

而不是使用gTTS,您可以使用不需要网络操作的pyTTsx

代码语言:javascript
复制
import pyttsx
engine = pyttsx.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-40)
engine.say('how are yo8u? 123456789 123 1 2 3 4 5 6 7 8 9')
engine.runAndWait()

this.Hope你的问题被解决了。

你的新代码变成;

代码语言:javascript
复制
import pyttsx


def speak(audioString):
    print(audioString)    
    engine = pyttsx.init()
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate-40)
    engine.say(audioString)
    engine.runAndWait()
票数 0
EN

Stack Overflow用户

发布于 2021-02-22 23:35:59

我在gtts上也有类似的问题(没有生成文本到语音的音频),我通过如下更新模块来修复它:

代码语言:javascript
复制
pip install -U gtts
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45347298

复制
相关文章

相似问题

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