CoolFace
Apppublic

Spooke/Login_System_using_Facial_Recognition

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py113 linesDownload Raw Back to root
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
12name , Id = '',''
13dic = {
14    'Name' : name,
15    'Ids' : Id
16}
17def store_data():
18    global name,Id,dic
19    name = str(input("Enter Name  "))
20   
21    Id  = str(input("Enter Id   "))
22   
23    dic = {
24        'Ids' : Id,
25        'Name': name
26    }
27    c = dic
28    return  c
29
30#Fucntion to check if entered ID is number or not
31def is_number(s):
32    try:
33        float(s)
34        return True
35    except ValueError:
36        pass
37 
38    try:
39        import unicodedata
40        unicodedata.numeric(s)
41        return True
42    except (TypeError, ValueError):
43        pass
44 
45    return False
46
47def TakeImages():
48    dict1 = store_data()
49    
50    #print(dict1)
51    #name = "Santhu"
52    #Id = '1'
53    if (name.isalpha() and is_number(Id)):
54        #Checking Id if it is 1 we are rewring the profile else just updating csv
55        if Id == '1':
56            fieldnames = ['Name','Ids']
57            with open('Profile.csv','w') as f:
58                writer = csv.DictWriter(f, fieldnames =fieldnames)
59                writer.writeheader()
60                writer.writerow(dict1)
61        else:
62            fieldnames = ['Name','Ids']
63            with open('Profile.csv','a+') as f:
64                writer = csv.DictWriter(f, fieldnames =fieldnames)
65                #writer.writeheader()
66                writer.writerow(dict1)
67        cam = cv2.VideoCapture(0)
68
69        #Haarcascade file for detctionof face
70        harcascadePath = "hh.xml"
71        detector = cv2.CascadeClassifier(harcascadePath)
72        sampleNum = 0
73        while (True):
74            ret, img = cam.read()
75            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
76            faces = detector.detectMultiScale(gray, 1.3, 5)
77            for (x, y, w, h) in faces:
78                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
79                # Incrementing sample number
80                sampleNum = sampleNum + 1
81                # Saving the captured face in the dataset folder TrainingImage
82                cv2.imwrite("TrainingImage\ " + name + "." + Id + '.' + str(sampleNum) + ".jpg", gray[y:y + h, x:x + w])
83                # display the frame
84            cv2.imshow('Cpaturing Face for Login ', img)
85        
86            # wait for 100 miliseconds
87            if cv2.waitKey(100) & 0xFF == ord('q'):
88                break
89            # break if the sample number is morethan 60
90            elif sampleNum > 60:
91                break
92            
93        
94        cam.release()
95        cv2.destroyAllWindows()
96        res = "Images Saved for Name : " + name + " with ID  " + Id
97        print(res)
98        print(' Images save location is TrainingImage\ ')
99      
100        
101    else:
102        if(name.isalpha()):
103            print('Enter Proper Id')
104        elif(is_number(Id)):
105            print('Enter Proper name')
106        else:
107            print('Enter Proper Id and Name')
108                    
109        
110
111TakeImages()
112
113