awais0300 commited on
Commit
e7db66b
·
verified ·
1 Parent(s): 85d2126

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -8
app.py CHANGED
@@ -4,22 +4,20 @@ import tensorflow as tf
4
  from tensorflow.keras.models import load_model
5
  from PIL import Image
6
 
7
- # Load your model
8
  model = load_model("model.h5")
9
 
10
- # Define class names (change these to match your training labels)
11
  class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
12
 
13
  # Prediction function
14
  def predict_expression(image):
15
  try:
16
- # Convert to grayscale if model expects 1 channel
17
- image = image.convert("L") # grayscale
18
- image = image.resize((48, 48)) # match training size
19
  img_array = np.array(image).astype("float32") / 255.0
20
- img_array = img_array.reshape(1, 48, 48, 1) # (batch, height, width, channels)
21
 
22
- # Predict
23
  prediction = model.predict(img_array)
24
  class_idx = int(np.argmax(prediction))
25
  confidence = float(np.max(prediction))
@@ -29,7 +27,7 @@ def predict_expression(image):
29
  except Exception as e:
30
  return f"⚠️ Error: {str(e)}"
31
 
32
- # Gradio Interface
33
  iface = gr.Interface(
34
  fn=predict_expression,
35
  inputs=gr.Image(type="pil"),
 
4
  from tensorflow.keras.models import load_model
5
  from PIL import Image
6
 
7
+ # Load the model
8
  model = load_model("model.h5")
9
 
10
+ # Your class labels (update as per your model)
11
  class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
12
 
13
  # Prediction function
14
  def predict_expression(image):
15
  try:
16
+ image = image.convert("RGB") # Ensure 3 channels
17
+ image = image.resize((224, 224)) # Resize to model's expected input
 
18
  img_array = np.array(image).astype("float32") / 255.0
19
+ img_array = img_array.reshape(1, 224, 224, 3)
20
 
 
21
  prediction = model.predict(img_array)
22
  class_idx = int(np.argmax(prediction))
23
  confidence = float(np.max(prediction))
 
27
  except Exception as e:
28
  return f"⚠️ Error: {str(e)}"
29
 
30
+ # Gradio interface
31
  iface = gr.Interface(
32
  fn=predict_expression,
33
  inputs=gr.Image(type="pil"),