Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,72 @@
|
|
| 1 |
from flask import Flask,render_template
|
| 2 |
from flask_socketio import SocketIO,emit
|
| 3 |
import base64
|
|
|
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
-
import numpy as np
|
| 7 |
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
app.config['SECRET_KEY'] = 'secret!'
|
| 11 |
socket = SocketIO(app,async_mode="eventlet")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 18 |
+
import pylab # this allows you to control figure size
|
| 19 |
+
pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
|
| 20 |
+
|
| 21 |
+
###loading model###
|
| 22 |
+
age_model = load_model('Copy of age_model_pretrained.h5')
|
| 23 |
+
gender_model = load_model('Copy of gender_model_pretrained.h5')
|
| 24 |
+
emotion_model = load_model('emotion_model_pretrained.h5')
|
| 25 |
+
|
| 26 |
+
# Labels on Age, Gender and Emotion to be predicted
|
| 27 |
+
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 |
+
def base64_to_image(base64_string):
|
| 32 |
+
# Extract the base64 encoded binary data from the input string
|
| 33 |
+
base64_data = base64_string.split(",")[1]
|
| 34 |
+
# Decode the base64 data to bytes
|
| 35 |
+
image_bytes = base64.b64decode(base64_data)
|
| 36 |
+
# Convert the bytes to numpy array
|
| 37 |
+
image_array = np.frombuffer(image_bytes, dtype=np.uint8)
|
| 38 |
+
# Decode the numpy array as an image using OpenCV
|
| 39 |
+
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
|
| 40 |
+
return image
|
| 41 |
+
|
| 42 |
+
@socket.on("connect")
|
| 43 |
+
def test_connect():
|
| 44 |
+
print("Connected")
|
| 45 |
+
emit("my response", {"data": "Connected"})
|
| 46 |
+
|
| 47 |
+
@socket.on("image")
|
| 48 |
+
def receive_image(image):
|
| 49 |
+
# Decode the base64-encoded image data
|
| 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 |
+
@app.route("/")
|
| 67 |
+
def home():
|
| 68 |
+
return render_template("index.html")
|
| 69 |
+
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
# app.run(debug=True)
|
| 72 |
+
socket.run(app,host="0.0.0.0", port=7860)
|