Pyimagej on google colab

Hi, pyimagej is very nice.

I tried to use pyimagej on colab.
(reference:How do I install pyimagej?)

My scripts are;

# install JDK8 in colab
import os       #importing os to set environment variable
def install_java():
  !apt-get install -y openjdk-8-jdk-headless -qq > /dev/null      #install openjdk
  os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"     #set environment variable
  !java -version       #check java version
install_java()

install maven current version

!sudo apt-get install maven

install pyimagej and cython

!pip install Cython pyimagej

install pyjnius (to /usr/local in colab runtime)

!wget https://anaconda.org/conda-forge/pyjnius/1.2.0/download/osx-64/pyjnius-1.2.0-py37h90b5fae_0.tar.bz2 && tar -xjf pyjnius-1.2.0-py37h90b5fae_0.tar.bz2 -C /usr/local
# set env
os.environ["PYJNIUS_JAR"] = "/usr/local/share/pyjnius/pyjnius.jar"

let’s use ij

import imagej;
ij = imagej.init(); 
print(ij.getVersion())

check feature,

from skimage import io
import numpy as np

url = 'https://www.fi.edu/sites/fi.live.franklinds.webair.com/files/styles/featured_large/public/General_EduRes_Heart_BloodVessels_0.jpg'
img = io.imread(url)
img = np.mean(img, axis=2)
ij.py.show(img)

pyimagej on colab is so great.
GPU freeeee.

tatsuaki

5 Likes

Awesome, thanks @11110 for this nice guide! I linked it from pyimagej’s README:

2 Likes

Hi, Mr.Alexandre,

Could you try this ?
I think that an error encountered by imglyb-0.4.0, so I replaced to 0.3.5 version.

tatsuaki

attached

(Attachment pyimagej_Success.ipynb is missing)pyimagej_Success.zip (168.2 KB)

Hello Mr.TaTsuaki,

It does work now with the change of version, thank you very much for your quick reply.

Have a good day,
Thanks,

Alexandre

If I wanted to run my plugins in Colab. Would it be enough to upload my local Fiji distribution to Google drive and call:

ij = imagej.init(“my/local/Fiji.App”)

I am doing this and I am not able to use jnius.autoclass

Regards.
Carlos

Hi,

You are pioneer. (lol)
I tried it, and one simple way found.
Could you try it ?

2020年5月9日(土) 1:25 Carlos via Image.sc Forum <imagej@discoursemail.com>:

pyimagej_Success2.zip (329 KB)

Thanks for the answer!
That also allows me to run my Fiji distribution. However I am still not able to instansiate a java class using jnius.autoclass.
It says:

JavaException: JVM exception occurred: ij/WindowManager java.lang.NoClassDefFoundError

Ive looking and it seems that the jdk version being installed is the 11.0.7 (as far as I remember you need to login into Oracle to download versions that are not the latest).
Could this be a problem?
Regards,
Carlos

Hi,

ij/WindowManager java.lang.NoClassDefFoundError

Maybe I think that is X11 related error. (this error is correct error)
Google Colaboratory does not have GUI, so you can not use window related java classes.
But, IJ has headless mode, perhaps, there’s a chance you can use it’s mode.
I do not know how to do that. sorry…

2020年5月12日(火) 20:36 Carlos via Image.sc Forum <imagej@discoursemail.com>:

1 Like

Hi @carlosuc3m,

I tried the solution of @11110 and adapted it to the specific problem of using clij on Colab. Everything seems to work so thanks @11110!

You can test it directly here on Colab: https://colab.research.google.com/gist/guiwitz/6386815515decd9a926f92cf2043afa8

You can skip the part where I install clij of course. And don’t forget to “Restart runtime” before actually using Fiji (in the section clij via macro). Also before anything, don’t forget to change the Runtime type to GPU.

Good luck,
Guillaume

3 Likes

Thanks a lot. It works perfectly! What happened is that I forgot to restart the runtime after installing the packeges and setting the environment variables.
Thanks again @guiwitz and @11110 !!