-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
118 lines (88 loc) · 3.26 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from threading import Thread
import tkinter as tk
from PIL import ImageTk
from PIL import Image
from tkinter import filedialog as fd
import pyautogui
import cv2
import clipboard
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np
import imutils
window = tk.Tk()
window.config(bg="black")
window.title("Color_Detection")
window.geometry("900x500")
width=900
height=500
window_list = []
window.bind('<Escape>', lambda e: window.quit())
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
model = load_model("Output2.hdf5")
def show_camera():
_,frame = cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
image_copy = img.copy()
image = image_copy.resize((width-200, height))
photo = ImageTk.PhotoImage(image)
label = tk.Label(image=photo)
label.image=photo
label.grid(row=0,column=3,rowspan = 8, columnspan = 9)
label.after(5,show_camera)
def Capture(image):
cv2.imsave(str(image), image)
def predict_smile():
_,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frameClone = frame.copy()
rects = detector.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 5,
minSize = (30, 30), flags = cv2.CASCADE_SCALE_IMAGE)
#for (x, y, w, h) in rects:
if(len(rects)!=0):
x,y,w,h = rects[0]
face = gray[y: y + h, x: x + w]
face = cv2.resize(face, (64, 64))
face = face.astype("float") / 255.0
face = img_to_array(face)
face = np.expand_dims(face, axis = 0)
(notSmiling, smiling) = model.predict(face)[0]
if smiling > notSmiling:
label = "Smiling"
side_image = Image.open("smile.jpg")
else :
label = "Not Smiling"
side_image = Image.open("not_smile.jpeg")
#Thread(target=show_result(label)).start()
#show_result(label)
#image_copy = side_image.copy()
side_image = side_image.resize((100, 100))
photo = ImageTk.PhotoImage(side_image)
side_label = tk.Label(image=photo)
side_label.image=photo
side_label.grid(row=0, column=1, columnspan = 2, padx=50, pady=50)
cv2.putText(frameClone, label, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.rectangle(frameClone, (x, y), (x + w, y + h),
(0, 0, 255), 2)
cv2image = cv2.cvtColor(frameClone, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
image_copy = img.copy()
image = image_copy.resize((width-200, height))
photo = ImageTk.PhotoImage(image)
label = tk.Label(image=photo)
label.image=photo
label.grid(row=0,column=3,rowspan = 8, columnspan = 9)
label.after(30,predict_smile)
#capture_btn = tk.Button(window, text="Capture Button", width=25, activebackground="grey", activeforeground="blue",bg="purple", command=lambda:Capture(frameClone))
#capture_btn.grid(row=3,column=1,columnspan=2, padx=10, pady=50)
def main():
predict_smile()
window.mainloop()
if __name__ == "__main__":
main()