-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
79 lines (62 loc) · 2.87 KB
/
gui.py
File metadata and controls
79 lines (62 loc) · 2.87 KB
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
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from tensorflow.keras.models import model_from_json
from PIL import Image,ImageTk
import numpy as np
import cv2
#after downloading haarcascade_frontalface_default.xml file and copying and pasting the data a separate file with the same name continue
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', metrices =['accuracy'])
top = tk.Tk()
top.geometry('800x600')
top.title('Face Expression Detection')
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_weights(1).h5")
EXPRESSIONS_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 = EXPRESSIONS_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 Expression" , 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.askopenfile()
uploaded = Image.open(file_path)
uploaded.thumbnail(((top.winfo_width()/2.3),(top.winfo_height()/2.3)))
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 ='Expression Detector',pady =20,font=('arial',25,'bold'))
heading.configure(background ='#CDCDCD', foreground='#364156')
heading.pack()
top.mainloop()