File size: 3,355 Bytes
1659c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

# --- 1. Load the Pre-trained Model and Label Encoder ---
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()

# Re-create the label encoder to decode predictions
labels = ['No', 'Yes']
label_encoder = LabelEncoder()
label_encoder.fit(labels)

# --- 2. Define the Prediction Function for Gradio ---
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
    }])

    # Preprocessing to match the training data format
    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]

    # Make prediction
    prediction = loaded_model.predict(input_data)[0]

    # Decode the prediction
    decoded_prediction = label_encoder.inverse_transform([prediction])[0]
    return decoded_prediction

# --- 3. Create and Launch the Gradio Interface ---
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()