Face and eye detection using OpenCV.

Faiz Khan
Faiz Khan
Aug 7 · 4 min read

Introduction

You recently had a baby and you a super happy and excited about it. You take good care of the baby. You give good food, love and everything you possibly could. Most hectic work which I have seen is monitoring sleep cycle of your baby. What if I say you can write face and eye detection code and integrate it with an IOT device. Alot of your problem would be solved right. You can easily monitor your baby’s sleeping patterns and work accordingly. Every time your baby awakens, you will be informed.

Skeleton

Technique of detection

I have been using Haar-like features and cascade classifiers to detect face and eyes. Traditionally working with image intensities(using RGB) made the task of feature calculation computationally expensive. Then people started to talk about feature calculation based on Haar wavelets to reduce the cost of computation. Paul Voila and Michel Jones in 2001 used this concept and developed the Haar-like features. A Haar-like feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region and calculates the difference between these sums. This difference is used to categorize the subsections of an image. For e.g in human face images, the region of eyes is darker than cheeks. So when detecting an object in an image, the window of target size is moved over the input image and for each subsection of the image the Haar-like feature is calculated. This difference is then compared to the learned threshold that separates the object from non-objects. The Haar-like features are weak learners or classifiers so large number of Haar-like features are necessary to classify images with higher accuracy. This is because one simple image can contain 180, 000+ Haar-like minute features. That’s why in Haar-like features are organized into something called classifier cascade to form a strong learner or classifier. Due to the use of the integral image, the Haar-like features can be calculated at a very higher speed than the traditional approach.

OpenCV

OpenCV comes with a detector and trainer, for detecting eyes and faces we will be needing 2 XML files that have been predefined Haar-like features or classifiers. OpenCV already comes with this classifiers but one can build his/her own classifier to detect objects.

Conclusion

Media

https://github.com/backSpace001/poor_man_rekognition/blob/master/face_eye_detection_photo.ipynb

This is the link to my source code.

https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf

Original Paul Viola and Michael Jones paper.

Code

# import libraries of python OpenCV

# where its functionality resides

import cv2

import matplotlib.pyplot as plt

#read images from directories

img1 = cv2.imread(‘C:/Users/Faiz Khan/Desktop/ddd/1.jpg’,0)

img2 = cv2.imread(‘C:/Users/Faiz Khan/Desktop/ddd/2.jpg’,0)

img3 = cv2.imread(‘C:/Users/Faiz Khan/Desktop/ddd/3.jpg’,0)

img4 = cv2.imread(‘C:/Users/Faiz Khan/Desktop/ddd/4.jpg’,0)

# Trained XML classifiers describes some features of some

# object we want to detect a cascade function is trained

# from a lot of positive(faces) and negative(non-faces)

# images.

face_cascade = cv2.CascadeClassifier(‘F:/dd/Library/etc/haarcascades/haarcascade_frontalface_alt2.xml’)

def detect_face(img):

face_img = img.copy()

face_rects = face_cascade.detectMultiScale(face_img)

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

cv2.rectangle(face_img,(x,y),(x+w,y+h),(255,255,255),5)

return face_img

#perform facial detection

result1 = detect_face(img2)

plt.imshow(result1)

ELI5

Here I have performed Face and Eye detection using Haar-like features and cascade classifiers. Basically what I am doing is applying some filters like in Instagram, Snapchat or like any other social media platform for more clear and vibrant image. After applying filters I am running a set of rules and regulations which is written in computer language and find out if a face or eye is present in an image. If the answer is yes then computer will automatically save the cropped image.

About the author

I am Md. Faizullah Khan, an ETC 6th semester candidate at International Institute of Information and Technology, Bhubaneswar. I am an entrepreneur, coder, gamer, blogger and digital marketer. I have a fairly decent amount of knowledge and experience in C, C++, Python, HTML, CSS, Git, JAVA, machine learning, OpenCV, Neural Networks and Javascript.

Connect me on: LinkedIn, Github

Some of Project : —

1. This is a portable real estate management system implemented in C++. I Have used Data file handling to manage the data. Data is stored in CSV Format for easy portability.

[Link] This was a group project.

2. Red Signal Alerting for car using Wireless Communication

[Link]

3. Google Summer of Code 2019

[Link]

Sayak is a Data Science Instructor at DataCamp where he develops projects that depict real world problems. He goes by the motto of understanding complex things and help people understand them as easily as possible. Sayak is an extensive blogger and all of his blogs can be found here. He is also working with his friends on the application of deep learning in Phonocardiogram classification

    Faiz Khan

    Written by

    Faiz Khan

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade