Face Detection+recognition

53,440

145

42

Introduction: Face Detection+recognition

About: I am an Engineer in the field of Embedded system & Robotics.

This is a simple example of running face detection and recognition with OpenCV from a camera.

NOTE: I MADE THIS PROJECT FOR SENSOR CONTEST AND I USED CAMERA AS A SENSOR TO TRACK AND RECOGNITION FACES.

So, Our Goal

In this session,



1. Install Anaconda
2. Download Open CV Package
3. Set Environmental Variables
4. Test to confirm
5. Make code for face detection
6. Make code to create data set
7. Make code to train the recognizer
8. Make code to recognize the faces &Result.


Step 1: Install Anaconda

Anaconda is essentially a nicely packaged Python IDE that is shipped with tons of useful packages, such as NumPy, Pandas, IPython Notebook, etc. It seems to be recommended everywhere in the scientific community. Check out Anaconda to get it installed.

Step 2: Download Open CV Package

Firstly, go to the official OpenCV site to download the complete OpenCV package. Pick a version you like (2.x or 3.x). I am on Python 2.x and OpenCV 2.x - mainly because this is how the OpenCV-Python Tutorials are setup/based on.

In my case, I've extracted the package (essentially a folder) straight to my F drive. (F:\opencv).

Step 3: Set Environmental Variables

Copy and Paste the cv2.pyd file

The Anaconda Site-packages directory (e.g. F:\Program Files\Anaconda2\Lib\site-packages in my case) contains the Python packages that you may import. Our goal is to copy and paste the cv2.pyd file to this directory (so that we can use the import cv2 in our Python codes.).

To do this, copy the cv2.pyd file...

From this OpenCV directory (the beginning part might be slightly different on your machine):

# Python 2.7 and 64-bit machine: F:\opencv\build\python\2.7\x64
# Python 2.7 and 32-bit machine: F:\opencv\build\python\2.7\x84

To this Anaconda directory (the beginning part might be slightly different on your machine):

 F:\Program Files\Anaconda2\Lib\site-packages

After performing this step we shall now be able to use import cv2 in Python code. BUT, we still need to do a little bit more work to get FFMPEG (video codec) to work (to enable us to do things like processing videos.)

Right-click on "My Computer" (or "This PC" on Windows 8.1) -> left-click Properties -> left-click "Advanced" tab -> left-click "Environment Variables..." button.
Add a new User Variable to point to the OpenCV (either x86 for 32-bit system or x64 for 64-bit system.) I am currently on a 64-bit machine.

32-bitOPENCV_DIRC:\opencv\build\x86\vc12

64-bitOPENCV_DIRC:\opencv\build\x64\vc12

Append %OPENCV_DIR%\bin to the User Variable PATH.

For example, my PATH user variable looks like this...

Before:

F:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts 

After:

F:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts;%OPENCV_DIR%\bin

This is it we are done! FFMPEG is ready to be used!

Step 4: Test to Confirm

We need to test whether we can now do these in Anaconda (via Spyder IDE):

  • Import OpenCV package
  • Use the FFMPEG utility (to read/write/process videos)

Test 1: Can we import OpenCV?

To confrim that Anaconda is now able to import the OpenCV-Python package (namely, cv2),

issue these in the IPython Console:

import cv2

print cv2.__version__

If the package cv2 is imported ok with no errors, and the cv2 version is printed out, then we are all good!

Test 2: Can we Use the FFMPEG codec?

Place a sample

 input_video.mp4 

video file in a directory. We want to test whether we can:

  • read this .mp4 video file, and
  • write out a new video file (can be .avi or .mp4 etc.)

To do this we need to have a test python code, call it test.py. Place it in the same directory as the sample

input_video.mp4 

file.

This is what

test.py 

may look like (Note: many thanks to Pete's and Warren's suggestions in the comment field - I have replaced my original test code with his - please test it yourself and let us know if this works better):

import cv2

cap = cv2.VideoCapture("input_video.mp4")

print cap.isOpened()   # True = read video successfully. False - fail to read video.

fourcc = cv2.VideoWriter_fourcc(*'XVID')

out = cv2.VideoWriter("output_video.avi", fourcc, 20.0, (640, 360))

print out.isOpened()  # True = write out video successfully. False - fail to write out video.

cap.release()

out.release()

This test is VERY IMPORTANT. If you'd like to process video files, you'd need to ensure that Anaconda / Spyder IDE can use the FFMPEG (video codec). It took me days to have got it working. But I hope it would take you much less time! :)
Note: one more very important tip when using the Anaconda Spyder IDE. Make sure you check the Current Working Directory (CWD)!!!

Step 5: Make Code for Face Detection

Goal

