|
|
from flask import Flask, render_template, request, jsonify
|
|
|
import tensorflow as tf
|
|
|
from PIL import Image
|
|
|
import numpy as np
|
|
|
import base64
|
|
|
import io
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model('unique_face_expression_model_.h5')
|
|
|
class_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
|
|
|
|
|
|
|
|
|
def preprocess_image(image):
|
|
|
image = image.resize((48, 48))
|
|
|
image = image.convert('L')
|
|
|
image = np.array(image) / 255.0
|
|
|
image = np.expand_dims(image, axis=-1)
|
|
|
image = np.expand_dims(image, axis=0)
|
|
|
return image
|
|
|
|
|
|
@app.route('/')
|
|
|
def index():
|
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route('/predict', methods=['POST'])
|
|
|
def predict():
|
|
|
if request.is_json and 'image' in request.json:
|
|
|
|
|
|
image_data = request.json['image'].split(",")[1]
|
|
|
image = Image.open(io.BytesIO(base64.b64decode(image_data)))
|
|
|
elif 'image' in request.files:
|
|
|
|
|
|
image_file = request.files['image']
|
|
|
image = Image.open(image_file)
|
|
|
else:
|
|
|
return jsonify({'error': 'No image provided'}), 400
|
|
|
|
|
|
|
|
|
processed_image = preprocess_image(image)
|
|
|
|
|
|
|
|
|
prediction = model.predict(processed_image)
|
|
|
predicted_class = np.argmax(prediction)
|
|
|
predicted_label = class_labels[predicted_class]
|
|
|
|
|
|
|
|
|
result = {'prediction': predicted_label}
|
|
|
return jsonify(result)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
app.run(debug=True)
|
|
|
|