Skip to content

A simple Java-based demo showcasing object detection using the Deep Java Library (DJL) with the PyTorch engine. It loads a pre-trained SSD model, detects objects in an image, and outputs the result with bounding boxes.

Notifications You must be signed in to change notification settings

NashTech-Labs/object-detection-with-djl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DJL Object Detection Demo (Java + PyTorch Engine)

  • This project demonstrates Object Detection in Java using the
  • Deep Java Library (DJL) with the PyTorch engine.
  • It loads a pre-trained SSD model from the DJL Model Zoo and performs object detection on an image, drawing bounding boxes and saving the output.

TechStack Used

  • Java 17+
  • Deep Java Library (DJL)
  • PyTorch Engine
  • ssd pretrained model
  • Maven
This is ideal as a beginner-friendly DJL demo or for showcasing how Java developers can run AI models without needing Python.

Features

  • Load images with DJL ImageFactory
  • Perform object detection using DJL Model Zoo
  • Prints detected classes with confidence scores
  • Draws bounding boxes on the input image
  • Saves the output image (output-detection.png)
  • Pure Java — no Python required!
  • Works on JVM, and integrates easily with enterprise applications.

Project Structure

djl-object-detection/
 ├── src/main/java/org/example/ObjectDetectionWithDjl.java
 ├── src/main/resources/street.jpg
 ├── pom.xml
 ├── output-detection.png (generated after running)
 └── README.md

Dependencies (Maven)

    <dependencies>
        <!-- DJL Core API -->
        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>api</artifactId>
            <version>0.35.0</version>
        </dependency>

        <!-- PyTorch Engine -->
        <dependency>
            <groupId>ai.djl.pytorch</groupId>
            <artifactId>pytorch-engine</artifactId>
            <version>0.35.0</version>
        </dependency>

        <!-- PyTorch Model Zoo -->
        <dependency>
            <groupId>ai.djl.pytorch</groupId>
            <artifactId>pytorch-model-zoo</artifactId>
            <version>0.35.0</version>
        </dependency>

        <!-- DJL Basic Model Zoo -->
        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>model-zoo</artifactId>
            <version>0.35.0</version>
        </dependency>

        <!-- SLF4J Logger -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.7</version>
        </dependency>
    </dependencies>

Code Overview

  • Load Image
  Image img = ImageFactory.getInstance().fromFile(imagePath);
  • Load Pre-trained SSD Model
   Criteria<Image, DetectedObjects> criteria = Criteria.builder()
       .optApplication(Application.CV.OBJECT_DETECTION)
       .setTypes(Image.class, DetectedObjects.class)
       .optArtifactId("ssd")
       .optEngine("PyTorch")  
       .build();

   ZooModel<Image, DetectedObjects> model = ModelZoo.loadModel(criteria);

Perform Detection

   DetectedObjects detections = predictor.predict(img);

Draw Bounding Boxes

   img.drawBoundingBoxes(detections);

Save Output (DJL 0.35.0 compatible)

Path outputPath = Paths.get("output-detection.png");
try (OutputStream os = Files.newOutputStream(outputPath)) {
    img.save(os, "png");
}

Running the Application

  • Inside the project root, run:
   mvn clean install

Then:

mvn exec:java -Dexec.mainClass="org.example.ObjectDetectionWithDjl"

Or simply run from IntelliJ IDEA.

Output

  • After running, the project generates:

    • output-detection.png

This file contains the original image with bounding boxes drawn around detected objects.

The console displays something like:

=== Detected Objects ===
car : 0.99
person : 0.84
traffic light : 0.97
...
Output saved to: /path/output-detection.png

How to Clone & Run This Project on Any System

This project can be cloned and executed easily on any system (Windows, macOS, Linux) using Java 17+ and Maven.

Follow these steps:

1. Clone the Repository:

  git clone https://github.com/NashTech-Labs/object-detection-with-djl.git

2. Switch to the directory:

  cd djl-object-detection

3. Verify Java & Maven Installation

Check Java and Maven versions if installed else download it first:

java -version  //You should see 17 or above.
mvn -version

4. Build the Project

  mvn clean install

This will:

  • Download all DJL dependencies
  • Download PyTorch model files
  • Compile your Java code
  • Run test cases

5. Run the Application (Recommended IntelliJ way)

Method 1: Run from IntelliJ IDEA

  • Open project → File > Open ,

  • Wait for Maven indexing

  • open ObjectDetectionWithDjl.java

  • Click ▶ (Run)

Learning Resources

DJL Documentation: https://docs.djl.ai/

Model Zoo: https://docs.djl.ai/model-zoo/index.html

GitHub: https://github.com/deepjavalibrary/djl

License

This project is for educational and demo purposes. Feel free to fork and use it in your own projects.

About

A simple Java-based demo showcasing object detection using the Deep Java Library (DJL) with the PyTorch engine. It loads a pre-trained SSD model, detects objects in an image, and outputs the result with bounding boxes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages