Search in sources :

Example 1 with Image

use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.

the class ImageClassification method main.

public static void main(String[] args) throws ModelException, IOException, TranslateException {
    // java default ImageIO doesn't work with GraalVM
    ImageFactory.setImageFactory(new GraalvmImageFactory());
    Image image;
    if (args.length == 0) {
        image = ImageFactory.getInstance().fromUrl(IMAGE_URL);
    } else {
        image = ImageFactory.getInstance().fromUrl(args[0]);
    }
    Criteria<Image, Classifications> criteria;
    if ("TensorFlow".equals(Engine.getInstance().getEngineName())) {
        Translator<Image, Classifications> translator = ImageClassificationTranslator.builder().addTransform(new Resize(224)).addTransform(new Normalize(MEAN, STD)).build();
        criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optArtifactId("resnet").optTranslator(translator).optProgress(new ProgressBar()).build();
    } else {
        criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optArtifactId("resnet").optProgress(new ProgressBar()).build();
    }
    try (ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
        Predictor<Image, Classifications> predictor = model.newPredictor()) {
        Classifications result = predictor.predict(image);
        System.out.println(result.toString());
    }
}
Also used : Normalize(ai.djl.modality.cv.transform.Normalize) Classifications(ai.djl.modality.Classifications) Resize(ai.djl.modality.cv.transform.Resize) Image(ai.djl.modality.cv.Image) ProgressBar(ai.djl.training.util.ProgressBar)

Example 2 with Image

use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.

the class WebCam method main.

public static void main(String[] args) throws IOException, ModelException, TranslateException {
    ZooModel<Image, DetectedObjects> model = loadModel();
    Predictor<Image, DetectedObjects> predictor = model.newPredictor();
    OpenCV.loadShared();
    VideoCapture capture = new VideoCapture(0);
    if (!capture.isOpened()) {
        System.out.println("No camera detected");
        return;
    }
    Mat image = new Mat();
    boolean captured = false;
    for (int i = 0; i < 10; ++i) {
        captured = capture.read(image);
        if (captured) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException ignore) {
        // ignore
        }
    }
    if (!captured) {
        JOptionPane.showConfirmDialog(null, "Failed to capture image from WebCam.");
    }
    ViewerFrame frame = new ViewerFrame(image.width(), image.height());
    ImageFactory factory = ImageFactory.getInstance();
    while (capture.isOpened()) {
        if (!capture.read(image)) {
            break;
        }
        Image img = factory.fromImage(image);
        DetectedObjects detections = predictor.predict(img);
        img.drawBoundingBoxes(detections);
        frame.showImage(toBufferedImage((Mat) img.getWrappedImage()));
    }
    capture.release();
    predictor.close();
    model.close();
    System.exit(0);
}
Also used : ImageFactory(ai.djl.modality.cv.ImageFactory) Mat(org.opencv.core.Mat) DetectedObjects(ai.djl.modality.cv.output.DetectedObjects) BufferedImage(java.awt.image.BufferedImage) Image(ai.djl.modality.cv.Image) VideoCapture(org.opencv.videoio.VideoCapture)

Example 3 with Image

use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.

the class CanaryTest method main.

public static void main(String[] args) throws IOException, ModelException, TranslateException {
    logger.info("");
    logger.info("----------Environment Variables----------");
    System.getenv().forEach((k, v) -> logger.info(k + ": " + v));
    logger.info("");
    logger.info("----------Default Engine----------");
    Engine.debugEnvironment();
    logger.info("");
    logger.info("----------Device information----------");
    int gpuCount = CudaUtils.getGpuCount();
    logger.info("GPU Count: {}", gpuCount);
    if (gpuCount > 0) {
        logger.info("CUDA: {}", CudaUtils.getCudaVersionString());
        logger.info("ARCH: {}", CudaUtils.getComputeCapability(0));
    }
    String djlEngine = System.getenv("DJL_ENGINE");
    if (djlEngine == null) {
        djlEngine = "mxnet-native-auto";
    }
    Device device = NDManager.newBaseManager().getDevice();
    if (djlEngine.contains("-native-cu") && !device.isGpu()) {
        throw new AssertionError("Expecting load engine on GPU.");
    } else if (djlEngine.startsWith("tensorrt")) {
        testTensorrt();
        return;
    } else if (djlEngine.startsWith("onnxruntime")) {
        testOnnxRuntime();
        return;
    } else if (djlEngine.startsWith("xgboost")) {
        testXgboost();
        return;
    } else if (djlEngine.startsWith("tflite")) {
        testTflite();
        return;
    } else if (djlEngine.startsWith("python")) {
        testPython();
        return;
    } else if (djlEngine.startsWith("dlr")) {
        testDlr();
        // similar to DLR, fastText and SentencePiece only support Mac and Ubuntu 16.04+
        testFastText();
        testSentencePiece();
        return;
    } else if (djlEngine.startsWith("paddle")) {
        testPaddle();
        return;
    }
    logger.info("");
    logger.info("----------Test inference----------");
    String url = "https://resources.djl.ai/images/dog_bike_car.jpg";
    Image img = ImageFactory.getInstance().fromUrl(url);
    String backbone = "resnet50";
    Map<String, String> options = null;
    if ("TensorFlow".equals(Engine.getInstance().getEngineName())) {
        backbone = "mobilenet_v2";
        options = new ConcurrentHashMap<>();
        options.put("Tags", "");
    }
    Criteria<Image, DetectedObjects> criteria = Criteria.builder().optApplication(Application.CV.OBJECT_DETECTION).setTypes(Image.class, DetectedObjects.class).optFilter("backbone", backbone).optOptions(options).build();
    try (ZooModel<Image, DetectedObjects> model = ModelZoo.loadModel(criteria)) {
        try (Predictor<Image, DetectedObjects> predictor = model.newPredictor()) {
            DetectedObjects detection = predictor.predict(img);
            logger.info("{}", detection);
        }
    }
}
Also used : Device(ai.djl.Device) DetectedObjects(ai.djl.modality.cv.output.DetectedObjects) Image(ai.djl.modality.cv.Image)

