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 device_start(): patient_id = request.json['patient_id'] device_id = request.json['device_id'] connection = get_connection() date_time = datetime.datetime.now() data = {'start_time': date_time} table_name = 'dp_pair' condition = {'device_id': device_id, 'patient_id': patient_id} update_data(table_name, data, condition) close_connection(connection) return str(1)
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)
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 handle_senti_score_request(): session_id = request.json['session_id'] seq = request.json['seq'] text_senti_score = request.json['sentiment_score'] again = request.json['again'] # upadte if record exists table_name = 'transaction_table' data = { 'session_id': session_id, 'seq': seq, 'text_senti_score': text_senti_score } connection = get_connection() if again == '1': condition = {'session_id': session_id, 'seq': seq} connection = update_data(table_name, data, condition, connection) elif again == '0': connection = insert_data(table_name, data, connection) close_connection(connection) return str(1)
def handle_request(): api_result = json.loads(request.form.get('data')) seq = api_result["seq"] device_id = api_result["device_id"] session_id = api_result["session_id"] imagefile = request.files['image'] filename = werkzeug.utils.secure_filename(imagefile.filename) imagefile.save(filename) with open(filename, 'rb') as f: data = f.read() _url = 'https://westus2.api.cognitive.microsoft.com/face/v1.0/detect' _key = 'bc027dc227484433a77d7b613807d230' #Here you have to paste your primary key headers = dict() headers['Ocp-Apim-Subscription-Key'] = _key headers['Content-Type'] = 'application/octet-stream' json_ = None params = { 'returnFaceId': 'true', 'returnFaceLandmarks': 'false', 'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise', } result = processRequest(json_, data, headers, params, _url) connection = get_connection() if result == []: date_time = datetime.datetime.now() data = { 'date': date_time, 'session_id': session_id, 'seq': seq, 'device_id': device_id, 'face_smile': 0, 'face_anger': 0, 'face_contempt': 0, 'face_disgust': 0, 'face_fear': 0, 'face_happiness': 0, 'face_neutral': 0, 'face_sadness': 0, 'face_surprise': 0 } table_name = 'transaction_table' condition = {'session_id': session_id, 'seq': seq} connection = update_data(table_name, data, condition, connection) close_connection(connection) return str(4) elif result is None: date_time = datetime.datetime.now() data = { 'date': date_time, 'session_id': session_id, 'seq': seq, 'device_id': device_id, 'face_smile': 0, 'face_anger': 0, 'face_contempt': 0, 'face_disgust': 0, 'face_fear': 0, 'face_happiness': 0, 'face_neutral': 0, 'face_sadness': 0, 'face_surprise': 0 } table_name = 'transaction_table' condition = {'session_id': session_id, 'seq': seq} connection = update_data(table_name, data, condition, connection) close_connection(connection) return str(5) elif result[0]['faceAttributes'] is None: date_time = datetime.datetime.now() data = { 'date': date_time, 'session_id': session_id, 'seq': seq, 'device_id': device_id, 'face_smile': 0, 'face_anger': 0, 'face_contempt': 0, 'face_disgust': 0, 'face_fear': 0, 'face_happiness': 0, 'face_neutral': 0, 'face_sadness': 0, 'face_surprise': 0 } table_name = 'transaction_table' condition = {'session_id': session_id, 'seq': seq} connection = update_data(table_name, data, condition, connection) close_connection(connection) return str(6) firstface_dic = result[0] faceAttributes_dic = firstface_dic['faceAttributes'] smile = faceAttributes_dic['smile'] #gender = faceAttributes_dic['gender'] anger = faceAttributes_dic['emotion']['anger'] contempt = faceAttributes_dic['emotion']['contempt'] disgust = faceAttributes_dic['emotion']['disgust'] fear = faceAttributes_dic['emotion']['fear'] happiness = faceAttributes_dic['emotion']['happiness'] neutral = faceAttributes_dic['emotion']['neutral'] sadness = faceAttributes_dic['emotion']['sadness'] surprise = faceAttributes_dic['emotion']['surprise'] connection = get_connection() date_time = datetime.datetime.now() data = { 'date': date_time, 'session_id': session_id, 'seq': seq, 'device_id': device_id, 'face_smile': smile, 'face_anger': anger, 'face_contempt': contempt, 'face_disgust': disgust, 'face_fear': fear, 'face_happiness': happiness, 'face_neutral': neutral, 'face_sadness': sadness, 'face_surprise': surprise } table_name = 'transaction_table' condition = {'session_id': session_id, 'seq': seq} connection = update_data(table_name, data, condition, connection) close_connection(connection) return str(1)