Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified FaceBase.db
Binary file not shown.
69 changes: 11 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,14 @@ Thanks to OpenCV, coding facial recognition is now easier than ever. There are t
01:Data Gathering: Gather face data (face images in this case) of the persons you want to identify.<br>
02:Train the Recognizer: Feed that face data and respective names of each face to the recognizer so that it can learn.<br>
03:Recognition: Feed new faces of that people and see if the face recognizer you just trained recognizes them.<br>

First Download SQLite Studio For python: https://sqlitestudio.pl/index.rvt?act=download <br>
After download you may goto download folder and extract the tools and click SQLiteStudio <br>
Then create database and create column of the table and set all attribute whatver you want. <br>

Follow this step now: <br>


import numpy as np<br>
import cv2<br>
import sqlite3<br>
cap = cv2.VideoCapture(0)<br>
detector= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')<br>

def insertOrUpdate(Id,Name):<br>
conn=sqlite3.connect("FaceBase.db")<br>
cmd="SELECT * FROM People WHERE ID="+str(Id)<br>
cursor=conn.execute(cmd)<br>
isRecordExist=0<br>
for row in cursor:<br>
isRecordExist=1<br>
if(isRecordExist==1):<br>
cmd="UPDATE people SET Name=' "+str(name)+" ' WHERE ID="+str(Id)<br>

else:<br>
cmd="INSERT INTO people(ID,Name) Values("+str(Id)+",' "+str(name)+" ' )"<br>

conn.execute(cmd)<br>
conn.commit()<br>
conn.close()<br>


id=raw_input('enter user id')<br>
name=raw_input('enter your name')<br>
insertOrUpdate(id,name)<br>
sampleNum=0;<br>

while(True):<br>
ret, img = cap.read()<br>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<br>
faces = detector.detectMultiScale(gray, 1.3, 5)<br>
for (x,y,w,h) in faces:<br>
sampleNum=sampleNum+1;<br>
cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])<br>
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)<br>
cv2.waitKey(100);<br>

cv2.imshow('Face',img)<br>
cv2.waitKey(1);<br>
if(sampleNum>20):<br>
break<br>

cap.release()<br>
cv2.destroyAllWindows()<br>




<h2>Requirements: </h2>
1. Python 3.7 <br>
2. Sqlite <br>
3. OpenCV2 <br>
4. Haarcascade file(usually comes with openCV) <br>

<h2>Follow this step now: </h2>
1. To check if all the dependencies are installed and the project will run, run `IntroPy.py` <br>
2. If `IntroPy.py` runs, then create the data set by running datasetCreator.py<br>
3. Now to train the recognizer run `trainner.py` <br>
4. After training the code, its time to test it out so run `detector.py`<br>
Binary file removed dataSet/User.1.1.jpg
Binary file not shown.
Binary file removed dataSet/User.1.10.jpg
Binary file not shown.
Binary file removed dataSet/User.1.11.jpg
Binary file not shown.
Binary file removed dataSet/User.1.12.jpg
Binary file not shown.
Binary file removed dataSet/User.1.13.jpg
Binary file not shown.
Binary file removed dataSet/User.1.14.jpg
Binary file not shown.
Binary file removed dataSet/User.1.15.jpg
Binary file not shown.
Binary file removed dataSet/User.1.16.jpg
Binary file not shown.
Binary file removed dataSet/User.1.17.jpg
Binary file not shown.
Binary file removed dataSet/User.1.18.jpg
Binary file not shown.
Binary file removed dataSet/User.1.19.jpg
Binary file not shown.
Binary file removed dataSet/User.1.2.jpg
Binary file not shown.
Binary file removed dataSet/User.1.20.jpg
Binary file not shown.
Binary file removed dataSet/User.1.21.jpg
Binary file not shown.
Binary file removed dataSet/User.1.3.jpg
Binary file not shown.
Binary file removed dataSet/User.1.4.jpg
Binary file not shown.
Binary file removed dataSet/User.1.5.jpg
Binary file not shown.
Binary file removed dataSet/User.1.6.jpg
Binary file not shown.
Binary file removed dataSet/User.1.7.jpg
Binary file not shown.
Binary file removed dataSet/User.1.8.jpg
Binary file not shown.
Binary file removed dataSet/User.1.9.jpg
Binary file not shown.
53 changes: 28 additions & 25 deletions dataSetCreator.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
import numpy as np
import cv2
import sqlite3
import os
import shutil

cap = cv2.VideoCapture(0)
detector= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

def insertOrUpdate(Id,Name):
def convertToBinaryData(filename):
with open(filename, 'rb') as file:
blobData = file.read()
return blobData

def insert(Name,img):
conn=sqlite3.connect("FaceBase.db")
cmd="SELECT * FROM People WHERE ID="+str(Id)
cursor=conn.execute(cmd)
isRecordExist=0
for row in cursor:
isRecordExist=1
if(isRecordExist==1):
cmd="UPDATE people SET Name=' "+str(name)+" ' WHERE ID="+str(Id)

else:
cmd="INSERT INTO people(ID,Name) Values("+str(Id)+",' "+str(name)+" ' )"

conn.execute(cmd)
cmd="INSERT INTO people(name,face) Values(?,?)"
conn.execute(cmd,(Name,img))
conn.commit()
conn.close()
id=raw_input('enter user id')
name=raw_input('enter your name')
insertOrUpdate(id,name)
sampleNum=0;

def cleanup():
shutil.rmtree("dataSet")

name=str(input('enter your name'))
sampleNum=0

while(True):
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
sampleNum=sampleNum+1;
cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])
sampleNum=sampleNum+1
if sampleNum == 1:
os.mkdir("dataSet")
cv2.imwrite("dataSet/User."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])
face = convertToBinaryData("dataSet/User."+str(sampleNum)+".jpg")
insert(name,face)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.waitKey(100);
cv2.waitKey(100)

cv2.imshow('Face',img)
cv2.waitKey(1);
if(sampleNum>20):
cv2.waitKey(1)
if(sampleNum>300):
break


cleanup()
cap.release()
cv2.destroyAllWindows()

16 changes: 5 additions & 11 deletions detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

detector= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

rec=cv2.createLBPHFaceRecognizer();
rec.load("recognizer/trainningData.yml")
rec=cv2.face.LBPHFaceRecognizer_create()
rec.read("recognizer/trainningData.yml")
id=0
def getProfile(id):
conn=sqlite3.connect("FaceBase.db")
Expand All @@ -23,7 +23,7 @@ def getProfile(id):


cap = cv2.VideoCapture(0)
font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 5, 1, 0, 4)
font = cv2.FONT_HERSHEY_SIMPLEX
while(True):
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Expand All @@ -33,15 +33,9 @@ def getProfile(id):
id,conf=rec.predict(gray[y:y+h,x:x+w])
profile=getProfile(id)
if(profile!=None):
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[1]),(x,y+h+30),font,255)#Draw the text
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[2]),(x,y+h+60),font,255)#Draw the text
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[3]),(x,y+h+90),font,255)#Draw the text
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[4]),(x,y+h+120),font,255)#Draw the text



cv2.putText(img,str(profile[1]),(x,y+h+30),font,1,255)#Draw the text

cv2.imshow('frame',img)
cv2.imshow('Press q to quit',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

Expand Down
Loading