File size: 2,227 Bytes
982b802
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import os
import cv2
import PIL
from PIL import Image
from mtcnn import MTCNN
import numpy as np
from tensorflow.keras.models import load_model
from keras.preprocessing.image import img_to_array

emotions = ['neutral','happiness','surprise','sadness','anger','disgust','fear','contempt','unknown']
classifier = load_model("model_9.keras")
face_detector_mtcnn = MTCNN()


def predict_emotion(image):

    faces = face_detector_mtcnn.detect_faces(image)

    for face in faces:
        x,y,w,h = face['box']

        roi = image[y:y+h,x:x+w]
        
        # Converting the region of interest to grayscale, and resize
        roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
        roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)

        img = roi_gray.astype('float')/255.0
        img = img_to_array(img)
        img = np.expand_dims(img,axis=0)

        prediction = classifier.predict(img)[0]
        #top_indices = np.argsort(prediction)[-2:]
        #top_emotion = top_indices[1]
        #second_emotion = top_indices[0]
        #label = emotions[top_emotion]
        confidences = {emotions[i]: float(prediction[i]) for i in range(len(emotions))}

    return confidences



demo = gr.Interface(
    fn = predict_emotion,
    inputs = gr.Image(type="numpy"),
    outputs = gr.Label(num_top_classes=9),
    #flagging_options=["blurry", "incorrect", "other"],
    examples = [
        os.path.join(os.path.dirname(__file__), "images/Image_1.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_2.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_3.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_4.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_5.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_6.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_7.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_8.jpg"),
        os.path.join(os.path.dirname(__file__), "images/Image_9.jpg"),
        #os.path.join(os.path.dirname(__file__), "images/Image_10.jpg"),
    ],
    title = "Whatchu feeling?"
)
    


if __name__ == "__main__":
    demo.launch()