Example 4 with Image

use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.

the class CanaryTest method testDlr.

private static void testDlr() throws ModelException, IOException, TranslateException {
    String os;
    if (System.getProperty("os.name").toLowerCase().startsWith("mac")) {
        os = "osx";
    } else if (System.getProperty("os.name").toLowerCase().startsWith("linux")) {
        os = "linux";
    } else {
        throw new AssertionError("DLR only work on mac and Linux.");
    }
    ImageClassificationTranslator translator = ImageClassificationTranslator.builder().addTransform(new Resize(224, 224)).addTransform(new ToTensor()).build();
    Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optApplication(Application.CV.IMAGE_CLASSIFICATION).optFilter("layers", "50").optFilter("os", os).optTranslator(translator).optEngine("DLR").optProgress(new ProgressBar()).build();
    String url = "https://resources.djl.ai/images/kitten.jpg";
    Image image = ImageFactory.getInstance().fromUrl(url);
    try (ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
        Predictor<Image, Classifications> predictor = model.newPredictor()) {
        Classifications classifications = predictor.predict(image);
        logger.info("{}", classifications);
    }
}
Also used : ImageClassificationTranslator(ai.djl.modality.cv.translator.ImageClassificationTranslator) Classifications(ai.djl.modality.Classifications) ToTensor(ai.djl.modality.cv.transform.ToTensor) Resize(ai.djl.modality.cv.transform.Resize) Image(ai.djl.modality.cv.Image) ProgressBar(ai.djl.training.util.ProgressBar)

Example 5 with Image

use of ai.djl.modality.cv.Image in project djl-demo by deepjavalibrary.

the class CanaryTest method testTflite.

private static void testTflite() throws ModelException, IOException, TranslateException {
    if (System.getProperty("os.name").startsWith("Win")) {
        throw new AssertionError("TFLite only work on macOS and Linux.");
    }
    Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optEngine("TFLite").optFilter("dataset", "aiyDish").build();
    try (ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
        Predictor<Image, Classifications> predictor = model.newPredictor()) {
        Image image = ImageFactory.getInstance().fromUrl("https://resources.djl.ai/images/sachertorte.jpg");
        Classifications prediction = predictor.predict(image);
        logger.info(prediction.toString());
        if (!"Sachertorte".equals(prediction.best().getClassName())) {
            throw new AssertionError("Wrong prediction result");
        }
    }
}
Also used : Classifications(ai.djl.modality.Classifications) Image(ai.djl.modality.cv.Image)

Aggregations

Image (ai.djl.modality.cv.Image)90 Classifications (ai.djl.modality.Classifications)43 Path (java.nio.file.Path)30 Test (org.testng.annotations.Test)29 DetectedObjects (ai.djl.modality.cv.output.DetectedObjects)25 ProgressBar (ai.djl.training.util.ProgressBar)21 NDArray (ai.djl.ndarray.NDArray)20 NDManager (ai.djl.ndarray.NDManager)17 Shape (ai.djl.ndarray.types.Shape)16 ToTensor (ai.djl.modality.cv.transform.ToTensor)15 ImageFactory (ai.djl.modality.cv.ImageFactory)13 Model (ai.djl.Model)9 Resize (ai.djl.modality.cv.transform.Resize)8 NDList (ai.djl.ndarray.NDList)7 BoundingBox (ai.djl.modality.cv.output.BoundingBox)6 Rectangle (ai.djl.modality.cv.output.Rectangle)6 ImageClassificationTranslator (ai.djl.modality.cv.translator.ImageClassificationTranslator)6 Block (ai.djl.nn.Block)6 DefaultTrainingConfig (ai.djl.training.DefaultTrainingConfig)6 Trainer (ai.djl.training.Trainer)6