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 matter 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 https://oss.sonatype.org/content/repositories/snapshots/ %maven ai.djl:api:0.23.0 %maven ai.djl.paddlepaddle:paddlepaddle-model-zoo:0.23.0 %maven org.slf4j:slf4j-simple:1.7.32 // second engine to do preprocessing and postprocessing %maven ai.djl.pytorch:pytorch-engine:0.23.0
import ai.djl.*; import ai.djl.inference.*; import ai.djl.modality.*; import ai.djl.modality.cv.*; import ai.djl.modality.cv.output.*; import ai.djl.modality.cv.transform.*; import ai.djl.modality.cv.translator.*; import ai.djl.modality.cv.util.*; import ai.djl.ndarray.*; import ai.djl.ndarray.types.Shape; import ai.djl.repository.zoo.*; import ai.djl.translate.*; import java.io.*; 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 = "https://raw.githubusercontent.com/PaddlePaddle/PaddleHub/release/v1.5/demo/mask_detection/python/images/mask.jpg"; Image img = ImageFactory.getInstance().fromUrl(url); img.getWrappedImage();