Spaces:
Configuration error
Configuration error
Upload 5 files
Browse files- Detectface.py +95 -0
- Profile.csv +2 -0
- README.md +34 -13
- Readface.py +112 -0
- Train.py +50 -0
Detectface.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
@author: Santhosh R
|
| 3 |
+
"""
|
| 4 |
+
import cv2,os
|
| 5 |
+
import shutil
|
| 6 |
+
import csv
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image, ImageTk
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
# This will make sure no duplicates exixts in profile.csv(using Pandas here)
|
| 12 |
+
df = pd.read_csv('Profile.csv')
|
| 13 |
+
df.sort_values('Ids', inplace = True)
|
| 14 |
+
df.drop_duplicates(subset = 'Ids', keep = 'first', inplace = True)
|
| 15 |
+
df.to_csv('Profile.csv', index = False)
|
| 16 |
+
|
| 17 |
+
# Fuction to detect the face
|
| 18 |
+
def 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
|
| 92 |
+
DetectFace()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
Profile.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Name,Ids
|
| 2 |
+
Santhu,1
|
README.md
CHANGED
|
@@ -1,13 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Face-Login
|
| 2 |
+
Face login using Open CV Python
|
| 3 |
+
|
| 4 |
+
This folder contains all the code and folders as well as files required to run this project
|
| 5 |
+
|
| 6 |
+
## Prerequisites
|
| 7 |
+
Numpy </br>
|
| 8 |
+
OpenCV </br>
|
| 9 |
+
Pillow </br>
|
| 10 |
+
Pandas
|
| 11 |
+
|
| 12 |
+
### Installation
|
| 13 |
+
|
| 14 |
+
You can choose to install any environment either system, pipenv, anaconda etc </br>
|
| 15 |
+
I used Visual Code and Python 3.6 and installed these using pip(Google gives more on this)
|
| 16 |
+
|
| 17 |
+
pip install opencv-python
|
| 18 |
+
pip install opencv-contrib-python
|
| 19 |
+
|
| 20 |
+
pip install pillow
|
| 21 |
+
|
| 22 |
+
pip install pandas
|
| 23 |
+
|
| 24 |
+
pip install numpy
|
| 25 |
+
|
| 26 |
+
## Running the test for Face login
|
| 27 |
+
|
| 28 |
+
1. Create the same folders while testing or download entire project
|
| 29 |
+
2. Run Readface.py to take some images of user for login, It asks for Name and Id in console
|
| 30 |
+
3. Run Train.py, this will train all images taken and put in TrainData Folder
|
| 31 |
+
4. Run Detectface.py to detect the face and Login, Console shows the login messages
|
| 32 |
+
|
| 33 |
+
Finally Face login is Successful and Unknow faces tried to login are stored in UnkownFaces folder </br>
|
| 34 |
+
✔✔✔✔✔✔✔✔✔
|
Readface.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
@author: Santhosh R
|
| 3 |
+
"""
|
| 4 |
+
import cv2, os
|
| 5 |
+
import shutil
|
| 6 |
+
import csv
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image, ImageTk
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
name , Id = '',''
|
| 13 |
+
dic = {
|
| 14 |
+
'Name' : name,
|
| 15 |
+
'Ids' : Id
|
| 16 |
+
}
|
| 17 |
+
def 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
|
| 31 |
+
def 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 |
+
|
| 47 |
+
def 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 |
+
|
| 111 |
+
TakeImages()
|
| 112 |
+
|
Train.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
@author: Santhosh R
|
| 3 |
+
"""
|
| 4 |
+
import cv2, os
|
| 5 |
+
import shutil
|
| 6 |
+
import csv
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image, ImageTk
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def getImagesAndLabels(path):
|
| 13 |
+
# Get the path of all the files in the folder
|
| 14 |
+
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
|
| 15 |
+
|
| 16 |
+
# Create empth face list
|
| 17 |
+
faces = []
|
| 18 |
+
# Create empty ID list
|
| 19 |
+
Ids = []
|
| 20 |
+
# Looping through all the image paths and loading the Ids and the images
|
| 21 |
+
for imagePath in imagePaths:
|
| 22 |
+
# Loading the image and converting it to gray scale
|
| 23 |
+
pilImage = Image.open(imagePath).convert('L')
|
| 24 |
+
# Now we are converting the PIL image into numpy array
|
| 25 |
+
imageNp = np.array(pilImage, 'uint8')
|
| 26 |
+
# getting the Id from the image
|
| 27 |
+
Id = int(os.path.split(imagePath)[-1].split(".")[1])
|
| 28 |
+
# extract the face from the training image sample
|
| 29 |
+
faces.append(imageNp)
|
| 30 |
+
|
| 31 |
+
Ids.append(Id)
|
| 32 |
+
return faces, Ids
|
| 33 |
+
|
| 34 |
+
# Train image using LBPHFFace recognizer
|
| 35 |
+
def TrainImages():
|
| 36 |
+
recognizer = cv2.face.LBPHFaceRecognizer_create() # recognizer = cv2.face.LBPHFaceRecognizer_create()#$cv2.createLBPHFaceRecognizer()
|
| 37 |
+
harcascadePath = "hh.xml"
|
| 38 |
+
detector = cv2.CascadeClassifier(harcascadePath)
|
| 39 |
+
faces , Id= getImagesAndLabels("TrainingImage")
|
| 40 |
+
recognizer.train(faces, np.array(Id))
|
| 41 |
+
#store data in file
|
| 42 |
+
recognizer.save("TrainData\Trainner.yml")
|
| 43 |
+
res = "Image Trained and data stored in TrainData\Trainner.yml "
|
| 44 |
+
|
| 45 |
+
print(res)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
TrainImages()
|
| 50 |
+
|