Skip to content

Instantly share code, notes, and snippets.

@vrakesh
Last active September 14, 2021 00:36
Show Gist options
  • Save vrakesh/0faacec9e9f8d88c4cb96c8ae812493a to your computer and use it in GitHub Desktop.
Save vrakesh/0faacec9e9f8d88c4cb96c8ae812493a to your computer and use it in GitHub Desktop.
SSD Inference example in Joule
import ai.djl.modality.cv.DetectedObjects;
import ai.djl.modality.cv.ImageVisualization;
import ai.djl.modality.cv.util.BufferedImageUtils;
import ai.djl.mxnet.zoo.MxModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class SimpleSSDExample {
public static void main(String[] args) throws Exception{
// Get image file path
BufferedImage img = BufferedImageUtils
.fromUrl("https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/pose/soccer.png");
//Get resnet model from model zoo.
ZooModel<BufferedImage, DetectedObjects> model =
MxModelZoo.SSD.loadModel(new ProgressBar());
//Create a new predictor from model and predict on image.
DetectedObjects predictResult = model.newPredictor().predict(img);
// Draw Bounding boxes on image
ImageVisualization.drawBoundingBoxes(img, predictResult);
//Save result
ImageIO.write(img, "png", new File("ssd.png"));
model.close();
}
}
@frankfliu
Copy link

import ai.djl.modality.cv.DetectedObjects;
import ai.djl.modality.cv.ImageVisualization;
import ai.djl.modality.cv.util.BufferedImageUtils;
import ai.djl.mxnet.zoo.MxModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public final class SimpleSSDExample {

public static void main(String[] args) throws Exception {
    // Get image file path
    BufferedImage img = BufferedImageUtils.fromUrl("https://djl-ai.s3.amazonaws.com/resources/images/dog_bike_car.jpg"));

    //Get resnet model from model zoo.
    ZooModel<BufferedImage, DetectedObjects> model = MxModelZoo.SSD.loadModel(new ProgressBar());
    //Create a new predictor from model and predict on image.
    DetectedObjects predictResult = model.newPredictor().predict(img);
    // Draw Bounding boxes on image
    ImageVisualization.drawBoundingBoxes(img, predictResult);
    //Save result
    ImageIO.write(img, "png", new File("ssd.png"));
    model.close();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment