Class ImageFactory

java.lang.Object
ai.djl.modality.cv.ImageFactory
Direct Known Subclasses:
BufferedImageFactory

public abstract class ImageFactory extends Object
ImageFactory contains image creation mechanism on top of different platforms like PC and Android. System will choose appropriate Factory based on the supported image type.
  • Constructor Details

    • ImageFactory

      public ImageFactory()
  • Method Details

    • getInstance

      public static ImageFactory getInstance()
      Gets new instance of Image factory from the provided factory implementation.
      Returns:
      ImageFactory
    • setImageFactory

      public static void setImageFactory(ImageFactory factory)
      Sets a custom instance of ImageFactory.
      Parameters:
      factory - a custom instance of ImageFactory
    • fromFile

      public abstract Image fromFile(Path path) throws IOException
      Gets Image from file.
      Parameters:
      path - the path to the image
      Returns:
      Image
      Throws:
      IOException - Image not found or not readable
    • fromUrl

      public Image fromUrl(URL url) throws IOException
      Gets Image from URL.
      Parameters:
      url - the URL to load from
      Returns:
      Image
      Throws:
      IOException - URL is not valid.
    • fromUrl

      public Image fromUrl(String url) throws IOException
      Gets Image from string representation.

      A string that is not an absolute URL is loaded as a local file. Use fromRequestUrl(String) for a URL that arrived with a request.

      Parameters:
      url - the String represent URL or base64 encoded image to load from
      Returns:
      Image
      Throws:
      IOException - URL is not valid.
    • fromRequestUrl

      public Image fromRequestUrl(String url) throws IOException
      Gets Image from a URL that arrived with a request, such as the image_url of a JSON input.

      Unlike fromUrl(String), this only accepts a base64 data: URI or an http or https URL, and the URL is fetched with Utils.openUrl(URL), so it gets the same checks as other remote resources. Set DJL_ALLOW_INSECURE_URL=true (or -Dai.djl.allow_insecure_url=true) to accept any URL or file path, as fromUrl(String) does; as for other remote resources, nothing is fetched in offline mode.

      Parameters:
      url - the data: URI, or the http or https URL
      Returns:
      Image
      Throws:
      IOException - if the URL is not supported, or the image cannot be read
    • fromInputStream

      public abstract Image fromInputStream(InputStream is) throws IOException
      Gets Image from InputStream.
      Parameters:
      is - InputStream
      Returns:
      Image
      Throws:
      IOException - image cannot be read from input stream.
    • fromImage

      public abstract Image fromImage(Object image)
      Gets Image from varies Java image types.

      Image can be BufferedImage or BitMap depends on platform

      Parameters:
      image - the image object.
      Returns:
      Image
    • fromNDArray

      public abstract Image fromNDArray(NDArray array)
      Gets Image from NDArray.
      Parameters:
      array - the NDArray with CHW format
      Returns:
      Image
    • fromPixels

      public abstract Image fromPixels(int[] pixels, int width, int height)
      Gets Image from array.
      Parameters:
      pixels - the array of ARGB values used to initialize the pixels.
      width - the width of the image
      height - the height of the image
      Returns:
      Image