Spaces:
Runtime error

File size: 1,483 Bytes
995387b
f569fb0
 
 
 
 
 
 
 
 
995387b
 
 
 
 
3a497ac
995387b
 
 
28d5a14
995387b
f569fb0
 
 
 
995387b
 
 
28d5a14
995387b
f569fb0
995387b
f569fb0
 
83455e1
f569fb0
 
 
995387b
f569fb0
 
 
 
 
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
41
42
43
44
45
import cv2
import numpy as np
import gradio as gr
import tensorflow as tf

IMG_SIZE = 48
CLASSES = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise']

model = tf.keras.models.load_model('CNN4G7.h5')

def extract_face(image):
    faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    faces = faceCascade.detectMultiScale(image, minNeighbors=3, minSize=(30, 30))

    if len(faces) == 0:
        return cv2.resize(image, (IMG_SIZE, IMG_SIZE), interpolation = cv2.INTER_CUBIC)

    x, y, w, h = faces[0]
    face = image[y:y+h,x:x+w]
    return cv2.resize(face, (IMG_SIZE, IMG_SIZE), interpolation = cv2.INTER_CUBIC)

def preprocess_image(image):
    return image.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

def predict_face_expression(image):
    if image is None:
        return "Please select an image..."

    face = extract_face(image)
    image = preprocess_image(face)
    prediction = model.predict(image)
    return { CLASSES[i]: float(prediction[0][i]) for i in range(len(CLASSES)) }

with gr.Blocks() as app:
    gr.Markdown('## Sistema de identificación de emociones basado en procesamiento de imágenes usando técnicas de Machine Learning')

    with gr.Tab('Image'):
        with gr.Row():
            image_input = gr.Image(image_mode='L')
        image_button = gr.Button('Submit')

    image_button.click(predict_face_expression, inputs=image_input, outputs=gr.outputs.Label(num_top_classes=3))

app.launch()