Rishi2455 commited on
Commit
7360df7
Β·
verified Β·
1 Parent(s): db174ae

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -32
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
- # ── Prepare example images from dataset (ALL 15 classes) ────────────────────
47
  EXAMPLE_DIR = "examples"
48
- os.makedirs(EXAMPLE_DIR, exist_ok=True)
49
-
50
- example_paths = []
51
- try:
52
- print("πŸ“¦ Loading example images from dataset...")
53
- ds = load_dataset("Bingsu/Human_Action_Recognition", split="test")
54
- # Grab one example per class β€” all 15 activities
55
- all_classes = set(range(15))
56
- seen = set()
57
- for row in ds:
58
- label = row["labels"]
59
- if label in all_classes and label not in seen:
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=sorted(example_paths),
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,