-
Notifications
You must be signed in to change notification settings - Fork 0
/
second.py
201 lines (176 loc) · 6.73 KB
/
second.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import tkinter as tk
from tkinter import *
from tkinter import Message ,Text
import os
import cv2 as cv
import mysql.connector
import csv
import numpy as np
from PIL import Image, ImageTk
import time
import pyrebase
mydb=mysql.connector.connect(host="Your host",user="Your User",passwd="Your Password",database="student_database")
mycursor=mydb.cursor(buffered=True)
config={"Google firebase connection key"}
firebase=pyrebase.initialize_app(config)
storage=firebase.storage()
root=Tk()
root.geometry("700x400")
root.title("Add New")
label1=tk.Label(root,text="Password",width=8,fg="white",bg="red",font=('times',20,'bold'))
label1.place(x=50,y=50)
text1=tk.Entry(root,fg="red",bg="red",font=('times',20,'bold'))
text1.place(x=200,y=50)
label2=tk.Label(root,text="Id",width=8,fg="white",bg="red",font=('times',20,'bold'))
label2.place(x=50,y=100)
text2=tk.Entry(root,fg="white",bg="red",font=('times',20,'bold'))
text2.place(x=200,y=100)
label3=tk.Label(root,text="Name",width=8,fg="white",bg="red",font=('times',20,'bold'))
label3.place(x=50,y=150)
text3=tk.Entry(root,fg="white",bg="red",font=('times',20,'bold'))
text3.place(x=200,y=150)
label4=tk.Label(root,text="Email",width=8,fg="white",bg="red",font=('times',20,'bold'))
label4.place(x=50,y=200)
text4=tk.Entry(root,fg="white",bg="red",font=('times',20,'bold'))
text4.place(x=200,y=200)
message1 = tk.Label(root,text="",bg="red" ,fg="white" ,width=30 ,height=2,font=('times', 15, ' bold '))
message1.place(x=50,y=250)
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def add_new():
Id=(text2.get())
Name=(text3.get())
Email=(text4.get())
if(is_number(Id) and Name.isalpha()):
cam = cv.VideoCapture(0)
harcascadePath = "haarcascade_frontalface_default.xml"
detector=cv.CascadeClassifier(harcascadePath)
sampleNum=0
while(True):
ret, img = cam.read()
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
#incrementing sample number
sampleNum=sampleNum+1
#saving the captured face in the dataset folder TrainingImage
cv.imwrite("Images\ "+Name +"."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
#display the frame
cv.imshow('frame',img)
#wait for 100 miliseconds
if cv.waitKey(100) & 0xFF == ord('q'):
break
# break if the sample number is morethan 100
elif sampleNum>60:
break
cam.release()
cv.destroyAllWindows()
row = (Id,Name,Email)
sql="INSERT INTO mydata (Id,Name,Email) VALUES (%s,%s,%s)"
mycursor.execute(sql,row)
mydb.commit()
mycursor.execute("UPDATE mydata SET Status='ABSENT' where Id='"+Id+"'")
mydb.commit()
mycursor.execute("SELECT Id,Name,Email FROM mydata")
data=mycursor.fetchall()
with open('StudentDetails\StudentDetails.csv','w') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(["Id","Name","Email"])
writer.writerows(data)
f.close()
message1.configure(text="Images Saved for ID : " + Id +" Name : "+ Name)
else:
if(is_number(Id)):
message1.configure(text="Enter Alphabetical Name")
if(Name.isalpha()):
message1.configure(text="Enter Numeric Id")
def TrainImages():
recognizer = cv.face_LBPHFaceRecognizer.create()#recognizer = cv2.face.LBPHFaceRecognizer_create()#$cv2.createLBPHFaceRecognizer()
harcascadePath = "haarcascade_frontalface_default.xml"
detector =cv.CascadeClassifier(harcascadePath)
faces,Id = getImagesAndLabels("Images")
recognizer.train(faces, np.array(Id))
recognizer.save("ImageLabel\Trainner.yml")
message1.configure(text="Trainning compleated")
def getImagesAndLabels(path):
#get the path of all the files in the folder
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
#print(imagePaths)
#create empth face list
faces=[]
#create empty ID list
Ids=[]
#now looping through all the image paths and loading the Ids and the images
for imagePath in imagePaths:
#loading the image and converting it to gray scale
pilImage=Image.open(imagePath).convert('L')
#Now we are converting the PIL image into numpy array
imageNp=np.array(pilImage,'uint8')
#getting the Id from the image
Id=int(os.path.split(imagePath)[-1].split(".")[1])
# extract the face from the training image sample
faces.append(imageNp)
Ids.append(Id)
return faces,Ids
def cloudupload():
path_on_cloud="StudentDetails/StudentDetails.csv"
path_local="StudentDetails\StudentDetails.csv"
storage.child(path_on_cloud).put(path_local)
message1.configure(text="Students list uploaded")
def quit():
TrainImages()
cloudupload()
root.destroy()
def clear():
text2.delete(0, 'end')
text3.delete(0, 'end')
text4.delete(0, 'end')
def convert(s):
# initialization of string to ""
new = ""
# traverse in the string
for x in s:
new += x
# return string
return new
def delete():
Id=(text2.get())
mycursor.execute("SELECT Name FROM mydata WHERE Id='"+Id+"'")
result=mycursor.fetchone()
Name=convert(result)
samplenum=1
while (samplenum<=61):
os.remove("Images/ "+Name +"."+Id +'.'+ str(samplenum) + ".jpg")
samplenum=samplenum+1
sql = "DELETE FROM mydata WHERE Id='"+Id+"'"
mycursor.execute(sql)
mydb.commit()
mycursor.execute("SELECT Id,Name,Email FROM mydata")
data=mycursor.fetchall()
with open('StudentDetails\StudentDetails.csv','w') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(["Id","Name","Email"])
writer.writerows(data)
f.close()
message1.configure(text="Deleted "+Name+" "+Id)
button1=tk.Button(root,text="Clear",command=clear,width=8,fg="white",bg="red",font=('times',20,'bold'))
button1.place(x=500,y=200)
button2=tk.Button(root,text="Quit",command=quit,width=8,fg="white",bg="red",font=('times',20,'bold'))
button2.place(x=500,y=275)
button3=tk.Button(root,text="Add New",command=add_new,width=8,fg="white",bg="red",font=('times',20,'bold'))
button3.place(x=500,y=50)
button4=tk.Button(root,text="Delete",command=delete,width=8,fg="white",bg="red",font=('times',20,'bold'))
button4.place(x=500,y=125)
root.mainloop()