File size: 7,955 Bytes
a65288a
287e508
 
 
a65288a
 
 
7057729
 
 
 
 
 
a65288a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7057729
 
 
 
 
 
 
 
b77223e
 
a65288a
 
 
 
 
 
 
 
 
2705bc4
a65288a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7057729
 
a65288a
 
 
 
 
ac0c5d3
a65288a
b77223e
 
a65288a
 
 
2705bc4
a65288a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2705bc4
a65288a
 
 
2705bc4
ac0c5d3
a65288a
 
 
 
 
 
 
 
 
 
 
 
 
 
2705bc4
a65288a
 
 
2705bc4
a65288a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2705bc4
a65288a
2705bc4
a65288a
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import streamlit as st
import numpy as np
import pandas as pd

# ── Constants ──────────────────────────────────────────────────────────────

LABEL_MAP = {
    0: "WALKING",
    1: "WALKING_UPSTAIRS",
    2: "WALKING_DOWNSTAIRS",
    3: "SITTING",
    4: "STANDING",
    5: "LAYING",
}

EXPLANATIONS = {
    "LAYING": "Minimal movement detected across all axes with low acceleration magnitude β€” consistent with a stationary horizontal posture.",
    "SITTING": "Low dynamic acceleration with a stable gravity component suggests a stationary upright posture with little body movement.",
    "STANDING": "Similar to sitting but with slight postural micro-movements. This class is often the hardest to distinguish from sitting.",
    "WALKING": "Rhythmic periodic acceleration with peaks on the vertical axis β€” consistent with level walking at normal cadence.",
    "WALKING_DOWNSTAIRS": "Downward gravitational shift with higher impact peaks characteristic of descending a staircase.",
    "WALKING_UPSTAIRS": "Elevated vertical acceleration effort with upward body displacement β€” consistent with climbing stairs.",
}

# ── Model loader ────────────────────────────────────────────────────────────

@st.cache_resource
def load_model():
    try:
        import tensorflow as tf
        from model_def import FeedForwardNetwork
        model = tf.keras.models.load_model(
            "model.keras",
            custom_objects={"FeedForwardNetwork": FeedForwardNetwork},
        )
        return model, "ready"
    except Exception as e:
        return None, f"error: {e}"

# ── Page config ─────────────────────────────────────────────────────────────

st.set_page_config(
    page_title="Human Activity Recognition",
    page_icon="πŸƒ",
    layout="centered"
)

st.title("Human Activity Recognition")
st.markdown(
    "Deep learning classifier trained on 561 smartphone sensor features "
    "from the [UCI HAR dataset](https://www.kaggle.com/datasets/uciml/human-activity-recognition-with-smartphones). "
    "Classifies six daily activities from accelerometer and gyroscope readings."
)

# ── Sidebar ──────────────────────────────────────────────────────────────────

with st.sidebar:
    st.header("About")
    st.markdown("""
    **Dataset:** UCI Human Activity Recognition
    **Subjects:** 30 volunteers aged 19–48
    **Sensor:** Samsung Galaxy S II (waist-mounted)
    **Sampling rate:** 50Hz
    **Features:** 561 time + frequency domain features
    **Classes:** 6 activities of daily living
    """)
    st.markdown("---")
    st.markdown("**Model performance on test set**")
    st.metric("Architecture", "FFN 512β†’256β†’128")
    st.metric("Status", "FFN live Β· CNN pending")
    st.markdown("---")
    st.caption("DAT606 Group Assignment Β· Pan-Atlantic University")

# ── Model status ─────────────────────────────────────────────────────────────

model, model_status = load_model()

if model_status != "ready":
    st.warning(f"Model not loaded β€” {model_status}")

# ── Tabs ─────────────────────────────────────────────────────────────────────

tab1, tab2 = st.tabs(["Select a Sample", "Upload Phyphox CSV"])

# ── Tab 1: Sample selector ───────────────────────────────────────────────────

with tab1:
    st.subheader("Select a pre-loaded test sample")
    st.caption(
        "Each sample is one 2.56-second window of sensor data "
        "from a test subject the model has never seen during training."
    )

    try:
        samples_df = pd.read_csv("data/samples.csv")
        feature_cols = [
            c for c in samples_df.columns
            if c not in ["Activity", "subject"]
        ]

        sample_labels = [
            f"Sample {i+1}  β€”  {row['Activity']}"
            for i, (_, row) in enumerate(samples_df.iterrows())
        ]

        selected = st.selectbox("Choose a sample:", sample_labels)
        selected_idx = sample_labels.index(selected)
        selected_row = samples_df.iloc[selected_idx]
        true_label = selected_row["Activity"]
        feature_vector = selected_row[feature_cols].values.astype(np.float32)

        col1, col2 = st.columns(2)
        with col1:
            st.metric("True Activity", true_label)
        with col2:
            st.metric("Feature count", len(feature_vector))

        if st.button("Classify this sample", type="primary"):
            if model_status == "no_model":
                st.error("Model not loaded β€” cannot predict yet.")
            else:
                arr = feature_vector.reshape(1, -1)
                probs = model.predict(arr, verbose=0)[0]
                pred_idx = int(np.argmax(probs))
                pred_label = LABEL_MAP[pred_idx]
                confidence = float(probs[pred_idx]) * 100
                correct = pred_label == true_label

                st.markdown("---")
                st.subheader("Result")

                if correct:
                    st.success(
                        f"**{pred_label}** Β· {confidence:.1f}% confidence Β· βœ“ Correct"
                    )
                else:
                    st.error(
                        f"**{pred_label}** Β· {confidence:.1f}% confidence Β· "
                        f"βœ— Incorrect (true: {true_label})"
                    )

                st.markdown(f"_{EXPLANATIONS[pred_label]}_")
                st.markdown("**Confidence across all classes**")

                chart_data = pd.DataFrame({
                    "Confidence (%)": [
                        float(probs[i]) * 100 for i in range(6)
                    ]
                }, index=[LABEL_MAP[i] for i in range(6)])

                st.bar_chart(chart_data)

    except FileNotFoundError:
        st.error("Sample data file not found. Add `data/samples.csv` to the repo.")

# ── Tab 2: Phyphox upload (placeholder) ─────────────────────────────────────

with tab2:
    st.subheader("Upload Phyphox sensor recording")
    st.markdown("""
    **How to record your own data:**
    1. Install [Phyphox](https://phyphox.org/) on your phone
    2. Open the **Acceleration (without g)** and **Gyroscope** experiments
    3. Record at least 3 seconds of a single activity
    4. Export as CSV and upload below
    """)

    uploaded_file = st.file_uploader(
        "Upload Phyphox CSV export",
        type=["csv"],
        help="Export from Phyphox as CSV β€” must contain accelerometer and gyroscope columns"
    )

    if uploaded_file is not None:
        st.info(
            "Phyphox pipeline coming soon. "
            "Feature extraction from raw sensor readings "
            "(filtering β†’ jerk β†’ FFT β†’ 561 features) is under development."
        )
        try:
            preview = pd.read_csv(uploaded_file)
            st.markdown("**File preview:**")
            st.dataframe(preview.head(10))
            st.caption(
                f"{len(preview)} rows Β· {len(preview.columns)} columns detected"
            )
        except Exception as e:
            st.error(f"Could not read file: {e}")