|
|
import gradio as gr
|
|
|
import joblib
|
|
|
import pandas as pd
|
|
|
from sklearn.preprocessing import LabelEncoder
|
|
|
from sklearn.pipeline import Pipeline
|
|
|
from sklearn.preprocessing import StandardScaler
|
|
|
from sklearn.svm import SVC
|
|
|
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
|
|
|
from sklearn.neighbors import KNeighborsClassifier
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
model_filename = 'stacking_classifier_model.joblib'
|
|
|
try:
|
|
|
loaded_model = joblib.load(model_filename)
|
|
|
except FileNotFoundError:
|
|
|
print(f"Error: The model file '{model_filename}' was not found.")
|
|
|
print("Please make sure you have run the training script and saved the model.")
|
|
|
exit()
|
|
|
|
|
|
|
|
|
labels = ['No', 'Yes']
|
|
|
label_encoder = LabelEncoder()
|
|
|
label_encoder.fit(labels)
|
|
|
|
|
|
|
|
|
def predict_fall(impact_force, body_orientation, heart_rate, temperature, movement_activity, location):
|
|
|
"""
|
|
|
This function takes user inputs, preprocesses them, and uses the
|
|
|
trained Stacking Classifier to predict if a fall has occurred.
|
|
|
"""
|
|
|
input_data = pd.DataFrame([{
|
|
|
'Impact Force Level': impact_force,
|
|
|
'Body Orientation (degrees)': body_orientation,
|
|
|
'Heart Rate (bpm)': heart_rate,
|
|
|
'Temperature (C)': temperature,
|
|
|
'Movement Activity': movement_activity,
|
|
|
'Location': location
|
|
|
}])
|
|
|
|
|
|
|
|
|
input_data['Location_Bathroom'] = 1 if location == 'Bathroom' else 0
|
|
|
input_data['Location_Bedroom'] = 1 if location == 'Bedroom' else 0
|
|
|
input_data['Location_Kitchen'] = 1 if location == 'Kitchen' else 0
|
|
|
input_data['Location_Living Room'] = 1 if location == 'Living Room' else 0
|
|
|
input_data.drop(['Location'], axis=1, inplace=True)
|
|
|
|
|
|
input_data['Movement Activity_Falling'] = 1 if movement_activity == 'Falling' else 0
|
|
|
input_data['Movement Activity_Standing'] = 1 if movement_activity == 'Standing' else 0
|
|
|
input_data['Movement Activity_Walking'] = 1 if movement_activity == 'Walking' else 0
|
|
|
input_data.drop(['Movement Activity'], axis=1, inplace=True)
|
|
|
|
|
|
training_columns = loaded_model.feature_names_in_
|
|
|
for col in training_columns:
|
|
|
if col not in input_data.columns:
|
|
|
input_data[col] = 0
|
|
|
|
|
|
input_data = input_data[training_columns]
|
|
|
|
|
|
|
|
|
prediction = loaded_model.predict(input_data)[0]
|
|
|
|
|
|
|
|
|
decoded_prediction = label_encoder.inverse_transform([prediction])[0]
|
|
|
return decoded_prediction
|
|
|
|
|
|
|
|
|
inputs = [
|
|
|
gr.Slider(1, 100, label="Impact Force Level"),
|
|
|
gr.Slider(0, 180, label="Body Orientation (degrees)"),
|
|
|
gr.Slider(30, 200, label="Heart Rate (bpm)"),
|
|
|
gr.Slider(35, 42, label="Temperature (C)"),
|
|
|
gr.Dropdown(["Walking", "Standing", "Falling"], label="Movement Activity"),
|
|
|
gr.Dropdown(["Living Room", "Bedroom", "Kitchen", "Bathroom"], label="Location")
|
|
|
]
|
|
|
|
|
|
outputs = gr.Textbox(label="Fall Detected")
|
|
|
|
|
|
demo = gr.Interface(fn=predict_fall, inputs=inputs, outputs=outputs, title="Fall Detection System")
|
|
|
demo.launch() |