Upload 2 files
Browse files- app.py +81 -0
- stacking_classifier_model.joblib +3 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.preprocessing import LabelEncoder
|
| 5 |
+
from sklearn.pipeline import Pipeline
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
from sklearn.svm import SVC
|
| 8 |
+
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
|
| 9 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 10 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
# --- 1. Load the Pre-trained Model and Label Encoder ---
|
| 14 |
+
model_filename = 'stacking_classifier_model.joblib'
|
| 15 |
+
try:
|
| 16 |
+
loaded_model = joblib.load(model_filename)
|
| 17 |
+
except FileNotFoundError:
|
| 18 |
+
print(f"Error: The model file '{model_filename}' was not found.")
|
| 19 |
+
print("Please make sure you have run the training script and saved the model.")
|
| 20 |
+
exit()
|
| 21 |
+
|
| 22 |
+
# Re-create the label encoder to decode predictions
|
| 23 |
+
labels = ['No', 'Yes']
|
| 24 |
+
label_encoder = LabelEncoder()
|
| 25 |
+
label_encoder.fit(labels)
|
| 26 |
+
|
| 27 |
+
# --- 2. Define the Prediction Function for Gradio ---
|
| 28 |
+
def predict_fall(impact_force, body_orientation, heart_rate, temperature, movement_activity, location):
|
| 29 |
+
"""
|
| 30 |
+
This function takes user inputs, preprocesses them, and uses the
|
| 31 |
+
trained Stacking Classifier to predict if a fall has occurred.
|
| 32 |
+
"""
|
| 33 |
+
input_data = pd.DataFrame([{
|
| 34 |
+
'Impact Force Level': impact_force,
|
| 35 |
+
'Body Orientation (degrees)': body_orientation,
|
| 36 |
+
'Heart Rate (bpm)': heart_rate,
|
| 37 |
+
'Temperature (C)': temperature,
|
| 38 |
+
'Movement Activity': movement_activity,
|
| 39 |
+
'Location': location
|
| 40 |
+
}])
|
| 41 |
+
|
| 42 |
+
# Preprocessing to match the training data format
|
| 43 |
+
input_data['Location_Bathroom'] = 1 if location == 'Bathroom' else 0
|
| 44 |
+
input_data['Location_Bedroom'] = 1 if location == 'Bedroom' else 0
|
| 45 |
+
input_data['Location_Kitchen'] = 1 if location == 'Kitchen' else 0
|
| 46 |
+
input_data['Location_Living Room'] = 1 if location == 'Living Room' else 0
|
| 47 |
+
input_data.drop(['Location'], axis=1, inplace=True)
|
| 48 |
+
|
| 49 |
+
input_data['Movement Activity_Falling'] = 1 if movement_activity == 'Falling' else 0
|
| 50 |
+
input_data['Movement Activity_Standing'] = 1 if movement_activity == 'Standing' else 0
|
| 51 |
+
input_data['Movement Activity_Walking'] = 1 if movement_activity == 'Walking' else 0
|
| 52 |
+
input_data.drop(['Movement Activity'], axis=1, inplace=True)
|
| 53 |
+
|
| 54 |
+
training_columns = loaded_model.feature_names_in_
|
| 55 |
+
for col in training_columns:
|
| 56 |
+
if col not in input_data.columns:
|
| 57 |
+
input_data[col] = 0
|
| 58 |
+
|
| 59 |
+
input_data = input_data[training_columns]
|
| 60 |
+
|
| 61 |
+
# Make prediction
|
| 62 |
+
prediction = loaded_model.predict(input_data)[0]
|
| 63 |
+
|
| 64 |
+
# Decode the prediction
|
| 65 |
+
decoded_prediction = label_encoder.inverse_transform([prediction])[0]
|
| 66 |
+
return decoded_prediction
|
| 67 |
+
|
| 68 |
+
# --- 3. Create and Launch the Gradio Interface ---
|
| 69 |
+
inputs = [
|
| 70 |
+
gr.Slider(1, 100, label="Impact Force Level"),
|
| 71 |
+
gr.Slider(0, 180, label="Body Orientation (degrees)"),
|
| 72 |
+
gr.Slider(30, 200, label="Heart Rate (bpm)"),
|
| 73 |
+
gr.Slider(35, 42, label="Temperature (C)"),
|
| 74 |
+
gr.Dropdown(["Walking", "Standing", "Falling"], label="Movement Activity"),
|
| 75 |
+
gr.Dropdown(["Living Room", "Bedroom", "Kitchen", "Bathroom"], label="Location")
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
outputs = gr.Textbox(label="Fall Detected")
|
| 79 |
+
|
| 80 |
+
demo = gr.Interface(fn=predict_fall, inputs=inputs, outputs=outputs, title="Fall Detection System")
|
| 81 |
+
demo.launch()
|
stacking_classifier_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bb844397f24d39d96698f1ca80cd6ff34aa62675e75714caae39c1524b5bd82b
|
| 3 |
+
size 15289447
|