FACE DETECTION IN 11 LINES OF CODE – AI PROJECTS

face detection ai

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.

SIMILAR PROJECT

graspcoding

About Diwas

🚀 I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! 🌍🤖🔬

View all posts by Diwas →

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

  1. Thank you for any other magnificent post. The place else may just anyone
    get that type of info in such a perfect manner of writing?

  2. Pingback: sildenafil
  3. Pingback: cialis
  4. Hello there, simply become aware of your blog through Google, and located that it’s truly informative. I’m going to be careful for brussels. I’ll appreciate in case you proceed this in future. A lot of other folks shall be benefited out of your writing. Cheers!

  5. Pingback: cialis
  6. Pingback: Marcus
  7. Heya i am for the primary time here. I came across this board and I in finding It really useful & it helped me out much. I hope to provide something back and aid others like you helped me.

  8. I am really impressed with your writing skills and also with the layout on your weblog.
    it is rare to see a great blog like this one nowadays.

  9. Wow! Thank you! I permanently wanted to write on my site something like that. Can I include a part of your post to my site?

  10. I have been absent for a while, but now I remember why I used to love this website. Thanks , I will try and check back more frequently. How frequently you update your web site?

  11. Great weblog right here! Also your web site lots up very fast! What host are you using? Can I am getting your affiliate link in your host? I want my website loaded up as fast as yours lol

  12. Hello there! I know this is kind of off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having trouble
    finding one? Thanks a lot!

  13. I’ve been surfing online more than 2 hours today, yet I never found any interesting article
    like yours. It is pretty worth enough for me. Personally, if
    all site owners and bloggers made good content as you did, the web
    will be much more useful than ever before.

  14. You are so awesome! I do not believe I’ve truly read through something like this before.

    So good to discover another person with a few genuine thoughts on this subject matter.
    Really.. many thanks for starting this up. This site is
    something that is needed on the web, someone with a bit
    of originality!

  15. Woah! I’m really loving the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance”
    between superb usability and visual appearance.
    I must say that you’ve done a very good job with this.
    In addition, the blog loads extremely fast for me on Firefox.
    Exceptional Blog!

  16. Unquestionably consider that which you stated.
    Your favorite justification seemed to be on the web
    the easiest thing to be mindful of. I say to you, I certainly get irked at the
    same time as other folks consider issues that they plainly don’t understand about.
    You controlled to hit the nail upon the top as neatly as defined out the whole thing with no need side effect
    , other people could take a signal. Will probably be back to get more.
    Thank you adreamoftrains best web hosting sites

  17. Hmm it seems like your blog ate my first comment (it was extremely long) so
    I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any recommendations for newbie blog writers? I’d certainly
    appreciate it.

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

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

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

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

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

  23. I was suggested this web site by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my trouble. You are incredible! Thanks!|

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

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

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

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

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

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

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

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

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

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

  34. Terrific article! That is the kind of info that are meant to be shared
    around the internet. Disgrace on the search engines for no longer
    positioning this put up upper! Come on over and talk over with my website .
    Thank you =)

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

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

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

  38. It’s in reality a nice and helpful piece of information. I’m glad that you just shared this useful information with us. Please stay us up to date like this. Thank you for sharing.|

  39. I have learn some excellent stuff here. Certainly value bookmarking for revisiting. I wonder how so much effort you set to create the sort of wonderful informative site.

Leave a Reply

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