Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,9 @@
|
|
1 |
-
from flask import Flask,render_template
|
2 |
-
from flask_socketio import SocketIO,emit
|
3 |
-
import base64
|
4 |
-
|
5 |
from keras.models import load_model
|
6 |
from PIL import Image
|
7 |
import numpy as np
|
8 |
import cv2
|
9 |
|
10 |
|
11 |
-
app = Flask(__name__)
|
12 |
-
app.config['SECRET_KEY'] = 'secret!'
|
13 |
-
socket = SocketIO(app,async_mode="eventlet")
|
14 |
-
|
15 |
|
16 |
#the following are to do with this interactive notebook code
|
17 |
from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
|
@@ -28,45 +20,54 @@ age_ranges = ['1-2', '3-9', '10-20', '21-27', '28-45', '46-65', '66-116']
|
|
28 |
gender_ranges = ['male', 'female']
|
29 |
emotion_ranges= ['positive','negative','neutral']
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
image = base64_to_image(image)
|
51 |
-
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
|
52 |
-
# emit("processed_image", image)
|
53 |
-
# Make the image a numpy array and reshape it to the models input shape.
|
54 |
-
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)
|
55 |
-
image = (image / 127.5) - 1
|
56 |
-
# Predicts the model
|
57 |
-
# prediction1 = age_model.predict(image)
|
58 |
-
prediction2 = gender_model.predict(image)
|
59 |
-
# prediction3 = emotion_model.predict(image)
|
60 |
|
61 |
-
index = np.argmax(prediction2)
|
62 |
-
gender_ranges = gender_ranges[index]
|
63 |
-
age = prediction1[0][index]
|
64 |
-
emit("result",{"gender":str(gender_ranges)})
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from keras.models import load_model
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
|
6 |
|
|
|
|
|
|
|
|
|
7 |
|
8 |
#the following are to do with this interactive notebook code
|
9 |
from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
|
|
|
20 |
gender_ranges = ['male', 'female']
|
21 |
emotion_ranges= ['positive','negative','neutral']
|
22 |
|
23 |
+
import streamlit as st
|
24 |
+
st.write("""
|
25 |
+
# Customer Age , Gender and Emotion Prediction
|
26 |
+
"""
|
27 |
+
)
|
28 |
+
st.write("This is a simple web app to predict age , gender and emotion of customer.")
|
29 |
+
file = st.file_uploader("Please upload an image file", type=["jpg", "png","jpeg"])
|
30 |
+
######
|
31 |
+
if file is None:
|
32 |
+
st.text("Please upload an image file")
|
33 |
+
else:
|
34 |
+
test_image = Image.open(file)
|
35 |
+
st.image(test_image, use_column_width=True)
|
36 |
+
st.write(type(test_image))
|
37 |
+
test_image = np.asarray(test_image)
|
38 |
+
gray = cv2.cvtColor(test_image,cv2.COLOR_BGR2GRAY)
|
39 |
+
face_cascade = cv2.CascadeClassifier('Copy of haarcascade_frontalface_default.xml')
|
40 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
41 |
+
|
42 |
+
i = 0
|
43 |
+
|
44 |
+
for (x,y,w,h) in faces:
|
45 |
+
i = i+1
|
46 |
+
cv2.rectangle(test_image,(x,y),(x+w,y+h),(203,12,255),2)
|
47 |
+
|
48 |
+
img_gray=gray[y:y+h,x:x+w]
|
49 |
|
50 |
+
emotion_img = cv2.resize(img_gray, (48, 48), interpolation = cv2.INTER_AREA)
|
51 |
+
emotion_image_array = np.array(emotion_img)
|
52 |
+
emotion_input = np.expand_dims(emotion_image_array, axis=0)
|
53 |
+
output_emotion= emotion_ranges[np.argmax(emotion_model.predict(emotion_input))]
|
54 |
+
|
55 |
+
gender_img = cv2.resize(img_gray, (100, 100), interpolation = cv2.INTER_AREA)
|
56 |
+
gender_image_array = np.array(gender_img)
|
57 |
+
gender_input = np.expand_dims(gender_image_array, axis=0)
|
58 |
+
output_gender=gender_ranges[np.argmax(gender_model.predict(gender_input))]
|
59 |
|
60 |
+
age_image=cv2.resize(img_gray, (200, 200), interpolation = cv2.INTER_AREA)
|
61 |
+
age_input = age_image.reshape(-1, 200, 200, 1)
|
62 |
+
output_age = age_ranges[np.argmax(age_model.predict(age_input))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
output_str = str(i) + ": "+ output_gender + ', '+ output_age + ', '+ output_emotion
|
66 |
+
st.write(output_str)
|
67 |
+
|
68 |
+
col = (0,255,0)
|
69 |
|
70 |
+
cv2.putText(test_image, str(i),(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,col,2)
|
71 |
+
st.image(cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB))
|
72 |
+
|
73 |
+
#st.image(test_image, use_column_width=True)
|