Pengi5659 commited on
Commit
36f8ca4
·
verified ·
1 Parent(s): d613b68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -1,27 +1,30 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
- from PIL import Image
5
  import cv2
6
 
7
- # Load model
8
  model = tf.keras.models.load_model("keras_model.h5")
9
 
10
- # Replace this with your actual label list if you have it
11
  labels = ["Good Posture", "Bad Posture"]
12
 
13
  def predict_from_webcam(frame):
14
- # Resize frame to match model input size (usually 224x224 for Teachable Machine)
 
 
 
 
15
  img = cv2.resize(frame, (224, 224))
16
- img = img / 255.0 # normalize
17
- img = np.expand_dims(img, axis=0) # batch dimension
18
 
19
- # Predict
20
  prediction = model.predict(img)[0]
21
- top_label = labels[np.argmax(prediction)]
22
  confidence = np.max(prediction)
23
 
24
- return f"{top_label} ({confidence*100:.2f}%)"
25
 
26
  # Gradio interface
27
  demo = gr.Interface(
@@ -29,8 +32,8 @@ demo = gr.Interface(
29
  inputs=gr.Image(source="webcam", streaming=True),
30
  outputs=gr.Text(),
31
  live=True,
32
- title="Posture Detector",
33
- description="Detects if your posture is good or bad using a webcam model trained with Teachable Machine."
34
  )
35
 
36
  demo.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
 
4
  import cv2
5
 
6
+ # Load Teachable Machine model
7
  model = tf.keras.models.load_model("keras_model.h5")
8
 
9
+ # Labels from your model change if needed
10
  labels = ["Good Posture", "Bad Posture"]
11
 
12
  def predict_from_webcam(frame):
13
+ # Convert to RGB if needed (Gradio usually provides RGB)
14
+ if frame.shape[-1] == 4:
15
+ frame = frame[:, :, :3] # remove alpha channel
16
+
17
+ # Resize to 224x224 (Teachable Machine default)
18
  img = cv2.resize(frame, (224, 224))
19
+ img = img / 255.0 # normalize to [0, 1]
20
+ img = np.expand_dims(img, axis=0) # add batch dimension
21
 
22
+ # Prediction
23
  prediction = model.predict(img)[0]
24
+ label = labels[np.argmax(prediction)]
25
  confidence = np.max(prediction)
26
 
27
+ return f"{label} ({confidence * 100:.2f}%)"
28
 
29
  # Gradio interface
30
  demo = gr.Interface(
 
32
  inputs=gr.Image(source="webcam", streaming=True),
33
  outputs=gr.Text(),
34
  live=True,
35
+ title="🪑 Posture Detector",
36
+ description="Detects good or bad posture from webcam using a Teachable Machine model."
37
  )
38
 
39
  demo.launch()