In this session,

  • We will see the basics of face detection using Haar Feature-based Cascade Classifiers
  • We will extend the same for eye detection etc

Haar-cascade Detection in OpenCV

Here we will deal with detection. OpenCV already contains many pre-trained classifiers for face, eyes, smile etc. Those XML files are stored in opencv/data/haarcascades/ folder. Let's create face and eye detector with OpenCV.
First we need to load the required XML classifiers. Then load our input image (or video) in grayscale mode OR we can use camera( for Real time face detection)

import numpy as np

import cv2

face_cascade = cv2.CascadeClassifier('F:/Program Files/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')

eye_cascade = cv2.CascadeClassifier('F:/Program Files/opencv/sources/data/haarcascades/haarcascade_eye.xml')

cap = cv2.VideoCapture(0)<br>while 1:

    ret, img = cap.read()

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.5, 5)

    for (x,y,w,h) in faces:

        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        roi_gray = gray[y:y+h, x:x+w]

        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)

        for (ex,ey,ew,eh) in eyes:

            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    print "found " +str(len(faces)) +" face(s)"

    cv2.imshow('img',img)

    k = cv2.waitKey(30) & 0xff

    if k == 27:

        break

cap.release()

cv2.destroyAllWindows()

Step 6: Make Code to Create Data Set

We are doing face recognition, so you’ll need some face images! You can either create your own dataset or start with one of the available face databases, http://face-rec.org/databases/ gives you an up-to-date overview. Three interesting databases are (parts of the description are quoted from http://face-rec.org):

  • AT&T Facedatabase
  • Yale Facedatabase A
  • Extended Yale Facedatabase B

HERE i m using my own dataset....with the help of code which is given below:

import numpy as np

import cv2

