|
19 | 19 | */
|
20 | 20 | package ai.nets.samj.ij;
|
21 | 21 |
|
| 22 | +import java.awt.GraphicsEnvironment; |
| 23 | +import java.awt.Rectangle; |
22 | 24 | import java.io.IOException;
|
| 25 | +import java.util.List; |
23 | 26 |
|
24 | 27 | import javax.swing.SwingUtilities;
|
25 | 28 |
|
| 29 | +import ai.nets.samj.annotation.Mask; |
| 30 | +import ai.nets.samj.communication.model.SAMModel; |
26 | 31 | import ai.nets.samj.gui.MainGUI;
|
27 | 32 | import ai.nets.samj.ui.SAMJLogger;
|
| 33 | +import ij.IJ; |
28 | 34 | import ij.ImageJ;
|
| 35 | +import ij.Macro; |
29 | 36 | import ij.gui.GUI;
|
30 | 37 | import ij.plugin.PlugIn;
|
31 | 38 | import ij.plugin.frame.Recorder;
|
| 39 | +import net.imglib2.RandomAccessibleInterval; |
| 40 | +import net.imglib2.type.NativeType; |
| 41 | +import net.imglib2.type.numeric.RealType; |
| 42 | +import net.imglib2.util.Cast; |
32 | 43 | import ai.nets.samj.ij.ui.Consumer;
|
| 44 | +import ai.nets.samj.models.AbstractSamJ.BatchCallback; |
33 | 45 |
|
34 | 46 | // TODO I (Carlos) don't know how to develop in IJ2 @Plugin(type = Command.class, menuPath = "Plugins>SAMJ>Annotator")
|
35 | 47 | //TODO I (Carlos) don't know how to develop in IJ2 public class Plugin_SamJAnnotator implements Command {
|
|
41 | 53 | * @author Vladimir Ulman
|
42 | 54 | */
|
43 | 55 | public class SAMJ_Annotator implements PlugIn {
|
| 56 | + |
| 57 | + private String macroModelName; |
| 58 | + |
| 59 | + private String macroMaskPrompt; |
| 60 | + |
44 | 61 | final static long MAX_IMAGE_SIZE_IN_BYTES = ((long)4)<<30; //4 GB
|
45 | 62 |
|
| 63 | + private final static String MACRO_INFO = "https://github.com/segment-anything-models-java/SAMJ-IJ/blob/main/README.md#macros"; |
| 64 | + |
46 | 65 | final static String MACRO_RECORD_COMMENT = ""
|
47 | 66 | + System.lineSeparator()
|
48 | 67 | + "// Note: SAMJ macros are supported only in BatchSAMize mode with preset prompts." + System.lineSeparator()
|
49 | 68 | + "// The macro recording feature will capture the command 'run(\"SAMJ Annotator\");', but executing it will have no effect." + System.lineSeparator()
|
50 | 69 | + "// To record something, please click the 'SAMJ BatchSAMize' button." + System.lineSeparator()
|
51 | 70 | + "// For more information, visit:" + System.lineSeparator()
|
52 |
| - + "// https://github.com/segment-anything-models-java/SAMJ-IJ/blob/main/README.md#macros" + System.lineSeparator() |
| 71 | + + "// " + MACRO_INFO + System.lineSeparator() |
53 | 72 | + System.lineSeparator();
|
| 73 | + |
| 74 | + /** |
| 75 | + * Keys required to run deepImageJ with a macro |
| 76 | + */ |
| 77 | + private final static String[] macroKeys = new String[] {"modelName="}; |
| 78 | + /** |
| 79 | + * Optional keys to run deepImageJ with a macro or in headless mode |
| 80 | + */ |
| 81 | + private final static String[] macroOptionalKeys = new String[] {"maskPrompt="}; |
| 82 | + |
| 83 | + private static Consumer MACRO_CONSUMER; |
| 84 | + |
| 85 | + private final static BatchCallback MACRO_CALLBACK = new BatchCallback() { |
| 86 | + @Override |
| 87 | + public void setTotalNumberOfRois(int nRois) {} |
| 88 | + @Override |
| 89 | + public void updateProgress(int n) {} |
| 90 | + |
| 91 | + @Override |
| 92 | + public void drawRoi(List<Mask> masks) { |
| 93 | + SwingUtilities.invokeLater(() -> MACRO_CONSUMER.addPolygonsFromGUI(masks)); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void deletePointPrompt(List<int[]> promptList) { |
| 99 | + SwingUtilities.invokeLater(() -> promptList.forEach(proi -> MACRO_CONSUMER.deletePointRoi(proi))); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void deleteRectPrompt(List<int[]> promptList) { |
| 104 | + SwingUtilities.invokeLater(() -> promptList.stream() |
| 105 | + .map(rect -> new Rectangle(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1])) |
| 106 | + .forEach(roi -> MACRO_CONSUMER.deleteRectRoi(roi))); |
| 107 | + } |
| 108 | + |
| 109 | + }; |
54 | 110 |
|
55 | 111 | // TODO I (Carlos) don't know how to develop in IJ2 @Parameter
|
56 | 112 | //private LogService logService = new LogService();
|
@@ -112,11 +168,68 @@ public static void main(String[] args) throws IOException, InterruptedException
|
112 | 168 |
|
113 | 169 | @Override
|
114 | 170 | public void run(String arg) {
|
| 171 | + boolean isMacro = IJ.isMacro(); |
| 172 | + boolean isHeadless = GraphicsEnvironment.isHeadless(); |
115 | 173 | try {
|
116 |
| - run(); |
| 174 | + if (isMacro) { |
| 175 | + runMacro(); |
| 176 | + } else if (isHeadless) { |
| 177 | + } else { |
| 178 | + run(); |
| 179 | + } |
117 | 180 | } catch (IOException | InterruptedException e) {
|
118 | 181 | e.printStackTrace();
|
119 | 182 | }
|
| 183 | + } |
| 184 | + |
| 185 | + private < T extends RealType< T > & NativeType< T > > |
| 186 | + void runMacro() throws IOException, RuntimeException, InterruptedException { |
| 187 | + parseCommand(); |
| 188 | + if (macroModelName == null) |
| 189 | + return; |
120 | 190 |
|
| 191 | + MACRO_CONSUMER = new Consumer(); |
| 192 | + SAMModel selected = MainGUI.DEFAULT_MODEL_LIST.stream() |
| 193 | + .filter(mm -> mm.getName().equals(macroModelName)).findFirst().orElse(null); |
| 194 | + if (selected == null) |
| 195 | + throw new IllegalArgumentException("Specified model does not exist. Please, for more info visit: " |
| 196 | + + MACRO_INFO); |
| 197 | + RandomAccessibleInterval<T> rai = MACRO_CONSUMER.getFocusedImageAsRai(); |
| 198 | + selected.setImage(rai, null); |
| 199 | + List<int[]> pointPrompts = MACRO_CONSUMER.getPointRoisOnFocusImage(); |
| 200 | + List<Rectangle> rectPrompts = MACRO_CONSUMER.getRectRoisOnFocusImage(); |
| 201 | + |
| 202 | + selected.processBatchOfPrompts(pointPrompts, rectPrompts, rai, MACRO_CALLBACK); |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + private void parseCommand() { |
| 207 | + String macroArg = Macro.getOptions(); |
| 208 | + if (macroArg == null) |
| 209 | + return; |
| 210 | + |
| 211 | + macroModelName = parseArg(macroArg, macroKeys[0], true); |
| 212 | + macroMaskPrompt = parseArg(macroArg, macroOptionalKeys[0], false); |
| 213 | + } |
| 214 | + |
| 215 | + private static String parseArg(String macroArg, String arg, boolean required) { |
| 216 | + int modelFolderInd = macroArg.indexOf(arg); |
| 217 | + if (modelFolderInd == -1 && required) |
| 218 | + throw new IllegalArgumentException("SAMJ macro requires to the variable '" + arg + "'. " |
| 219 | + + "For more info, please visit: " + MACRO_INFO); |
| 220 | + else if (modelFolderInd == -1) |
| 221 | + return null; |
| 222 | + int modelFolderInd2 = macroArg.indexOf(arg + "["); |
| 223 | + int endInd = macroArg.indexOf(" ", modelFolderInd); |
| 224 | + String value; |
| 225 | + if (modelFolderInd2 != -1) { |
| 226 | + endInd = macroArg.indexOf("] ", modelFolderInd2); |
| 227 | + value = macroArg.substring(modelFolderInd2 + arg.length() + 1, endInd); |
| 228 | + } else { |
| 229 | + value = macroArg.substring(modelFolderInd + arg.length(), endInd); |
| 230 | + } |
| 231 | + if (value.equals("null") || value.equals("")) |
| 232 | + value = null; |
| 233 | + return value; |
121 | 234 | }
|
122 | 235 | }
|
0 commit comments