Upload app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,6 @@ import tensorflow as tf
|
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
-
from datasets import load_dataset
|
| 14 |
|
| 15 |
# ββ Configuration ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 16 |
MODEL_REPO = "Rishi2455/Human-Activity-Recognition"
|
|
@@ -30,12 +29,6 @@ ACTIVITY_EMOJI = {
|
|
| 30 |
"Sitting": "πͺ", "Sleeping": "π΄", "Texting": "π±", "Using Laptop": "π»",
|
| 31 |
}
|
| 32 |
|
| 33 |
-
CLASS_NAMES_LOWER = [
|
| 34 |
-
"calling", "clapping", "cycling", "dancing", "drinking",
|
| 35 |
-
"eating", "fighting", "hugging", "laughing", "listening_to_music",
|
| 36 |
-
"running", "sitting", "sleeping", "texting", "using_laptop",
|
| 37 |
-
]
|
| 38 |
-
|
| 39 |
# ββ Download & load model βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
print("β¬οΈ Downloading model...")
|
| 41 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
|
@@ -43,30 +36,20 @@ print("π§ Loading model...")
|
|
| 43 |
model = tf.keras.models.load_model(model_path, compile=False)
|
| 44 |
print("β
Model loaded!")
|
| 45 |
|
| 46 |
-
# ββ
|
| 47 |
EXAMPLE_DIR = "examples"
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
img = row["image"]
|
| 61 |
-
fname = f"{EXAMPLE_DIR}/{CLASS_NAMES_LOWER[label]}.jpg"
|
| 62 |
-
img.save(fname)
|
| 63 |
-
example_paths.append(fname)
|
| 64 |
-
seen.add(label)
|
| 65 |
-
if len(seen) == 15:
|
| 66 |
-
break
|
| 67 |
-
print(f"β
Saved {len(example_paths)} example images (all 15 classes).")
|
| 68 |
-
except Exception as e:
|
| 69 |
-
print(f"β οΈ Could not load examples: {e}")
|
| 70 |
|
| 71 |
# ββ Inference ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
def predict(pil_img: Image.Image) -> dict:
|
|
@@ -149,10 +132,10 @@ with gr.Blocks(
|
|
| 149 |
label="π Prediction Results",
|
| 150 |
)
|
| 151 |
|
| 152 |
-
# Examples β all 15 activity classes
|
| 153 |
if example_paths:
|
| 154 |
gr.Examples(
|
| 155 |
-
examples=
|
| 156 |
inputs=image_input,
|
| 157 |
outputs=label_output,
|
| 158 |
fn=predict,
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 13 |
|
| 14 |
# ββ Configuration ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
MODEL_REPO = "Rishi2455/Human-Activity-Recognition"
|
|
|
|
| 29 |
"Sitting": "πͺ", "Sleeping": "π΄", "Texting": "π±", "Using Laptop": "π»",
|
| 30 |
}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# ββ Download & load model βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
print("β¬οΈ Downloading model...")
|
| 34 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
|
|
|
| 36 |
model = tf.keras.models.load_model(model_path, compile=False)
|
| 37 |
print("β
Model loaded!")
|
| 38 |
|
| 39 |
+
# ββ Example images (baked into the repo under examples/) ββββββββββββββββββββ
|
| 40 |
EXAMPLE_DIR = "examples"
|
| 41 |
+
EXAMPLE_FILES = [
|
| 42 |
+
"calling.jpg", "clapping.jpg", "cycling.jpg", "dancing.jpg",
|
| 43 |
+
"drinking.jpg", "eating.jpg", "fighting.jpg", "hugging.jpg",
|
| 44 |
+
"laughing.jpg", "listening_to_music.jpg", "running.jpg",
|
| 45 |
+
"sitting.jpg", "sleeping.jpg", "texting.jpg", "using_laptop.jpg",
|
| 46 |
+
]
|
| 47 |
+
example_paths = [
|
| 48 |
+
os.path.join(EXAMPLE_DIR, f)
|
| 49 |
+
for f in EXAMPLE_FILES
|
| 50 |
+
if os.path.exists(os.path.join(EXAMPLE_DIR, f))
|
| 51 |
+
]
|
| 52 |
+
print(f"πΈ Found {len(example_paths)} example images.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# ββ Inference ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
def predict(pil_img: Image.Image) -> dict:
|
|
|
|
| 132 |
label="π Prediction Results",
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# Examples β all 15 activity classes, baked into the repo
|
| 136 |
if example_paths:
|
| 137 |
gr.Examples(
|
| 138 |
+
examples=example_paths,
|
| 139 |
inputs=image_input,
|
| 140 |
outputs=label_output,
|
| 141 |
fn=predict,
|