2018-04-21 20:50:53 +08:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
2021-05-16 19:44:05 +08:00
|
|
|
|
#
|
|
|
|
|
# Copyright 2016-2099 Ailemon.net
|
|
|
|
|
#
|
|
|
|
|
# This file is part of ASRT Speech Recognition Tool.
|
|
|
|
|
#
|
|
|
|
|
# ASRT is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
# ASRT is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with ASRT. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
# ============================================================================
|
|
|
|
|
|
2018-04-21 20:50:53 +08:00
|
|
|
|
"""
|
|
|
|
|
@author: nl8590687
|
2021-11-20 20:32:09 +08:00
|
|
|
|
ASRT语音识别API的HTTP服务器程序
|
2018-04-21 20:50:53 +08:00
|
|
|
|
"""
|
2021-05-16 19:44:05 +08:00
|
|
|
|
|
2018-04-21 20:50:53 +08:00
|
|
|
|
import http.server
|
2021-05-16 19:44:05 +08:00
|
|
|
|
import socket
|
2021-11-20 20:32:09 +08:00
|
|
|
|
from speech_model import ModelSpeech
|
2022-03-27 21:47:12 +08:00
|
|
|
|
from speech_model_zoo import SpeechModel251BN
|
2021-11-20 20:32:09 +08:00
|
|
|
|
from speech_features import Spectrogram
|
|
|
|
|
from LanguageModel2 import ModelLanguage
|
|
|
|
|
|
2021-11-26 18:27:35 +08:00
|
|
|
|
AUDIO_LENGTH = 1600
|
|
|
|
|
AUDIO_FEATURE_LENGTH = 200
|
|
|
|
|
CHANNELS = 1
|
2021-11-20 20:32:09 +08:00
|
|
|
|
# 默认输出的拼音的表示大小是1428,即1427个拼音+1个空白块
|
2021-11-26 18:27:35 +08:00
|
|
|
|
OUTPUT_SIZE = 1428
|
2022-03-27 21:47:12 +08:00
|
|
|
|
sm251bn = SpeechModel251BN(
|
2021-11-26 18:27:35 +08:00
|
|
|
|
input_shape=(AUDIO_LENGTH, AUDIO_FEATURE_LENGTH, CHANNELS),
|
|
|
|
|
output_size=OUTPUT_SIZE
|
2021-11-20 20:32:09 +08:00
|
|
|
|
)
|
|
|
|
|
feat = Spectrogram()
|
2022-03-27 21:47:12 +08:00
|
|
|
|
ms = ModelSpeech(sm251bn, feat, max_label_length=64)
|
|
|
|
|
ms.load_model('save_models/' + sm251bn.get_model_name() + '.model.h5')
|
2018-05-11 16:56:59 +08:00
|
|
|
|
|
|
|
|
|
ml = ModelLanguage('model_language')
|
|
|
|
|
ml.LoadModel()
|
2018-04-21 20:50:53 +08:00
|
|
|
|
|
2019-01-09 16:37:10 +08:00
|
|
|
|
|
2021-11-26 18:27:35 +08:00
|
|
|
|
class ASRTHTTPHandle(http.server.BaseHTTPRequestHandler):
|
2021-05-16 19:44:05 +08:00
|
|
|
|
def setup(self):
|
|
|
|
|
self.request.settimeout(10)
|
|
|
|
|
http.server.BaseHTTPRequestHandler.setup(self)
|
|
|
|
|
|
|
|
|
|
def _set_response(self):
|
|
|
|
|
self.send_response(200)
|
|
|
|
|
self.send_header('Content-type', 'text/html')
|
|
|
|
|
self.end_headers()
|
|
|
|
|
|
2021-11-26 18:27:35 +08:00
|
|
|
|
def do_GET(self):
|
|
|
|
|
buf = 'ASRT_SpeechRecognition API'
|
|
|
|
|
self.protocal_version = 'HTTP/1.1'
|
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
self._set_response()
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
buf = bytes(buf,encoding="utf-8")
|
2021-11-26 18:27:35 +08:00
|
|
|
|
self.wfile.write(buf)
|
2021-05-16 19:44:05 +08:00
|
|
|
|
|
2021-11-26 18:27:35 +08:00
|
|
|
|
def do_POST(self):
|
2021-05-16 19:44:05 +08:00
|
|
|
|
'''
|
|
|
|
|
处理通过POST方式传递过来并接收的语音数据
|
|
|
|
|
通过语音模型和语言模型计算得到语音识别结果并返回
|
|
|
|
|
'''
|
2021-11-26 18:27:35 +08:00
|
|
|
|
path = self.path
|
|
|
|
|
print(path)
|
|
|
|
|
#获取post提交的数据
|
|
|
|
|
datas = self.rfile.read(int(self.headers['content-length']))
|
|
|
|
|
#datas = urllib.unquote(datas).decode("utf-8", 'ignore')
|
2021-05-16 19:44:05 +08:00
|
|
|
|
datas = datas.decode('utf-8')
|
|
|
|
|
datas_split = datas.split('&')
|
|
|
|
|
token = ''
|
|
|
|
|
fs = 0
|
|
|
|
|
wavs = []
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
for line in datas_split:
|
|
|
|
|
[key, value]=line.split('=')
|
2021-11-26 18:27:35 +08:00
|
|
|
|
if key == 'wavs' and value != '':
|
2021-05-16 19:44:05 +08:00
|
|
|
|
wavs.append(int(value))
|
2021-11-26 18:27:35 +08:00
|
|
|
|
elif key == 'fs':
|
2021-05-16 19:44:05 +08:00
|
|
|
|
fs = int(value)
|
2021-11-26 18:27:35 +08:00
|
|
|
|
elif key == 'token':
|
2021-05-16 19:44:05 +08:00
|
|
|
|
token = value
|
|
|
|
|
else:
|
|
|
|
|
print(key, value)
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
|
|
|
|
if token != 'qwertasd':
|
2021-05-16 19:44:05 +08:00
|
|
|
|
buf = '403'
|
|
|
|
|
print(buf)
|
|
|
|
|
buf = bytes(buf,encoding="utf-8")
|
2021-11-26 18:27:35 +08:00
|
|
|
|
self.wfile.write(buf)
|
2021-05-16 19:44:05 +08:00
|
|
|
|
return
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
|
|
|
|
if len(wavs)>0:
|
2021-05-16 19:44:05 +08:00
|
|
|
|
r = self.recognize([wavs], fs)
|
|
|
|
|
else:
|
|
|
|
|
r = ''
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
|
|
|
|
if token == 'qwertasd':
|
2021-05-16 19:44:05 +08:00
|
|
|
|
buf = r
|
|
|
|
|
else:
|
|
|
|
|
buf = '403'
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
self._set_response()
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
print(buf)
|
|
|
|
|
buf = bytes(buf,encoding="utf-8")
|
2021-11-26 18:27:35 +08:00
|
|
|
|
self.wfile.write(buf)
|
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
def recognize(self, wavs, fs):
|
|
|
|
|
r=''
|
|
|
|
|
try:
|
2021-11-20 20:32:09 +08:00
|
|
|
|
r_speech = ms.recognize_speech(wavs, fs)
|
2021-05-16 19:44:05 +08:00
|
|
|
|
print(r_speech)
|
|
|
|
|
str_pinyin = r_speech
|
|
|
|
|
r = ml.SpeechToText(str_pinyin)
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
r=''
|
|
|
|
|
print('[*Message] Server raise a bug. ', ex)
|
|
|
|
|
return r
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
def recognize_from_file(self, filename):
|
|
|
|
|
pass
|
|
|
|
|
|
2019-01-09 16:37:10 +08:00
|
|
|
|
|
|
|
|
|
class HTTPServerV6(http.server.HTTPServer):
|
2021-05-16 19:44:05 +08:00
|
|
|
|
address_family = socket.AF_INET6
|
|
|
|
|
|
2019-01-09 16:37:10 +08:00
|
|
|
|
|
2021-11-26 18:27:35 +08:00
|
|
|
|
def start_server(ip, port):
|
|
|
|
|
if ':' in ip:
|
2021-05-16 19:44:05 +08:00
|
|
|
|
http_server = HTTPServerV6((ip, port), ASRTHTTPHandle)
|
|
|
|
|
else:
|
|
|
|
|
http_server = http.server.HTTPServer((ip, int(port)), ASRTHTTPHandle)
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
print('服务器已开启')
|
2021-11-26 18:27:35 +08:00
|
|
|
|
|
2021-05-16 19:44:05 +08:00
|
|
|
|
try:
|
2021-11-26 18:27:35 +08:00
|
|
|
|
http_server.serve_forever() #设置一直监听并接收请求
|
2021-05-16 19:44:05 +08:00
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
pass
|
|
|
|
|
http_server.server_close()
|
|
|
|
|
print('HTTP server closed')
|
|
|
|
|
|
|
|
|
|
|
2018-04-21 20:50:53 +08:00
|
|
|
|
if __name__ == '__main__':
|
2021-05-16 19:44:05 +08:00
|
|
|
|
start_server('', 20000) # For IPv4 Network Only
|
|
|
|
|
#start_server('::', 20000) # For IPv6 Network
|