File size: 1,164 Bytes
6f2110a
 
 
 
 
 
e7db66b
6f2110a
 
e7db66b
6f2110a
 
 
 
85d2126
e7db66b
 
85d2126
e7db66b
6f2110a
85d2126
 
 
6f2110a
85d2126
 
 
 
6f2110a
e7db66b
6f2110a
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image

# Load the model
model = load_model("model.h5")

# Your class labels (update as per your model)
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']

# Prediction function
def predict_expression(image):
    try:
        image = image.convert("RGB")  # Ensure 3 channels
        image = image.resize((224, 224))  # Resize to model's expected input
        img_array = np.array(image).astype("float32") / 255.0
        img_array = img_array.reshape(1, 224, 224, 3)

        prediction = model.predict(img_array)
        class_idx = int(np.argmax(prediction))
        confidence = float(np.max(prediction))

        return f"Expression: {class_names[class_idx]} ({confidence:.2%})"
    
    except Exception as e:
        return f"⚠️ Error: {str(e)}"

# Gradio interface
iface = gr.Interface(
    fn=predict_expression,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Facial Expression Classifier",
    description="Upload a face image and get the predicted emotion"
)

iface.launch()