-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFace_recognition.py
156 lines (120 loc) · 6 KB
/
Face_recognition.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from cProfile import label
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
from tkinter import messagebox, filedialog
import mysql.connector
import cv2
from datetime import datetime
from time import strftime
import os
# import numpy as np
class Face_Recognition:
def __init__(self, root):
self.root = root
self.root.geometry("1590x790+0+0")
self.root.title("Face Recognition System")
# ==============================Labe; from traindata========================================
title_lbl = Label(self.root, text="FACE RECOGNITION", font=(
"times new roman", 35, "bold"), bg="white", fg="black")
title_lbl.place(x=0, y=0, width=1590, height=45)
img_top = Image.open(r"img\Fc.jpg")
img_top = img_top.resize((650, 800), Image.ANTIALIAS)
self.photoimg_top = ImageTk.PhotoImage(img_top)
f_lbl = Label(self.root, image=self.photoimg_top)
f_lbl.place(x=0, y=55, width=650, height=800)
img_bottom = Image.open(r"face.jpg")
img_bottom = img_bottom.resize((950, 800), Image.ANTIALIAS)
self.photoimg_bottom = ImageTk.PhotoImage(img_bottom)
f_lbl = Label(self.root, image=self.photoimg_bottom)
f_lbl.place(x=650, y=55, width=950, height=800)
# ==========button===========================
b1_1 = Button(f_lbl, text="Face Recognition", cursor="hand2", font=(
"times new roman", 18, "bold"), bg="blue", fg="white", command=self.face_recog)
b1_1.place(x=365, y=620, width=200, height=40)
def face_recog(self):
def draw_boundry(img, classifier, scalefactor, minNeighbours, color, text, clf):
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
feature = classifier.detectMultiScale(
grey_img, scalefactor, minNeighbours)
coords = []
for (x, y, w, h) in feature:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
id, predict = clf.predict(grey_img[y:y+h, x:x+w])
confidence = int((100*(1-predict/300)))
conn = mysql.connector.connect(
host='localhost', user='root', password='rootpassword', port="3310", database='new')
my_cursor = conn.cursor()
my_cursor.execute(
"select student_name from attandance_management_system where student_id="+str(id))
n = my_cursor.fetchone()
n = "+".join(n)
my_cursor.execute(
"select Department from attandance_management_system where student_id="+str(id))
d = my_cursor.fetchone()
d = "+".join(d)
my_cursor.execute(
"select Roll_No from attandance_management_system where student_id="+str(id))
r = my_cursor.fetchone()
r = "+".join(r)
my_cursor.execute(
"select student_id from attandance_management_system where student_id="+str(id))
i = my_cursor.fetchone()
i = "+".join(i)
if confidence > 77:
cv2.putText(
img, f"Id:{i}", (x, y-85), cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 0), 3)
cv2.putText(
img, f"Roll:{r}", (x, y-55), cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 0), 3)
cv2.putText(
img, f"Name:{n}", (x, y-30), cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 0), 3)
cv2.putText(
img, f"Department:{d}", (x, y-5), cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 0), 3)
self.mark_attendance(i,r,n,d)
else:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 3)
cv2.putText(img, "Unknown Face", (x, y-5),
cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 0, 255), 3)
coords = [x, y, w, y]
return coords
def recognizer(img, clf, faceCascade):
coords = draw_boundry(img, faceCascade, 1.1,
10, (255, 255, 255), "Face", clf)
return img
faceCascade = cv2.CascadeClassifier("face_recog.xml")
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("classifier.xml")
video_cap = cv2.VideoCapture(0)
while True:
ret, img = video_cap.read()
img1 = recognizer(img, clf, faceCascade)
cv2.imshow("Welcome to face recognition", img1)
if cv2.waitKey(1) == 13:
break
video_cap.release()
cv2.destroyAllWindows()
# def mark_attendance(self,i,r,n,d):
# already_in_file = set()
# with open('atten.csv','r+',newline="\n") as f:
# for line in f:
# already_in_file.add(line.split(",")[0])
# if((i not in already_in_file) and (r not in already_in_file) and (n not in already_in_file) and (d not in already_in_file)):
# with open('atten.csv','r+',newline="\n") as f:
# now=datetime.now()
# d1=now.strftime("%d/%m/%Y")
# dtString=now.strftime("%H:%M:%S")
# f.writelines(f"\n{i},{r},{n},{d},{dtString},{d1},present")
def mark_attendance(self,i,r,n,d):
already_in_file = set()
with open('atten.csv','r+',newline="\n") as f:
for line in f:
already_in_file.add(line.split(",")[0])
if((i not in already_in_file) and (r not in already_in_file) and (n not in already_in_file) and (d not in already_in_file)):
now=datetime.now()
d1=now.strftime("%d/%m/%Y")
dtString=now.strftime("%H:%M:%S")
f.writelines(f"\n{i},{r},{n},{d},{dtString},{d1},present")
if __name__ == "__main__":
root = Tk()
obj = Face_Recognition(root)
root.mainloop()