face_cascade = cv2.CascadeClassifier('F:/Program Files/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)

id = raw_input('enter user id')

sampleN=0;

while 1:

    ret, img = cap.read()

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:

        sampleN=sampleN+1;

        cv2.imwrite("F:/Program Files/projects/face_rec/facesData/User."+str(id)+ "." +str(sampleN)+ ".jpg", gray[y:y+h, x:x+w])

        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        cv2.waitKey(100)

    cv2.imshow('img',img)

    cv2.waitKey(1)

    if sampleN > 20:

        break

cap.release()

cv2.destroyAllWindows()

Step 7: Make Code to Train the Recognizer

Create the function to prepare the training set

Now, we will define a function

getImagesWithID(path)

that takes the absolute path to the image database as input argument and returns tuple of 2 list, one containing the detected faces and the other containing the corresponding label for that face. For example, if the ith index in the list of faces represents the 5th individual in the database, then the corresponding ith location in the list of labels has value equal to 5.

Now convert the dataset faces(which is created in step 6) into .yml file with the help of code which is given below:

import os

import numpy as np

import cv2

from PIL import Image # For face recognition we will the the LBPH Face Recognizer 

recognizer = cv2.createLBPHFaceRecognizer();

path="F:/Program Files/projects/face_rec/facesData"

def getImagesWithID(path):

    imagePaths = [os.path.join(path, f) for f in os.listdir(path)]   

 # print image_path   

 #getImagesWithID(path)

    faces = []

    IDs = []

    for imagePath in imagePaths:      

  # Read the image and convert to grayscale

        facesImg = Image.open(imagePath).convert('L')

        faceNP = np.array(facesImg, 'uint8')

        # Get the label of the image

        ID= int(os.path.split(imagePath)[-1].split(".")[1])

         # Detect the face in the image

        faces.append(faceNP)

        IDs.append(ID)

        cv2.imshow("Adding faces for traning",faceNP)

        cv2.waitKey(10)

    return np.array(IDs), faces

Ids,faces  = getImagesWithID(path)

recognizer.train(faces,Ids)

recognizer.save("F:/Program Files/projects/face_rec/faceREC/trainingdata.yml")

cv2.destroyAllWindows()

by using this code all face dataset converted into a single .yml file.....path location is ("F:/Program Files/projects/face_rec/faceREC/trainingdata.yml")

Step 8: Make Code to Recognize the Faces & Result

Guyzz this is the final step in which we can create the code to recognize the faces with the help of your webcam

IN THIS STEP THERE ARE TWO OPERATIONS WHICH ARE GOING TO PERFORME....
1. capturing the video from cam
2. compare it with your .yml file


import numpy as np
import cv2 face_cascade = cv2.CascadeClassifier('F:/Program Files/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) rec = cv2.createLBPHFaceRecognizer(); rec.load("F:/Program Files/projects/face_rec/faceREC/trainingdata.yml") id=0 font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,5,1,0,4) while 1: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.5, 5) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) id,conf=rec.predict(gray[y:y+h,x:x+w]) if(id==2): id="alok" if id==1: id="alok" if id==3: id="anjali" if id==4: id="Gaurav" if id==5: id='rahul' if id==6: id="akshay" cv2.cv.PutText(cv2.cv.fromarray(img),str(id),(x,y+h),font,255) cv2.imshow('img',img) if cv2.waitKey(1) == ord('q'): break cap.release()

cv2.destroyAllWindows()



and finally result will came in front off your eyes......

u can also download the zip file from below the link :

Click here to download the codes

So, in this instructable we performed the task of face detection+recognition using OpenCV.....if you like this instructable..... plzzz subscribe me and vote for me .....thanks friends :)

Sensors Contest 2017

Participated in the
Sensors Contest 2017

1 Person Made This Project!

Recommendations

  • "Can't Touch This" Family Contest

    "Can't Touch This" Family Contest
  • CNC Contest 2020

    CNC Contest 2020
  • Robots Contest

    Robots Contest

42 Discussions

0
fizzat74
fizzat74

Question 5 weeks ago

Hi, I've been getting the PermissionError: [Errno 13] Permission denied: 'C:\\Users\\User\\opencv\\face_rec\\user_data\\user' when I try to convert the dataset to yml file. Please help.

0
Vandana goud
Vandana goud

Question 6 weeks ago

I have python 3.7 IDLE app is it ok for this project

0
abbasids_15
abbasids_15

2 months ago

i want to build a attendence system.let suppose i have initially 100 students and i trained the system.
now if a new student come, i have to train the system again with 101 students.
is there any way that i can train system with only the newcommers.
thanks in advance

0
MatloobA
MatloobA

Question 2 years ago

I'm using same concept to detect and recognize face I need a little help from you I'm using opencv and python 3 for my work.

Please answer to my questions asap.


Q no 1:when I try to detect a face that is not in my dataset or training data then it predicts me with false result?

Q no 2:I also wanted to know weather the image contains face or not?
Thanks!

0
robopathshala
robopathshala

Answer 2 years ago

1: yes it will give u false result and u can use else : print("unknown person")

2:yes, image contains faces..

0
ASHOKSAM
ASHOKSAM

9 months ago

Hi
I am working on a face recognition project and i need to save the predicted data (result) which is available from the video recognition.Could you please tell (or) explain me how to do that?
Thanks in advance

0
AnuragM18
AnuragM18

2 years ago

need help,

everything is working fine so far, just wanted to ask how can I save the data of those whose face is getting recognized into an excel sheet or anywhere

Thank-you

0
AnuragM18
AnuragM18

Reply 2 years ago

I am just trying to make the attendance marking project

0
ASHOKSAM
ASHOKSAM

Reply 9 months ago

Hi Anurag
Did you get answer for your post.If yes,can you please explain me

0
robopathshala
robopathshala

Reply 2 years ago

You have to read about file concept like how to open,close or append file by using python...

0
arbindjha12
arbindjha12

Question 1 year ago

I'am also creating my own multi-face-recognizer with my own dataset. On execution, it does'nt raise any error or exception but even when the LBPH Face Recognizer has been trained with two different faces, it recognizes two diffferent people with the same name (name being the last person the model was trained with). BTW I'am using python, haar cascade for face detection, cv2 for image operations, LBPH Face Recognizer for the training model and my own dataset.

0
ermias123
ermias123

1 year ago on Introduction

what about in java..i using java and webcam-capture jar..so what shall i do to make face detection

0
Nadeem Ansari01
Nadeem Ansari01

1 year ago on Step 7

attributeError: module 'cv2.cv2' has no attributes 'createLBPHFaceRecognizer'

accouring this problem
Hoe to solve this problem plz help me, Sir

IMG_20190403_175910.jpg
0
dpdeepika219
dpdeepika219

Reply 1 year ago

try this, if it helps
recognizer=cv2.face.LBPHFaceRecognizer_create()

0
educatedbird
educatedbird

1 year ago on Step 7

nameerror name getimageswithid is not defined python 2.7

0
BHARATHI RAJA T
BHARATHI RAJA T

Question 1 year ago

Why did you use haarcascade and LBPH?
Why not other algorithms?

0
BhushanJ5
BhushanJ5

1 year ago

why u have redirected to wrong site if u don't want to give code then don't make fool

0
Manupatip
Manupatip

Question 1 year ago

Hi there I have few questions regarding programming in python

1. How to code python program for fingerprint " For student attendance record"

2. What are the requirements to built a fingerprint system for college having around 500 students

0
musaibm1
musaibm1

Tip 1 year ago on Step 6

For the error i questioned was...

.write only input instead of raw_input