Skip to content

Commit

Permalink
find from where is it being launched
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 13, 2024
1 parent f37f30b commit 23a25c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/main/java/DeepImageJ_Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import io.bioimage.modelrunner.bioimageio.description.exceptions.ModelSpecsException;
import io.bioimage.modelrunner.exceptions.LoadModelException;
import io.bioimage.modelrunner.exceptions.RunModelException;
import io.bioimage.modelrunner.system.PlatformDetection;
import io.bioimage.modelrunner.tensor.Tensor;
import io.bioimage.modelrunner.utils.Constants;
import net.imglib2.type.NativeType;
Expand Down Expand Up @@ -101,14 +102,6 @@ static public void main(String args[]) {
}
@Override
public void run(String arg) {
System.out.println(System.getProperty("java.vm.name"));
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("java.vendor"));
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.specification.vendor"));
File modelsDir = new File("models");
if (!modelsDir.isDirectory() && !modelsDir.mkdir())
throw new RuntimeException("Unable to create 'models' folder inside ImageJ/Fiji directory. Please create it yourself.");
boolean isMacro = IJ.isMacro();
boolean isHeadless = GraphicsEnvironment.isHeadless();
if (!isMacro) {
Expand All @@ -121,6 +114,9 @@ public void run(String arg) {
}

private void runGUI() {
File modelsDir = new File("models");
if (!modelsDir.isDirectory() && !modelsDir.mkdir())
throw new RuntimeException("Unable to create 'models' folder inside ImageJ/Fiji directory. Please create it yourself.");
final Gui[] guiRef = new Gui[1];
if (SwingUtilities.isEventDispatchThread())
guiRef[0] = new Gui(new IjAdapter());
Expand All @@ -131,6 +127,25 @@ private void runGUI() {
}
}

private static String getFijiFolder() {
File jvmFolder = new File(System.getProperty("java.home"));
String imageJExecutable;
if (PlatformDetection.isWindows())
imageJExecutable = "ImageJ-win64.exe";
else if (PlatformDetection.isLinux())
imageJExecutable = "ImageJ-linux64";
else if (PlatformDetection.isMacOS())
imageJExecutable = "Contents/MacOS/ImageJ-macosx";
else
throw new IllegalArgumentException("Unsupported Operating System");
while (true && jvmFolder != null) {
jvmFolder = jvmFolder.getParentFile();
if (new File(jvmFolder + File.separator + imageJExecutable).isFile())
return jvmFolder.getAbsolutePath();
}
throw new RuntimeException("Unable to find the path to the ImageJ/Fiji being used.");
}

/**
* Macro example:
* run("DeepImageJ Run", "modelPath=/path/to/model/LiveCellSegmentationBou
Expand All @@ -154,7 +169,7 @@ private <T extends RealType<T> & NativeType<T>, R extends RealType<R> & NativeTy
e.printStackTrace();
return;
}
try (Runner runner = Runner.create(model)) {
try (Runner runner = Runner.create(model, getFijiFolder() + File.separator + "engines")) {
runner.load();
if (this.inputFolder != null) {
executeOnPath(runner, adapter);
Expand Down Expand Up @@ -258,7 +273,7 @@ private void parseCommand() {

modelFolder = parseArg(macroArg, macroKeys[0], true);
if (!(new File(modelFolder).isAbsolute()))
modelFolder = new File("models", modelFolder).getAbsolutePath();
modelFolder = new File(getFijiFolder() + File.separator + "models", modelFolder).getAbsolutePath();
inputFolder = parseArg(macroArg, macroOptionalKeys[0], false);
outputFolder = parseArg(macroArg, macroOptionalKeys[1], false);
display = parseArg(macroArg, macroOptionalKeys[2], false);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/deepimagej/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ private Runner(ModelDescriptor descriptor) {
+ Types.stackTrace(e));
}
}

private Runner(ModelDescriptor descriptor, String enginesPath) {
this.descriptor = descriptor;
try {
this.model = Model.createBioimageioModel(new File(descriptor.getModelPath()).getAbsolutePath(), enginesPath);
} catch (ModelSpecsException | LoadEngineException | IOException e) {
e.printStackTrace();
throw new IllegalArgumentException("Something has happened, the model wanted does not "
+ "seem to exist anymore or it has been moved." + System.lineSeparator()
+ Types.stackTrace(e));
}
}

/**
*
Expand All @@ -106,6 +118,10 @@ public static Runner create(ModelDescriptor descriptor) {
return new Runner(descriptor);
}

public static Runner create(ModelDescriptor descriptor, String enginesPath) {
return new Runner(descriptor, enginesPath);
}

public void load() throws LoadModelException {
if (closed)
throw new RuntimeException("The model has already been closed");
Expand Down

0 comments on commit 23a25c2

Please sign in to comment.