2018-04-21 20:50:53 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
@author: nl8590687
|
|
|
|
语音识别API的HTTP服务器程序
|
|
|
|
|
|
|
|
"""
|
|
|
|
import http.server
|
2018-04-25 21:18:28 +08:00
|
|
|
import urllib
|
2018-04-21 20:50:53 +08:00
|
|
|
import keras
|
2018-04-26 12:10:34 +08:00
|
|
|
from SpeechModel22 import ModelSpeech
|
|
|
|
from LanguageModel import ModelLanguage
|
2018-04-21 20:50:53 +08:00
|
|
|
|
2018-05-11 16:56:59 +08:00
|
|
|
datapath = 'data/'
|
|
|
|
modelpath = 'model_speech/'
|
|
|
|
ms = ModelSpeech(datapath)
|
|
|
|
ms.LoadModel(modelpath + 'speech_model22_e_0_step_216500.model')
|
|
|
|
|
|
|
|
ml = ModelLanguage('model_language')
|
|
|
|
ml.LoadModel()
|
2018-04-21 20:50:53 +08:00
|
|
|
|
|
|
|
class TestHTTPHandle(http.server.BaseHTTPRequestHandler):
|
|
|
|
|
|
|
|
def _set_response(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-type', 'text/html')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
|
|
|
|
buf = 'ASRT_SpeechRecognition API'
|
|
|
|
self.protocal_version = 'HTTP/1.1'
|
|
|
|
|
|
|
|
self._set_response()
|
|
|
|
|
|
|
|
buf = bytes(buf,encoding="utf-8")
|
|
|
|
self.wfile.write(buf)
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
'''
|
|
|
|
处理通过POST方式传递过来并接收的语音数据
|
|
|
|
通过语音模型和语言模型计算得到语音识别结果并返回
|
|
|
|
'''
|
|
|
|
path = self.path
|
|
|
|
print(path)
|
|
|
|
#获取post提交的数据
|
|
|
|
datas = self.rfile.read(int(self.headers['content-length']))
|
2018-04-25 21:18:28 +08:00
|
|
|
#datas = urllib.unquote(datas).decode("utf-8", 'ignore')
|
|
|
|
datas = datas.decode('utf-8')
|
|
|
|
datas_split = datas.split('&')
|
|
|
|
token = ''
|
|
|
|
fs = 0
|
|
|
|
wavs = []
|
2018-05-08 11:35:18 +08:00
|
|
|
#type = 'wavfilebytes' # wavfilebytes or python-list
|
2018-04-25 21:18:28 +08:00
|
|
|
|
|
|
|
for line in datas_split:
|
|
|
|
[key, value]=line.split('=')
|
2018-04-25 21:55:34 +08:00
|
|
|
if('wavs' == key):
|
2018-04-25 21:18:28 +08:00
|
|
|
wavs.append(int(value))
|
|
|
|
elif('fs' == key):
|
2018-04-26 12:10:34 +08:00
|
|
|
fs = int(value)
|
2018-04-25 21:18:28 +08:00
|
|
|
elif('token' == key ):
|
|
|
|
token = value
|
2018-05-08 11:35:18 +08:00
|
|
|
#elif('type' == key):
|
|
|
|
# type = value
|
2018-04-25 21:55:34 +08:00
|
|
|
else:
|
|
|
|
print(key, value)
|
2018-04-25 21:18:28 +08:00
|
|
|
|
2018-05-08 11:35:18 +08:00
|
|
|
#if('python-list' == type):
|
2018-04-26 12:10:34 +08:00
|
|
|
r = self.recognize([wavs], fs)
|
2018-05-08 11:35:18 +08:00
|
|
|
#else:
|
|
|
|
# r = self.recognize_from_file('')
|
2018-04-25 21:55:34 +08:00
|
|
|
|
|
|
|
if(token == 'qwertasd'):
|
|
|
|
#buf = '成功\n'+'wavs:\n'+str(wavs)+'\nfs:\n'+str(fs)
|
2018-05-11 16:56:59 +08:00
|
|
|
buf = r
|
2018-04-25 21:18:28 +08:00
|
|
|
else:
|
|
|
|
buf = '403'
|
|
|
|
|
|
|
|
#print(datas)
|
2018-04-21 20:50:53 +08:00
|
|
|
|
|
|
|
self._set_response()
|
|
|
|
|
2018-04-25 21:55:34 +08:00
|
|
|
#buf = '<!DOCTYPE HTML> \n<html> \n<head>\n<title>Post page</title>\n</head> \n<body>Post Data:%s <br />Path:%s\n</body> \n</html>'%(datas,self.path)
|
2018-05-08 11:35:18 +08:00
|
|
|
buf = bytes(buf,encoding="utf-8")
|
2018-04-25 21:55:34 +08:00
|
|
|
self.wfile.write(buf)
|
|
|
|
|
2018-04-26 12:10:34 +08:00
|
|
|
def recognize(self, wavs, fs):
|
|
|
|
r_speech = ms.RecognizeSpeech(wavs, fs)
|
2018-04-25 21:18:28 +08:00
|
|
|
|
2018-04-26 12:10:34 +08:00
|
|
|
str_pinyin = r_speech
|
2018-04-25 21:55:34 +08:00
|
|
|
r = ml.SpeechToText(str_pinyin)
|
|
|
|
return r
|
|
|
|
pass
|
2018-04-21 20:50:53 +08:00
|
|
|
|
2018-05-08 11:35:18 +08:00
|
|
|
def recognize_from_file(self, filename):
|
|
|
|
pass
|
|
|
|
|
2018-04-21 20:50:53 +08:00
|
|
|
def start_server(ip, port):
|
|
|
|
http_server = http.server.HTTPServer((ip, int(port)), TestHTTPHandle)
|
|
|
|
print('服务器已开启')
|
|
|
|
|
|
|
|
try:
|
|
|
|
http_server.serve_forever() #设置一直监听并接收请求
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
http_server.server_close()
|
|
|
|
print('HTTP server closed')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
start_server('', 20000)
|
|
|
|
|
|
|
|
|
|
|
|
|