diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java index 725be82..5063107 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java @@ -19,24 +19,17 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.EfficientViTSamJ; -import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.EfficientViTSamEnvManager; -import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.ui.SAMJLogger; /** @@ -46,28 +39,6 @@ */ public class EfficientViTSAML0 extends SAMModel { - private EfficientViTSamJ efficientSamJ; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private Boolean installed = false; - private boolean onlyBiggest = false; /** * Official name of the model */ @@ -103,14 +74,6 @@ public String getName() { return FULL_NAME; } - @Override - /** - * {@inheritDoc} - */ - public boolean isInstalled() { - return installed; - } - @Override /** * {@inheritDoc} @@ -120,112 +83,22 @@ public & NativeType> void setImage(final RandomAccessi Objects.requireNonNull(image, "The image cannot be null."); if (useThisLoggerForIt != null) this.log = useThisLoggerForIt; - if (this.efficientSamJ == null) - this.efficientSamJ = EfficientViTSamJ.initializeSam("l0", manager); + if (this.samj == null) + this.samj = EfficientViTSamJ.initializeSam("l0", manager); try { AbstractSamJ.DebugTextPrinter filteringLogger = text -> { int idx = text.indexOf("contours_x"); if (idx > 0) this.log.info( text.substring(0,idx) ); else this.log.info( text ); }; - this.efficientSamJ.setDebugPrinter(filteringLogger); - this.efficientSamJ.setImage(Cast.unchecked(image));; + this.samj.setDebugPrinter(filteringLogger); + this.samj.setImage(Cast.unchecked(image));; } catch (IOException | InterruptedException | RuntimeException e) { log.error(FULL_NAME + " experienced an error: " + e.getMessage()); throw e; } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return efficientSamJ.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return efficientSamJ.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (efficientSamJ != null) - efficientSamJ.close(); - efficientSamJ = null; - } - @Override /** * {@inheritDoc} @@ -233,44 +106,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return efficientSamJ.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java index 5f3390a..6daf3cc 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java @@ -19,22 +19,15 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; -import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.EfficientViTSamEnvManager; -import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.EfficientViTSamJ; import ai.nets.samj.ui.SAMJLogger; @@ -45,28 +38,6 @@ * @author Vladimir Ulman */ public class EfficientViTSAML1 extends SAMModel { - - private EfficientViTSamJ efficientSamJ; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private boolean onlyBiggest = false; /** * "Official" name of the model */ @@ -110,112 +81,22 @@ public & NativeType> void setImage(final RandomAccessi Objects.requireNonNull(image, "The image cannot be null."); if (useThisLoggerForIt != null) this.log = useThisLoggerForIt; - if (this.efficientSamJ == null) - this.efficientSamJ = EfficientViTSamJ.initializeSam("l1", manager); + if (this.samj == null) + this.samj = EfficientViTSamJ.initializeSam("l1", manager); try { AbstractSamJ.DebugTextPrinter filteringLogger = text -> { int idx = text.indexOf("contours_x"); if (idx > 0) this.log.info( text.substring(0,idx) ); else this.log.info( text ); }; - this.efficientSamJ.setDebugPrinter(filteringLogger); - this.efficientSamJ.setImage(Cast.unchecked(image));; + this.samj.setDebugPrinter(filteringLogger); + this.samj.setImage(Cast.unchecked(image));; } catch (IOException | InterruptedException | RuntimeException e) { log.error(FULL_NAME + " experienced an error: " + e.getMessage()); throw e; } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return efficientSamJ.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return efficientSamJ.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (efficientSamJ != null) - efficientSamJ.close(); - efficientSamJ = null; - } - @Override /** * {@inheritDoc} @@ -223,44 +104,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return efficientSamJ.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java index c8536ef..fde4f79 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java @@ -19,22 +19,16 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.EfficientViTSamJ; -import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.EfficientViTSamEnvManager; import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.ui.SAMJLogger; @@ -45,28 +39,6 @@ * @author Vladimir Ulman */ public class EfficientViTSAML2 extends SAMModel { - - private EfficientViTSamJ efficientSamJ; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private boolean onlyBiggest = false; /** * Model complete name */ @@ -110,112 +82,22 @@ public & NativeType> void setImage(final RandomAccessi Objects.requireNonNull(image, "The image cannot be null."); if (useThisLoggerForIt != null) this.log = useThisLoggerForIt; - if (this.efficientSamJ == null) - this.efficientSamJ = EfficientViTSamJ.initializeSam("l2", manager); + if (this.samj == null) + this.samj = EfficientViTSamJ.initializeSam("l2", manager); try { AbstractSamJ.DebugTextPrinter filteringLogger = text -> { int idx = text.indexOf("contours_x"); if (idx > 0) this.log.info( text.substring(0,idx) ); else this.log.info( text ); }; - this.efficientSamJ.setDebugPrinter(filteringLogger); - this.efficientSamJ.setImage(Cast.unchecked(image));; + this.samj.setDebugPrinter(filteringLogger); + this.samj.setImage(Cast.unchecked(image));; } catch (IOException | InterruptedException | RuntimeException e) { log.error(FULL_NAME + " experienced an error: " + e.getMessage()); throw e; } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return efficientSamJ.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return efficientSamJ.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (efficientSamJ != null) - efficientSamJ.close(); - efficientSamJ = null; - } - @Override /** * {@inheritDoc} @@ -223,44 +105,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return efficientSamJ.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java index 2b9745f..71f0234 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java @@ -19,24 +19,17 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.EfficientViTSamJ; -import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.EfficientViTSamEnvManager; -import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.ui.SAMJLogger; /** @@ -45,28 +38,6 @@ * @author Vladimir Ulman */ public class EfficientViTSAMXL0 extends SAMModel { - - private EfficientViTSamJ efficientSamJ; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private boolean onlyBiggest = false; /** * Name of the model */ @@ -109,112 +80,22 @@ public & NativeType> void setImage(final RandomAccessi Objects.requireNonNull(image, "The image cannot be null."); if (useThisLoggerForIt != null) this.log = useThisLoggerForIt; - if (this.efficientSamJ == null) - this.efficientSamJ = EfficientViTSamJ.initializeSam("xl0", manager); + if (this.samj == null) + this.samj = EfficientViTSamJ.initializeSam("xl0", manager); try { AbstractSamJ.DebugTextPrinter filteringLogger = text -> { int idx = text.indexOf("contours_x"); if (idx > 0) this.log.info( text.substring(0,idx) ); else this.log.info( text ); }; - this.efficientSamJ.setDebugPrinter(filteringLogger); - this.efficientSamJ.setImage(Cast.unchecked(image));; + this.samj.setDebugPrinter(filteringLogger); + this.samj.setImage(Cast.unchecked(image));; } catch (IOException | InterruptedException | RuntimeException e) { log.error(FULL_NAME + " experienced an error: " + e.getMessage()); throw e; } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return efficientSamJ.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return efficientSamJ.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (efficientSamJ != null) - efficientSamJ.close(); - efficientSamJ = null; - } - @Override /** * {@inheritDoc} @@ -222,44 +103,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return efficientSamJ.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java index afb2dc7..93af555 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java @@ -19,24 +19,17 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.EfficientViTSamJ; -import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.EfficientViTSamEnvManager; -import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.ui.SAMJLogger; /** @@ -45,28 +38,6 @@ * @author Vladimir Ulman */ public class EfficientViTSAMXL1 extends SAMModel { - - private EfficientViTSamJ efficientSamJ; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private boolean onlyBiggest = false; /** * Name of the model @@ -111,112 +82,22 @@ public & NativeType> void setImage(final RandomAccessi Objects.requireNonNull(image, "The image cannot be null."); if (useThisLoggerForIt != null) this.log = useThisLoggerForIt; - if (this.efficientSamJ == null) - this.efficientSamJ = EfficientViTSamJ.initializeSam("xl1", manager); + if (this.samj == null) + this.samj = EfficientViTSamJ.initializeSam("xl1", manager); try { AbstractSamJ.DebugTextPrinter filteringLogger = text -> { int idx = text.indexOf("contours_x"); if (idx > 0) this.log.info( text.substring(0,idx) ); else this.log.info( text ); }; - this.efficientSamJ.setDebugPrinter(filteringLogger); - this.efficientSamJ.setImage(Cast.unchecked(image));; + this.samj.setDebugPrinter(filteringLogger); + this.samj.setImage(Cast.unchecked(image));; } catch (IOException | InterruptedException | RuntimeException e) { log.error(FULL_NAME + " experienced an error: " + e.getMessage()); throw e; } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return efficientSamJ.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return efficientSamJ.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (efficientSamJ != null) - efficientSamJ.close(); - efficientSamJ = null; - } - @Override /** * {@inheritDoc} @@ -224,45 +105,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return efficientSamJ.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - this.efficientSamJ.setDebugPrinter(null); - efficientSamJ.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Large.java b/src/main/java/ai/nets/samj/communication/model/SAM2Large.java index 71df7bc..fe7a40d 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Large.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Large.java @@ -19,24 +19,17 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; +import ai.nets.samj.install.Sam2EnvManager; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.Sam2; -import ai.nets.samj.annotation.Mask; -import ai.nets.samj.install.Sam2EnvManager; -import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.ui.SAMJLogger; /** @@ -46,27 +39,6 @@ */ public class SAM2Large extends SAMModel { - private Sam2 efficientSamJ; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private boolean onlyBiggest = false; /** * Name of the model */ @@ -115,109 +87,16 @@ public & NativeType> void setImage(final RandomAccessi if (idx > 0) this.log.info( text.substring(0,idx) ); else this.log.info( text ); }; - if (this.efficientSamJ == null) - efficientSamJ = Sam2.initializeSam("large", manager, filteringLogger, false); + if (this.samj == null) + samj = Sam2.initializeSam("large", manager, filteringLogger, false); try { - this.efficientSamJ.setImage(Cast.unchecked(image));; + this.samj.setImage(Cast.unchecked(image));; } catch (IOException | InterruptedException | RuntimeException e) { log.error(FULL_NAME + " experienced an error: " + e.getMessage()); throw e; } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return efficientSamJ.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return efficientSamJ.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (efficientSamJ != null) - efficientSamJ.close(); - efficientSamJ = null; - } - @Override /** * {@inheritDoc} @@ -225,44 +104,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return efficientSamJ.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Small.java b/src/main/java/ai/nets/samj/communication/model/SAM2Small.java index b623adf..b15d039 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Small.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Small.java @@ -19,24 +19,17 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.Sam2; -import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.Sam2EnvManager; -import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.ui.SAMJLogger; /** @@ -45,28 +38,6 @@ * @author Vladimir Ulman */ public class SAM2Small extends SAMModel { - - private Sam2 efficientSamJ; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private boolean onlyBiggest = false; /** * Name of the model */ @@ -110,115 +81,22 @@ public & NativeType> void setImage(final RandomAccessi Objects.requireNonNull(image, "The image cannot be null."); if (useThisLoggerForIt != null) this.log = useThisLoggerForIt; - if (this.efficientSamJ == null) - efficientSamJ = Sam2.initializeSam("small", manager); + if (this.samj == null) + samj = Sam2.initializeSam("small", manager); try { AbstractSamJ.DebugTextPrinter filteringLogger = text -> { int idx = text.indexOf("contours_x"); if (idx > 0) this.log.info( text.substring(0,idx) ); else this.log.info( text ); }; - this.efficientSamJ.setDebugPrinter(filteringLogger); - this.efficientSamJ.setImage(Cast.unchecked(image));; + this.samj.setDebugPrinter(filteringLogger); + this.samj.setImage(Cast.unchecked(image));; } catch (IOException | InterruptedException | RuntimeException e) { log.error(FULL_NAME + " experienced an error: " + e.getMessage()); throw e; } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest); - else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return efficientSamJ.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return efficientSamJ.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (efficientSamJ != null) - efficientSamJ.close(); - efficientSamJ = null; - } - @Override /** * {@inheritDoc} @@ -226,44 +104,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return efficientSamJ.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - efficientSamJ.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java b/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java index 6526d71..5d397d1 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java @@ -19,24 +19,17 @@ */ package ai.nets.samj.communication.model; -import net.imglib2.Interval; -import net.imglib2.Localizable; import net.imglib2.RandomAccessibleInterval; import net.imglib2.type.NativeType; import net.imglib2.type.numeric.RealType; import net.imglib2.util.Cast; -import java.awt.Rectangle; import java.io.IOException; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.models.Sam2; -import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.Sam2EnvManager; -import ai.nets.samj.install.SamEnvManagerAbstract; import ai.nets.samj.ui.SAMJLogger; /** @@ -45,28 +38,6 @@ * @author Vladimir Ulman */ public class SAM2Tiny extends SAMModel { - - private Sam2 samj; - private final SamEnvManagerAbstract manager; - private SAMJLogger log = new SAMJLogger() { - - @Override - public void info(String text) { - System.out.println(text); - } - - @Override - public void warn(String text) { - System.err.println("[WARNING] -- " + text); - } - - @Override - public void error(String text) { - System.err.println(text); - } - - }; - private boolean onlyBiggest = false; /** * Name of the model */ @@ -125,99 +96,6 @@ public & NativeType> void setImage(final RandomAccessi } } - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) - throws IOException, InterruptedException, RuntimeException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return samj.processPoints(list, !onlyBiggest); - else return samj.processPoints(list, negList, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, - Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { - try { - List list = listOfPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - List negList = listOfNegPoints2D.stream() - .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); - if (negList.size() == 0) return samj.processPoints(list, zoomedRectangle, !onlyBiggest); - else return samj.processPoints(list, negList, zoomedRectangle, !onlyBiggest); - } catch (IOException | RuntimeException | InterruptedException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public List fetch2dSegmentation(Interval boundingBox2D) - throws IOException, InterruptedException, RuntimeException { - try { - //order to processBox() should be: x0,y0, x1,y1 - final int bbox[] = { - (int)boundingBox2D.min(0), - (int)boundingBox2D.min(1), - (int)boundingBox2D.max(0), - (int)boundingBox2D.max(1) - }; - return samj.processBox(bbox, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) - throws IOException, InterruptedException, RuntimeException { - try { - return samj.processMask(rai, !onlyBiggest); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage()); - throw e; - } - } - - @Override - /** - * {@inheritDoc} - */ - public void notifyUiHasBeenClosed() { - if (log != null) - log.info(FULL_NAME+": OKAY, I'm closing myself..."); - closeProcess(); - } - - @Override - /** - * {@inheritDoc} - */ - public void closeProcess() { - if (samj != null) - samj.close(); - samj = null; - } - @Override /** * {@inheritDoc} @@ -225,44 +103,4 @@ public void closeProcess() { public String getInputImageAxes() { return INPUT_IMAGE_AXES; } - - @Override - public void setReturnOnlyBiggest(boolean onlyBiggest) { - this.onlyBiggest = onlyBiggest; - } - - @Override - public String persistEncoding() throws IOException, InterruptedException { - try { - return samj.persistEncoding(); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage()); - throw e; - } - } - - @Override - public void selectEncoding(String encodingName) throws IOException, InterruptedException { - try { - samj.selectEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public void deleteEncoding(String encodingName) throws IOException, InterruptedException { - try { - samj.deleteEncoding(encodingName); - } catch (IOException | InterruptedException | RuntimeException e) { - log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); - throw e; - } - } - - @Override - public SamEnvManagerAbstract getInstallationManger() { - return this.manager; - } } diff --git a/src/main/java/ai/nets/samj/communication/model/SAMModel.java b/src/main/java/ai/nets/samj/communication/model/SAMModel.java index 8f26e0c..7f8eab5 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAMModel.java +++ b/src/main/java/ai/nets/samj/communication/model/SAMModel.java @@ -22,9 +22,11 @@ import java.awt.Rectangle; import java.io.IOException; import java.util.List; +import java.util.stream.Collectors; import ai.nets.samj.annotation.Mask; import ai.nets.samj.install.SamEnvManagerAbstract; +import ai.nets.samj.models.AbstractSamJ; import ai.nets.samj.ui.SAMJLogger; import net.imglib2.Interval; import net.imglib2.Localizable; @@ -45,6 +47,7 @@ public abstract class SAMModel { protected String fullName; + protected AbstractSamJ samj; protected double size; protected int performanceRank; protected int speedRank; @@ -53,6 +56,28 @@ public abstract class SAMModel { protected String githubLink; protected String paperLink; protected boolean isHeavy; + protected boolean onlyBiggest = false; + protected SamEnvManagerAbstract manager; + + + protected SAMJLogger log = new SAMJLogger() { + + @Override + public void info(String text) { + System.out.println(text); + } + + @Override + public void warn(String text) { + System.err.println("[WARNING] -- " + text); + } + + @Override + public void error(String text) { + System.err.println(text); + } + + }; private static final String CAUTION_STRING = "

CAUTION: This model is" + " computationally heavy. It is not recommended to use it on lower-end computers.

"; @@ -95,11 +120,14 @@ public abstract class SAMModel { * @return the axes order required for the input image to the model */ public abstract String getInputImageAxes(); + /** * * @return the {@link SamEnvManagerAbstract} used to install this model */ - public abstract SamEnvManagerAbstract getInstallationManger(); + public SamEnvManagerAbstract getInstallationManger() { + return this.manager; + } /** * Instantiate a SAM based model. Provide also an image that will be encoded by the model encoder @@ -115,6 +143,43 @@ public abstract class SAMModel { void setImage(final RandomAccessibleInterval image, final SAMJLogger useThisLoggerForIt) throws IOException, RuntimeException, InterruptedException; + /** + * + * @return a text describing the model. + */ + public String getDescription() { + String description = String.format(HTML_MODEL_FORMAT, fullName, "" + size, + "" + speedRank, "" + performanceRank, githubLink, githubName, paperLink, paperName); + boolean installed = this.isInstalled(); + description = this.isHeavy & installed ? CAUTION_STRING + description: description; + description = installed ? description : HTML_NOT_INSTALLED + description; + return description; + } + + /** + * + * @return true or false whether all the things needed to run the model are already installed or not. + * This includes the Python environment and requirements, the model weights or micrommaba + */ + public boolean isInstalled() { + return this.getInstallationManger().checkEverythingInstalled(); + } + + + /** + * Set whether SAMJ will only return the biggest ROI or all of them when several are obtained from the model + * @param onlyBiggest + * whether the ouput of SAMJ will only be the biggest ROI obtained or all of them. + */ + public void setReturnOnlyBiggest(boolean onlyBiggest) { + this.onlyBiggest = onlyBiggest; + } + + public List processBatchOfPoints(List points) throws IOException, RuntimeException, InterruptedException { + //return samj.processBathcOfPoints(points, !onlyBiggest); + return null; + } + /** * Get a 2D segmentation/annotation using two lists of points as the prompts. * @param listOfPoints2D @@ -127,7 +192,21 @@ void setImage(final RandomAccessibleInterval image, final SAMJLogger useThisL * @throws RuntimeException if there is any error running the Python process * @throws InterruptedException if the process in interrupted */ - public abstract List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) throws IOException, RuntimeException, InterruptedException; + public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D) + throws IOException, InterruptedException, RuntimeException { + try { + List list = listOfPoints2D.stream() + .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); + List negList = listOfNegPoints2D.stream() + .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); + if (negList.size() == 0) return samj.processPoints(list, !onlyBiggest); + else return samj.processPoints(list, negList, !onlyBiggest); + } catch (IOException | RuntimeException | InterruptedException e) { + log.error(this.getName()+", providing empty result because of some trouble: "+e.getMessage()); + throw e; + } + } + /** * Get a 2D segmentation/annotation using two lists of points as the prompts. @@ -143,7 +222,20 @@ void setImage(final RandomAccessibleInterval image, final SAMJLogger useThisL * @throws RuntimeException if there is any error running the Python process * @throws InterruptedException if the process in interrupted */ - public abstract List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException; + public List fetch2dSegmentation(List listOfPoints2D, List listOfNegPoints2D, + Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException { + try { + List list = listOfPoints2D.stream() + .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); + List negList = listOfNegPoints2D.stream() + .map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList()); + if (negList.size() == 0) return samj.processPoints(list, zoomedRectangle, !onlyBiggest); + else return samj.processPoints(list, negList, zoomedRectangle, !onlyBiggest); + } catch (IOException | RuntimeException | InterruptedException e) { + log.error(getName()+", providing empty result because of some trouble: "+e.getMessage()); + throw e; + } + } /** * Get a 2D segmentation/annotation using a bounding box as the prompt. @@ -154,14 +246,22 @@ void setImage(final RandomAccessibleInterval image, final SAMJLogger useThisL * @throws RuntimeException if there is any error running the Python process * @throws InterruptedException if the process in interrupted */ - public abstract List fetch2dSegmentation(Interval boundingBox2D) throws IOException, RuntimeException, InterruptedException; - - /** - * Set whether SAMJ will only return the biggest ROI or all of them when several are obtained from the model - * @param onlyBiggest - * whether the ouput of SAMJ will only be the biggest ROI obtained or all of them. - */ - public abstract void setReturnOnlyBiggest(boolean onlyBiggest); + public List fetch2dSegmentation(Interval boundingBox2D) + throws IOException, InterruptedException, RuntimeException { + try { + //order to processBox() should be: x0,y0, x1,y1 + final int bbox[] = { + (int)boundingBox2D.min(0), + (int)boundingBox2D.min(1), + (int)boundingBox2D.max(0), + (int)boundingBox2D.max(1) + }; + return samj.processBox(bbox, !onlyBiggest); + } catch (IOException | InterruptedException | RuntimeException e) { + log.error(getName()+", providing empty result because of some trouble: "+e.getMessage()); + throw e; + } + } /** * Get a 2D segmentation/annotation using an existing mask as the prompt. @@ -174,44 +274,59 @@ void setImage(final RandomAccessibleInterval image, final SAMJLogger useThisL * @throws RuntimeException if there is any error running the Python process * @throws InterruptedException if the process in interrupted */ - public abstract & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) throws IOException, RuntimeException, InterruptedException; - - - public abstract String persistEncoding() throws IOException, InterruptedException; - - public abstract void selectEncoding(String encodingName) throws IOException, InterruptedException; - - public abstract void deleteEncoding(String encodingName) throws IOException, InterruptedException; - - /** - * Close the Python process where the model is being executed - */ - public abstract void closeProcess(); + public & NativeType> List fetch2dSegmentationFromMask(RandomAccessibleInterval rai) + throws IOException, InterruptedException, RuntimeException { + try { + return samj.processMask(rai, !onlyBiggest); + } catch (IOException | InterruptedException | RuntimeException e) { + log.error(getName()+", providing empty result because of some trouble: "+e.getMessage()); + throw e; + } + } /** * Notify the User Interface that the model has been closed */ - public abstract void notifyUiHasBeenClosed(); - - /** - * - * @return a text describing the model. - */ - public String getDescription() { - String description = String.format(HTML_MODEL_FORMAT, fullName, "" + size, - "" + speedRank, "" + performanceRank, githubLink, githubName, paperLink, paperName); - boolean installed = this.isInstalled(); - description = this.isHeavy & installed ? CAUTION_STRING + description: description; - description = installed ? description : HTML_NOT_INSTALLED + description; - return description; + public void notifyUiHasBeenClosed() { + if (log != null) + log.info(getName()+": OKAY, I'm closing myself..."); + closeProcess(); } /** - * - * @return true or false whether all the things needed to run the model are already installed or not. - * This includes the Python environment and requirements, the model weights or micrommaba + * Close the Python process where the model is being executed */ - public boolean isInstalled() { - return this.getInstallationManger().checkEverythingInstalled(); + public void closeProcess() { + if (samj != null) + samj.close(); + samj = null; + } + + public String persistEncoding() throws IOException, InterruptedException { + try { + return samj.persistEncoding(); + } catch (IOException | InterruptedException | RuntimeException e) { + log.error(getName()+", unable to persist the encoding: "+e.getMessage()); + throw e; + } } + + public void selectEncoding(String encodingName) throws IOException, InterruptedException { + try { + samj.selectEncoding(encodingName); + } catch (IOException | InterruptedException | RuntimeException e) { + log.error(getName()+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage()); + throw e; + } + } + + public void deleteEncoding(String encodingName) throws IOException, InterruptedException { + try { + samj.deleteEncoding(encodingName); + } catch (IOException | InterruptedException | RuntimeException e) { + log.error(getName()+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage()); + throw e; + } + } + }