File size: 6,513 Bytes
2c7bc63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7360df7
2c7bc63
7360df7
 
 
 
 
 
 
 
 
 
 
 
2c7bc63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00677e
 
 
 
2c7bc63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00677e
 
2c7bc63
1a5e3ed
2c7bc63
 
 
 
 
 
 
 
 
 
 
 
 
7360df7
2c7bc63
 
7360df7
2c7bc63
 
 
 
1a5e3ed
2c7bc63
 
 
c00677e
 
 
 
 
2c7bc63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
πŸƒ Human Activity Recognition β€” Gradio Demo
Fine-tuned MobileNetV2 classifying 15 human activities from images.
Model: Rishi2455/Human-Activity-Recognition
"""

import os
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
from huggingface_hub import hf_hub_download

# ── Configuration ────────────────────────────────────────────────────────────
MODEL_REPO = "Rishi2455/Human-Activity-Recognition"
MODEL_FILE = "mobilenetv2_finetuned.h5"
IMG_SIZE = (224, 224)

CLASS_NAMES = [
    "Calling", "Clapping", "Cycling", "Dancing", "Drinking",
    "Eating", "Fighting", "Hugging", "Laughing", "Listening to Music",
    "Running", "Sitting", "Sleeping", "Texting", "Using Laptop",
]

ACTIVITY_EMOJI = {
    "Calling": "πŸ“ž", "Clapping": "πŸ‘", "Cycling": "🚴", "Dancing": "πŸ’ƒ",
    "Drinking": "πŸ₯€", "Eating": "🍽️", "Fighting": "πŸ₯Š", "Hugging": "πŸ€—",
    "Laughing": "πŸ˜‚", "Listening to Music": "🎧", "Running": "πŸƒ",
    "Sitting": "πŸͺ‘", "Sleeping": "😴", "Texting": "πŸ“±", "Using Laptop": "πŸ’»",
}

# ── Download & load model ───────────────────────────────────────────────────
print("⬇️  Downloading model...")
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
print("πŸ”§  Loading model...")
model = tf.keras.models.load_model(model_path, compile=False)
print("βœ…  Model loaded!")

# ── Example images (baked into the repo under examples/) ────────────────────
EXAMPLE_DIR = "examples"
EXAMPLE_FILES = [
    "calling.jpg", "clapping.jpg", "cycling.jpg", "dancing.jpg",
    "drinking.jpg", "eating.jpg", "fighting.jpg", "hugging.jpg",
    "laughing.jpg", "listening_to_music.jpg", "running.jpg",
    "sitting.jpg", "sleeping.jpg", "texting.jpg", "using_laptop.jpg",
]
example_paths = [
    os.path.join(EXAMPLE_DIR, f)
    for f in EXAMPLE_FILES
    if os.path.exists(os.path.join(EXAMPLE_DIR, f))
]
print(f"πŸ“Έ  Found {len(example_paths)} example images.")

# ── Inference ────────────────────────────────────────────────────────────────
def predict(pil_img: Image.Image) -> dict:
    """Classify a human activity from an image."""
    if pil_img is None:
        return {}
    img = pil_img.convert("RGB").resize(IMG_SIZE)
    arr = np.expand_dims(np.array(img, dtype=np.float32), axis=0)
    arr = tf.keras.applications.mobilenet_v2.preprocess_input(arr)
    preds = model.predict(arr, verbose=0)[0]
    emoji_labels = {
        f"{ACTIVITY_EMOJI.get(c, '')} {c}": float(preds[i])
        for i, c in enumerate(CLASS_NAMES)
    }
    return emoji_labels

def clear_all():
    """Reset both image and predictions."""
    return None, None

# ── Gradio UI ────────────────────────────────────────────────────────────────
DESCRIPTION = """
Upload a photo of a person performing an activity, and the model will predict which of **15 activities** they are doing.

**Supported activities:** Calling Β· Clapping Β· Cycling Β· Dancing Β· Drinking Β· Eating Β· Fighting Β· Hugging Β· Laughing Β· Listening to Music Β· Running Β· Sitting Β· Sleeping Β· Texting Β· Using Laptop

**Model:** [MobileNetV2](https://huggingface.co/Rishi2455/Human-Activity-Recognition) fine-tuned on the [Human Action Recognition dataset](https://huggingface.co/datasets/Bingsu/Human_Action_Recognition)
"""

css = """
.main-header { text-align: center; margin-bottom: 0.5rem; }
.main-header h1 { font-size: 2.2rem; margin-bottom: 0; }
.footer { text-align: center; margin-top: 1rem; color: #888; font-size: 0.85rem; }
"""

with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="sky",
        font=gr.themes.GoogleFont("Inter"),
    ),
    css=css,
    title="πŸƒ Human Activity Recognition",
    analytics_enabled=False,
) as demo:

    # Header
    gr.HTML("""
    <div class="main-header">
        <h1>πŸƒ Human Activity Recognition</h1>
        <p style="color: #555; font-size: 1.1rem;">Powered by MobileNetV2 Β· 15 Activity Classes</p>
    </div>
    """)

    gr.Markdown(DESCRIPTION)

    with gr.Row(equal_height=True):
        with gr.Column(scale=1):
            image_input = gr.Image(
                type="pil",
                label="πŸ“Έ Upload Image",
                sources=["upload", "webcam", "clipboard"],
                height=380,
            )
            with gr.Row():
                clear_btn = gr.Button(
                    "πŸ—‘οΈ Clear",
                    variant="secondary",
                    size="lg",
                )
                submit_btn = gr.Button(
                    "πŸ” Classify Activity",
                    variant="primary",
                    size="lg",
                )

        with gr.Column(scale=1):
            label_output = gr.Label(
                num_top_classes=5,
                label="πŸ“Š Prediction Results",
            )

    # Examples β€” all 15 activity classes, baked into the repo
    if example_paths:
        gr.Examples(
            examples=example_paths,
            inputs=image_input,
            outputs=label_output,
            fn=predict,
            cache_examples=True,
            label="πŸ–ΌοΈ Try these examples β€” one for each activity",
        )

    # Event handlers
    clear_btn.click(
        fn=clear_all,
        inputs=[],
        outputs=[image_input, label_output],
    )
    submit_btn.click(
        fn=predict,
        inputs=image_input,
        outputs=label_output,
        api_name="predict",
    )
    image_input.change(
        fn=predict,
        inputs=image_input,
        outputs=label_output,
        api_name=False,
    )

    # Footer
    gr.HTML("""
    <div class="footer">
        Made with ❀️ using <a href="https://www.gradio.app/" target="_blank">Gradio</a> &
        <a href="https://huggingface.co/" target="_blank">Hugging Face</a> Β·
        <a href="https://huggingface.co/Rishi2455/Human-Activity-Recognition" target="_blank">Model Card</a>
    </div>
    """)

# Launch with show_api=True
demo.launch(show_api=True)