- 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.
- 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.
- 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.
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>
<!-- 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>
- 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);
DetectedObjects detections = predictor.predict(img);
img.drawBoundingBoxes(detections);
Path outputPath = Paths.get("output-detection.png");
try (OutputStream os = Files.newOutputStream(outputPath)) {
img.save(os, "png");
}
- Inside the project root, run:
mvn clean install
Then:
mvn exec:java -Dexec.mainClass="org.example.ObjectDetectionWithDjl"
Or simply run from IntelliJ IDEA.
-
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
This project can be cloned and executed easily on any system (Windows, macOS, Linux) using Java 17+ and Maven.
Follow these steps:
git clone https://github.com/NashTech-Labs/object-detection-with-djl.git
cd djl-object-detection
Check Java and Maven versions if installed else download it first:
java -version //You should see 17 or above.
mvn -version
mvn clean install
This will:
- Download all DJL dependencies
- Download PyTorch model files
- Compile your Java code
- Run test cases
Method 1: Run from IntelliJ IDEA
-
Open project → File > Open ,
-
Wait for Maven indexing
-
open ObjectDetectionWithDjl.java
-
Click ▶ (Run)
DJL Documentation: https://docs.djl.ai/
Model Zoo: https://docs.djl.ai/model-zoo/index.html
GitHub: https://github.com/deepjavalibrary/djl
This project is for educational and demo purposes. Feel free to fork and use it in your own projects.