要想让机器人变得聪明,也得教会它响应的内容,但凭一己之力终究教的内容是有限的,那么就联网从网上获取相应的内容吧。而联网就需要一种接口来来连接机器人和网络,这种接口有一个名字叫做应用程序接口(Application Programming Interface),简称API,因为网络上的有用的信息都是别人做好的,我们要想调用的话一般就需要注册他们对应平台的账号,然后付费使用api,
    image.png
    当然也有一些免费开放的api供开发测试。
    现在我们就联网查询一下天气信息而不是我们自己手写的天气内容,这里就用到了天气API。
    http://wthrcdn.etouch.cn/weather_mini?city=北京
    把上面城市名称可以换成自己所在城市

    1. url = "http://wthrcdn.etouch.cn/weather_mini?city=郑州"

    要从网络上获取url地址的内容的话,需要pip install requests安装这样一个库,然后调用该库的get方法,get到整个网址里面的所有内容,而我们只需要里面text的内容,所以再取其text就行:

    1. import requests
    2. url = "http://wthrcdn.etouch.cn/weather_mini?city=郑州"
    3. res = requests.get(url)
    4. txt = res.text
    5. print(txt)

    如此我们就拿到了郑州近一周的天气情况,该天气格式为json。
    image.png
    然后就可以再取出具体的内容通过语音合成播放出来了。
    或者使用百度天气的API:
    http://api.map.baidu.com/telematics/v3/weather?location={城市名}&output={返回格式}&ak={百度AppKey}
    而偶然发现的一个apihttps://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=关键词f&json=1,可以把在对话中要问的问题给进行进一步联想从而逼疯提问者,可以一直跟你打太极,只需要适时替换关键词。
    除了天气之外,如果想要让改机器人更加聪明,比如能讲笑话、能背古诗、能回答一些百科知识,就需要特定的API接口了,那么有没有更加方便的呢?也就是智能聊天机器人的API,这里推荐使用有图灵机器人、小i机器人和青云客提供的在线机器人接口。其中图灵机器人的效果应该是最为出色的,但是要花钱包月,而小i机器人的连接也时断时续,所以我们挑选了青云客提供的在线机器人。不用注册账户直接使用。
    image.png

    1. import requests
    2. url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=郑州天气"
    3. res = requests.get(url)
    4. txt = res.text
    5. print(txt)

    返回结果如下:
    image.png

    1. import requests
    2. url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=讲个笑话"
    3. res = requests.get(url)
    4. txt = res.text
    5. print(txt)

    返回结果如下:
    image.png
    也就是只需要用存储我们讲话的变量替换掉url里面最后的关键词即可:

    1. import requests
    2. content = "讲个笑话"
    3. url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+content
    4. res = requests.get(url)
    5. txt = res.text
    6. print(txt)

    现在再把之前写的能够语音对答的程序拿过来:

    1. import json
    2. from aip import AipSpeech
    3. import pyaudio
    4. import wave
    5. input_filename = "input.wav" # 麦克风采集的语音输入
    6. input_filepath = "../src/" # 输入文件的path
    7. in_path = input_filepath + input_filename
    8. def get_audio(filepath):
    9. CHUNK = 256
    10. FORMAT = pyaudio.paInt16
    11. CHANNELS = 1 # 声道数
    12. RATE = 11025 # 采样率
    13. RECORD_SECONDS = 5
    14. WAVE_OUTPUT_FILENAME = filepath
    15. p = pyaudio.PyAudio()
    16. stream = p.open(format=FORMAT,
    17. channels=CHANNELS,
    18. rate=RATE,
    19. input=True,
    20. frames_per_buffer=CHUNK)
    21. print("*"*10, "开始录音:请在5秒内输入语音")
    22. frames = []
    23. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    24. data = stream.read(CHUNK)
    25. frames.append(data)
    26. print("*"*10, "录音结束\n")
    27. stream.stop_stream()
    28. stream.close()
    29. p.terminate()
    30. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    31. wf.setnchannels(CHANNELS)
    32. wf.setsampwidth(p.get_sample_size(FORMAT))
    33. wf.setframerate(RATE)
    34. wf.writeframes(b''.join(frames))
    35. wf.close()
    36. #申请百度语音识别
    37. APP_ID = '16835749'
    38. API_KEY = 'UnZlBOVhwYu8m5eNqwOPHt99'
    39. SECRET_KEY = '6jhCitggsR0Ew91fdC47oMa1qtibTrsK'
    40. client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    41. # 读取文件
    42. def get_file_content(filePath):
    43. with open(filePath, 'rb') as fp:
    44. return fp.read()
    45. def result_word():
    46. # 识别本地文件
    47. # path='/Users/alice/Documents/Blog/AI/语音识别/speechrecognition/audiofiles'
    48. get_audio(in_path)
    49. test1 = client.asr(get_file_content(in_path), 'pcm', 16000, {'dev_pid': 1536, })
    50. # print(test1["result"])
    51. if test1["err_no"]==3307:
    52. test1["result"]=["请讲话"]
    53. return test1["result"][0]
    54. print(result_word())
    55. from playsound import playsound
    56. def playAudio(txt):
    57. resultAudio = client.synthesis(txt, 'zh', 1, { 'vol': 2,'per':4 })
    58. if not isinstance(resultAudio, dict):
    59. with open('audio.mp3', 'wb') as f:
    60. f.write(resultAudio)
    61. playsound("audio.mp3")
    62. import os
    63. os.remove("audio.mp3")
    64. with open("语料库.txt","r",encoding="utf-8") as lib:
    65. lists = lib.readlines()
    66. # print(lists)
    67. # print(len(lists))
    68. while True:
    69. print("请开始对话:")
    70. ask = result_word()
    71. a = 0
    72. b = 0
    73. while a==0:
    74. for i in range(0,len(lists),2):
    75. if lists[i].find(ask)!=-1:
    76. print(lists[i+1][2:])
    77. playAudio(lists[i+1][2:])
    78. b =1
    79. break
    80. a = 1
    81. if b==0:
    82. print("你问的问题太深奥,我还没学会呢,换个问题吧")
    83. playAudio("你问的问题太深奥,我还没学会呢,换个问题吧")
    84. print("请开始对话:")
    85. ask = result_word()
    86. a = 0

    然后进行修改替换如下:就是把whlie循环里面的内容替换掉:

    1. import json
    2. from aip import AipSpeech
    3. import pyaudio
    4. import wave
    5. input_filename = "input.wav" # 麦克风采集的语音输入
    6. input_filepath = "../src/" # 输入文件的path
    7. in_path = input_filepath + input_filename
    8. def get_audio(filepath):
    9. CHUNK = 256
    10. FORMAT = pyaudio.paInt16
    11. CHANNELS = 1 # 声道数
    12. RATE = 11025 # 采样率
    13. RECORD_SECONDS = 5
    14. WAVE_OUTPUT_FILENAME = filepath
    15. p = pyaudio.PyAudio()
    16. stream = p.open(format=FORMAT,
    17. channels=CHANNELS,
    18. rate=RATE,
    19. input=True,
    20. frames_per_buffer=CHUNK)
    21. print("*"*10, "开始录音:请在5秒内输入语音")
    22. frames = []
    23. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    24. data = stream.read(CHUNK)
    25. frames.append(data)
    26. print("*"*10, "录音结束\n")
    27. stream.stop_stream()
    28. stream.close()
    29. p.terminate()
    30. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    31. wf.setnchannels(CHANNELS)
    32. wf.setsampwidth(p.get_sample_size(FORMAT))
    33. wf.setframerate(RATE)
    34. wf.writeframes(b''.join(frames))
    35. wf.close()
    36. #申请百度语音识别
    37. APP_ID = '16835749'
    38. API_KEY = 'UnZlBOVhwYu8m5eNqwOPHt99'
    39. SECRET_KEY = '6jhCitggsR0Ew91fdC47oMa1qtibTrsK'
    40. client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    41. # 读取文件
    42. def get_file_content(filePath):
    43. with open(filePath, 'rb') as fp:
    44. return fp.read()
    45. def result_word():
    46. # 识别本地文件
    47. # path='/Users/alice/Documents/Blog/AI/语音识别/speechrecognition/audiofiles'
    48. get_audio(in_path)
    49. test1 = client.asr(get_file_content(in_path), 'pcm', 16000, {'dev_pid': 1536, })
    50. # print(test1["result"])
    51. if test1["err_no"]==3307:
    52. test1["result"]=["请讲话"]
    53. return test1["result"][0]
    54. print(result_word())
    55. from playsound import playsound
    56. def playAudio(txt):
    57. resultAudio = client.synthesis(txt, 'zh', 1, { 'vol': 2,'per':4 })
    58. if not isinstance(resultAudio, dict):
    59. with open('audio.mp3', 'wb') as f:
    60. f.write(resultAudio)
    61. playsound("audio.mp3")
    62. import os
    63. os.remove("audio.mp3")
    64. import requests
    65. while True:
    66. content_word=result_word()
    67. url="http://api.qingyunke.com/api.php?key=free&appid=0&msg="+content_word
    68. res = requests.get(url)
    69. txt = json.loads(res.text)
    70. txt=txt["content"]
    71. playAudio(txt)

    现在就可智能对话了,只不过还有一些问题,首先就是第一句话是识别不到的,然后是返回的内容里面还有一些其他字符像换行符{br}也会读出来,而影响对话体验。还有就是程序启动之后一直傻啦吧唧的捕捉外部声音,捕捉不到就一直循环播放“因为人家笨嘛”,一点都不聪明。那么如何改进程序呢,如下添加判断语句:

    1. import os
    2. import requests
    3. import wave
    4. import json
    5. import pyaudio
    6. from aip import AipSpeech
    7. from playsound import playsound
    8. #申请百度语音识别
    9. APP_ID = '16835749'
    10. API_KEY = 'UnZlBOVhwYu8m5eNqwOPHt99'
    11. SECRET_KEY = '6jhCitggsR0Ew91fdC47oMa1qtibTrsK'
    12. client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    13. input_filename = "input.wav" # 麦克风采集的语音输入
    14. input_filepath = "../src/" # 输入文件的path
    15. in_path = input_filepath + input_filename
    16. def get_audio(filepath):
    17. CHUNK = 256
    18. FORMAT = pyaudio.paInt16
    19. CHANNELS = 1 # 声道数
    20. RATE = 11025 # 采样率
    21. RECORD_SECONDS = 5
    22. WAVE_OUTPUT_FILENAME = filepath
    23. p = pyaudio.PyAudio()
    24. stream = p.open(format=FORMAT,
    25. channels=CHANNELS,
    26. rate=RATE,
    27. input=True,
    28. frames_per_buffer=CHUNK)
    29. print("*"*10, "开始录音:请在5秒内输入语音")
    30. frames = []
    31. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    32. data = stream.read(CHUNK)
    33. frames.append(data)
    34. print("*"*10, "录音结束\n")
    35. stream.stop_stream()
    36. stream.close()
    37. p.terminate()
    38. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    39. wf.setnchannels(CHANNELS)
    40. wf.setsampwidth(p.get_sample_size(FORMAT))
    41. wf.setframerate(RATE)
    42. wf.writeframes(b''.join(frames))
    43. wf.close()
    44. # 读取文件
    45. def get_file_content(filePath):
    46. with open(filePath, 'rb') as fp:
    47. return fp.read()
    48. def result_word():
    49. # 识别本地文件
    50. # path='/Users/alice/Documents/Blog/AI/语音识别/speechrecognition/audiofiles'
    51. test1 = client.asr(get_file_content(in_path), 'pcm', 16000, {'dev_pid': 1536, })
    52. # print(test1["result"])
    53. if test1["err_no"]==3307:
    54. test1["result"]=["请讲话"]
    55. return test1["result"][0]
    56. print(result_word())
    57. def playAudio(txt):
    58. resultAudio = client.synthesis(txt, 'zh', 1, { 'vol': 2,'per':4 })
    59. if not isinstance(resultAudio, dict):
    60. with open('audio.mp3', 'wb') as f:
    61. f.write(resultAudio)
    62. playsound("audio.mp3")
    63. os.remove("audio.mp3")
    64. a = 0
    65. while True:
    66. get_audio(in_path)
    67. content_word=result_word()
    68. if content_word=="请讲话":
    69. if a == 0:
    70. playAudio("你是不是没有讲话")
    71. a=1
    72. else:
    73. url="http://api.qingyunke.com/api.php?key=free&appid=0&msg="+content_word
    74. res = requests.get(url)
    75. txt = json.loads(res.text)
    76. txt=txt["content"].replace("br","")
    77. print(txt)
    78. playAudio(txt)
    79. a=0