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.

About Admin_DS

Computer Engineer ! Admin of AIHUB (insta)

View all posts by Admin_DS →

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

  1. I used to be recommended this website by way of my cousin. I’m no longer positive whether or not this put up is written by
    means of him as nobody else know such precise approximately my trouble.
    You are incredible! Thanks!

  2. Simply want to say your article is as surprising. The clearness
    for your submit is simply spectacular and that i could suppose you’re a professional in this subject.
    Well along with your permission let me to grasp your RSS feed to
    keep up to date with impending post. Thanks one million and please keep up the
    enjoyable work.

  3. Hi there! I know this is somewhat off topic but I was wondering which
    blog platform are you using for this website? I’m getting fed up
    of WordPress because I’ve had issues with hackers and I’m looking at alternatives
    for another platform. I would be awesome if you
    could point me in the direction of a good platform.

  4. Hello there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly?
    My site looks weird when browsing from my iphone 4.
    I’m trying to find a theme or plugin that might be able to correct this problem.
    If you have any recommendations, please share. Thank you!

  5. Hello there! This is my first visit to your blog!
    We are a collection of volunteers and starting a new
    project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a wonderful job!

  6. Fantastic blog! Do you have any recommendations for aspiring writers?
    I’m hoping to start my own blog soon but I’m a little lost on everything.

    Would you suggest starting with a free platform
    like WordPress or go for a paid option? There are so many options out there that I’m completely confused
    .. Any recommendations? Thank you!

  7. you’re in point of fact a just right webmaster.
    The web site loading pace is incredible. It sort of feels that you are doing
    any distinctive trick. Moreover, The contents are
    masterwork. you have done a excellent activity on this matter!

  8. I believe what you published made a lot of sense. However, what about this? what if you added a little content? I mean, I don’t wish to tell you how to run your website, however what if you added something that makes people desire more? I mean BLOG_TITLE is kinda boring. You could glance at Yahoo’s front page and watch how they create post titles to get viewers interested. You might try adding a video or a picture or two to get readers interested about what you’ve got to say. Just my opinion, it might make your blog a little bit more interesting.|

  9. Today, while I was at work, my sister stole my iphone and tested to see if
    it can survive a twenty five foot drop, just so she can be a
    youtube sensation. My iPad is now broken and she has 83 views.
    I know this is totally off topic but I had to share it with someone!

  10. naturally like your website but you need to take a look
    at the spelling on quite a few of your posts. A number of them are rife with spelling issues and I find it very bothersome to inform the truth on the other hand I will surely come again again.

  11. 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

  12. 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..

  13. 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!

  14. 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

  15. 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!

  16. 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!

  17. 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!

  18. 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!

Leave a Reply

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