-
Notifications
You must be signed in to change notification settings - Fork 1
/
face_recognition.py
142 lines (109 loc) · 5.42 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
# import re
from sys import path
from tkinter import*
from tkinter import ttk
from PIL import Image,ImageTk
import os
import mysql.connector
import cv2
import numpy as np
from tkinter import messagebox
from time import strftime
from datetime import datetime
class Face_Recognition:
def __init__(self,root):
self.root=root
self.root.state('zoomed')
self.root.title("Hệ thống quản lý điểm danh sử dụng nhận dạng khuôn mặt")
# This part is image labels setting start
# first header image
img=Image.open(r"D:\VKU\DoAnCoSo4\Attendance_Management_System_Using_Face_Recognition\Images_GUI\banner2.png")
img=img.resize((1280,130),Image.ANTIALIAS)
self.photoimg=ImageTk.PhotoImage(img)
# set image as lable
f_lb1 = Label(self.root,image=self.photoimg)
f_lb1.place(x=0,y=0,width=1280,height=130)
# backgorund image
bg1=Image.open(r"D:\VKU\DoAnCoSo4\Attendance_Management_System_Using_Face_Recognition\Images_GUI\bg-reg.png")
bg1=bg1.resize((1200,520),Image.ANTIALIAS)
self.photobg1=ImageTk.PhotoImage(bg1)
# set image as lable
bg_img = Label(self.root,image=self.photobg1)
bg_img.place(x=0,y=180,width=1280,height=500)
#title section
title_lb1 = Label(bg_img,text="VKU - Chức năng nhận diện gương mặt",font=("verdana",20,"bold"),bg="navyblue",fg="white")
title_lb1.place(x=0,y=0,width=1280,height=40)
# Create buttons below the section
# -------------------------------------------------------------------------------------------------------------------
# Training button 1
""" std_img_btn=Image.open(r"D:\VKU\DoAnCoSo4\Attendance_Management_System_Using_Face_Recognition\Images_GUI\f_det.jpg")
std_img_btn=std_img_btn.resize((180,180),Image.ANTIALIAS)
self.std_img1=ImageTk.PhotoImage(std_img_btn)
std_b1 = Button(bg_img,command=self.face_recog,image=self.std_img1,cursor="hand2")
std_b1.place(x=600,y=170,width=180,height=180) """
std_b1_1 = Button(bg_img,command=self.face_recog,text="Nhận Diện",cursor="hand2",font=("tahoma",12,"bold"),bg="white",fg="navyblue")
std_b1_1.place(x=255,y=330,width=180,height=35)
#=====================Attendance===================
def mark_attendance(self,i,r,n):
with open("attendance.csv","r+",newline="\n") as f:
myDatalist=f.readlines()
name_list=[]
for line in myDatalist:
entry=line.split((","))
name_list.append(entry[0])
if((i not in name_list)) and ((r not in name_list)) and ((n not in name_list)):
now=datetime.now()
d1=now.strftime("%d/%m/%Y")
dtString=now.strftime("%H:%M:%S")
f.writelines(f"\n{i}, {r}, {n}, {dtString}, {d1}, Co mat")
#================face recognition==================
def face_recog(self):
def draw_boundray(img,classifier,scaleFactor,minNeighbors,color,text,clf):
gray_image=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
featuers=classifier.detectMultiScale(gray_image,scaleFactor,minNeighbors)
coord=[]
for (x,y,w,h) in featuers:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
id,predict=clf.predict(gray_image[y:y+h,x:x+w])
confidence=int((100*(1-predict/300)))
conn = mysql.connector.connect(user='root', password='2912002',host='localhost',database='face_recognition',port=3306)
cursor = conn.cursor()
cursor.execute("select Name from student where Student_ID="+str(id))
n=cursor.fetchone()
n="+".join(n)
cursor.execute("select Roll_No from student where Student_ID="+str(id))
r=cursor.fetchone()
r="+".join(r)
cursor.execute("select Student_ID from student where Student_ID="+str(id))
i=cursor.fetchone()
i="+".join(i)
if confidence > 77:
cv2.putText(img,f"Student_ID:{i}",(x,y-80),cv2.FONT_HERSHEY_COMPLEX,0.8,(64,15,223),2)
cv2.putText(img,f"Name:{n}",(x,y-55),cv2.FONT_HERSHEY_COMPLEX,0.8,(64,15,223),2)
cv2.putText(img,f"Roll-No:{r}",(x,y-30),cv2.FONT_HERSHEY_COMPLEX,0.8,(64,15,223),2)
self.mark_attendance(i,r,n)
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,(255,255,0),3)
coord=[x,y,w,y]
return coord
#==========
def recognize(img,clf,faceCascade):
coord=draw_boundray(img,faceCascade,1.1,10,(255,25,255),"Face",clf)
return img
faceCascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
clf=cv2.face.LBPHFaceRecognizer_create()
clf.read("clf.xml")
videoCap=cv2.VideoCapture(0)
while True:
ret,img=videoCap.read()
img=recognize(img,clf,faceCascade)
cv2.imshow("Nhận Diện",img)
if cv2.waitKey(1) == 13:
break
videoCap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
root=Tk()
obj=Face_Recognition(root)
root.mainloop()