Run StarDist from Fiji with jython problem

Hi all,

I’m trying to run StarDist using jython code :
1.

IJ.run(imahe, “StarDist 2D”, “”)

its pop a window : Object Detection with Star-convex Shapes. and you need to press OK to continue

I want my code to run automatically without interference , so how can I solve this ?

I also try this code (since i want to run from folder without opening windows , hidden):

from de.csbdresden.stardist import StarDist2D 
from glob import glob
import os

# run stardist on all tiff files in <indir> and save the label image to <outdir>
indir   = os.path.expanduser("~/Tmp/images/")
outdir  = os.path.expanduser("~/Tmp/labels/")

for f in sorted(glob(os.path.join(indir,"*.tif"))):
	print "processing ", f
  
	imp = io.open(f)
  
	res = command.run(StarDist2D, False,
			"input", imp,
			"modelChoice", "Versatile (fluorescent nuclei)",
			).get()
	label = res.getOutput("label")
  
	io.save(label, os.path.join(outdir,"label."+os.path.basename(f)))

my problem here is that command is not define :

NameError: name ‘command’ is not defined

i would be grateful for your help

Best Regards

2 Likes

You should have posted the original link to this code snippet

As you can see you are missing the service imports, including the command service.
That should fix it

# @DatasetIOService io
# @CommandService command
3 Likes

Hi ,
thanks for your quick response (Sorry for not posting the link)

this is my problem I don’t know how to import command module (in Fiji)

1 Like

just copy past those 2 lines in your fiji editor at the top of the script like in the file from git
Its a special Fiji/scijava feature there are not usual python comments

It works like the script parameters

3 Likes

Great thanks (my problem was that I did not put it on the top of my script).

but now I don’t get the segmented image just the labels …

1 Like

It’s mentioned in the header of the file, you can mention the outputType argument.
In your script replace the res statement by the following one

res = command.run(StarDist2D, False,
			"input", imp,
			"modelChoice", "Versatile (fluorescent nuclei)",
			"outputType","Label Image",
			).get()
3 Likes

Sorry its not working this way (no output) …

1 Like

Nothing will be shown, but the label should be saved in outDir.
You can edit outDir for another folder.
To save the image, you have to write a couple of lines to save them like the label.

3 Likes

Thanks , Sorry i’m new , can you please guide me how .

1 Like

Hi @Ebi,

here’s a minimal example that runs StarDist on the current image and displays the predicted label image:

# @Dataset img
# @UIService ui
# @CommandService command

from de.csbdresden.stardist import StarDist2D 

res = command.run(StarDist2D, False,
		"input", img,
		"modelChoice", "Versatile (fluorescent nuclei)",
		"outputType", "Label Image",
		).get()
label = res.getOutput("label")
ui.show(label)

Best,
Uwe

2 Likes

Thanks!
How can i save the labels before showing it ?
is there a guide that I can use (for StarDist With jython ) ?

1 Like

Hi @Ebi,

the line calling io.save as in the code from your first post will save the label image to disk.

You can learn everything you need to know here: https://imagej.net/Jython_Scripting_Examples

1 Like

Hi @Ebi ,

we just released a new version of StarDist for ImageJ, which is macro-recordable and that can also be called from a macro. Hence, you could use that instead of Jython if you’re more comfortable with macro.

Note that the syntax is a bit weird though, so it’s best to record with the parameters that you need, and then go from there.

Best,
Uwe

1 Like

Hello

I need to use the code as a module and call it from another function something like this:

my_function(a,b, ...):
# @Dataset img
# @UIService ui
# @CommandService command

from de.csbdresden.stardist import StarDist2D 

res = command.run(StarDist2D, False,
		"input", img,
		"modelChoice", "Versatile (fluorescent nuclei)",
		"outputType", "Label Image",
		).get()
label = res.getOutput("label")
ui.show(label)

returm .... 

give me the error again : NameError: name ‘command’ is not defined

Hope you can help me

Best Regards

it will give the same error also with this code:

# my module 

# @Dataset img
# @UIService ui
# @CommandService command

from de.csbdresden.stardist import StarDist2D 

my_function(a,b,....)
res = command.run(StarDist2D, False,
		"input", img,
		"modelChoice", "Versatile (fluorescent nuclei)",
		"outputType", "Label Image",
		).get()
label = res.getOutput("label")
ui.show(label)

return ....

Please use ``` to format your code blocks (one triple quote before the code block and another one below the end of the code block) that would greatly improve readibility.

The @ notation is importing so-called Scijava services at runtime. Which mean you cannot unfortunately put them in a module file to import (for jython so module file should be located in Fiji.app/jars/lib by the way)

There are work around for some cases like the PrefService see

However, since now the StarDist plugin is macro-recordable, you could use in your module what the recorder returns you when Java is selected as language for the recorder.
It should be something like IJ.run(StarDist, Arg1...)

1 Like

Thanks so much !

Still is there a way to work (jython) hidden in this mode IJ.run(StarDist, Arg1…) ?

You can use IJ.run commands in jython.
Just make sure to have from ij import IJ

1 Like

Sorry if I was not clear,
I know I can use IJ. my problem is that the images are pop to the screen and I won’t to work in hidden mode

It should work to pass img, ui, and command as additional arguments to your function.
(You need to use the #@ syntax in your main script to get these.)

That should work if you use the Jython code (fixing the problem as I mentioned above).

If you were to use the ImageJ1 macro language, you could call StarDist for example like this:

run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2D], args=['input':'blobs.gif', 'modelChoice':'Versatile (fluorescent nuclei)', 'outputType':'Label Image', 'label':''], process=[false]");

However, that will pop up the resulting label image, which you don’t want.

Best,
Uwe