Skip to content

Run this notebook online:Binder

Face Mask Detection using PaddlePaddle

In this tutorial, we will be using pretrained PaddlePaddle model from PaddleHub to do mask detection on the sample image. To complete this procedure, there are two steps needs to be done:

  • Recognize face on the image (no maater wearing mask or not) using Face object detection model
  • classify the face is wearing mask or not

These two steps will involve two paddle models. We will implement the corresponding preprocess and postprocess logic to it.

Import dependencies and classes

PaddlePaddle is one of the Deep Engines that requires DJL hybrid mode to run inference. Itself does not contains NDArray operations and needs a supplemental DL framework to help with that. So we import Pytorch DL engine as well in here to do the processing works.

%mavenRepo snapshots

%maven ai.djl:api:0.10.0-SNAPSHOT
%maven ai.djl.paddlepaddle:paddlepaddle-model-zoo:0.10.0-SNAPSHOT
%maven ai.djl.paddlepaddle:paddlepaddle-native-auto:2.0.0
%maven org.slf4j:slf4j-api:1.7.26
%maven org.slf4j:slf4j-simple:1.7.26

// second engine to do preprocessing and postprocessing
%maven ai.djl.pytorch:pytorch-engine:0.10.0-SNAPSHOT
%maven ai.djl.pytorch:pytorch-native-auto:1.7.1
import ai.djl.Application;
import ai.djl.MalformedModelException;
import ai.djl.ModelException;
import ai.djl.inference.Predictor;
import ai.djl.modality.Classifications;
import ai.djl.ndarray.*;
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.zoo.*;
import ai.djl.translate.*;

import java.nio.file.*;
import java.util.*;

Face Detection model

Now we can start working on the first model. The model can do face detection and require some additional processing before we feed into it:

  • Resize: Shrink the image with a certain ratio to feed in
  • Normalize the image with a scale

Fortunatly, DJL offers a Translator interface that can help you with these processing. The rough Translator architecture looks like below:

In the following sections, we will implement a FaceTranslator class to do the work.


In this stage, we will load an image and do some preprocessing work to it. Let's load the image first and take a look at it:

String url = "";
Image img = ImageFactory.getInstance().fromUrl(url);