Implement a blink counter in the face detection

Asked

Viewed 202 times

4

As q do to display the amount of flashes of people detected at the time. The code below detects face with Haar Cascade, where it displays a face counter.

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!")
    text = "{} face(s) Encontrada(s)".format(len(faces))
    cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
        0.5, (0, 0, 255), 2)
    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()
  • 2

    Do you have a dataset that has flashing images of people? You would have to retrain this algorithm in these images, so you could detect the blinks

1 answer

3

One way you can solve your problem is by looking for a facial landmarks. You need to keep track of eye markings and calculate eye closure based on distance of markings.

Below is a 2D representation of facial markings (facial landmarks). Considering them on a Cartesian plane, we can measure the distance from point 40 to point 37 to achieve the width of the person’s eye. Once this is done, we also calculate the distances of points 38 and 39 in relation to points 42 and 41, respectively, to achieve the height of the person’s eye.

In this way, we can compare frame by frame the height and width of the eyes and identify typical changes of a blink in this ratio of magnitude.

Facial landmark

There are libraries ready for you to work with facial landmarks without having to reinvent the wheel. The website pyimagesearch explains all that I have said and gives some examples of code. It can be a starting point for you to start working with a solution with facial landmarks.

Browser other questions tagged

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