Class SAM2Utils


  • public class SAM2Utils
    extends Object
    Helper class for Segment Anything 2.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String[] MODEL_NAMES
      the available models.
    • Constructor Summary

      Constructors 
      Constructor Description
      SAM2Utils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static BufferedImage combineProbabilityDistributions​(List<float[][]> probDists, Color color)
      Combines the probability distribution masks into a single image segmentation layer.
      static ai.djl.modality.cv.output.DetectedObjects detectObjects​(ai.djl.inference.Predictor<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects> predictor, BufferedImage img, List<Point> points)
      Applies SAM2 to the image and points.
      static ai.djl.modality.cv.output.DetectedObjects detectObjects​(ai.djl.inference.Predictor<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects> predictor, BufferedImage img, List<Point> points, Rectangle box)
      Applies SAM2 to the image and points, limiting the detection to the specified rectangle.
      static com.github.fracpete.javautils.struct.Struct2<ai.djl.repository.zoo.ZooModel<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects>,​ai.djl.inference.Predictor<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects>> loadModel​(String modelName)
      Loads the model and predictor.
      static List<float[][]> probabilityDistributions​(ai.djl.modality.cv.output.DetectedObjects detection, double minDetect, double minMask)
      Extracts the (full-image) probability distributions from the detected objects.
    • Field Detail

      • MODEL_NAMES

        public static final String[] MODEL_NAMES
        the available models.
    • Constructor Detail

      • SAM2Utils

        public SAM2Utils()
    • Method Detail

      • loadModel

        public static com.github.fracpete.javautils.struct.Struct2<ai.djl.repository.zoo.ZooModel<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects>,​ai.djl.inference.Predictor<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects>> loadModel​(String modelName)
                                                                                                                                                                                                                                                                                                                                                       throws Exception
        Loads the model and predictor.
        Parameters:
        modelName - the name of the model to load
        Throws:
        Exception - if model loading fails
        Exception - if loading fails
        See Also:
        MODEL_NAMES
      • detectObjects

        public static ai.djl.modality.cv.output.DetectedObjects detectObjects​(ai.djl.inference.Predictor<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects> predictor,
                                                                              BufferedImage img,
                                                                              List<Point> points)
                                                                       throws Exception
        Applies SAM2 to the image and points.
        Parameters:
        predictor - the predictor instance to use
        img - the image to analyze
        points - the prompt points
        Returns:
        the detected objects
        Throws:
        Exception - if detection fails
      • detectObjects

        public static ai.djl.modality.cv.output.DetectedObjects detectObjects​(ai.djl.inference.Predictor<ai.djl.modality.cv.translator.Sam2Translator.Sam2Input,​ai.djl.modality.cv.output.DetectedObjects> predictor,
                                                                              BufferedImage img,
                                                                              List<Point> points,
                                                                              Rectangle box)
                                                                       throws Exception
        Applies SAM2 to the image and points, limiting the detection to the specified rectangle.
        Parameters:
        predictor - the predictor instance to use
        img - the image to analyze
        points - the prompt points
        box - the rectangle to limit the detection to, ignored if null
        Returns:
        the detected objects
        Throws:
        Exception - if detection fails
      • probabilityDistributions

        public static List<float[][]> probabilityDistributions​(ai.djl.modality.cv.output.DetectedObjects detection,
                                                               double minDetect,
                                                               double minMask)
        Extracts the (full-image) probability distributions from the detected objects.
        Parameters:
        detection - the detected objects to process
        minDetect - the minimum probability that detections require
        minMask - the minimum probability in the masks, use <=0 for no restriction
        Returns:
        the distributions
      • combineProbabilityDistributions

        public static BufferedImage combineProbabilityDistributions​(List<float[][]> probDists,
                                                                    Color color)
        Combines the probability distribution masks into a single image segmentation layer.
        Parameters:
        probDists - the distributions to combine
        color - the color to use in the image
        Returns:
        the generated ARGB image, null if no masks provided