|
from flask import Flask,render_template |
|
from flask_socketio import SocketIO,emit |
|
import base64 |
|
|
|
from keras.models import load_model |
|
from PIL import Image |
|
import numpy as np |
|
import cv2 |
|
|
|
|
|
app = Flask(__name__) |
|
app.config['SECRET_KEY'] = 'secret!' |
|
socket = SocketIO(app,async_mode="eventlet") |
|
|
|
|
|
|
|
from matplotlib import pyplot as plt |
|
import pylab |
|
pylab.rcParams['figure.figsize'] = (10.0, 8.0) |
|
|
|
|
|
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') |
|
|
|
|
|
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'] |
|
|
|
def base64_to_image(base64_string): |
|
|
|
base64_data = base64_string.split(",")[1] |
|
|
|
image_bytes = base64.b64decode(base64_data) |
|
|
|
image_array = np.frombuffer(image_bytes, dtype=np.uint8) |
|
|
|
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) |
|
return image |
|
|
|
@socket.on("connect") |
|
def test_connect(): |
|
print("Connected") |
|
emit("my response", {"data": "Connected"}) |
|
|
|
@socket.on("image") |
|
def receive_image(image): |
|
|
|
image = base64_to_image(image) |
|
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) |
|
|
|
|
|
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3) |
|
image = (image / 127.5) - 1 |
|
|
|
|
|
prediction2 = gender_model.predict(image) |
|
|
|
|
|
index = np.argmax(prediction2) |
|
gender_ranges = gender_ranges[index] |
|
age = prediction1[0][index] |
|
emit("result",{"gender":str(gender_ranges)}) |
|
|
|
@app.route("/") |
|
def home(): |
|
return render_template("index.html") |
|
|
|
if __name__ == '__main__': |
|
|
|
socket.run(app,host="0.0.0.0", port=7860) |