脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 实现语音聊天机器人的示例代码

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

2021-04-24 00:28氢立方 Python

这篇文章主要介绍了python 实现语音聊天机器人的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

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

原理简介

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

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

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

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

原理流程图.png

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

话不多说,上代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
##@氢立方 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 实现语音聊天机器人的示例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/16a226ca8139

延伸 · 阅读

精彩推荐