top of page

Face recognition with RetinaFace

Updated: Sep 20, 2022




If you are looking for an algorithm for face detection, I might be able to help.

I built this program to be able to find faces in a video and blur them to protect privacy.


I used RetinaFace which was built by Sefik Ilkin Serengil. I would like to thank him for making this code available here. It works like a charm and is super easy to use!!


As usual, we start with our imports.



# import the necessary packages
from PIL import Image as im
from imutils.video import VideoStream
import numpy as np
import time, cv2, os
import matplotlib.pyplot as plt

#You might have to install RetinaFace or the line below might throw an error. On command line, run pip3 install retina-face [this is the command for python 3+]
from retinaface import RetinaFace

Then we identify the source of our video

#Initialize the video capture with the video 
vidcap = cv2.VideoCapture('myvideo.mp4')
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
#I want to save the output with the blurred faces, so I am going to create a writer
writer = cv2.VideoWriter("output.mp4", fourcc, 20.0,(int(vidcap.get(3)),int(vidcap.get(4))))

#if you want to run this on the output of a video camera, you can initialize as follows
#vidcap = cv2.VideoCapture(0)

Next, I define my blur function

#Blur function
def blurFace(image, factor=1.0):
     # automatically determine the size of the blurring kernel
     # based on the spatial dimensions of the input image
     (h, w) = image.shape[:2]
     kW = int(w / factor)
     kH = int(h / factor)
     # ensure the width of the kernel is odd
     if kW % 2 == 0:
          kW -= 1
     # ensure the height of the kernel is odd
     if kH % 2 == 0:
          kH -= 1
     # apply a Gaussian blur to the input image using our computed
     # kernel size
     return cv2.GaussianBlur(image, (kW, kH), 0)

Now we read frame by frame using vidcap, check if there is a face using retina face, and if yes, I call by blur function on that frame.


from retinaface.commons import postprocess
success = 1
# loop over the frames from the video stream
while success == 1:
    success,frame = vidcap.read()
    if frame is None:
        # If no frame found, go to the next one
        continue
    (h, w) = frame.shape[:2]	
    # pass the frame to the RetinaFace model to obtain the face detections
    faces = RetinaFace.detect_faces(frame)
    """
    This returns face data in the following format.
    {
    "face_1": {
        "score": 0.9993440508842468,
        "facial_area": [155, 81, 434, 443],
        "landmarks": {
          "right_eye": [257.82974, 209.64787],
          "left_eye": [374.93427, 251.78687],
          "nose": [303.4773, 299.91144],
          "mouth_right": [228.37329, 338.73193],
          "mouth_left": [320.21982, 374.58798]
           }
        }
     }
    """
    if len(faces)>0:
    # its possible that there is more than one face
    for key in faces:
        if len(key) >0:
            identity = faces[key]                                                  
            #identify the facial area
            facial_area = identity["facial_area"]
            face = frame[facial_area[1]: facial_area[3], facial_area[0]: facial_area[2]]
            # Call the blur function on this frame
            blurface = blurFace(face)
            # Update the frame with the blurred image
            frame[facial_area[1]: facial_area[3], facial_area[0]:                           facial_area[2]] = blurface
# Write the frame to the writer
writer.write(frame)
# do a bit of cleanup
writer.release()
vidcap.release()

That's it!

I hope this helps you. If you have any questions, please feel free to leave a comment!

This algorithm is based on this paper.







22 views0 comments

Recent Posts

See All

Comments


bottom of page