Open in Colab

Face Detection and blurring

In this tutorials we will show how to use

%%capture
!pip install git+https://github.com/kornia/kornia
%%capture
!wget -O image.jpg https://www.reader.gr/sites/default/files/styles/schema_cover_1_1/public/2021-09/squid-game-3.jpg

Import the needed libraries

import cv2
import numpy as np
import matplotlib.pyplot as plt

import torch
import kornia as K
from kornia.contrib import FaceDetector, FaceDetectorResult

# select the device and type
device = torch.device('cpu')  # use 'cuda:0'
dtype = torch.float32
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/tmp/ipykernel_495/252690.py in <module>
      5 import torch
      6 import kornia as K
----> 7 from kornia.contrib import FaceDetector, FaceDetectorResult
      8 
      9 # select the device and type

ImportError: cannot import name 'FaceDetector' from 'kornia.contrib' (/home/docs/checkouts/readthedocs.org/user_builds/kornia-tutorials/envs/latest/lib/python3.7/site-packages/kornia/contrib/__init__.py)

Read the image and convert to a torch.Tensor

# load the image and scale (if needed)
img_raw = cv2.imread('image.jpg', cv2.IMREAD_COLOR)

# preprocess
img = K.image_to_tensor(img_raw, keepdim=False).to(device, dtype)
img = K.color.bgr_to_rgb(img)

img_vis = K.tensor_to_image(img.byte().clone())  # to later visualise
plt.figure(figsize=(8, 8))
plt.imshow(K.tensor_to_image(img.byte()))
plt.axis('off')
(-0.5, 1199.5, 1199.5, -0.5)
_images/face_detection_7_1.png

Create the FaceDetector object and apply to the image

# create the detector and find the faces !
face_detection = FaceDetector().to(device, dtype)

with torch.no_grad():
    dets = face_detection(img)

# to decode later the detections
dets = [FaceDetectorResult(o) for o in dets]

Create a function to crop the faces from the original image and apply blurring using the gaussian_blurd2d operator.

Alternatively, explore other blur operator in kornia.filters.

# blurring paramters
k: int = 21     # kernel_size
s: float = 35.  # sigma

def apply_blur_face(img: torch.Tensor, img_vis: np.ndarray, det: FaceDetectorResult):
    # crop the face
    x1, y1 = det.xmin.int(), det.ymin.int()
    x2, y2 = det.xmax.int(), det.ymax.int()
    roi = img[..., y1:y2, x1:x2]

    # apply blurring and put back to the visualisation image
    roi = K.filters.gaussian_blur2d(roi, (k, k), (s, s))
    roi = K.color.rgb_to_bgr(roi)
    img_vis[y1:y2, x1:x2] = K.tensor_to_image(roi)

Let draw the detections and save/visualize the image

for b in dets:
    if b.score < 0.6:  # skip detections with lower score
        continue
    
    # draw face bounding box around each detected face
    x1, y1 = b.top_left.int().tolist()
    x2, y2 = b.bottom_right.int().tolist()
    img_vis = cv2.rectangle(img_vis, (x1, y1), (x2, y2), (0, 255, 0), 4)

    # blur the detected faces
    apply_blur_face(img, img_vis, b)

plt.figure(figsize=(8, 8))
plt.imshow(img_vis)
plt.axis('off')

# save and show image
cv2.imwrite('img_out.jpg', img_vis)
True
_images/face_detection_13_1.png

Play with the Real Time Demo

You can achieve 60 FPS in CPU using a standard WebCam.

See: https://github.com/kornia/kornia/blob/master/examples/face_detection/main_video.py

from IPython.display import YouTubeVideo
YouTubeVideo('hzQroGp5FSQ')