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
/home/docs/checkouts/readthedocs.org/user_builds/kornia-tutorials/envs/latest/lib/python3.7/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

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/842d9ea487a32ae31e18cad9e79b453a92215e0a7924c43fb0f3c5e4d9408ffa.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]
Downloading: "https://github.com/ShiqiYu/libfacedetection.train/raw/master/tasks/task1/weights/yunet_final.pth" to /home/docs/.cache/torch/hub/checkpoints/yunet_final.pth
  0%|          | 0.00/396k [00:00<?, ?B/s]
100%|██████████| 396k/396k [00:00<00:00, 11.3MB/s]

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/fe8dbfe88e142edca67e9b1488a5c00dbde8d45fbd2b4af758e7e6724878ecef.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')