Simple Face Recognizing System using python and openCV
Tags: python, opencv
Published: 2017-08-22 08:39:08

---
title: Simple Face Recognizing System using python and openCV
published: true
description: 
tags: python,openCV
cover_image: https://thepracticaldev.s3.amazonaws.com/i/5tby4vsyubd79dlb7uhs.jpg
---
#Face Recognising System
Face Recognising System is a computer application that is used to identify people from a image or a video footage.It is mainly used in security purposes to get track of who is entering a certain facility or to search someone in a certain place.It may not be as proper as bio metric or iris scanner but it is much easy to implement.This face recognising system works with a database where image of faces of the people are saved.This system runs a couple of algorithms to determine whether the face of the person in camera or footage matches with any image of database.One of those algorithm saves the images of the people in database in `grayScale` format.Another algorithm searches for a face.Another algorithm compares whether a certain area or size or shape of the face matches with the database.The combine effect of all these algorithms is this system.For making a basic `Face Recognising System` one of the best way is to use `openCV`
What is openCV?
openCV is a cross platform open source library written in C++,developed by Intel.openCV is used for `Face Recognising System` , `motion sensor` , <code>mobile robotics</code> etc.This library is supported in most of the operating system i.e. Windows,Linux,Mac,openBSD.This library can be used in `python , java , perl , ruby , C#` etc.
#Face Recognition with Python and openCV
<img src="https://thepracticaldev.s3.amazonaws.com/i/1h2qja0q40beil1p0wn7.jpg">
######Image Source: Wikipedia , url: https://commons.wikimedia.org/wiki/File:Face_detection.jpg

##Source Code :
###Database for Faces:
```Python
import cv2
import numpy as np
import sqlite3

faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam=cv2.VideoCapture(0);

def insertOrUpdate(Id,Name,Age,Gen,CR):
    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)
        cmd2="UPDATE People SET Age="+str(Age)+"WHERE ID="+str(Id)
        cmd3="UPDATE People SET Gender="+str(Gen)+"WHERE ID="+str(Id)
        cmd4="UPDATE People SET CR="+str(CR)+"WHERE ID="+str(Id)
    else:
        cmd="INSERT INTO People(ID,Name,Age,Gender,CR) Values("+str(Id)+","+str(Name)+","+str(Age)+","+str(Gen)+","+str(CR)+")"
        cmd2=""
        cmd3=""
        cmd4=""
    conn.execute(cmd)
    conn.execute(cmd2)
    conn.execute(cmd3)
    conn.execute(cmd4)
    conn.commit()
    conn.close()

Id=raw_input('Enter User Id')
name=raw_input('Enter User Name')
age=raw_input('Enter User Age')
gen=raw_input('Enter User Gender')
cr=raw_input('Enter User Criminal Records')
insertOrUpdate(Id,name,age,gen,cr)
sampleNum=0
while(True):
    ret,img=cam.read();
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces=faceDetect.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])
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        cv2.waitKey(100);
    cv2.imshow("Face",img);
    cv2.waitKey(1);
    if(sampleNum>20):
        break;
cam.release()
cv2.destroyAllWindows()
```
######Source: https://www.youtube.com/watch?v=1Jz24sVsLE4&list=PLnjEM1fs09cGGjdCLSue8Kw7GmWDhGlMh
This program asks for a person to provide a unique id , name , age , gender and criminal  records.Then it collects 20 picture of faces of the person and then converts it to `grayScale`.It saves all the data of that person in database or updates if existing.Here `cv2` is the openCV library and the database use is `sqlite3`.The <a href="http://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html">cascade classifier</a> `haarcascade_frontalface_default.xml` is used for face recognising system.

###face Trainer:
```Python
import os
import cv2
import numpy as np
from PIL import Image

recognizer=cv2.createLBPHFaceRecognizer();
path='dataSet'

def getImagesWithID(path):
    imagepaths=[os.path.join(path,f) for f in os.listdir(path)]
    faces=[]
    IDs=[]
    for imagepath in imagepaths:
        faceImg=Image.open(imagepath).convert('L');
        faceNp=np.array(faceImg,'uint8')
        ID=int(os.path.split(imagepath)[-1].split('.')[1])
        faces.append(faceNp)
        IDs.append(ID)
        cv2.imshow("training",faceNp)
        cv2.waitKey(10)
    return np.array(IDs),faces

IDs,faces=getImagesWithID(path)
recognizer.train(faces,IDs)
recognizer.save('recognizer/trainningData.yml')
cv2.destroyAllWindows()
```
######Source: https://www.youtube.com/watch?v=1Jz24sVsLE4&list=PLnjEM1fs09cGGjdCLSue8Kw7GmWDhGlMh
In this part the program trains the faces and saves the data in a `.yml` format.The used library `os` is predefined in python.The other two libraries `numpy` and `pillow` have to be installed manually.

###face Recognition:
```Python
import cv2
import numpy as np
import sqlite3

faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam=cv2.VideoCapture(0);
rec=cv2.createLBPHFaceRecognizer();
rec.load("recognizer/trainningData.yml")
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX,0.4,1,0,1)

def getProfile(id):
    conn=sqlite3.connect("FaceBase.db")
    cmd="SELECT * FROM People WHERE ID="+str(id)
    cursor=conn.execute(cmd)
    profile=None
    for row in cursor:
        profile=row
    conn.close()
    return profile

while(True):
    ret,img=cam.read();
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces=faceDetect.detectMultiScale(gray,1.3,5);
    for(x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        id,conf=rec.predict(gray[y:y+h,x:x+w])
        profile=getProfile(id)
        if(profile!=None):
            cv2.cv.PutText(cv2.cv.fromarray(img),"Name : "+str(profile[1]),(x,y+h+20),font,(0,255,0));
            cv2.cv.PutText(cv2.cv.fromarray(img),"Age : "+str(profile[2]),(x,y+h+45),font,(0,255,0));
            cv2.cv.PutText(cv2.cv.fromarray(img),"Gender : "+str(profile[3]),(x,y+h+70),font,(0,255,0));
            cv2.cv.PutText(cv2.cv.fromarray(img),"Criminal Records : "+str(profile[4]),(x,y+h+95),font,(0,0,255));
    cv2.imshow("Face",img);
    if(cv2.waitKey(1)==ord('q')):
        break;
cam.release()
cv2.destroyAllWindows()
```
######Source: https://www.youtube.com/watch?v=1Jz24sVsLE4&list=PLnjEM1fs09cGGjdCLSue8Kw7GmWDhGlMh
This part takes photos of faces,converts them to `grayScale` format,then it compares the photos with the photos from database.When match found it shows all the information on the screen.Without the trainer proper face recognition is not possible.
#Output:
<img src="https://thepracticaldev.s3.amazonaws.com/i/kozuueq5fg45b08sa6e9.png">
---
###My Thoughts
The face recognition system is far away from perfect.This is not nearly as good as a retina scan or so.And the main problem is it also sometimes misidentifies people.It also becomes less predictable to detect a face from a low resolution mage or footage.This thing only works form a certain angle,from a side view of a person the face detection fails most of the time.Even with some facial expression i.e. smile, face recognition may not be proper.


Though it's true that this system has all it's problems , it can be very useful in certain situations.When dealing with crowd , where direct contact with every person is not possible.In this case the more convenient ways like fingerprint or biometric scan is not possible.And another truth is that due to the use of face recognising system in surveillance camera crime in certain places , robbery in banks has been decreased .This technology if properly developed can be a very helpful one for mankind.