python 实现语音聊天机器人的示例代码

yipeiwu_com6年前Python基础

前言

在不远的将来,实现一定程度上的语音支持将成为日常科技的基本要求,整合了语音识别的python程序提供了其他技术无法比拟的交互性和可访问性。最重要的是,在python程序中实现语音识别非常简单。整个代码实现下来还不到150行。

原理简介

许多现代语音识别系统会在HMM识别之前使用神经网络,通过特征变换和降维技术来简化语音信号,也可以使用语音活动检测器将音频信号减少到可能包含语音的部分。

幸运的是,对于python来讲,一些语音识别的服务可通过API在线使用,且其中大部分也提供了Python SDK。

本文做的聊天机器人是基于百度语音识别和图灵机器人二者之上共同实现的。大致的流程如下图:

原理流程图.PNG

这里需要用的模块库有 requests、time、datetime、pyaudio、wave、aipspeech 等。

话不多说,上代码:

##@氢立方 2018.0911

import requests
import time
import pygame
from datetime import datetime
from aip import AipSpeech
from pyaudio import PyAudio,paInt16
import wave
import os


framerate=8000
NUM_SAMPLES=2000
channels=1
sampwidth=2
TIME=2


def save_wave_file(filename,data):
  '''save the date to the wavfile'''
  wf=wave.open(filename,'wb')
  wf.setnchannels(channels)
  wf.setsampwidth(sampwidth)
  wf.setframerate(framerate)
  wf.writeframes(b"".join(data))
  wf.close()


def my_record():
  pa=PyAudio()
  stream=pa.open(format = paInt16,channels=1,
          rate=framerate,input=True,
          frames_per_buffer=NUM_SAMPLES)
  my_buf=[]
  count=0
  while count<TIME*6:#控制录音时间
    string_audio_data = stream.read(NUM_SAMPLES)
    my_buf.append(string_audio_data)
    count+=1
    print('.')
  save_wave_file('0001.wav',my_buf)
  stream.close()

##def play():
##  wf=wave.open(r"D:/41125.mp3",'rb')
##  p=PyAudio()
##  stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=
##  wf.getnchannels(),rate=wf.getframerate(),output=True)
##  while True:
##    data=wf.readframes(chunk)
##    if data=="":break
##    stream.write(data)
##  stream.close()
##  p.terminate()
##

这里大家需要改成自己的ID和KEY


APP_ID = '11****843'
API_KEY = '3Mnv***8**88******GbXa'

SECRET_KEY = '147***8*88****1227684'
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)




def getText(url):
  text = requests.post(url).json()
  return text['text']



##
##key = '6ddc57c5761a4c62a30ea840e5ae163f'
#api = 'http://www.tuling123.com/openapi/api?key=' + key +'&info ='
key = '8b005db5f57556fb96dfd98fbccfab84' 
api = 'http://www.tuling123.com/openapi/api?key=' + key + '&info=' 
##
while True:
  
##  info = input("我说\n")

 
##  chunk=2014

  my_record()
  print("录音完成")
  

  
  def get_file_content(filePath):
    with open(filePath,'rb') as fp:
      return fp.read()
    
  a = aipSpeech.asr(get_file_content('0001.wav '),'wav',8000,{})
  print(a)
  b = str(a['result'])
  info = b

  
  url = api + info
  #print(url)

  text_01 = getText(url)
  print("机器人回\n",text_01)

  now = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
  filename_01 = now + ".mp3"

  result = aipSpeech.synthesis(  text_01,'zh',1,{'vol': 5,'per' : 2} )
  
  if not isinstance(result, dict):
    
    with open(filename_01, 'wb') as f:
      f.write(result)
  print("--------------------------------------")
  time.sleep(1)
  
  
  pygame.mixer.init()
  print("语音1")
  file= filename_01
  track = pygame.mixer.music.load(file)

  pygame.mixer.music.play()
  time.sleep(15)
  pygame.mixer.music.stop()
  pygame.quit()

运行结果如下:

小编说的是:今天看了电视剧。机器人回复的是:看了有没有开心点

在某种意义上来说,语境还是符合常理的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

好的Python培训机构应该具备哪些条件

想要追赶 Python 的热潮,应该如何学习呢?现在许多人在自学之外,都会选择去培训机构学习。选择培训机构的的标准是什么呢?什么样的python培训机构靠谱? 是否有循序...

IronPython连接MySQL的方法步骤

原以为在IronPython中使用MySQL是一件很容易的事情,即使有麻烦,也可以改变路径,加载Python下的MySQL模块。事实证明,这是我一厢情愿的想法。要想在IronPython...

Python反射和内置方法重写操作详解

本文实例讲述了Python反射和内置方法重写操作。分享给大家供大家参考,具体如下: isinstance和issubclass isinstance(obj,cls)检查是否obj是否是...

简洁的十分钟Python入门教程

【简介】 Python是一种动态解释型的编程语言。Python可以在Windows、UNIX、MAC等多种操作系统上使用,也可以在Java、.NET开发平台上使用。 【特点】 1 Pyt...

Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程

前提 官网上提供了 Mac 和 Windows 上的安装包和 Linux 上安装需要的源码。 下载地址如下: https://www.python.org/downloads/relea...