File size: 1,159 Bytes
0b19a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
!pip install gradio -q
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from PIL import Image

model = load_model('final_emotion_model.keras')
emotion_english = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']

def classify_emotion(img):
    img_gray = img.convert('L').resize((48, 48))
    img_array = image.img_to_array(img_gray)
    img_array = np.expand_dims(img_array, axis=0) / 255.0
    predictions = model.predict(img_array)[0]
    results = {emotion_english[i]: float(predictions[i]) for i in range(len(emotion_english))}
    return results, img_gray

with gr.Blocks() as demo:
    gr.Markdown("## تصنيف المشاعر من الصور")
    with gr.Row():
        input_image = gr.Image(type="pil", label="تحميل صورة")
        processed = gr.Image(label="معالجة (رمادي 48x48)", interactive=False)
    with gr.Row():
        classify_btn = gr.Button("تصنيف")
    output_label = gr.Label()
    classify_btn.click(fn=classify_emotion, inputs=input_image, outputs=[output_label, processed])
    demo.launch(share=True)