How to create a counter in face detection?

Asked

Viewed 434 times

3

As you can see in the code below, it only detects the faces with Haar Cascade, I would like to know how to display on the webcam the amount of people detected at the time.

from  __future__ import print_function #importa a funcao da biblioteca future
import cv2 
cap = cv2.VideoCapture(0) #webcam

 face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

while (cap.isOpened()):
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #faz a conversao pra cinza por ser mais leve pro pc
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, flags=cv2.CASCADE_SCALE_IMAGE,minSize=(50, 50), maxSize=None)

if len(faces) > 0:
    print("Pessoa detectada!")
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x - 10, y - 20), (x + w + 10, y + h + 10), (0, 255, 0), 2)
        roi_gray = frame[y-15:y + h+10, x-10:x + w+10]

    cv2.imshow("imagem", frame) #mostra a face detectada
if cv2.waitKey(1) & 0xFF == ord('q'): # q definido para sair do projeto
    break #para o programa

cap.release() #mostra as coordenadas da deteccao
cv2.destroyAllWindows()
  • By name, len(faces) is already the amount of people detected, so I did not understand exactly what the doubt is.

  • @Andersoncarloswoss For example, will detect and show in a corner of the screen the amount of people q were detected: x detected people

2 answers

1


I believe this is what you’re looking for...

text = "{} face(s) found".format(len(faces))
cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
    0.5, (0, 0, 255), 2)

0

print('Total de Pessoas: {}'.format(len(faces)))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.