-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOlamiNlp.py
220 lines (169 loc) · 7.08 KB
/
OlamiNlp.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# -*- coding: utf-8 -*-
'''
Created on Jun 30, 2017
@author: make ma
'''
import time
from datetime import datetime
import hashlib
import urllib.request, urllib.error
import json
from speex import *
from LedControl import LedControl
class OlamiNlp(object):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
API_NAME_ASR = "asr";
apiBaseUrl = ''
appKey = ''
appSecret = ''
cookies = ''
def setAuthorization(self, appKey, appSecret):
self.appKey = appKey
self.appSecret = appSecret
def setLocalization(self, apiBaseURL):
self.apiBaseUrl = apiBaseURL
def stopProcess(self):
self.stop = True
def encodeSpeex(self, audioData):
encoder = WBEncoder()
encoder.quality = 10
vocoded = b''
packet_size = encoder.frame_size * 2 * 1
for i in range(0, len(audioData), packet_size):
packet = audioData[i:i + packet_size]
if len(packet) != packet_size:
end = len(audioData) - len(audioData) % (encoder.frame_size * 2)
packet = audioData[i:end]
raw = encoder.encode(packet)
vocoded += raw
return vocoded
def encodeSpeexEx(self, audioDatas):
encoder = WBEncoder()
encoder.quality = 10
vocoded = b''
packet_size = encoder.frame_size * 2 * 1
for audioData in audioDatas:
for i in range(0, len(audioData), packet_size):
packet = audioData[i:i + packet_size]
if len(packet) != packet_size:
end = len(audioData) - len(audioData) % (encoder.frame_size * 2)
packet = audioData[i:end]
raw = encoder.encode(packet)
vocoded += raw
return vocoded
def getNlpResult(self, audioSrc):
ret = None
self.cookies = None
self.stop = False
startTime = time.time()
foundVoice = False
ledctrl = LedControl()
ledctrl.LightAll("white")
print("send data~~~"+str(datetime.now()))
speechLen = 0
while True:
#audioData = audioSrc.getRecordData()
audioDatas = audioSrc.getRecordDataEx(3)
lenAudio = len(audioDatas) * len(audioDatas[0])
speexData = self.encodeSpeexEx(audioDatas)
#print(lenAudio)
#audioData = bytearray(audioData)
postData = str(self.getBasicQueryString(OlamiNlp.API_NAME_ASR, "seg,nli"))
postData += "&compress=" + "1"
postData += "&stop=" + "0"
url = str(self.apiBaseUrl) + "?" + str(postData)
if self.cookies == None:
headers = { 'Connection' : "Keep-Alive", \
'Content-Type' : "application/octet-stream" }
else:
headers = { 'Connection' : "Keep-Alive", \
'Content-Type' : "application/octet-stream", \
'Cookie': self.cookies }
#print("url begin", time.time())
req = urllib.request.Request(url,speexData, headers)
f = urllib.request.urlopen(req)
response = f.read().decode()
#print("url return", time.time())
print(response + "\n")
if self.cookies == None:
self.cookies = f.getheader('Set-Cookie')
resAsr = None
speechStatus = None
resData = None
res = json.loads(response)
if res != None:
resData = res.get("data")
if resData != None:
resAsr = resData.get("asr")
if resAsr != None:
speechStatus = resAsr.get("speech_status")
if speechStatus == 1:
foundVoice = True;
speechLen += lenAudio
if (not foundVoice) and time.time() > startTime + 8:
break;
if foundVoice and (speechStatus == 0 or speechLen > 32000 * 8):
startTime = time.time()
while True:
response = self.getRecognitionResult(OlamiNlp.API_NAME_ASR, "nli,seg")
print(response + "\n")
res = json.loads(response)
resAsr = None
isFinal = None
resData = None
if res != None:
resData = res.get("data")
if resData != None:
resAsr = resData.get("asr")
if resAsr != None:
isFinal = resAsr.get("final")
#retTemp = resAsr.get("result")
#if retTemp != None:
# ret = retTemp
if isFinal:
ret = resData.get("nli")
print("get result~~~"+str(datetime.now()))
print(response + "\n")
break
if isFinal or time.time() > startTime + 10:
break
break
return ret
def getRecognitionResult(self, apiName, seqValue):
query = self.getBasicQueryString(apiName, seqValue) + "&stop=1"
'''Request speech recognition service by HTTP GET'''
url = str(self.apiBaseUrl) + "?" + str(query)
req = urllib.request.Request(url,headers = {'Cookie': self.cookies})
with urllib.request.urlopen(req) as f:
getResponse = f.read().decode()
'''Now you can check the status here.'''
#print("Sending 'GET' request to URL : " + self.apiBaseUrl)
#print("get parameters : " + str(query))
#print("Response Code : " + str(f.getcode()))
'''Get the response'''
return str(getResponse)
def getBasicQueryString(self, apiName, seqValue):
timestamp = int(round(time.time() * 1000))
'''Prepare message to generate an MD5 digest.'''
signMsg = str(self.appSecret)
signMsg += 'api='+apiName
signMsg += 'appkey='+str(self.appKey)
signMsg += 'timestamp='+str(timestamp)
signMsg += str(self.appSecret)
'''Generate MD5 digest.'''
md = hashlib.md5()
md.update(signMsg.encode('utf-8'))
sign = md.hexdigest()
'''Assemble all the HTTP parameters you want to send'''
postData = 'appkey='+str(self.appKey)
postData +='&api='+apiName
postData +='×tamp='+str(timestamp)
postData +='&sign='+str(sign)
postData +='&seq=' +seqValue
return str(postData)