Search code examples
pythonopencvwebcamface-detection

Count people in realtime video


Im using the following code to count the number of people in realtime webcam from morning till night

people_list = []

while True:
    _, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(gray, 1.3, 5)

    detections = faceCascade.detectMultiScale(gray, 1.15, 5)

    for i in range(len(detections)):
        face_i = detections[i]
        x, y, w, h = face_i

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 222, 0), 1)
        font = cv2.FONT_HERSHEY_SIMPLEX
        people_list.insert(len(people_list)+1,i)

        cv2.putText(frame, "id: "+str ( people_list[i]), (x, y), font, 2, (255, 255, 255), 2, cv2.LINE_AA)

    # Display the resulting frame
    cv2.imshow('Video', frame)

everytime when a new face is detected, the people_list count increases. However, the people_list count is being increased for every frame instead of every new faces. How can I be able to sort this out?


Solution

  • No need for a list...

    First of all, as you have no IDs for the people, storing the people seen in a list is pointless so you should just use a variable storing an int:

    people_count = 0
    

    and then instead of this:

    people_list.insert(len(people_list)+1,i)
    

    you need to check if the number of people in the current frame is greater than the number of people in the last frame and if it is then increment the people_count by the number of people in the current frame - the number of people in the last frame. So if there were 4 people last frame and there are 6 this frame then increment by 2.

    So instead of the line above, do something like:

    if len(detections) > last_count:
       people_count += len(detections - last_count)
    
    last_count = len(detection)
    

    This should work for you assuming you declare last_count as 0 at the start of your code...

    However saying this, as mentioned in the comments, if you cannot uniquely identify the faces, then there will be a significant floor in your program.

    The floor is that if person A enters the room, people_count will increment and then if person B enters as well, people_count will now be 2. If person A leaves now it remains at 2 (as there have been 2 people). But now if person A returns, it will raise the count to 3 which is wrong as you have seen only 2 people.

    In addition, if you miss a face for just one frame, then the count will increment as it will take this as that person left and a new person came into the room.

    p.s as a sidenote, when adding to the end of the list, you shouldn't use .insert(len(lst), val), instead you should use .append(val) as this is much neater :)