def register_device(): patient_id = request.json['patient_id'] spimo_id = request.json['spimo_id'] pedal_id = request.json['pedal_id'] date_time = datetime.datetime.now() connection = get_connection() table_name = 'dp_pair' data = {'device_id': spimo_id} row = read_data(table_name, data) #scenario 2: No such device_id if row is None: close_connection(connection) return str(2) #scenario 1: Successful elif row.patient_id is None: data = {'patient_id': patient_id, 'updated_date': date_time} condition = {'device_id': spimo_id} update_data(table_name, data, condition) condition = {'device_id': pedal_id} update_data(table_name, data, condition) close_connection(connection) return str(1) #scenario 2: duplicated id else: close_connection(connection) return str(0)
def return_device(): patient_id = request.json['patient_id'] device_id = request.json['device_id'] date_time = datetime.datetime.now() table_name = 'dp_pair' row = read_data(table_name, data) # device has been returned if row.patient_id is None: return str(0) else: data = {'patient_id': None} condition = {'device_id': device_id} update_data(table_name, data, condition)
def handle_audio(): api_result = json.loads(request.form.get('data')) seq = api_result["seq"] device_id = api_result["device_id"] session_id = api_result["session_id"] audiofile = request.files['audio'] filename = werkzeug.utils.secure_filename(audiofile.filename) audiofile.save(filename) if filename is not None: signal, rate = sf.read(filename) wav_length = len(signal) / rate #second window_length = 1 # for index in range(sample_per_wav): # take ten windows in an audio file window_seed = random.uniform(0, abs(wav_length - window_length)) window_range = (window_seed, window_seed + window_length) window_signal = signal[int(window_range[0] * rate):int(window_range[1] * rate)] mfcc_feat = mfcc(window_signal) mfcc_feat_scale = mfcc_feat / np.linalg.norm(mfcc_feat) mfcc_feat_scale = mfcc_feat_scale[np.newaxis, :] print(mfcc_feat_scale.shape) # result_array=(mfcc_feat-np.min(mfcc_feat))/np.ptp(mfcc_feat) model = tensorflow.keras.models.load_model('./model') predictions = model.predict(mfcc_feat_scale) print(predictions) into_sad_prob = predictions[0][0] into_angry_prob = predictions[0][1] into_happy_prob = predictions[0][2] into_neutral_prob = predictions[0][3] table_name = 'transaction_table' data = { 'session_id': session_id, 'seq': seq, 'into_sad_prob': str(into_sad_prob), 'into_angry_prob': str(into_angry_prob), 'into_happy_prob': str(into_happy_prob), 'into_neutral_prob': str(into_neutral_prob) } r_data = {'session_id': session_id, 'seq': seq} connection = get_connection() row, number_rows = read_data(table_name, r_data, connection) if number_rows == 0: connection = insert_data(table_name, data, connection) else: condition = {'session_id': session_id, 'seq': seq} connection = update_data(table_name, data, condition, connection) return str(1) else: return str(0)
def device_refresh(): patient_id = request.args.get('patient_id') print(patient_id) device_id = request.args.get('device_id') print(device_id) connection = get_connection() table_name = 'dp_pair' data = {'device_id': device_id, 'patient_id': patient_id} row = read_data(table_name, data) start_time = row.start_time current_time = datetime.datetime.now() if (device_id[0:3] == 'SPI'): table_name = 'spimo' elif (device_id[0:3] == 'PED'): table_name = 'pedal' row = read_count(table_name, start_time, current_time, device_id) result = row.total_count close_connection(connection) return str(result)
def handle_ml(): session_id = request.json['session_id'] seq = request.json['seq'] table_name = 'transaction_table' connection = get_connection() text_senti_avg, text_senti_std, text_senti_min, text_senti_max = calculate_features( table_name, session_id, seq, connection) condition = {'session_id': session_id, 'seq': seq} features_data = { 'text_senti_avg': text_senti_avg, 'text_senti_std': text_senti_std, 'text_senti_min': text_senti_min, 'text_senti_max': text_senti_max } update_data(table_name, features_data, condition, connection) r_data = {'session_id': session_id, 'seq': seq} rows, number_rows = read_data(table_name, r_data, connection) emotion_label = send_request_to_ml(rows) features_data = {'result': emotion_label} update_data(table_name, features_data, condition, connection) close_connection(connection) return emotion_label
def return_device(): patient_id = request.json['patient_id'] spimo_id = request.json['spimo_id'] pedal_id = request.json['pedal_id'] date_time = datetime.datetime.now() connection = get_connection() table_name = 'dp_pair' data = {'device_id': spimo_id} row = read_data(table_name, data) # device has been returned if row.patient_id is None: close_connection(connection) return str(0) else: data = {'patient_id': None, 'updated_date': None} condition = {'device_id': spimo_id} update_data(table_name, data, condition) condition = {'device_id': pedal_id} update_data(table_name, data, condition) close_connection(connection) return str(1)