Closed rebelate closed 2 years ago
@rebelate
DJL uses Image
class to represent a image object, it wraps BufferedImage
or Bitmap
(for Android). Using DJL's built-in pre-processing pipeline, you can convert the image into a NDArray
object. You can convert NDArray
back to Image if want to display the processed image:
Path imageFile = Paths.get("my_image.jpg");
ImageFactory factory = ImageFactory.getInstance();
Image img = factory.fromFile(imageFile);
try (NDManager manager = NDManager.newBaseManager()) {
NDArray array = img.toNDArray(manager);
Pipeline pipeline = new Pipeline();
pipeline.add(new Crop(1, 1, 300, 300))
.add(new CenterCrop(224, 224))
.add(new ToTensor())
.add(new Normalize(new float[]{0.485f, 0.456f, 0.406f), new float[]{
0.229f, 0.224f, 0.225f
});
NDArray output = pipeline.transform(new NDList(array)).head();
Image result = factory.fromNDArray(output);
// You can save your processed image into a file:
result.save(Files.newOutputStream(Paths.get("output.png")), "png");
// You can extract Java BufferedImage and display it Jupyter notebook or using AWT
BufferedImage bufferedImage = (BufferedImage)result.getWrappedImage();
}
I want to take a peek on how an image looks like after being preprocessed from a translator eg:
https://stackoverflow.com/questions/53623472/how-do-i-display-a-single-image-in-pytorch/55196345