-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
496 lines (467 loc) · 19.9 KB
/
app.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import os
import tracemalloc
import gc
import psutil
import json
import matplotlib.pyplot as plt
# import matplotlib
# matplotlib.use("Agg")
import time
from flask import Flask, request, make_response
from flask_cors import CORS, cross_origin
from impulse_response import run_ir_task, estimate_samples_per_mls_, adjust_mls_length, compute_impulse_resp
from inverted_impulse_response import run_component_iir_task, run_system_iir_task, run_convolution_task
from volume import run_volume_task,run_volume_task_nonlinear
from volume import get_model_parameters
import numpy as np
from scipy.signal import max_len_seq
from scipy.fft import fft
from utils import allHzPowerCheck, volumePowerCheck
import math
app = Flask(__name__)
CORS(app, resources = {r"/*": {"origins": "*"}})
process = psutil.Process(os.getpid())
tracemalloc.start()
def handle_autocorrelation_task(request_json, task):
if "payload" not in request_json:
return 400, "Request Body is missing a 'payload' entry"
if "sample-rate" not in request_json:
return 400, "Request Body us missing a 'sample-rate' entry"
if "mls" not in request_json:
return 400, "Request Body is missing a 'mls' entry"
if "numPeriods" not in request_json:
return 400, "Request Body is missing a 'numPeriods' entry"
sig = request_json["payload"]
mls = request_json["mls"]
sampleRate = request_json["sample-rate"]
NUM_PERIODS = int(request_json["numPeriods"]) - 1
print("number of period ", NUM_PERIODS)
sig = np.array(sig, dtype=np.float32)
MLS = fft(np.array(mls, dtype=np.float32))
L = len(MLS)
fs2, L_new_n, dL_n, autocorrelation = estimate_samples_per_mls_(sig, NUM_PERIODS, sampleRate, L)
print_memory_usage()
gc.collect()
return 200, {
str(task): {
'autocorrelation': autocorrelation.tolist(),
'fs2': fs2.tolist(),
"L_new_n": L_new_n.tolist(),
"dL_n": dL_n.tolist()
}
}
def handle_impulse_response_task(request_json, task):
start_time = time.time()
if "sample-rate" not in request_json:
return 400, "Request Body us missing a 'sample-rate' entry"
if "mls" not in request_json:
return 400, "Request Body is missing a 'mls' entry"
if "numPeriods" not in request_json:
return 400, "Request Body is missing a 'numPeriods' entry"
if "L_new_n" not in request_json:
return 400, "Request Body is missing a 'L_new_n' entry"
if "dL_n" not in request_json:
return 400, "Request Body is missing a 'dL_n' entry"
if "sig" not in request_json:
return 400, "Request Body is missing a 'sig' entry"
if "fs2" not in request_json:
return 400, "Request Body is missing a 'fs2' entry"
MLS = fft(np.array(request_json['mls'], dtype=np.float32))
L = len(MLS)
sig = np.array(request_json["sig"], dtype=np.float32)
L_new_n = request_json["L_new_n"]
dL_n = request_json["dL_n"]
NUM_PERIODS = request_json["numPeriods"]
fs2 = request_json["fs2"]
NUM_PERIODS = int(NUM_PERIODS) - 1
print("Starting IR Task")
OUT_MLS2_n = adjust_mls_length(sig, NUM_PERIODS, L, L_new_n, dL_n)
ir = compute_impulse_resp(MLS, OUT_MLS2_n, L, fs2, NUM_PERIODS)
print_memory_usage()
end_time = time.time()
elapsed_time = end_time - start_time
print(f"============== handle_impulse_response task, time taken: {elapsed_time}s ==============")
return 200, {
str(task): {
'ir':ir.tolist()
}
}
def handle_all_hz_power_check_task(request_json, task):
recordedSignalsJson = request_json["payload"]
sampleRate = request_json["sampleRate"]
binDesiredSec = request_json["binDesiredSec"]
burstSec = request_json["burstSec"]
repeats = request_json["repeats"]
warmupT, warmupDb, recT, recDb, sd, postT, postDb = allHzPowerCheck(recordedSignalsJson, sampleRate, binDesiredSec, burstSec,repeats)
print_memory_usage()
gc.collect()
return 200, {
str(task): {
'sd':sd,
'warmupT': warmupT,
'warmupDb': warmupDb,
'recT': recT,
'recDb': recDb,
'postT': postT,
'postDb': postDb
}
}
def handle_volume_power_check_task(request_json, task):
recordedSignalsJson = request_json["payload"]
sampleRate = request_json["sampleRate"]
preSec = request_json["preSec"]
Sec = request_json["Sec"]
binDesiredSec = request_json["binDesiredSec"]
preT, preDb, recT, recDb, postT, postDb, sd = volumePowerCheck(recordedSignalsJson, sampleRate, preSec, Sec, binDesiredSec)
return 200, {
str(task): {
'sd':sd,
'preT': preT,
'preDb': preDb,
'recT': recT,
'recDb': recDb,
'postT': postT,
'postDb': postDb
}
}
def handle_component_inverse_impulse_response_task(request_json, task):
if "payload" not in request_json:
return 400, "Request Body is missing a 'payload' entry"
if "mls" not in request_json:
return 400, "Request Body is missing a 'mls' entry"
if "lowHz" not in request_json:
return 400, "Request Body is missing a 'lowHz' entry"
if "highHz" not in request_json:
return 400, "Request Body is missing a 'highHz' entry"
if "componentIRGains" not in request_json:
return 400, "Request body is missing a 'componentIRGains'"
if "componentIRFreqs" not in request_json:
return 400, "Request body is missing a 'componentIRFreqs'"
if "sampleRate" not in request_json:
return 400, "Request body is missing a 'sampleRate'"
if "iirLength" not in request_json:
return 400, "Request body is missing a 'iirLength'"
if "mlsAmplitude" not in request_json:
return 400, "Request body is missing a 'mlsAmplitude'"
if "irLength" not in request_json:
return 400, "Request body is missing a 'irLength'"
if "calibrateSoundSmoothOctaves" not in request_json:
return 400, "Request body is missing a 'calibrateSoundSmoothOctaves'"
if "calibrateSoundSmoothMinBandwidthHz" not in request_json:
return 400, "Request body is missing a 'calibrateSoundSmoothMinBandwidthHz'"
if "calibrateSoundBurstFilteredExtraDb" not in request_json:
return 400, "Request body is missing a 'calibrateSoundBurstFilteredExtraDb'"
start_time = time.time()
impulseResponsesJson = request_json["payload"]
iir_length = request_json["iirLength"]
mls = request_json["mls"]
lowHz = request_json["lowHz"]
highHz = request_json["highHz"]
componentIRGains = request_json["componentIRGains"]
componentIRFreqs = request_json["componentIRFreqs"]
sampleRate = request_json["sampleRate"]
mls_amplitude = request_json["mlsAmplitude"]
irLength = request_json["irLength"]
calibrateSoundSmoothOctaves = request_json["calibrateSoundSmoothOctaves"]
calibrateSoundSmoothMinBandwidthHz = request_json["calibrateSoundSmoothMinBandwidthHz"]
calibrate_sound_burst_filtered_extra_db = request_json["calibrateSoundBurstFilteredExtraDb"]
_calibrateSoundIIRPhase = request_json["calibrateSoundIIRPhase"]
result, ir,frequencies, iir_no_bandpass, ir_time, angle, ir_origin, system_angle, attenuatorGain_dB, fMaxHz = run_component_iir_task(impulseResponsesJson,mls,lowHz,highHz,iir_length,componentIRGains,componentIRFreqs,sampleRate, mls_amplitude, irLength, calibrateSoundSmoothOctaves,calibrateSoundSmoothMinBandwidthHz, calibrate_sound_burst_filtered_extra_db, _calibrateSoundIIRPhase)
print_memory_usage()
end_time = time.time()
elapsed_time = end_time - start_time
print(f"============== component_inverse_impulse_response task, time taken: {elapsed_time}s ==============")
return 200, {
str(task): {
"iir":result,
"ir":ir,
"frequencies":frequencies,
"iirNoBandpass":iir_no_bandpass,
"irTime": ir_time,
"component_angle": angle,
"system_angle":system_angle,
"irOrigin": ir_origin,
"attenuatorGain_dB":attenuatorGain_dB,
"fMaxHz":fMaxHz
}
}
def handle_system_inverse_impulse_response_task(request_json, task):
start_time = time.time()
if "payload" not in request_json:
return 400, "Request Body is missing a 'payload' entry"
if "mls" not in request_json:
return 400, "Request Body is missing a 'mls' entry"
if "lowHz" not in request_json:
return 400, "Request Body is missing a 'lowHz' entry"
if "highHz" not in request_json:
return 400, "Request Body is missing a 'highHz' entry"
if "sampleRate" not in request_json:
return 400, "Request body is missing a 'sampleRate'"
if "iirLength" not in request_json:
return 400, "Request body is missing a 'iirLength'"
if "calibrateSoundBurstFilteredExtraDb" not in request_json:
return 400, "Request body is missing a 'calibrateSoundBurstFilteredExtraDb'"
impulseResponsesJson = request_json["payload"]
iir_length = request_json["iirLength"]
mls = request_json["mls"]
lowHz = request_json["lowHz"]
highHz = request_json["highHz"]
sampleRate = request_json["sampleRate"]
mls_amplitude = request_json["mlsAmplitude"]
calibrate_sound_burst_filtered_extra_db = request_json["calibrateSoundBurstFilteredExtraDb"]
_calibrateSoundIIRPhase = request_json["calibrateSoundIIRPhase"]
result, ir, iir_no_bandpass, attenuatorGain_dB, fMaxHz = run_system_iir_task(impulseResponsesJson,mls,lowHz,iir_length,highHz,sampleRate, mls_amplitude,calibrate_sound_burst_filtered_extra_db, _calibrateSoundIIRPhase)
print_memory_usage()
end_time = time.time()
elapsed_time = end_time - start_time
print(f"============== system_inverse_impulse_response task, time taken: {elapsed_time}s ==============")
return 200, {
str(task): {
"iir":result,
"ir":ir,
"iirNoBandpass":iir_no_bandpass,
"attenuatorGain_dB":attenuatorGain_dB,
"fMaxHz":fMaxHz
}
}
def handle_convolution_task(request_json, task):
if "mls" not in request_json:
return 400, "Request Body is missing a 'mls' entry"
if "inverse_response" not in request_json:
return 400, "Request body is missing a 'inverse_response'"
if "inverse_response_no_bandpass" not in request_json:
return 400, "Request body is missing a 'inverse_response_no_bandpass'"
if "attenuatorGain_dB" not in request_json:
return 400, "Request body is missing a 'attenuatorGain_dB'"
if "mls_amplitude" not in request_json:
return 400, "Request body is missing a 'mls_amplitude'"
mls = request_json['mls']
inverse_response = request_json['inverse_response']
inverse_response_no_bandpass = request_json['inverse_response_no_bandpass']
attenuatorGain_dB = request_json['attenuatorGain_dB']
mls_amplitude = request_json['mls_amplitude']
conv, conv_nbp = run_convolution_task(inverse_response, mls, inverse_response_no_bandpass, attenuatorGain_dB, mls_amplitude)
gc.collect()
return 200, {
str(task): {
'convolution': conv,
'convolution_no_bandpass': conv_nbp
}
}
def handle_volume_task(request_json, task):
if "payload" not in request_json:
return 400, "Request Body is missing a 'payload' entry"
if "sample-rate" not in request_json:
return 400, "Request Body us missing a 'sample-rate' entry"
recordedSignalJson = request_json["payload"]
sampleRate = request_json["sample-rate"]
soundGainDbSPL, _, _, _ = run_volume_task(recordedSignalJson, sampleRate)
return 200, {
str(task): soundGainDbSPL
}
def handle_volume_task_nonlinear(request_json, task):
start_time = time.time()
if "payload" not in request_json:
return 400, "Request Body is missing a 'payload' entry"
if "sample-rate" not in request_json:
return 400, "Request Body us missing a 'sample-rate' entry"
recordedSignalJson = request_json["payload"]
sampleRate = request_json["sample-rate"]
lCalib = request_json["lCalib"]
soundGainDbSPL, P, L, _, L1000, P1000, thd, rms, soundGainDbSPL1000 = run_volume_task_nonlinear(recordedSignalJson, sampleRate) #L is outDbSPL
print("soundGainDbSPL", soundGainDbSPL)
print("outDbSPL", L)
print("outDbSPL1000", L1000)
print("thd", thd)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"============== volume_task_nonlinear task, time taken: {elapsed_time}s ==============")
return 200, {
str(task): {
"outDbSPL":L,
"outDbSPL1000": float(L1000),
"thd": thd,
}
}
def handle_volume_parameters(request_json,task):
start_time = time.time()
inDB = request_json["inDBValues"]
outDBSPL = request_json["outDBSPLValues"]
lCalib = request_json["lCalib"]
componentGainDBSPL = request_json["componentGainDBSPL"]
backgroundDBSPL, gainDBSPL, T, R, W, rmsError, modelGuesses = get_model_parameters(inDB,outDBSPL,lCalib,componentGainDBSPL)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"============== handle_volume_parameters task, time taken: {elapsed_time}s ==============")
return 200, {
str(task): {
"backgroundDBSPL":backgroundDBSPL,
"gainDBSPL":gainDBSPL,
"T":T,
"R":R,
"W":W,
"RMSError":rmsError,
"initialGuesses":{
"backgroundDBSPL": modelGuesses[0],
"gainDBSPL": modelGuesses[1],
"T": modelGuesses[2],
"R": modelGuesses[3],
"W": modelGuesses[4],
}
}
}
def handle_psd_task(request_json,task):
start_time = time.time()
rec_unconv = request_json["unconv_rec"]
rec_conv = request_json["conv_rec"]
sampleRate = request_json["sampleRate"]
print('length of rec')
print(len(rec_unconv))
[y_unconv, x_unconv] = plt.psd(rec_unconv,Fs=sampleRate, window=plt.mlab.window_none, NFFT=2048,scale_by_freq=False)
[y_conv,x_conv] = plt.psd(rec_conv, Fs=sampleRate, window=plt.mlab.window_none, NFFT=2048,scale_by_freq=False)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"================ psd_task, time taken: {elapsed_time}s ================")
return 200, {
str(task): {
"x_unconv":x_unconv.tolist(),
"y_unconv":y_unconv.tolist(),
"x_conv":x_conv.tolist(),
"y_conv":y_conv.tolist(),
}
}
def handle_background_psd_task(request_json,task):
start_time = time.time()
background_rec = request_json["background_rec"]
sampleRate = request_json["sampleRate"]
[y_background, x_background] = plt.psd(background_rec,Fs=sampleRate,NFFT=2048,scale_by_freq=False)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"================ handle_background_psd task, time taken: {elapsed_time}s ================")
return 200, {
str(task): {
"x_background":x_background.tolist(),
"y_background":y_background.tolist(),
}
}
def handle_mls_psd_task(request_json,task):
start_time = time.time()
mls = request_json["mls"]
sampleRate = request_json["sampleRate"]
[y_mls, x_mls] = plt.psd(mls,Fs=sampleRate, window=plt.mlab.window_none, NFFT=2048,scale_by_freq=False)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"================ handle_mls_psd task, time taken: {elapsed_time}s ================")
return 200, {
str(task): {
"x_mls":x_mls.tolist(),
"y_mls":y_mls.tolist(),
}
}
def handle_mls_task(request_json,task):
start_time = time.time()
#length of mls will be 2**nbits - 1
calibrateSoundBurstMLSVersions = int(request_json['calibrateSoundBurstMLSVersions'])
desired_length = request_json["length"]
amplitude = request_json["amplitude"]
mls_transformed = []
scaled_mls_transformed = []
nbits = math.ceil(math.log(desired_length + 1, 2))
ret_arr = max_len_seq(nbits,length=desired_length*calibrateSoundBurstMLSVersions)
mls = ret_arr[0]
for i in range(calibrateSoundBurstMLSVersions):
print(i)
mls_transformed.append((np.where(mls == 0, -1, 1)[i*desired_length:(i+1)*desired_length]).tolist())
scaled_mls_transformed.append(
(np.where(mls == 0, -1, 1)[i*desired_length:(i+1)*desired_length]*amplitude).tolist())
print(mls_transformed[i] == mls_transformed[0])
print(len(mls_transformed[i]))
end_time = time.time()
elapsed_time = end_time - start_time
print(f"================ handle_mls task, time taken: {elapsed_time}s ================")
return 200, {
str(task):{
"mls": scaled_mls_transformed,
"unscaledMLS": mls_transformed
}
}
def handle_subtracted_psd_task(request_json,task):
start_time = time.time()
#print(request_json);
rec = request_json["rec"]
# knownGain = request_json["knownGains"]
# knownFreq = request_json["knownFrequencies"]
sample_rate = request_json["sampleRate"]
[y, x] = plt.psd(rec,Fs=sample_rate,NFFT=2048,scale_by_freq=False)
#[x_conv,y_conv] = plt.psd(rec_conv, Fs=96000, scale_by_freq=False)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"================ handle psd task, time taken: {elapsed_time}s ================")
return 200, {
str(task): {
"x":x.tolist(),
"y":y.tolist(),
}
}
SUPPORTED_TASKS = {
'impulse-response': handle_impulse_response_task,
'autocorrelation': handle_autocorrelation_task,
'all-hz-check': handle_all_hz_power_check_task,
'volume-check': handle_volume_power_check_task,
'component-inverse-impulse-response': handle_component_inverse_impulse_response_task,
'system-inverse-impulse-response': handle_system_inverse_impulse_response_task,
'convolution': handle_convolution_task,
'volume': handle_volume_task_nonlinear,
'volume-parameters': handle_volume_parameters,
'psd': handle_psd_task,
'subtracted-psd':handle_subtracted_psd_task,
'mls':handle_mls_task,
'background-psd': handle_background_psd_task,
'mls-psd': handle_mls_psd_task
}
def print_memory_usage():
print("memory used:", round(process.memory_info().rss / 1024 ** 2), "mb")
@app.route("/task/<string:task>", methods=['POST'])
@cross_origin()
def task_handler(task):
print_memory_usage()
gc.collect()
if task not in SUPPORTED_TASKS:
return 'ERROR'
content_type = request.headers.get('Content-Type')
if (content_type == 'application/json'):
headers = {"Content-Type": "application/json"}
status, result = SUPPORTED_TASKS[task](request.get_json(cache=False), task)
request.data
resp = make_response(result, status)
resp.headers = headers
print_memory_usage()
return resp
else:
return 'Content-Type not supported'
@app.route('/memory', methods=['POST'])
@cross_origin()
def print_memory():
return {'memory': process.memory_info().rss / 1024 ** 2}
@app.route("/snapshot")
@cross_origin()
def snap():
global s
if not s:
s = tracemalloc.take_snapshot()
return "taken snapshot\n"
else:
lines = []
top_stats = tracemalloc.take_snapshot().compare_to(s, 'lineno')
for stat in top_stats[:5]:
lines.append(str(stat))
return "\n".join(lines)
@app.route("/model/faceDetect")
@cross_origin()
def face_detect():
# load and return the JSON File: mediapipe_tfjs_model_face_detection_full.json
with open('mediapipe_tfjs_model_face_detection_full.json') as f:
data = json.load(f)
return data
if __name__ == '__main__':
app.run()