File size: 3,916 Bytes
d0606a3
 
 
 
 
 
ab243e4
d0606a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b58afd
d0606a3
 
 
 
 
 
 
 
 
 
 
39b8304
3a14fbb
39b8304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebe79b1
d0606a3
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import streamlit as st
from PIL import Image
from streamlit_image_select import image_select
import base64
import cv2
import tensorflow as tf

# set title of page
st.markdown("<h1 '><font color='red' ><center>Emotion Detector App</center></h1>",unsafe_allow_html=True)


# ctreating gride of image to select
img = image_select(
    label="_Select a image to predict_",
    images=[
        "images/1.jpg",
        "images/2.jpeg",
        "images/3.jpeg",
        "images/4.jpg"

    ],

)

# image select for background
titleimg = "giphy (1).gif"

#impliment background formating
def set_bg_hack(main_bg):
    # set bg name
    main_bg_ext = "gif"
    st.markdown(
        f"""
         <style>
         .stApp {{
             background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
             background-repeat: no-repeat;
             background-position: right 50% bottom 95% ;
             background-size: cover;
             background-attachment: scroll;
         }}
         </style>
         """,
        unsafe_allow_html=True,
    )

set_bg_hack(titleimg)

uploaded_img = st.file_uploader("Upload an image file",
                                type  = ["png", "jpg", "jpeg"])


# load model
@st.cache_resource
def cache_model(model_add):
    model = tf.keras.models.load_model(model_add)
    return model


model = cache_model("emotion_detector")

# creating predict button
predict = st.button("Predict")

# defining harcascade classifier and class_names
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
class_names = ["Angry", "Happy", "Sad"]


def model_pred(model, image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    results = face_detector.detectMultiScale(gray, scaleFactor=1.05,
                                             minNeighbors=10,
                                             minSize=(100, 100))
    if len(results) != 0:
        for x, y, w, h in results:
            img_crp = image[y:y + h, x:x + w]
            img_crp = cv2.resize(img_crp, (350, 350))
            y_pred_prob = model.predict(tf.expand_dims(img_crp,
                                                       axis=0))
            y_pred = np.argmax(y_pred_prob, axis=-1)
            # print(y_pred_prob)
            label = class_names[int(y_pred)]
            cv2.rectangle(image, (x, y), (x + w, y + h),
                          color=(0, 255, 0),
                          thickness=10)
            cv2.putText(image, f"{label},{round(round(np.max(y_pred_prob), 2)*100,2)}%",
                        (x, y + h), cv2.FONT_HERSHEY_COMPLEX, 2,
                        (0, 255, 255), 2)
    return image


if predict:
    if uploaded_img:
        # img_array = np.array(uploaded_img)
        img_array = np.array(Image.open(uploaded_img))
        result_img = model_pred(model, img_array)
        st.image(result_img)

        # image select for background
        titleimg = "CLOUD.png"
        
        #impliment background formating
        def set_bg_hack(main_bg):
            # set bg name
            main_bg_ext = "png"
            st.markdown(
                f"""
                 <style>
                 .stApp {{
                     background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
                     background-repeat: no-repeat;
                     background-position: right 50% bottom 95% ;
                     background-size: cover;
                     background-attachment: scroll;
                 }}
                 </style>
                 """,
                unsafe_allow_html=True,
            )
        
        set_bg_hack(titleimg)

    else:
        st.write("Please upload a valid image")

else:
    image_array = np.array(Image.open(img))
    result_img = model_pred(model, image_array)
    st.image(result_img)