0

私はgTTSを使った音声認識プロジェクトに取り組んでいます。問題は、私がコードを実行すると、システムはそれに応答しないということです。 (それは私の質問に答えることはできません)私は私の知る限り試しましたが、解決できませんでした。もし誰かがこれを解決するのを助けることができたら、本当に感謝します。非常に前もってありがとう。ここ は私のコードです:gTTS with python does not working

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) 

私は、彼らが作業したりしていないかどうかを確認するために、別途音声認識やCisco ITPの初期導入のライブラリをテストしています。両方とも間違っていることはありません。しかし、私が実際のコードでそれらを使用しようとすると、何かがうまくいかず、わかりません。

以下は、ライブラリのコードスニペットです。

Cisco ITPの初期導入

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

私は学生ですし、これは私の学術的なプロジェクトです。誰かがこれを理解するのを助けてください。

答えて

0

これを試してください。

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) 

の代わりに、あなたは問題が解決さrはthis.Hope

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

のように動作させるためのネットを必要としないpyTTsxを使用することができますCisco ITPの初期導入を使用。

新しいコードは次のようになります。

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

私にとってはうまくいかなかった。今、それは "注意、何かを言いなさい"で立ち往生し、この後に何もポップアップしない。プログラムはそれがまだ聞いているふりをし続けます。 –

+0

あなたはマイクの中で何かを言っていましたが、どのようなOSを使っていますか?私はこれをwin xpでテストしました。それはバターのように働きました –

+0

ここにビデオリンクです[リンク](https://drive.google.com/file/d/0B7ccI33Aew5fRlNYbE5MeUZ5YU0/view?usp=sharing) –