|
|
|
""" |
|
Created on Sun Dec 25 08:38:00 2022 |
|
|
|
@author: ROSHAN |
|
""" |
|
|
|
import tensorflow as tf |
|
import gradio as gr |
|
import numpy as np |
|
import cv2 |
|
from PIL import Image as im |
|
from matplotlib import pyplot as plt |
|
cls=['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'] |
|
model = tf.keras.models.load_model("56fer.h5") |
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') |
|
def show(img): |
|
img=img[:, :, ::-1].copy() |
|
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
|
faces = face_cascade.detectMultiScale(gray, 1.5, 1) |
|
r=[] |
|
x=faces[0][0] |
|
y=faces[0][1] |
|
w=faces[0][2] |
|
h=faces[0][3] |
|
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2) |
|
r.append(img) |
|
sharp_kernel = np.array([[0, -1, 0], |
|
[-1, 5, -1], |
|
[0, -1, 0]]) |
|
sharp_img = cv2.filter2D(src=gray, ddepth=-1, kernel=sharp_kernel) |
|
|
|
crop_img = sharp_img[y:y+h, x:x+w] |
|
|
|
npa=np.array(crop_img)/255.0 |
|
predictions = model.predict(np.resize(npa,(48,48)).reshape(-1,48,48,1)) |
|
score =predictions[0] |
|
score=tf.nn.softmax(predictions[0]) |
|
plt.figure() |
|
confidences = {cls[i]: float(score[i]) for i in range(len(cls))} |
|
return confidences |
|
demo = gr.Interface( |
|
fn=show, |
|
inputs="image", |
|
outputs=gr.outputs.Label(num_top_classes=7), |
|
) |
|
demo.launch() |