Skip to content

Commit

Permalink
improve text that is printed
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Dec 2, 2024
1 parent 54a9d6e commit 36dbe35
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 29 deletions.
16 changes: 13 additions & 3 deletions src/main/java/ai/nets/samj/communication/model/EfficientSAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import ai.nets.samj.models.AbstractSamJ;
Expand Down Expand Up @@ -115,9 +117,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
efficientSamJ = EfficientSamJ.initializeSam(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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
this.efficientSamJ.setDebugPrinter(filteringLogger);
this.efficientSamJ.setImage(Cast.unchecked(image));;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientViTSamJ;
Expand Down Expand Up @@ -87,9 +89,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
this.samj.setDebugPrinter(filteringLogger);
this.samj.setImage(Cast.unchecked(image));;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.install.EfficientViTSamEnvManager;
import ai.nets.samj.models.AbstractSamJ;
Expand Down Expand Up @@ -85,9 +87,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
this.samj.setDebugPrinter(filteringLogger);
this.samj.setImage(Cast.unchecked(image));;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientViTSamJ;
Expand Down Expand Up @@ -85,9 +87,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
this.samj.setDebugPrinter(filteringLogger);
this.samj.setImage(Cast.unchecked(image));;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientViTSamJ;
Expand Down Expand Up @@ -84,9 +86,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
this.samj.setDebugPrinter(filteringLogger);
this.samj.setImage(Cast.unchecked(image));;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientViTSamJ;
Expand Down Expand Up @@ -86,9 +88,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
this.samj.setDebugPrinter(filteringLogger);
this.samj.setImage(Cast.unchecked(image));;
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/ai/nets/samj/communication/model/SAM2Large.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.install.Sam2EnvManager;
import ai.nets.samj.models.AbstractSamJ;
Expand Down Expand Up @@ -83,9 +85,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
if (useThisLoggerForIt != null)
this.log = useThisLoggerForIt;
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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
if (this.samj == null)
samj = Sam2.initializeSam("large", manager, filteringLogger, false);
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/ai/nets/samj/communication/model/SAM2Small.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.Sam2;
Expand Down Expand Up @@ -85,9 +87,17 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
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 );
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
this.samj.setDebugPrinter(filteringLogger);
this.samj.setImage(Cast.unchecked(image));;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.Sam2;
Expand Down Expand Up @@ -83,9 +85,16 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
this.log = useThisLoggerForIt;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
int idxProgress = text.indexOf("\"message\": \"8f821f82-db6f-42a3-8500-794a5033114e\"");
if (idx > 0) this.log.info( text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}");
else this.log.info( text );
int idxProgress = text.indexOf(AbstractSamJ.getProgressString());
if (idx > 0)
text = text.substring(0,idx) + "\"responseType\": \"COMPLETION\"}";
if (idxProgress != -1) {
String regex = "\"outputs\"\\s*:\\s*\\{.*?\\},";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
text = matcher.replaceAll("");
}
this.log.info( text );
};
if (this.samj == null)
samj = Sam2.initializeSam("tiny", manager, filteringLogger, false);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/ai/nets/samj/models/AbstractSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public abstract class AbstractSamJ implements AutoCloseable {

protected static int MAX_IMG_SIZE = 2024;

protected static String UPDATE_ID_N_CONTOURS = UUID.randomUUID().toString();
protected static String UPDATE_ID_N_CONTOURS = "PROMPT_NUMBER_" + UUID.randomUUID().toString();

protected static String UPDATE_ID_CONTOUR = UUID.randomUUID().toString();
protected static String UPDATE_ID_CONTOUR = "FOUND_CONTOUR_" + UUID.randomUUID().toString();

/** Essentially, a syntactic-shortcut for a String consumer */
public interface BatchCallback {
Expand Down Expand Up @@ -1215,4 +1215,8 @@ else if (task.status == TaskStatus.CRASHED)
}
this.savedEncodings.remove(encodingName);
}

public static String getProgressString() {
return UPDATE_ID_CONTOUR;
}
}

0 comments on commit 36dbe35

Please sign in to comment.