Hey folks,
This time came up with a security system that can protect our house and lockers.
In market, door locks with facial recognition, fingerprint scan, password are available as individual. but combination of these there technologies are not available. so I decided to build one..
STEP 1collecting the necessary components
Setting up the RPI 3B +
- download raspbian stretch from official website
- install stretch on to a micro SD card and insert into Pi and boot it.
install OpenCV
STEP 3there are 3 codes to run for this thing to work
1 face.py
2 train.py
3 recognition.py
open each code using python
3 compailer
the codes are given below
face.py
import cv2
cam = cv2.VideoCapture(0)
detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Id=input('enter your id')
sampleNum=0
while(True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
#incrementing sample number
sampleNum=sampleNum+1
#saving the captured face in the dataset folder
cv2.imwrite("dataset/User."+Id +'.'+ str(sampleNum) + ".jpg", img[y:y+h,x:x+w])
cv2.imshow('frame',img)
#wait for 100 miliseconds
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# break if the sample number is morethan 20
elif sampleNum>20:
break
cam.release()
cv2.destroyAllWindows()
Run this module
while running the camera takes 21 sample images of the individuals face for training
train.py
also
run this module
import cv2
import os
import numpy as np
from PIL import Image
# Path for face image database
path = 'dataset'
recognizer = cv2.face_LBPHFaceRecognizer.create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
#import cv2,os
#import numpy as np
#from PIL import Image
#recognizer = cv2.createLBPHFaceRecognizer()
#recognizer = cv2.face.LBPHFaceRecognizer_create()
#detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
def getImagesAndLabels(path):
#get the path of all the files in the folder
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
print (imagePaths)
#create empth face list
faceSamples=[]
#create empty ID list
Ids=[]
#now looping through all the image paths and loading the Ids and the images
for imagePath in imagePaths:
#loading the image and converting it to gray scale
pilImage=Image.open(imagePath).convert('L')
#Now we are converting the PIL image into numpy array
imageNp=np.array(pilImage,'uint8')
#getting the Id from the image
Id=int(os.path.split(imagePath)[-1].split(".")[1])
# extract the face from the training image sample
faces=detector.detectMultiScale(imageNp)
#If a face is there then append that in the list as well as Id of it
for (x,y,w,h) in faces:
faceSamples.append(imageNp[y:y+h,x:x+w])
Ids.append(Id)
return faceSamples,Ids
faces,Ids = getImagesAndLabels('dataset')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainner/trainner.yml')
recognition.py
import cv2
import numpy as np
import datetime
import serial
import RPi.GPIO as GPIO
from time import sleep
ser = serial.Serial('/dev/ttyS0', 9600)
GPIO.setwarnings(False)
#Select GPIO mode
GPIO.setmode(GPIO.BCM)
#Set pin 23 as output
relay=23
GPIO.setup(relay,GPIO.OUT)
#recognizer = cv2.face.createLBPHFaceRecognizer()
#recognizer = cv2.face_LBPHFaceRecognizer.create()
recognizer=cv2.face.LBPHFaceRecognizer_create();
recognizer.read('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
#dbx = dropbox.Dropbox('9CnZVpr3KTAAAAAAAAABA0lVPpe61WhVSisHuyO6MGAD0raLJwXZvp1TlaqGUAAAAAAAAAHZoO4B74NWXnTrNnjYevssOklUtLvi8ScYitq2IFmW-j3OfLs_lgmjHwzf')
#dbx.users_get_current_account()
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
fontscale = 1
fontcolor = (255, 255, 255)
def upload(Id):
f = open("Test.txt", "w");
f.write(Id)
n= datetime.datetime.now()
f.write("%s"%n)
f.close()
f = open("Test.txt", "r");
contents = f.read()
#dbx.files_upload(contents,'/folder.txt')
print (contents)
print ("%s"%n)
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, 1.2,5)
for(x,y,w,h) in faces:
cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if(conf<50):
if(Id==1):
Id="Shibin"
upload(Id)
cam.release()
cv2.destroyAllWindows()
e=input("Password:")
if(e=="Hallo"):
print("Place Finger")
u=ser.read()
print(u)
if(u==b'1'):
GPIO.output(relay,GPIO.HIGH)
print ("Switched On")
sleep(2) # Delay in seconds
GPIO.output(relay,GPIO.LOW)
else:
print("Unauthorized")
else:
print("Unauthorized")
if(Id==5):
#else:
print("Unauthorized")
upload(Id)
#cv2.imwrite("Indruder/In",im)
cv2.putText(im,str(Id), (x,y+h),font,fontscale,fontcolor)
cv2.imshow('im',im)
if cv2.waitKey(10) & 0xFF==ord('q'):
break
cam.release()
cv2.destroyAllWindows()
STEP 4run this module too
facial recognition
if face recognises, the next step comes, password
password
if these two stages are completed, then the last one fingerprint
if all these goes right then the lock will open
Comments