-
Notifications
You must be signed in to change notification settings - Fork 8
/
recognizer.py
77 lines (65 loc) · 2.24 KB
/
recognizer.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
import cv2
import os
import numpy as np
import time
import sys
import sqlite3 as sql
conn = sql.connect('database/database.db')
c = conn.cursor()
#flag for dismatch count
flag = 0
#recording the time of attendance
time_of_attendance = time.time()
#instance for cascade file
face_cas = cv2.CascadeClassifier('cascades/haarcascade_frontalface_default.xml')
#initializing camera for the time untill it records 30 images into dataset
cap_video = cv2.VideoCapture(0)
#creeating instance for LBPH algorithm
recognizer = cv2.face.LBPHFaceRecognizer_create()
#reading trainer.yml file
recognizer.read('trainer/trainer.yml')
#selecting font
font = cv2.FONT_HERSHEY_SIMPLEX
#starting loop to recognize faces
while True:
#ret is boolean variable to check if frame has been read correctly or not
ret, img = cap_video.read()
#converting color BGR to grayscale format
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#reading and scaling face from gray image using scale factor 1.3 and neighbor number =7
faces = face_cas.detectMultiScale(gray, 1.3, 7)
for (x,y,w,h) in faces:
#extracting facial cordinates
roi_gray = gray[y:y+h,x:x+w]
#creating a rectangle around the face
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0))
#recognizer predicting the image after matching it and returning its id and confidence level
id,conf = recognizer.predict(roi_gray)
#putting text on rectangle around face
cv2.putText(img,str(id),(x,y-10),font,0.55,(120,255,120),1)
if(conf<80):
#marking present against that id
for row in c.execute("SELECT * FROM students WHERE UID = ?",(str(id),)):
if(str(id) == row[0]):
c.execute("UPDATE students SET attendance= ? WHERE UID = ?",('Present',str(id)))
conn.commit()
else:
os.system("python error_gui.py")
flag = flag+1
break
cv2.imshow('frame',img)
#window opening time = 15seconds
period = 15
if(flag == 10):
os.system("python error_gui.py")
break
if(time.time()) > (time_of_attendance+period):
break
if(cv2.waitKey(100) & 0xFF == ord('q')):
break
#writing attendance in excel
os.system("python Marking_attendance.py")
#closing the connection to sqlite3
conn.close()
cap_video.release();
cv2.destroyAllWindows();