DJL Spring Boot Example

In this article, we will discuss what the DJL in Spring Boot is (Deep Java Library) and its uses. We will also see an example of making use of an existing model to detect an object using a spring boot application.

1.What is DJL in Spring Boot

Deep Java Library is one of Java’s libraries that provides a platform for Deep Learning. We can use DJL to train, build, and run deep learning models. It uses the existing deep learning framework to predict and develop models. It provides a simple API to use deep learning by abstracting out all the complexity.
DJL also provides automatic processing choices(CPU/GPU) based on the hardware configuration from a hardware perspective.
It uses MXNet, PyTorch, TensorFlow, and several other popular deep-learning/machine learning frameworks under the hood and provides a very simple java API with a small learning curve.

2. Sample Application

Here we will create a simple application which will use the existing model-zoo models to detect objects in a given image. In this application we will upload any image with different objects like car, traffic, people,etc.

Once we feed the image to the model, the model will run and draw a box surrounding the objects it has detected with the name given.

We will be using springboot to build the application with maven. Lets get started

2.1 The pom file

We will add all the dependency required to run the application

<project xmlns=""

In the above file, we have the web springboot starter dependency and others are the dependencies for the Deep Java Library. Which consists of the existing training models and also the engine for processing and analyzing the model into the input passed.

2.2 Code for the object detection

This is the controller which will accept the uploaded image and process it using the DJL engines and provide the output in the browser

package com.jcg.djl;
import ai.djl.Application;
import ai.djl.ModelException;
import ai.djl.inference.Predictor;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.translate.TranslateException;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ImageDetectController {
    @PostMapping(value = "/upload", produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity diagnose(@RequestParam("file") MultipartFile file) throws ModelException, TranslateException, IOException {
        byte[] bytes = file.getBytes();
        Path imageFile = Paths.get(file.getOriginalFilename());
        Files.write(imageFile, bytes);
        return predict(imageFile);
    public ResponseEntity predict(Path imageFile) throws IOException, ModelException, TranslateException {
        Image img = ImageFactory.getInstance().fromFile(imageFile);
        Criteria criteria =
                        .setTypes(Image.class, DetectedObjects.class)
                        .optFilter("backbone", "resnet50")
                        .optProgress(new ProgressBar())
        try (ZooModel model = ModelZoo.loadModel(criteria)) {
            try (Predictor predictor = model.newPredictor()) {
                DetectedObjects detection = predictor.predict(img);
                return saveBoundingBoxImage(img, detection);
    private ResponseEntity saveBoundingBoxImage(Image img, DetectedObjects detection)
            throws IOException {
        Path outputDir = Paths.get("src/main/resources");
        Image newImage = img.duplicate(Image.Type.TYPE_INT_ARGB);
        Path imagePath = outputDir.resolve("detected.png");, "png");
        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
        return ResponseEntity.ok(fileDownloadUri);
            value = "/get",
            produces = MediaType.IMAGE_PNG_VALUE
    public @ResponseBody
    byte[] getImageWithMediaType() throws IOException {
        InputStream in = new ClassPathResource(
        return IOUtils.toByteArray(in);

This file contains the actual code which uses the model-zoo model to detect the objects in the image passed

2.3 Application Main File

This is the main class which will run the springboot application

package com.jcg.djl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class ImageObjectDetectionApplication {
    public static void main(String[] args) {, args);

3. Running the application

You can run the application using the command below from the root folder

./mvnw spring-boot:run

Once the springboot application has started, you can go to the http://localhost:8080/index.html

Upload image

Now we will upload the sample file as shown below

Demo image: picture from

Once after uploading the image, you can click the upload button. It processes the image and provides you with the link as shown below

Image processed

Enter the link into the browser and you will get the processed image with the object detected as a person, car, etc. into the browser as shown below.

Final image with object detected

4. Summary

In this article, we discussed the DJL Spring Boot and its uses. We also created a spring boot application to use the existing training model and applied that into an image and verified the object detected.

You can read more Spring Boot articles here.

5. Download the source code

That was a DJL spring boot example.

You can download the full source code of this example here: DJL Spring Boot Example
Exit mobile version