Weka Segmentation Script on Linux Machine Headless

weka
linux
headless
scripting
Tags: #<Tag:0x00007fd54873b638> #<Tag:0x00007fd54873b020> #<Tag:0x00007fd54873ab70> #<Tag:0x00007fd54873a490>

#1

Dear all,

I want to run WekaSegmentation in headless mode without GUI on a Linux machine (connected via ssh). The Macro I wrote (ImageJ Macro language) should just open an Image file, apply a classifier and safe the result. I always get this error:

Loading Weka properties…
WARNING: core mtj jar files are not available as resources to this classloader (sun.misc.Launcher$AppClassLoader@764c12b6)
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
at java.awt.List.(List.java:233)
at java.awt.List.(List.java:205)
at trainableSegmentation.Weka_Segmentation.run(Weka_Segmentation.java:1253)
at ij.IJ.runUserPlugIn(IJ.java:217)
at ij.IJ.runPlugIn(IJ.java:181)
at ij.Executer.runCommand(Executer.java:137)
at ij.Executer.run(Executer.java:66)
at ij.IJ.run(IJ.java:297)
at ij.IJ.run(IJ.java:272)
at ij.macro.Functions.doRun(Functions.java:603)
at ij.macro.Functions.doFunction(Functions.java:96)
at ij.macro.Interpreter.doStatement(Interpreter.java:230)
at ij.macro.Interpreter.doStatements(Interpreter.java:218)
at ij.macro.Interpreter.run(Interpreter.java:115)
at ij.macro.Interpreter.run(Interpreter.java:85)
at ij.macro.Interpreter.run(Interpreter.java:96)
at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:155)
at ij.plugin.Macro_Runner.runMacroFile(Macro_Runner.java:139)
at ij.IJ.runMacroFile(IJ.java:148)
at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:1069)
at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:1065)
at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:999)
at net.imagej.legacy.IJ1Helper.runMacroFile(IJ1Helper.java:1065)
at net.imagej.legacy.LegacyCommandline$Macro.handle(LegacyCommandline.java:187)
at org.scijava.console.DefaultConsoleService.processArgs(DefaultConsoleService.java:102)
at net.imagej.legacy.LegacyConsoleService.processArgs(LegacyConsoleService.java:81)
at org.scijava.AbstractGateway.launch(AbstractGateway.java:95)
at net.imagej.Main.main(Main.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:279)
at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:186)
at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:77)
No window with the title “Trainable Weka Segmentation v3.2.20” found.

The command to run my script in Putty is as follows:

$ /homeb/…/Fiji.app/ImageJ-linux64 --headless -macro /homeb/…/MacroTest2.ijm

My Macro called MacroTest2.ijm goes as follows:

input = "/work/…/ImagesIn/"
output = “/work/…/ImagesOut/”;
filename= "11611_2461_1_1-1.tif"
open(input + filename);
run(“Trainable Weka Segmentation”);
selectWindow(“Trainable Weka Segmentation v3.2.20”);
call(“trainableSegmentation.Weka_Segmentation.loadClassifier”, “/work/…/classifiertest.model”);
call(“trainableSegmentation.Weka_Segmentation.getResult”);
saveAs(“Tiff”, output + filename);
close();

What is going wrong? I have to admit I am neither an expert with linux nor with java.

Thank you!

best,
Lars


#2

Hi @ladida

when using ij-macro commands I struggled too to get things running…
Also if you want to run it headless, you cannot use the open command, since it will want to create an image display.

My suggestion would be to go for something like groovy to script this. An outline would look like

import ij.IJ
import trainableSegmentation.WekaSegmentation

img = IJ.openImage('path/to/image.tif');

classifier = new WekaSegmentation()
classifier.loadClassifier('/path/to/classifier.model')
res = classifier.applyClassifier(img, 0, false)

...

For reference:

Sorry for not helping to debug, but I think switching the language will actually be easier.

Felix


#3

Thank you @FelixM !

Writing the code in beanshell was indeed a good idea. It is working now!

Best,
Lars