-
Notifications
You must be signed in to change notification settings - Fork 0
/
final.py
266 lines (212 loc) · 10.5 KB
/
final.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import tkinter as tk
from tkinter import *
import cv2,os
import csv
import numpy as np
from PIL import Image
import pandas as pd
import datetime
import time
from tkinter import messagebox
a = Tk()
a.title("Intro")
a.geometry("900x600")
a.configure(bg = "black")
Label(a, text = "Online Examination Module", font = ("arial",30,"bold"), fg = "black", bg = "white").place(x = 200, y = 100)
def register():
reg = Tk()
reg.title("Register")
reg.geometry("900x600")
reg.configure(bg = "black")
def TrainImages():
recognizer = cv2.face_LBPHFaceRecognizer.create()
faces,Id = getImagesAndLabels("TrainingImage")
recognizer.train(faces, np.array(Id))
recognizer.save("TrainingImageLabel\Trainner.yml")
tk.messagebox.showinfo('Congratulations',' You have been registered')
def getImagesAndLabels(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
Ids=[]
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 clearAll():
text1.delete(0, END)
text2.delete(0, END)
def submit():
pass
Label(reg, text = "Register", font = ("arial",32,"bold"), fg = "dark green", bg = "magenta").place(x = 300, y = 100)
Label(reg, text = "Name :", font = ("arial",16,"bold"), fg = "white", bg = "black").place(x = 220, y = 200)
Label(reg, text = "Roll No. :", font = ("arial",16,"bold"), fg = "white", bg = "black").place(x = 220, y = 240)
Label(reg, text = "Biometric Scan :", font = ("arial",16,"bold"), fg = "white", bg = "black").place(x = 220, y = 280)
name = StringVar()
Id = StringVar()
text1 = Entry(reg, textvariable = name, font = ("arial",16,"bold"), fg = "black", bg = "white")
text1.place(x = 400, y = 200, height = 25, width = 200)
def on_change1():
print(text1.get())
return text1.get()
def on_change2():
print(text2.get())
return text2.get()
text2 = Entry(reg, textvariable = Id, font = ("arial",16,"bold"), fg = "black", bg = "white")
text2.place(x = 400, y = 240, height = 25, width = 200)
Id1= text2.get()
print(Id1)
def webcam():
name=on_change1()
Id=on_change2()
cam = cv2.VideoCapture(0)
harcascadePath = "haarcascade_frontalface_default.xml"
detector=cv2.CascadeClassifier(harcascadePath)
sampleNum=0
while(True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.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
cv2.imwrite("TrainingImage\ "+name +"."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
#display the frame
cv2.imshow('frame',img)
#wait for 100 miliseconds
if cv2.waitKey(100) & 0xFF == ord('q'):
break
# break if the sample number is more than 60
elif sampleNum>60:
break
cam.release()
cv2.destroyAllWindows()
row = [Id, name]
with open('StudentDetails\StudentDetails.csv','a+') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()
TrainImages()
Button(reg, text = "Open WebCam", font = ("arial",14,"bold"), fg = "dark green", bg = "magenta", command = webcam).place(x = 400, y = 280)
Button(reg, text = "Submit", font = ("arial",24,"bold"), fg = "dark green", bg = "magenta", command = submit).place(x = 350, y = 400)
def start_exam():
att = Tk()
att.title("Start Exam")
att.geometry("900x600")
att.configure(bg = "black")
def clearAll():
text1.delete(0, END)
text2.delete(0, END)
def webcam():
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("TrainingImageLabel\Trainner.yml")
harcascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(harcascadePath);
df=pd.read_csv("StudentDetails\StudentDetails.csv")
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
col_names = ['Id','Name','Date','Time']
attendance = pd.DataFrame(columns = col_names)
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, 1.2,5)
for(x,y,w,h) in faces:
cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if(conf < 50):
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
aa=df.loc[df['Id'] == Id]['Name'].values
w=Id
tt=str(Id)+"-"+aa
n=aa
attendance.loc[len(attendance)] = [Id,aa,date,timeStamp]
else:
Id='Unknown'
tt=str(Id)
if(conf > 75):
noOfFile=len(os.listdir("ImagesUnknown"))+1
cv2.imwrite("ImagesUnknown\Image"+str(noOfFile) + ".jpg", im[y:y+h,x:x+w])
cv2.putText(im,str(tt),(x,y+h), font, 1,(255,255,255),2)
attendance=attendance.drop_duplicates(subset=['Id'],keep='first')
cv2.imshow('im',im)
if (cv2.waitKey(1)==ord('q')):
break
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
Hour,Minute,Second=timeStamp.split(":")
fileName="Attendance\Attendance_"+date+"_"+Hour+"-"+Minute+"-"+Second+".csv"
attendance.to_csv(fileName,index=False)
Label(att, text = aa, font = ("arial",16,"bold"), fg = "black", bg = "white").place(x = 600, y = 200)
Label(att, text = w, font = ("arial",16,"bold"), fg = "black", bg = "white").place(x = 600, y = 240)
tk.messagebox.showinfo('Congratulations,Your attendance has been marked successfully for the day! You can take the test',icon = 'warning')
cam.release()
cv2.destroyAllWindows()
def submit():
me=webcam()
print(me)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
video_capture = cv2.VideoCapture(0)
face=0
eye=0
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if type(faces) == tuple:
cv2.putText(frame, "Face : NO" ,(50,50), cv2.FONT_HERSHEY_SIMPLEX,
1, (0,0,255), 2, cv2.LINE_AA)
face=face+1
else:
cv2.putText(frame, "Face : YES" ,(50,50), cv2.FONT_HERSHEY_SIMPLEX,
1, (0,255,0), 2, cv2.LINE_AA)
face=face-1
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 3)
if type(eyes) == tuple:
cv2.putText(frame, "Eyes : NO" ,(50,75),
cv2.FONT_HERSHEY_SIMPLEX,
1, (0,0,255), 2, cv2.LINE_AA)
eye=eye+1
else:
cv2.putText(frame, "Eyes : YES" ,(50,75),
cv2.FONT_HERSHEY_SIMPLEX,
1, (0,255,0), 2, cv2.LINE_AA)
eye=eye-1
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
if eye <=30 and face <=200 :
cv2.imshow('Video', frame)
print("Eye")
print(eye)
print("Face")
print(face)
else :
video_capture.release()
cv2.destroyAllWindows()
if cv2.waitKey(100) & 0xFF == ord('q'):
break
Label(att, text = "Attendance", font = ("arial",32,"bold"), fg = "dark green", bg = "magenta").place(x = 260, y = 100)
Label(att, text = "Biometric Scan :", font = ("arial",16,"bold"), fg = "white", bg = "black").place(x = 150, y = 200)
Label(att, text = "Name :", font = ("arial",16,"bold"), fg = "white", bg = "black").place(x = 500, y = 200)
Label(att, text = "Roll No. :", font = ("arial",16,"bold"), fg = "white", bg = "black").place(x = 500, y = 240)
Button(att, text = "Open WebCam", font = ("arial",14,"bold"), fg = "dark green", bg = "magenta", command = webcam).place(x = 160, y = 230)
Button(att, text = "Start Exam", font = ("arial",24,"bold"), fg = "dark green", bg = "magenta", command = submit).place(x = 300, y = 400)
Button(a, text = "Register", font = ("arial",24,"bold"), fg = "dark green", bg = "magenta", command = register).place(x = 180, y = 380)
Button(a, text = "Start Exam", font = ("arial",24,"bold"), fg = "dark green", bg = "magenta", command = start_exam).place(x = 520, y = 380)
a.mainloop()