Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
8 |
model = load_model("model.h5")
|
9 |
|
10 |
-
#
|
11 |
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
|
12 |
|
13 |
# Prediction function
|
14 |
def predict_expression(image):
|
15 |
try:
|
16 |
-
|
17 |
-
image = image.
|
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,
|
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
|
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"),
|