RoAr777 commited on
Commit
cd7476a
·
1 Parent(s): 96afc45

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Sun Dec 25 08:38:00 2022
4
+
5
+ @author: ROSHAN
6
+ """
7
+
8
+ import tensorflow as tf
9
+ import gradio as gr
10
+ import numpy as np
11
+ import cv2
12
+ from PIL import Image as im
13
+ from matplotlib import pyplot as plt
14
+ cls=['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
15
+ model = tf.keras.models.load_model("D:/56fer.h5")
16
+ face_cascade = cv2.CascadeClassifier('D:/haarcascade_frontalface_default.xml')
17
+ def show(img):
18
+ img=img[:, :, ::-1].copy()
19
+
20
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
21
+ faces = face_cascade.detectMultiScale(gray, 1.5, 1)
22
+ r=[]
23
+ x=faces[0][0]
24
+ y=faces[0][1]
25
+ w=faces[0][2]
26
+ h=faces[0][3]
27
+ cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
28
+ r.append(img)
29
+ sharp_kernel = np.array([[0, -1, 0],
30
+ [-1, 5, -1],
31
+ [0, -1, 0]])
32
+ sharp_img = cv2.filter2D(src=gray, ddepth=-1, kernel=sharp_kernel)
33
+
34
+ crop_img = sharp_img[y:y+h, x:x+w]
35
+
36
+ npa=np.array(crop_img)/255.0
37
+ predictions = model.predict(np.resize(npa,(48,48)).reshape(-1,48,48,1))
38
+ score =predictions[0]
39
+ score=tf.nn.softmax(predictions[0])
40
+ plt.figure()
41
+ confidences = {cls[i]: float(score[i]) for i in range(len(cls))}
42
+ return confidences
43
+ demo = gr.Interface(
44
+ fn=show,
45
+ inputs="image",
46
+ outputs=gr.outputs.Label(num_top_classes=7),
47
+ )
48
+ demo.launch()