-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam_face_verification.py
72 lines (55 loc) · 1.81 KB
/
cam_face_verification.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
import tensorflow as tf
import cv2
import time
import sqlite3
from Face_detector_cascade import faceDetector
from config import image_shape, THRESHOLD
from FRmodel import get_faceRecoModel
conn = sqlite3.connect('face_database.db')
c = conn.cursor()
def verify(roi, model, identity):
emb = model.predict(roi)
emb = tf.math.reduce_sum(emb)
p = c.execute(f"SELECT * FROM face_data WHERE name=?", (identity))
person = p.fetchone()
if person is not None:
stored_emb = tf.constant(person[1])
distance = tf.math.reduce_sum(stored_emb - emb)
if distance < THRESHOLD:
return True
else:
return False
else:
return False
def live_face_detection(model):
detector = faceDetector("haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture(0)
identity = input("Name of the person: ")
print("Verifying...")
identity = identity.lower()
time.sleep(3)
while cap.isOpened():
succes, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector.detect(image=gray)
for (x,y,w,h) in faces:
roi = frame[y:y+h,x:x+w]
roi = tf.image.resize(roi, (image_shape[0], image_shape[1]))
res = verify(roi, model, identity)
if res:
return res, identity
cv2.imshow("video", frame)
if cv2.waitKey(0) & 0xFF == ord('q'):
break
def face_verification(model):
res = False
res, identity = live_face_detection(model)
door_open = False
if res:
print(f"Welcome {identity}")
door_open = True
else:
print("Wrong person")
if __name__ == "__main__":
model = get_faceRecoModel()
face_verification(model)