当前位置:   article > 正文

Python只需要三十行代码,打造一款简单的人工语音对话_could not request results from google speech recog

could not request results from google speech recognition service

gtts
gtts是将文字转化为语音,但是需要在VPN下使用。这个因为要接谷歌服务器。

具体gtts的官方文档

下面,让我们看一段简单的的代码

from gtts import gTTS

def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    os.system("audio.mp3")
    
speak("Hi Runsen, what can I do for you?")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

执行上面的代码,就可以生成一个mp3文件,播放就可以听到了Hi Runsen, what can I do for you?。这个MP3会自动弹出来的。

speech_recognition
speech_recognition用于执行语音识别的库,支持在线和离线的多个引擎和API。

speech_recognition具体官方文档

安装speech_recognition可以会出现错误,对此解决的方法是通过该网址安装对应的whl包

在官方文档中提供了具体的识别来自麦克风的语音输入的代码
在这里插入图片描述
下面就是 speech_recognition 用麦克风记录下你的话,这里我使用的是
recognize_google,speech_recognition 提供了很多的类似的接口。

import time
import speech_recognition as sr

# 录下来你讲的话
def recordAudio():
    # 用麦克风记录下你的话
    print("开始麦克风记录下你的话")
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
    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

if __name__ == '__main__':
    time.sleep(2)
    while True:
        data = recordAudio()
        print(data)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

下面是我乱说的英语
在这里插入图片描述
对话
上面,我们实现了用麦克风记录下你的话,并且得到了对应的文本,那么下一步就是字符串的文本操作了,比如说how are you,那回答"I am fine”,然后将"I am fine”通过gtts是将文字转化为语音

# @Author:Runsen
# -*- coding: UTF-8 -*-
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS


# 讲出来AI的话
def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    os.system("audio.mp3")


# 录下来你讲的话
def recordAudio():
    # 用麦克风记录下你的话
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)

    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


# 自带的对话技能(逻辑代码:rules)
def jarvis():
    while True:
        data = recordAudio()
        print(data)
        if "how are you" in data:
            speak("I am fine")
        if "time" in data:
            speak(ctime())
        if "where is" in data:
            data = data.split(" ")
            location = data[2]
            speak("Hold on Runsen, I will show you where " + location + " is.")
            # 打开谷歌地址
            os.system("open -a Safari https://www.google.com/maps/place/" + location + "/&")

        if "bye" in data:
            speak("bye bye")
            break


if __name__ == '__main__':
    # 初始化
    time.sleep(2)
    speak("Hi Runsen, what can I do for you?")

    # 跑起
    jarvis()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

在这里插入图片描述
当我说how are you?会弹出I am fine的mp3
在这里插入图片描述
当我说where is Chiana?会弹出Hold on Runsen, I will show you where China is.的MP3
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/940524
推荐阅读
相关标签
  

闽ICP备14008679号