Spooke/Login_System_using_Facial_Recognition
0
1"""
2@author: Santhosh R
3"""
4import cv2,os
5import shutil
6import csv
7import numpy as np
8from PIL import Image, ImageTk
9import pandas as pd
10
11# This will make sure no duplicates exixts in profile.csv(using Pandas here)
12df = pd.read_csv('Profile.csv')
13df.sort_values('Ids', inplace = True)
14df.drop_duplicates(subset = 'Ids', keep = 'first', inplace = True)
15df.to_csv('Profile.csv', index = False)
16
17# Fuction to detect the face
18def DetectFace():
19 reader = csv.DictReader(open('Profile.csv'))
20 print('Detecting Login Face')
21 for rows in reader:
22 result = dict(rows)
23 #print(result)
24 if result['Ids'] == '1':
25 name1 = result['Name']
26 elif result['Ids'] == '2':
27 name2 = result["Name"]
28 recognizer = cv2.face.LBPHFaceRecognizer_create() #cv2.createLBPHFaceRecognizer()
29 recognizer.read("TrainData\Trainner.yml")
30 harcascadePath = "hh.xml"
31 faceCascade = cv2.CascadeClassifier(harcascadePath);
32 cam = cv2.VideoCapture(0)
33 font = cv2.FONT_HERSHEY_SIMPLEX
34 Face_Id = ''
35 name2 = ''
36
37 # Camera ON Everytime
38 while True:
39 ret, frame = cam.read()
40 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
41 faces = faceCascade.detectMultiScale(gray, 1.3, 5)
42 Face_Id = 'Not detected'
43
44 # Drawing a rectagle around the face
45 for (x, y, w, h) in faces:
46 Face_Id = 'Not detected'
47 cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
48 Id, confidence = recognizer.predict(gray[y:y + h, x:x + w])
49 if (confidence < 80):
50 if (Id == 1):
51 name = name1
52
53 elif (Id == 2):
54 name = name2
55
56 Predicted_name = str(name)
57 Face_Id = Predicted_name
58 else:
59 Predicted_name = 'Unknown'
60 Face_Id = Predicted_name
61 # Here unknown faces detected will be stored
62 noOfFile = len(os.listdir("UnknownFaces")) + 1
63 if int(noOfFile) < 100:
64 cv2.imwrite("UnknownFaces\Image" + str(noOfFile) + ".jpg", frame[y:y + h, x:x + w])
65
66 else:
67 pass
68
69
70 cv2.putText(frame, str(Predicted_name), (x, y + h), font, 1, (255, 255, 255), 2)
71
72 cv2.imshow('Picture', frame)
73 #print(Face_Id)
74 cv2.waitKey(1)
75
76 # Checking if the face matches for Login
77 if Face_Id == 'Not detected':
78 print("-----Face Not Detected, Try again------")
79 pass
80
81 elif Face_Id == name1 or name2 and Face_Id != 'Unknown' :
82 print('----------Detected as {}----------'.format(name1))
83 print('-----------login successfull-------')
84 print('***********WELCOME {}**************'.format(name1))
85 break
86 else:
87 print('-----------Login failed please try agian-------')
88
89
90 #if (cv2.waitKey(1) == ord('q')):
91 # break
92DetectFace()
93
94
95
96 