Commit 5a81d975 authored by Rathnayaka R.M.D.S's avatar Rathnayaka R.M.D.S

Trained Model Prod

parent 81f84197
import json
from flask_cors import CORS
from werkzeug.utils import secure_filename
from flask import Flask, Response, request
from inference import *
app = Flask(__name__)
CORS(app)
# Emotion Detection
@app.route("/emotion", methods=["POST"])
def Faces():
try:
FaceImagefile = request.files['FaceImage'].read()
FaceImage = np.frombuffer(FaceImagefile, np.uint8)
FaceImage = cv.imdecode(FaceImage, cv.IMREAD_COLOR)
if FaceImage.any():
emotion, proba = Inference(FaceImage)
Face_json = {
"emotion": f"{emotion}",
"probability": f"{proba}"
}
return Response(
response=json.dumps(Face_json),
status=200,
mimetype="application/json"
)
except Exception as e:
return Response(
response=json.dumps({
"status": "Unscuccessful",
"error": str(e)
}),
status=500,
mimetype="application/json"
)
if __name__ == '__main__':
app.run(
debug=True,
host=host,
port=port
)
\ No newline at end of file
import random
import base64
import re, json, os
import nltk, pickle
import requests, os, gtts
from datetime import datetime
from google.cloud import speech
import cv2 as cv
import numpy as np
import pandas as pd
import tensorflow as tf
from arcface import ArcFace
from mtcnn.mtcnn import MTCNN
from PIL import Image, ImageOps
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics.pairwise import cosine_similarity
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('punkt')
nltk.download('omw-1.4')
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import RegexpTokenizer
#################################### PATHS / VARIABLES #######################################
model_converter = 'weights/emotion-detection.tflite'
width = 71
height = 71
target_size = (width, height)
input_shape = (width, height, 3)
class_dict = {'happy': 1, 'sad': 0}
class_dict_reverse = {v:k for k,v in class_dict.items()}
host = '0.0.0.0'
port = 5000
#################################### MODEL / FILES ###################################
emotion_interpreter = tf.lite.Interpreter(model_path=model_converter) # Load tflite model
emotion_interpreter.allocate_tensors()
# feature_extractor = tf.keras.models.load_model(facenet_weights)
face_rec = ArcFace.ArcFace()
def preprocessing_function(img):
img = tf.keras.applications.xception.preprocess_input(img)
return img
def extract_face(
filename,
detector = MTCNN(),
required_size=(160, 160)
):
if isinstance(filename, str):
image = Image.open(filename)
image = image.convert('RGB')
else:
image = Image.fromarray(filename)
image = ImageOps.exif_transpose(image)
pixels = np.asarray(image)
try:
results = detector.detect_faces(pixels)
x1, y1, width, height = results[0]['box']
x1, y1 = abs(x1), abs(y1)
x2, y2 = x1 + width, y1 + height
face = pixels[y1:y2, x1:x2]
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = np.asarray(image)
return face_array
except:
pixels = cv.resize(pixels, required_size)
return pixels
def Inference(image):
image = extract_face(
image,
required_size=target_size
)
image = preprocessing_function(image)
image = np.expand_dims(image, axis=0)
input_shape = emotion_interpreter.get_input_details()[0]['shape']
assert np.array_equal(input_shape, image.shape), "Expected Tensor Shape : {} but recieved {}".format(input_shape, image.shape)
emotion_interpreter.set_tensor(emotion_interpreter.get_input_details()[0]['index'], image)
emotion_interpreter.invoke() # set the inference
pred = emotion_interpreter.get_tensor(emotion_interpreter.get_output_details()[0]['index']) # Get predictions
pred = pred.squeeze()
Label = np.argmax(pred) # Get the label
emotion = class_dict_reverse[Label]
proba = pred[Label]
return emotion, proba
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment