How to get the exact count of people by face detection in python(opencv)?

asked 2018-03-19 13:38:40 -0500

mueez gravatar image

I am working on getting the total count of people along with gender in a sample video or webcam.

Gender identification is working well but in counting it will show the count of people in that frame but is again reset to 0.

How do I get the count accurately(i.e, count must not increment when the same person appears more than once)

Like comparing presently detected face and check if it's unique or not and then increment (Can it be done ?How to do it?)

Or by extracting all the detected faces to a folder can continuous similarity checking be done on those extracted faces and increment when new face is scanned?


Is there any other way by which I can get this?

This is the function which gives count, please help with the modifications for total count with unique faces detected.

def start_webcam(model_gender, window_size, window_name='live', update_time=50):
cv2.namedWindow(window_name, WINDOW_NORMAL)
if window_size:
    width, height = window_size
    cv2.resizeWindow(window_name, width, height)

video_feed = cv2.VideoCapture(0)
video_feed.set(3, width)
video_feed.set(4, height)
read_value, webcam_image =

delay = 0
init = True
while read_value:
    read_value, webcam_image =
    faces = face_cascade.detectMultiScale(webcam_image)
    for normalized_face, (x, y, w, h) in find_faces(webcam_image):
      if init or delay == 0:
        init = False
        gender_prediction = model_gender.predict(normalized_face)
      if (gender_prediction[0] == 0):
          cv2.rectangle(webcam_image, (x,y), (x+w, y+h), (0,0,255), 2)
          cv2.putText(webcam_image, 'female', (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
          cv2.rectangle(webcam_image, (x,y), (x+w, y+h), (255,0,0), 2)
          cv2.putText(webcam_image, 'male', (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,0,0), 2)

    delay += 1
    delay %= 20

    cv2.putText(webcam_image, "Number of faces detected: " + str(len(faces)), (0,webcam_image.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.7,  (255,255,255), 1)
    cv2.imshow(window_name, webcam_image)
    key = cv2.waitKey(update_time)
    if key == ESC:

edit retag flag offensive close merge delete


If you want to count unique persons than you should

  • Detect people through there face
  • Make a descriptor of their face and store those
  • For each new detection, see if there is a close match with the existing database
  • If not, add one to counter and add face to database

But we will not provide you with the complete code for that.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-03-20 05:56:50 -0500 )edit

@StevenPuttemans , you just rephrased my question and commented as an answer! No need of providing the complete code (I never mentioned asking for it, you are just exaggerating. All I asked was for modification or code snippet). How can we expect someone to give the code when he himself has no idea!. Well, it would have been much better and nicer on your part if you hadn't even commented.

mueez gravatar imagemueez ( 2018-03-20 06:47:14 -0500 )edit

Wait wut? People these days ... As far as I see there is not a single line of code up here that works un the reidentifaction task. You can have a look at the MACE filters, right here which could be exactly what you need.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-03-20 07:04:30 -0500 )edit

okay. Thanks , anyways.

mueez gravatar imagemueez ( 2018-03-20 09:15:37 -0500 )edit

i also want to source code as you ? Who can help me ? please gravatar image[email protected] ( 2018-08-31 05:11:30 -0500 )edit

^^ please do not post an answer here, if you have a question or comment, thank you.

berak gravatar imageberak ( 2018-08-31 05:17:04 -0500 )edit