FACE DETECTION IN 11 LINES OF CODE – AI PROJECTS

OpenCV (Open source computer vision) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel). The library is cross-platform and free for use under the open-source BSD license.

The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc.

In this tutorial we are developing a facial recognition on a photo using Haar Cascades.

So Lets get started

OpenCV comes with a trainer as well as detector. If you want to train your own classifier for any object like car, planes etc. you can use OpenCV to create one. Here we will deal with detection. OpenCV already contains many pre-trained classifiers for face, eyes,smiles, etc. Those XML files are stored in the opencv/data/haarcascades/ folder. Let’s create a face and eye detector with OpenCV.

INSTALLATION GUIDE

1. run pip install opencv-python if you need only main modules
2. run pip install opencv-contrib-python if you need both main and contrib modules (check extra modules listing from OpenCV documentation)
3. Create a new file as face.py and open it in your choice of editor.
4. And Type or paste following Code as save it.

FACE DETECTION CODE

import cv2
import sys
image=sys.argv[1]
face_cascade =cv2.CascadeClassifier(‘data/haarcascade_frontalface_default.xml’)
img = cv2.imread(image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow(‘img’, img)
cv2.waitKey()

Thats it guys try it with various images.

since this tutorial was short with no description on how the commands works. A detailed tutorial might come very soon.

Next time we will return for live facial detection with Webcam.

All required resources available in github below. You can download and simply run too.

18 Comments on “FACE DETECTION IN 11 LINES OF CODE – AI PROJECTS”

  1. First of all I would like to say excellent blog! I had a quick question in which
    I’d like to ask if you do not mind. I was curious to
    find out how you center yourself and clear your thoughts before writing.
    I’ve had difficulty clearing my thoughts in getting my ideas out there.
    I do take pleasure in writing but it just seems
    like the first 10 to 15 minutes are generally lost just trying to figure out
    how to begin. Any ideas or tips? Cheers!

  2. What i don’t understood is in reality how you are no longer actually much more neatly-liked than you might be right now. You’re so intelligent. You already know thus considerably on the subject of this matter, produced me individually believe it from a lot of numerous angles. Its like men and women aren’t fascinated except it is one thing to do with Lady gaga! Your personal stuffs great. All the time deal with it up!

  3. Hi there! Quick question that’s entirely off topic. Do you know how
    to make your site mobile friendly? My website looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might be able to correct this problem.
    If you have any recommendations, please share. With thanks!

  4. Hello there! This article could not be written much better!
    Looking through this article reminds me of my previous
    roommate! He continually kept talking about this. I most certainly will send this article to him.
    Pretty sure he’s going to have a good read. Thank you for sharing!

  5. Great beat ! I would like to apprentice while you amend your site,
    how can i subscribe for a blog web site? The account
    helped me a acceptable deal. I had been a little bit acquainted
    of this your broadcast provided bright clear idea

  6. I’m really enjoying the design and layout of
    your site. It’s a very easy on the eyes which makes it
    much more enjoyable for me to come here and visit
    more often. Did you hire out a designer to create your theme?

    Great work!

  7. I am really impressed together with your writing abilities and
    also with the structure on your blog. Is that this a paid theme
    or did you customize it yourself? Either way stay up the excellent quality writing, it’s rare
    to peer a nice weblog like this one nowadays..

  8. Undeniably believe that which you said. Your favorite reason seemed to be on the
    web the simplest thing to be aware of. I
    say to you, I definitely get annoyed while people think about worries that they just
    don’t know about. You managed to hit the nail upon the top as well as
    defined out the whole thing without having side effect , people could take a
    signal. Will likely be back to get more.
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *