30行Python代碼打造一款簡單的人工語音對話
@Author:Runsen
1876年,亞歷山大·格雷厄姆·貝爾(Alexander Graham Bell)發(fā)明了一種電報機,可以通過電線傳輸音頻。托馬斯·愛迪生(Thomas Edison)于1877年發(fā)明了留聲機,這是第一臺記錄聲音并播放聲音的機器。
最早的語音識別軟件之一是由Bells Labs在1952年編寫的,只能識別數(shù)字。1985年,IBM發(fā)布了使用“隱馬爾可夫模型”的軟件,該軟件可識別1000多個單詞。
幾年前,一個replace("?","")代碼價值一個億
如今,在Python中Tensorflow,Keras,Librosa,Kaldi和語音轉(zhuǎn)文本API等多種工具使語音計算變得更加容易。
今天,我使用gtts和speech_recognition,教大家如何通過三十行代碼,打造一款簡單的人工語音對話。思路就是將語音變成文本,然后文本變成語音。
gtts
gtts是將文字轉(zhuǎn)化為語音,但是需要在VPN下使用。這個因為要接谷歌服務(wù)器。
具體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?")
執(zhí)行上面的代碼,就可以生成一個mp3文件,播放就可以聽到了Hi Runsen, what can I do for you?。這個MP3會自動彈出來的。
speech_recognition
speech_recognition用于執(zhí)行語音識別的庫,支持在線和離線的多個引擎和API。
speech_recognition具體官方文檔
安裝speech_recognition可以會出現(xiàn)錯誤,對此解決的方法是通過該網(wǎng)址安裝對應(yīng)的whl包
在官方文檔中提供了具體的識別來自麥克風(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)
下面是我亂說的英語

對話
上面,我們實現(xiàn)了用麥克風(fēng)記錄下你的話,并且得到了對應(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
# 自帶的對話技能(邏輯代碼: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?會彈出I am fine的mp3

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

同樣也會彈出China的谷歌地圖

本項目對應(yīng)的Github
以上就是30行Python代碼打造一款簡單的人工語音對話的詳細內(nèi)容,更多關(guān)于Python人工語音對話的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python中用函數(shù)作為返回值和實現(xiàn)閉包的教程
這篇文章主要介紹了Python中用函數(shù)作為返回值和實現(xiàn)閉包的教程,代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
py3nvml實現(xiàn)GPU相關(guān)信息讀取的案例分析
這篇文章主要介紹了py3nvml實現(xiàn)GPU相關(guān)信息讀取,此時就可以考慮使用py3nvml這樣的工具,針對于GPU任務(wù)執(zhí)行的過程進行細化的分析,有助于提升GPU的利用率和程序執(zhí)行的性能,需要的朋友可以參考下2022-01-01
Python調(diào)用Prometheus監(jiān)控數(shù)據(jù)并計算
Prometheus是一套開源監(jiān)控系統(tǒng)和告警為一體,由go語言(golang)開發(fā),是監(jiān)控+報警+時間序列數(shù)據(jù)庫的組合。本文將介紹Python如何調(diào)用Prometheus實現(xiàn)數(shù)據(jù)的監(jiān)控與計算,需要的可以參考一下2021-12-12
用Python編寫一個簡單的FUSE文件系統(tǒng)的教程
這篇文章主要介紹了用Python編寫一個簡單的FUSE文件系統(tǒng)的教程,對于數(shù)據(jù)的備份很有幫助,需要的朋友可以參考下2015-04-04
python將字符串列表轉(zhuǎn)換為數(shù)值列表的幾種方法
這篇文章主要介紹了python將字符串列表轉(zhuǎn)換為數(shù)值列表的幾種方法,包括使用map()和float()/int()、列表推導(dǎo)式、pandas庫以及numpy庫,每種方法都有其適用場景,如小型列表、靈活轉(zhuǎn)換、處理復(fù)雜數(shù)據(jù)集和大規(guī)模數(shù)據(jù)計算等,需要的朋友可以參考下2025-04-04

