-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
84 lines (64 loc) · 2.83 KB
/
gui.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from sklearn import metrics
from tensorflow.keras.models import model_from_json
from PIL import Image, ImageTk
import numpy as np
import cv2
# donwload haarcascade_frontalface_default from here "https://github.com/opencv/opencv/tree/master/data/haarcascades"
def FacialExpressionModel(json_file, weights_file):
with open(json_file,"r") as file:
loaded_model_json = file.read()
model = model_from_json(loaded_model_json)
model.load_weights(weights_file)
model.compile(optimizer ='adam', loss='categorical_crossentropy', metrics = ['accuracy'])
return model
top =tk.Tk()
top.geometry('800x600')
top.title('Emotion Detector')
top.configure(background='#CDCDCD')
label1 = Label(top, background='#CDCDCD', font=('arial',15,'bold'))
sign_image = Label(top)
facec = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
model = FacialExpressionModel("model_a1.json","model_weights1.h5")
EMOTIONS_LIST = ["Angry","Disgust","Fear","Happy","Neutral","Sad","Surprise"]
def Detect(file_path):
global Label_packed
image = cv2.imread(file_path)
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
faces = facec.detectMultiScale(gray_image,1.3,5)
try:
for (x,y,w,h) in faces:
fc = gray_image[y:y+h,x:x+w]
roi = cv2.resize(fc,(48,48))
pred = EMOTIONS_LIST[np.argmax(model.predict(roi[np.newaxis,:,:,np.newaxis]))]
print("Predicted Emotion is" + pred)
label1.configure(foreground="#011638",text = pred)
except:
label1.configure(foreground="#011638",text = "Unable to detect")
def show_Detect_button(file_path):
detect_b = Button(top,text="Detect Emotion", command= lambda: Detect(file_path),padx=10,pady=5)
detect_b.configure(background="#364156",foreground='white',font=('arial',10,'bold'))
detect_b.place(relx =0.79,rely=0.46)
def upload_image():
try:
file_path = filedialog.askopenfilename()
uploaded = Image.open(file_path)
uploaded.thumbnail(((top.winfo_width()/2.25), (top.winfo_height()/2.25)))
im = ImageTk.PhotoImage(uploaded)
sign_image.configure(image=im)
sign_image.image = im
label1.configure(text='')
show_Detect_button(file_path)
except:
pass
upload = Button(top, text="Upload Image", command=upload_image, padx=10, pady=5)
upload.configure(background="#364156",foreground='white',font=('arial',20,'bold'))
upload.pack(side='bottom',pady=50)
sign_image.pack(side='bottom', expand='True')
label1.pack(side='bottom', expand='True')
heading = Label(top,text='Emotion Detector',pady=20,font=('arial',25,'bold'))
heading.configure(background='#CDCDCD',foreground="#364156")
heading.pack()
top.mainloop()