File size: 2,903 Bytes
63e9f40
 
c167c24
 
 
 
63e9f40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d433f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63e9f40
3d433f3
 
 
 
 
 
 
 
 
63e9f40
3d433f3
 
 
63e9f40
 
3d433f3
 
 
 
63e9f40
3d433f3
 
 
 
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
from keras.models import load_model
from PIL import Image
import numpy as np
import cv2 



#the following are to do with this interactive notebook code
from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
import pylab # this allows you to control figure size 
pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook

###loading model###
age_model = load_model('Copy of age_model_pretrained.h5')
gender_model = load_model('Copy of gender_model_pretrained.h5')
emotion_model = load_model('emotion_model_pretrained.h5')

# Labels on Age, Gender and Emotion to be predicted
age_ranges = ['1-2', '3-9', '10-20', '21-27', '28-45', '46-65', '66-116']
gender_ranges = ['male', 'female']
emotion_ranges= ['positive','negative','neutral']

import streamlit as st
st.write("""
         # Customer  Age , Gender  and  Emotion Prediction
         """
         )
st.write("This is a simple  web app to predict age , gender and emotion of customer.")
file = st.file_uploader("Please upload an image file", type=["jpg", "png","jpeg"])
######
if file is None:
    st.text("Please upload an image file")
else:
    test_image = Image.open(file)
    st.image(test_image, use_column_width=True)
    st.write(type(test_image))
    test_image = np.asarray(test_image)
    gray = cv2.cvtColor(test_image,cv2.COLOR_BGR2GRAY)
    face_cascade = cv2.CascadeClassifier('Copy of haarcascade_frontalface_default.xml')
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    i = 0

    for (x,y,w,h) in faces:
            i = i+1
            cv2.rectangle(test_image,(x,y),(x+w,y+h),(203,12,255),2)

            img_gray=gray[y:y+h,x:x+w]

            emotion_img = cv2.resize(img_gray, (48, 48), interpolation = cv2.INTER_AREA)
            emotion_image_array = np.array(emotion_img)
            emotion_input = np.expand_dims(emotion_image_array, axis=0)
            output_emotion= emotion_ranges[np.argmax(emotion_model.predict(emotion_input))]
            
            gender_img = cv2.resize(img_gray, (100, 100), interpolation = cv2.INTER_AREA)
            gender_image_array = np.array(gender_img)
            gender_input = np.expand_dims(gender_image_array, axis=0)
            output_gender=gender_ranges[np.argmax(gender_model.predict(gender_input))]

            age_image=cv2.resize(img_gray, (200, 200), interpolation = cv2.INTER_AREA)
            age_input = age_image.reshape(-1, 200, 200, 1)
            output_age = age_ranges[np.argmax(age_model.predict(age_input))]


            output_str = str(i) + ": "+  output_gender + ', '+ output_age + ', '+ output_emotion
            st.write(output_str)
            
            col = (0,255,0)

            cv2.putText(test_image, str(i),(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,col,2)
    st.image(cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB))
  
     #st.image(test_image, use_column_width=True)