自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python只需要三十行代碼,打造一款簡(jiǎn)單的人工語音對(duì)話

開發(fā) 后端
今天,我使用gtts和speech_recognition,教大家如何通過三十行代碼,打造一款簡(jiǎn)單的人工語音對(duì)話。思路就是將語音變成文本,然后文本變成語音。

?[[402986]]?

1876年,亞歷山大·格雷厄姆·貝爾(Alexander Graham Bell)發(fā)明了一種電報(bào)機(jī),可以通過電線傳輸音頻。托馬斯·愛迪生(Thomas Edison)于1877年發(fā)明了留聲機(jī),這是第一臺(tái)記錄聲音并播放聲音的機(jī)器。

最早的語音識(shí)別軟件之一是由Bells Labs在1952年編寫的,只能識(shí)別數(shù)字。1985年,IBM發(fā)布了使用“隱馬爾可夫模型”的軟件,該軟件可識(shí)別1000多個(gè)單詞。

幾年前,一個(gè)replace("?","")代碼價(jià)值一個(gè)億

如今,在Python中Tensorflow,Keras,Librosa,Kaldi和語音轉(zhuǎn)文本API等多種工具使語音計(jì)算變得更加容易。

今天,我使用gtts和speech_recognition,教大家如何通過三十行代碼,打造一款簡(jiǎn)單的人工語音對(duì)話。思路就是將語音變成文本,然后文本變成語音。

gtts

gtts是將文字轉(zhuǎn)化為語音,但是需要在虛擬網(wǎng)絡(luò)下使用。這個(gè)因?yàn)橐庸雀璺?wù)器。

具體gtts的官方文檔:

下面,讓我們看一段簡(jiǎn)單的的代碼

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

執(zhí)行上面的代碼,就可以生成一個(gè)mp3文件,播放就可以聽到了Hi Runsen, what can I do for you?。這個(gè)MP3會(huì)自動(dòng)彈出來的。

speech_recognition

speech_recognition用于執(zhí)行語音識(shí)別的庫,支持在線和離線的多個(gè)引擎和API。

speech_recognition具體官方文檔

安裝speech_recognition可以會(huì)出現(xiàn)錯(cuò)誤,對(duì)此解決的方法是通過該網(wǎng)址安裝對(duì)應(yīng)的whl包

在官方文檔中提供了具體的識(shí)別來自麥克風(fēng)的語音輸入的代碼

??

下面就是 speech_recognition 用麥克風(fēng)記錄下你的話,這里我使用的是 recognize_google,speech_recognition 提供了很多的類似的接口。

import time
import speech_recognition as sr

# 錄下來你講的話
def recordAudio():
# 用麥克風(fēng)記錄下你的話
print("開始麥克風(fēng)記錄下你的話")
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)

下面是我亂說的英語

??

對(duì)話

上面,我們實(shí)現(xiàn)了用麥克風(fēng)記錄下你的話,并且得到了對(duì)應(yīng)的文本,那么下一步就是字符串的文本操作了,比如說how are you,那回答"I am fine”,然后將"I am fine”通過gtts是將文字轉(zhuǎn)化為語音

# @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():
# 用麥克風(fēng)記錄下你的話
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


# 自帶的對(duì)話技能(邏輯代碼: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()

 ??

當(dāng)我說how are you?會(huì)彈出I am fine的mp3

??

當(dāng)我說where is Chiana?會(huì)彈出Hold on Runsen, I will show you where China is.的MP3

??

同樣也會(huì)彈出China的谷歌地圖

本項(xiàng)目對(duì)應(yīng)的Github

??https://github.com/MaoliRUNsen/Simple-intelligent-voice-dialogue??

 

責(zé)任編輯:姜華 來源: Python之王
相關(guān)推薦

2022-10-08 06:26:48

人工智能機(jī)器學(xué)習(xí)藝術(shù)

2015-11-27 09:18:11

AngularJSWeb應(yīng)用

2023-12-06 18:09:01

2020-12-03 09:00:02

Java外賣系統(tǒng)

2020-12-07 11:50:14

Java學(xué)習(xí)系統(tǒng)eclipse

2018-08-03 12:21:02

2014-12-16 10:11:22

2019-07-15 15:20:52

Python語音識(shí)別機(jī)器人

2018-08-01 09:22:29

人工智能機(jī)器學(xué)習(xí)機(jī)器人

2017-01-13 08:37:57

PythonAlphaGoMuGo

2022-03-14 09:57:30

Python代碼

2021-12-16 22:51:03

手機(jī)功能定位

2020-11-08 14:44:37

VSCode代碼編碼

2020-02-25 23:36:04

代碼開發(fā)工具

2018-03-07 10:03:40

2009-04-09 16:52:47

LinuxUbuntu 9.04

2023-05-23 10:01:51

冪等性抽象代數(shù)

2019-10-28 11:30:43

架構(gòu)數(shù)據(jù)結(jié)構(gòu)布隆過濾器

2023-10-11 12:45:49

Windows系統(tǒng)

2020-08-03 09:48:04

Python 機(jī)器學(xué)習(xí)代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)