1876年,AlexanderGrahamBell发明了一种可以通过电线传输音频的电报机。托马斯·爱迪生(ThomasEdison)于1877年发明了留声机,这是第一台能够录制和播放声音的机器。贝尔实验室于1952年编写的首批语音识别软件之一只能识别数字。1985年,IBM发布了使用“隐马尔可夫模型”的软件,可以识别1000多个单词。几年前,一个replace("?","")的代码就值一个亿。如今,Tensorflow、Keras、Librosa、Kaldi和Python中的语音转文本API等各种工具使语音计算变得更加容易。今天,我用gtts和speech_recognition教大家用30行代码制作一个简单的人工语音对话。这个想法是将语音转换为文本,然后将文本转换为语音。gttsgtts将文本转换为语音,但需要在虚拟网络下使用。这是因为它需要连接到谷歌服务器。具体的gtts官方文档:下面,让我们看一段来自gttsimportgTTSdefspeak(audioString):print(audioString)tts=gTTS(text=audioString,lang='en')tts.save("audio.mp3")os.system的简单代码("audio.mp3")speak("HiRunsen,whatcanIdoforyou?")执行上面的代码生成一个mp3文件,然后就可以听到HiRunsen,whatcanIdoforyou?播放后。MP3将自动弹出。speech_recognitionspeech_recognition是一个用于执行语音识别的库,支持在线和离线的多个引擎和API。Speech_recognition具体官方文档安装speech_recognition可能会出错。解决办法是通过这个网站安装对应的whl包。官方文档提供了识别麦克风输入语音的具体代码。下面是speech_recognition。使用麦克风记录你的话,这里我使用recognize_google,speech_recognition提供了很多类似的接口。importtimeimportspeech_recognitionassr#记录你的话defrecordAudio():#用麦克风记录你的话print("启动麦克风记录你的话")r=sr.Recognizer()withsr.Microphone()assource:audio=r.listen(source)data=""try:data=r.recognize_google(audio)print("Yousaid:"+data)exceptsr.UnknownValueError:print("GoogleSpeechRecognitioncouldnotunderstandaudio")exceptsr.RequestErrorase:print("CouldnotrequestresultsfromGoogleSpeechRecognitionservice;{0}".format(e))returndataif__name__=='__main__':time.sleep(2)whileTrue:data=recordAudio()print(data)下面是我上面的废话英文对话,我们实现了用麦克风录你的话,得到了对应的text,那么接下来就是字符串的文本操作,比如你好吗,然后回答“Iamfine”,然后“Iamfine”通过gtts转换成语音#@作者:润森#-*-编码:UTF-8-*-importspeech_recognitionassrfromtimeimportctimeimporttimeimportosfromgttsimportgTTS#SpeakA我的话defspeak(audioString):print(audioString)tts=gTTS(text=audioString,lang='en')tts.save("audio.mp3")os.system("audio.mp3")#Recordwhatyousaid定义recordAudio():#用麦克风记录你的话r=sr.Recognizer()withsr.Microphone()assource:audio=r.listen(source)data=""try:data=r.recognize_google(audio)print("Yousaid:"+data)exceptsr.UnknownValueError:print("GoogleSpeechRecognitioncouldnotunderstandaudio")exceptsr.RequestErrorase:print("CouldnotrequestresultsfromGoogleSpeechRecognitionservice;{0}".format(e))returndata#内置对话技巧(逻辑代码:规则)defjarvis():whileTrue:data=recordAudio()print(data)if"howareyou"indata:speak("Iamfine")if"time"indata:speak(ctime())if"whereis"indata:data=data.split("")location=data[2]speak("HoldonRunsen,Iwillshowyouwhere"+location+"is.")#打开谷歌地址os.system("open-aSafarihttps://www.google.com/maps/place/"+location+"/&")if"bye"indata:speak("byebye")breakif__name__=='__main__':#Initializationtime.sleep(2)speak("HiRunsen,whatcanIdoforyou?")#Runjarvis()当我说Howareyou?时,会弹出Iamfine的mp3。当我说Chiana在哪里时,HoldonRunsen的MP3,我会告诉你中国在哪里。还会弹出中国的谷歌地图。本项目对应Githubhttps://github.com/MaoliRUNsen/Simple-intelligent-voice-dialogue
