-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceunlock.py
154 lines (134 loc) · 3.54 KB
/
faceunlock.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
import cv2
import numpy as np
import os
import MySQLdb
from rpi_lcd import LCD
from time import sleep
import string
from gpiozero import LED, Buzzer
import time, datetime
from picamera import PiCamera
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('../trainer/trainer.yml')
cascadePath = "../haarcascades/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
font = cv2.FONT_HERSHEY_SIMPLEX
#iniciate id counter
id = 0
def greenLED():
greenled = LED(24)
greenled.on()
sleep(1)
greenled.off()
greenled.close()
def buzzer():
buzz = Buzzer(21)
buzz.on()
sleep(1)
buzz.off()
buzz.close()
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL database")
sql = "SELECT Username FROM assignment.Users"
curs.execute(sql)
result = curs.fetchall()
userslist = ['None']
for x in result:
userslist.append(result[0][0])
print(userslist)
sql = "SELECT FaceScanConfidence FROM assignment.Security"
curs.execute(sql)
result = curs.fetchall()
setconfidence = int(result[0][0])
# Initialize and start realtime video capture
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video widht
cam.set(4, 480) # set video height
# Define min window size to be recognized as a face
minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)
lcd = LCD()
lcd.text('Scanning...', 1)
starttime = time.time()
while True:
confidentint = 0
ret, img =cam.read()
img = cv2.flip(img, -1) # Flip vertically
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 5,
minSize = (int(minW), int(minH)),
)
for(x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
# Check if confidence is less them 100 ==> "0" is perfect match
if (confidence < 100):
id = userslist[id]
confidentint = round(100 - confidence)
print(confidentint)
confidence = " {0}%".format(round(100 - confidence))
else:
id = "unknown"
confidence = " {0}%".format(round(100 - confidence))
cv2.putText(img, str(id), (x+5,y-5), font, 1, (255,255,255), 2)
cv2.putText(img, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1)
#cv2.imshow('camera',img)
if confidentint > setconfidence:
lcd.text('Identity', 1)
lcd.text('Confirmed!', 2)
greenLED()
buzzer()
sleep(2)
lcd.text('Welcome, {}!'.format(id), 1)
sleep(1)
lcd.text('Safe Room', 1)
lcd.text('Unlocked!', 2)
sleep(5)
lcd.clear()
try:
print(id)
sql = "SELECT UserID FROM assignment.Users WHERE Username = '%s'" % (id)
curs.execute(sql)
result = curs.fetchall()
for x in result:
userid = x[0]
sql = "INSERT into UserLog(UserID) VALUES (%d)" % (int(userid))
print(sql)
curs.execute(sql)
db.commit()
print('\nDatabase Modified')
cam.release()
cv2.destroyAllWindows()
except MySQLdb.Error as e:
print(e)
break
nowtime = time.time()
timediff = nowtime - starttime
print(timediff)
if timediff > 30:
cam.release()
cv2.destroyAllWindows()
lcd.text('Identity', 1)
lcd.text('Unconfirmed!', 2)
sleep(5)
try:
name = datetime.datetime.now()
sql = "INSERT into FailedEntryLog(DateTime) VALUES ('%s')" % (name)
print(sql)
curs.execute(sql)
db.commit()
print('\nDatabase Modified')
camera = PiCamera()
camera.capture('../captures/{}.jpg'.format(name))
except MySQLdb.Error as e:
print(e)
break
lcd.clear()
import startprogram