File size: 1,355 Bytes
cd7476a
 
 
 
 
 
 
 
 
 
 
 
 
 
7afe3ea
 
cd7476a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
"""
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()