Snigs98 commited on
Commit
1a45e9e
·
verified ·
1 Parent(s): bb78861

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -49
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, LabelEncoder
7
- import zipfile
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
- # Ensure dataset is extracted
17
- if not os.path.exists(dataset_dir) or not os.path.exists(csv_path):
18
- if os.path.exists(dataset_zip):
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
- # Load dataset
25
- if os.path.exists(csv_path):
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
- # Load trained model
35
- if os.path.exists(model_path):
36
- model = load_model(model_path)
37
- else:
38
- raise FileNotFoundError("Model file not found. Please upload the trained model.")
 
 
 
39
 
40
- def predict_activity(sensor_data):
41
- """
42
- Predict human activity based on sensor input.
43
- Input should be comma-separated values (e.g., "0.1, 0.2, -0.3, ...").
44
- """
45
- try:
46
- features = np.array([float(x) for x in sensor_data.split(",")])
47
- if features.shape[0] != 60: # Ensure input shape matches expected (20 timesteps * 3 features)
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
- except Exception as e: # Ensure the except block exists
58
- return f"Error: {str(e)}"
 
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()