Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import tensorflow as tf
|
| 5 |
from tensorflow.keras.models import load_model
|
| 6 |
-
from sklearn.preprocessing import StandardScaler
|
| 7 |
-
import
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
# Define dataset paths
|
| 11 |
-
dataset_zip = "/mnt/data/HUMAN_ACTIVITY_RECOGNITION.zip"
|
| 12 |
-
dataset_dir = "/mnt/data/human_activity_dataset"
|
| 13 |
-
csv_path = os.path.join(dataset_dir, "time_series_data_human_activities.csv")
|
| 14 |
-
model_path = "/mnt/data/human_activity_recognition_model.h5"
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
with zipfile.ZipFile(dataset_zip, 'r') as zip_ref:
|
| 20 |
-
zip_ref.extractall(dataset_dir)
|
| 21 |
-
else:
|
| 22 |
-
raise FileNotFoundError(f"Dataset zip file '{dataset_zip}' not found. Please upload it.")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
df = pd.read_csv(csv_path)
|
| 27 |
-
scaler = StandardScaler()
|
| 28 |
-
df[['x-axis', 'y-axis', 'z-axis']] = scaler.fit_transform(df[['x-axis', 'y-axis', 'z-axis']])
|
| 29 |
-
label_encoder = LabelEncoder()
|
| 30 |
-
df['activity'] = label_encoder.fit_transform(df['activity'])
|
| 31 |
-
else:
|
| 32 |
-
raise FileNotFoundError("Dataset CSV file not found after extraction. Please check the ZIP contents.")
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
""
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return "Error: Expected 60 values (20 timesteps * 3 features)."
|
| 49 |
-
|
| 50 |
-
features = features.reshape(1, 20, 3) # Reshape for LSTM model
|
| 51 |
-
features = scaler.transform(features.reshape(-1, 3)).reshape(1, 20, 3) # Scale and reshape
|
| 52 |
-
|
| 53 |
-
prediction = model.predict(features)
|
| 54 |
-
predicted_label = label_encoder.inverse_transform([np.argmax(prediction)])[0]
|
| 55 |
-
return f"Predicted Activity: {predicted_label}"
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow.keras.models import load_model
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load model and label encoder
|
| 9 |
+
model = load_model("human_activity_recognition_model.h5")
|
| 10 |
+
label_encoder = {0: "Walking", 1: "Running", 2: "Jumping", 3: "Sitting"} # Adjust as per your dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Sample scaler (ensure this matches what you used during training)
|
| 13 |
+
scaler = StandardScaler()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Define prediction function
|
| 16 |
+
def predict_activity(x, y, z):
|
| 17 |
+
sample_input = np.array([[x, y, z]])
|
| 18 |
+
sample_input = scaler.fit_transform(sample_input) # Apply the same scaling as training
|
| 19 |
+
sample_input = np.expand_dims(sample_input, axis=0) # Reshape for LSTM input
|
| 20 |
+
prediction = model.predict(sample_input)
|
| 21 |
+
predicted_label = label_encoder[np.argmax(prediction)]
|
| 22 |
+
return f"Predicted Activity: {predicted_label}"
|
| 23 |
|
| 24 |
+
# Create Gradio Interface
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=predict_activity,
|
| 27 |
+
inputs=[gr.Number(label="X-axis"), gr.Number(label="Y-axis"), gr.Number(label="Z-axis")],
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="Human Activity Recognition",
|
| 30 |
+
description="Enter accelerometer values (X, Y, Z) to predict activity.",
|
| 31 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Launch the app
|
| 34 |
+
demo.launch()
|