【问题标题】:gTTS with python not working带有python的gTTS不起作用
【发布时间】:2017-07-27 09:54:41
【问题描述】:

我正在使用 gTTS 进行语音识别项目。问题是,当我运行代码时,系统没有响应。 (它不会回答我的查询)我根据我的知识进行了尝试,但无法解决。如果有人能帮我解决这个问题,我将不胜感激。首先十分感谢。 这是我的代码:

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) 

我已经分别测试了speechrecognition 和gtts 库以检查它们是否正常工作。他们俩都没有错。但是当我尝试在我的实际代码中使用它们时,出现了问题,我无法弄清楚。

这里是库的代码 sn-ps。

gTTS

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")

语音识别

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))

我是一名学生,这是我的学术项目。请有人帮我解决这个问题。

【问题讨论】:

    标签: python speech-recognition text-to-speech speech-to-text


    【解决方案1】:

    试试这个;

    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) 
    

    您可以使用pyTTsx 而不是使用 gTTS,它不需要 net 就可以像这样操作

    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()
    

    这个。希望你的问题得到解决。

    你的新代码变成了;

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

    【讨论】:

    • 它对我不起作用。现在它停留在“注意,说点什么”之后,什么也没有弹出。该程序继续假装它仍在收听。
    • 你对着麦克风说了什么吗?你用的是什么操作系统?我在 win xp 上测试过它就像黄油一样工作
    • 这里是视频链接link
    • 我使用的是 Windows 10 x64。我说了不同的话,所以它可能会响应,但它没有。
    • 让我看看链接
    【解决方案2】:

    我在使用 gtts 时遇到了类似的问题(没有生成文本到语音的音频),我通过像这样更新模块来修复它:

    pip install -U gtts
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2015-11-06
      • 2021-06-29
      • 2019-01-23
      • 2011-01-15
      • 1970-01-01
      相关资源
      最近更新 更多