Hey!!!  Are you tired of counting faces in photos of your wedding/class party/ birthday program??? Must be “Yes” 😉 😉 😉

So, Here is the solution for you.

we are using opencv-python for detecting the number of faces. The cascade classifier ‘haarcascade_frontalface_default.xml’ will detect faces in the photo.


credit: www.classpictures.co.uk

Python Code:

import cv2
face_cascade = cv2.CascadeClassifier('C:\Users\Desktop\Python\
haarcascade_frontalface_default.xml')
img = cv2.imread('C:\Users\Desktop\Python\image.JPG')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
count_faces=str(len(faces))
print "number of face(s)= " + count_faces

 Discussion:

in this code we have to import opencv by using

import cv2

directory of the xml file (frontal face) should be written correctly..  In this code cv2.CascadeClassifier is using the XML file by,

face_cascade = cv2.CascadeClassifier('C:\Users\Desktop\Python\
haarcascade_frontalface_default.xml')

Also the image directory should be written in this manner,

img = cv2.imread('C:\Users\Desktop\Python\image.JPG')

Output:

Output should be like this,

 

1 Comment

  • https://routeripaddress.site

    May 24, 2019 - 8:35 pm

    Definitely believe that which you said. Your favorite reason appeared to be on the web the easiest thing to be aware of.
    I say to you, I definitely get irked while people consider worries that they plainly don’t know
    about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people could take
    a signal. Will probably be back to get more. Thanks

Leave a Reply

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