[3739ec]: / server / server.py

Download this file

133 lines (99 with data), 4.3 kB

  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
from flask import Flask, jsonify, request
from flask_cors import CORS
from threading import Thread
import pandas as pd
import os
import time
from lstm_model import LSTMModel # Import the LSTMModel class
app = Flask(__name__)
CORS(app) # Enable CORS
# Path to the directory containing elder CSV files
ELDER_FILES_DIR = 'elders_live'
# Initialize the elders data and the current row indices
elders_data = []
current_indices = {}
def update_elders_data():
global elders_data, current_indices
while True:
new_elders_data = []
for file_name in os.listdir(ELDER_FILES_DIR):
if file_name.endswith('.csv'):
elder_id = file_name.split('.')[0]
file_path = os.path.join(ELDER_FILES_DIR, file_name)
# Read the CSV file
df = pd.read_csv(file_path)
# Check if the DataFrame is not empty
if not df.empty:
# Initialize the current index if not already
if elder_id not in current_indices:
current_indices[elder_id] = 0
# Get the current index and activity
current_index = current_indices[elder_id]
# Ensure the index is within the bounds of the DataFrame
if current_index < len(df):
current_activity = df['predicted_label'].iloc[current_index].capitalize(
)
new_elders_data.append({
'id': elder_id,
'name': f'Elder {elder_id}',
'activity': current_activity
})
# Update the index for the next step
current_indices[elder_id] = (
current_index + 1) % len(df)
else:
print(f"Index {current_index} is out of bounds for {
file_name}")
elders_data = new_elders_data
time.sleep(0.1) # Update every second
@app.route('/get_elders', methods=['GET'])
def get_elders():
return jsonify(elders_data)
@app.route('/add_elder', methods=['POST'])
def add_elder():
data = request.json
elder_id = data.get('id')
if not elder_id:
return jsonify({'error': 'Elder ID is required'}), 400
file_path = os.path.join('data', f'{elder_id}.csv')
output_path = os.path.join(ELDER_FILES_DIR, f'{elder_id}.csv')
if os.path.isfile(output_path):
return jsonify({'message': f'Elder {elder_id} already exsist.'}), 200
if not os.path.isfile(file_path):
return jsonify({'error': f'Input file for elder {elder_id} not found'}), 404
# Define the labels dictionary
labels_dic = {
1: 'Walking',
3: 'Shuffling',
4: 'Stairs (asce)',
5: 'Stairs (desc)',
6: 'Standing',
7: 'Sitting',
8: 'Lying'
}
model_path = 'model.h5'
# Create an instance of the LSTMModel class
lstm_model = LSTMModel(model_path, labels_dic)
# Predict and save to the output CSV
lstm_model.predict_and_save(file_path, output_path)
return jsonify({'message': f'Elder {elder_id} Admitted to ElderCare+'}), 200
@app.route('/remove_elder/<id>', methods=['DELETE'])
def remove_elder(id):
global elders_data, current_indices
file_path = os.path.join(ELDER_FILES_DIR, f'{id}.csv')
if os.path.isfile(file_path):
os.remove(file_path)
# Remove elder from elders_data and current_indices
elders_data = [elder for elder in elders_data if elder['id'] != id]
if id in current_indices:
del current_indices[id]
return jsonify({'message': f'Elder {id} removed successfully!'}), 200
else:
return jsonify({'error': f'Elder {id} not found'}), 404
if __name__ == '__main__':
# Start the background thread
thread = Thread(target=update_elders_data)
thread.daemon = True
thread.start()
# Make the server accessible externally
app.run(debug=True, host='0.0.0.0')